gear

module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2023 License: MIT

README

gear

gear is a simple and easy to use golang library, like lodash

codecov GitHub

install

go get github.com/CiroLee/gear

uses

all function modules startwidth gearXXX.
such as gearslice that expands the ability of slice

import 
  (
    "fmt",
    "github.com/CiroLee/gear/gearslice"
  )

func main() {
  s := []int{1,2,3,3,4,5,6,6}
  r := gearslice.Uniq(s)
  // []int{1,2,3,4,5,6}
}

docs

gearslice

slice expansion functions

gearstring

string expansion functions

gearmap

map expansion functions

gearmath

math expansion functions

geardate

date expansion function

IndexOf

return the index of the first element in the slice, if the element is not in the slice, return -1
signature:

func IndexOf[T comparable](s []T, el T) int

example:

s := []string{"a", "b", "c"}
i := gearslice.IndexOf(s, "c")
// 2

⬆️ back

LastIndexOf

return the index of the first element in the slice, if the element is not in the slice, return -1
signature:

arr := []int{1, 2, 3, 4, 5, 2, -1}
index := gearslice.LastIndexOf(arr, 2) // 5

⬆️ back

FindIndex

return the index of the first element in the slice that passed the test implemented by the provided function. return -1 if no corresponding element is found.
signature:

func FindIndex[T any](s []T, fn func(el T, index int) bool) int 

example:

s := []int{0,1,2,3,4,5,6}
r := FindIndex(s, func(el int, _ int) bool {
  return el > 0
})
// 1

⬆️ back

FindLastIndex

FindIndex return the index of the last element in the slice that passed the test implemented by the provided function. return -1 if no corresponding element is found
signature:

func FindLastIndex[T any](s []T, fn func(el T, index int) bool) int

example:

arr := []string{"g", "r", "e", "e", "n"}
index := gearslice.FindLastIndex(arr, func(el string, _ int) bool {
  return el == "e"
})
// 3

⬆️ back

Find

return the value of the first element of the slice that passed the test function provided, return zero value if no corresponding element is found
signature:

func Find[T any](s []T, fn func(el T, index int) bool) (T, bool)

example:

type Person struct {
  Name   string
  Age    int
  Gender int
  Grade  int
}

s := []Person{
  {Name: "Tom", Age: 12, Gender: 1, Grade: 2},
  {Name: "Jim", Age: 11, Gender: 1, Grade: 1},
  {Name: "Dave", Age: 13, Gender: 0, Grade: 3},
}

r, ok := Find(s, func(el Person, _ int) bool {
  return el.Age > 11 && el.Gender ==0
})
// Person{Name: "Dave", Age: 13, Gender: 0, Grade: 3}, true

⬆️ back

Includes

weather the slice contains a certain element
signature:

func Includes[T comparable](s []T, el T) bool

example:

s := []string{"a", "b", "c"}
r := gearslice.Includes(s, "d")
//  false

⬆️ back

Every

weather all elements in the slice passed the test implemented by the provided function
signature:

func Every[T any](s []T, fn func(el T, index int) bool) bool 

example:

s := []int{1, 2, 3, -1}
r := gearslice.Every(s, func(el int, _ int) bool {
  return el > 0
})
// false

⬆️ back

Some

weather at least one element in the slice passed the test implemented by the provide function
signature:

func Some[T any](s []T, fn func(el T, index int) bool) bool

example:

s := []int{1, 2, 3, -1}

r := gearslice.Some(s, func(el int, _ int) bool {
 return el > 0
})
// true

⬆️ back

Uniq

remove duplicate elements in the slice
signature:

func Uniq[T constraints.Ordered | constraints.Complex](s []T) []T 

example:

s := []int{1, 2, 3, 4, 4, 5}
r := gearslice.Uniq(s)
// []int{1,2,3,4,5}

⬆️ back

Map

create a new slice populated with the results of calling the provide function on every element in the calling slice
signature:

func Map[T, K any](s []T, cb func(el T, index int) K) []K 

example:

s := []int{1, 2, 3, 4, 5}
r := gearslice.Map(s, func(el int, _ int) int {
  return el * 2
})
// []int{2, 4, 6, 8, 10}

⬆️ back

ForEach

execute a provided function once for each element in the slice
signature:

func ForEach[T any](s []T, cb func(el T, index int))

example:

s := []int{1, 2, 3, 4, 5}
var r = make([]string, 0, len(s))
gearslice.ForEach(s, func(el int, _ int) {
  r = append(r, fmt.Sprintf("%d", el))
})
// r: []string{"1", "2", "3", "4", "5"}

⬆️ back

Filter

filtered down to just the elements from the given slice that passed the test implemented by the provided function
signature:

func Filter[T any](s []T, filter func(el T, index int) bool) []T 

example:

s := []int{1, 2, 3, 4, -1}
r := gearslice.Filter(s, func(el int, _ int) bool {
  return el > 0
})
// []int{1, 2, 3, 4}

⬆️ back

Contact

Concatenate multiple slices and return a new slice
signature:

func Contact[T any](args ...[]T) []T

example:

s1 := []int{1, 2, 3, 4}
s2 := []int{5, 6}
s3 := []int{6, 7, 8}

r := gearslice.Contact(s1, s2, s3)
// []]int{1, 2, 3, 4, 5, 6, 6, 7, 8}

⬆️ back

Pop

remove the last element from a slice and return that element, it will change the length of the slice
signature:

func Pop[T any](s *[]T) T

example:

s := []int{1, 2, 3, 4}
r := gearslice.Pop(&s)
// r: 4
// s: []int{1, 2, 3}

⬆️ back

Shift

remove the first element from a slice and return that removed element. This method changes the length of the slice
signature:

func Shift[T any](s *[]T) T

example:

s := []int{1, 2, 3, 4}
r := gearslice.Shift(&s)
// r: 1
// s: []int{2, 3, 4}

⬆️ back

Remove

remove a value in the slice at a given index. it will modify the origin slice
signature:

func Remove[T any](s *[]T, index int) error

example:

s := []int{1, 2, 3}
r := gearslice.Remove(&s, 1)
// []int{1, 3}

⬆️ back

Insert

insert a value in the slice at a given index. it will modify the origin slice
signature:

func Insert[T any](s *[]T, index int, value T) error

example:

s := []int{1, 2, 3}
gearslice.Insert(&s, 1, 20)
// s: []int{1, 20, 2, 3}

⬆️ back

Drop

drop n elements from the beginning(if n greater than zero) or the end(if n less than zero) of the slice
signature:

func Drop[T any](s []T, n int) ([]T, error)

example:

s := []int{1, 2, 3, 4, 5, 6, 7}
r, _ := gearslice.Drop(s, 2)
// []int{3, 4, 5, 6, 7}

⬆️ back

Sample

get a random element from the slice
signature:

func Sample[T any](s []T) T

example:

s := []int{1, 2, 3, 4, 5, 6, 7, 8}
r := gearslice.Includes(s, gearslice.Sample(s))
// true

⬆️ back

Reverse

reverse a slice, return a new slice
signature:

func Reverse[T any](s []T) []T 

example:

s := []int{1, 2, 3, 4, 5}
r := gearslice.Reverse(s)
// []int{5, 4, 3, 2, 1}

⬆️ back

Count

return the number of elements in the slice that equal to value
signature:

func Count[T comparable](s []T, value T) int

example:

s := []int{1, 2, 3, 4, 4}
r := gearslice.Count(s, 4)
// 2

⬆️ back

CountBy

return the number of the elements in the slice that pass the test implemented by the provided the function
signature:

func CountBy[T comparable](s []T, fn func(el T, index int) bool) int

example:

s := []int{1, 2, 3, 4, 5, 5, 6, 7, 8}
r := gearslice.CountBy(s, func(el int, _ int) bool {
    return el > 5
})
// 3

⬆️ back

Substring

return the part of the string from the start and excluding the end, or to the end of the string if no end index is supplied. Not include the index element
signature:

func Substring(str string, start, end int) string

example:

str := "hello world"
r := gearstring.Substring(s, 1, 5)
// "ello"

⬆️ back

CharAt

return a specified character from a string
signature:

func CharAt(str string, index int) string

example:

str := "hello world"
r := gearstring.CharAt(str, 2)
// "l"

⬆️ back

Contact

Concatenate multiple strings and return a new string
signature:

func Contact(args ...string) string

example:

r := gearslice.Contact("hello ", "world")
// "hello world"

⬆️ back

ToUpperCase

change the first letter of the string to upper
signature:

func ToUpperCase(s string) string

example:

str := "hello world"
r := gearstring.ToUpperCase(str)
// "Hello world"

⬆️ back

ToLowerCase

change the first letter of the string to lower
sigmature:

func ToLowerCase(s string) string

example:

str := "HELLO WORLD"
r := gearstring.ToLowerCase(str)
// "hELLO WORLD"

⬆️ back

DesensitizeData

make data insensitive via hidden part of the data
signature:

func DesensitizeData

example:

str := "123这段文字加密00000"
r, _ := gearstring.DesensitizeData(str, 3, 9, "@")
// "123@@@@@@00000"

⬆️ back

DesensitizePhone

hidden middle 4 numbers of the mobile phone, default placeholder is '*'
signature:

func DesensitizePhone(val string, placeholder string) (string, error)

example:

phone := "13299889988"
r := gearstring.DesensitizePhone(phone, "#")
// "132####9988"

⬆️ back

Pick

return parts of the map according to keys
signature:

func Pick[K comparable, V any](m map[K]V, keys []K) map[K]V 

example:

m := map[string]int{"a": 1, "b": 2, "c": 3}
r := gearmap.Pick(m, []string{"a", "b"})
// map[string]int{"a":1, "b": 2}

⬆️ back

PickBy

return parts of a map passed the test implemented by the provided function
signature:

func PickBy[K comparable, V any](m map[K]V, fn func(k K, v V) bool) map[K]V 

example:

m := map[string]int{"a": 1, "b": 2, "c": 3}
r := gearmap.PickBy(m, func(_ string, v int) bool {
  return v > 2
})
// map[string]int{"c": 3}

⬆️ back

Omit

remove parts of a map according to keys
signature:

func Omit[K comparable, V any](m map[K]V, keys []K) map[K]V

example:

m := map[string]int{"a": 1, "b": 2, "c": 3}
r := gearmap.Omit(m, []string{"a", "b"})
// map[string]int{"c": 3}

⬆️ back

OmitBy

remove parts of a map passed the test implemented by the provided function
signature:

func OmitBy[K comparable, V any](m map[K]V, fn func(k K, v V) bool) map[K]V

example:

m := map[string]int{"a": 1, "b": 2, "c": 3}
r := gearmap.OmitBy(m, func(_ string, v int) bool {
  return v < 2
})
// map[string]int{"b": 2, "c": 3}

⬆️ back

Values

return values of the map
signature:

func Values[K comparable, V any](m map[K]V) []V

example:

m := map[string]int{"a": 1, "b": 2, "c": 3}
r := gearmap.Values(m)
sort.Strings(r)
// []int{1, 2, 3}

⬆️ back

Keys

return keys of the map
signature:

func Keys[K comparable, V any](m map[K]V) []K

example:

m := map[string]int{"a": 1, "b": 2, "c": 3}
r := gearmap.Keys(m)
sort.Strings(r)
// []string{"a", "b", "c"}

⬆️ back

Assign

merge multiple maps into a new map
signature:

func Assign[K comparable, V any](maps ...map[K]V) map[K]V

example:

m1 := map[string]int{"a": 1, "b": 2, "c": 3}
m2 := map[string]int{"d": 4}
r := gearmap.Assign(m1, m2)
// map[string]int{"a": 1, "b": 2, "c": 3, "d": 4}

⬆️ back

Sum

return a sum of the slice
signature:

func Sum[T constraints.Integer | constraints.Float | constraints.Complex](s []T) T

example:

s := []int{1, 2, 3, 4, 5}
r := gearmath.Sum(s)
// 15

⬆️ back

SumBy

summarize the values in the slice using the given return value from the function
signature:

func SumBy[T any, V constraints.Integer | constraints.Float](s []T, fn func(el T, index int) V) V

example:

s := []string{"hello", "world"}
r := gearmath.SumBy(s, func(el string, _ int) int {
  return len(el)
})
// 10

⬆️ back

Min

return the minimum value of the slice, return zero value if the slice is empty
signature:

func Min[T constraints.Integer | constraints.Float](s []T) T

example:

s := []int{1, 2, 3, 4, -4, 5, 6}
r := gearmath.Min(s)
// -4

⬆️ back

Max

return the minimum value of the slice, return zero value if the slice is empty
signature:

func Max[T constraints.Integer | constraints.Float](s []T) T 

example:

s := []int{1, 2, 3, 4, -4, 5, 6}
r := gearmath.Max(s)
// 6

⬆️ back

Mean

return the mean value of the slice
signature:

func Mean(s []float64) float64

example:

s := []float64{2, 4, 6, 8}
r := gearmath.Mean(s)
// 5.0

⬆️ back

IsPrime

weather a number is a prime
signature:

func IsPrime(num int) bool

example:

gearmath.IsPrime(4)
// false

⬆️ back

IsSubset

return true if the slice contains all the elements in the subset signature:

func IsSubset[T comparable](s, subset []T) bool

example:

s1 := []int{1, 2, 3, 4}
s2 := []int{1, 3}
r := gearslice.IsSubset(s1, s2)
// true

⬆️ back

Union

return the union values of slices
signature:

func Union[T constraints.Ordered | constraints.Complex](args ...[]T) []T

example:

s1, s2, s3 := []int{1, 2, 3, 4}, []int{2, 5, 7}, []int{-1, 0, 0}
r := Union(s1, s2, s3)
sort.Ints(r)
// []int{-1, 0, 1, 2, 3, 4, 5, 7}

⬆️ back

Intersection

return the same elements of two slices
signature:

func Intersection[T constraints.Ordered | constraints.Complex](slice1, slice2 []T) []T

example:

s1, s2 := []int{1, 2, 3, 4}, []int{1, 3, 5, 7}
r := Intersection(s1, s2) // []int{1, 3}

⬆️ back

Format

format a unix timestamp according the layout
signature:

func Format(t int64, layout string) string

example:

var timestamp int64 = 1673259412 // 2023-01-09 18:16:52
r := geardate.Format(timestamp, geardate.DefaultLayout)
// "2023-01-09 18:16:52"

⬆️ back

IsLeap

weather the year is leap
signature:

func IsLeap(year int) bool

example:

geardate.IsLeap(2023) // false

⬆️ back

TimeOffset

get the time before or after the specified time
signature:

func TimeOffset(date time.Time, offset string) (time.Time, error)

example:

date := time.Date(2023, time.May, 6, 12, 0, 0, 0, time.UTC)
r, _ := geardate.TimeOffset(date, "1h30m") // 2023-05-06 13:30:00 +0000 UTC

⬆️ back

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL