/\ \
__ __ ___ \_\ \ __ _ __ ____ ___ ___ _ __ __ __ ___
/\ \/\ \ /' _ `\ /'_ \ /'__`\/\ __\/ ,__\ / ___\ / __`\/\ __\/'__`\ /'_ `\ / __`\
\ \ \_\ \/\ \/\ \/\ \ \ \/\ __/\ \ \//\__, `\/\ \__//\ \ \ \ \ \//\ __/ __ /\ \L\ \/\ \L\ \
\ \____/\ \_\ \_\ \___,_\ \____\\ \_\\/\____/\ \____\ \____/\ \_\\ \____\/\_\\ \____ \ \____/
\/___/ \/_/\/_/\/__,_ /\/____/ \/_/ \/___/ \/____/\/___/ \/_/ \/____/\/_/ \/___L\ \/___/
/\____/
\_/__/
like underscore.js, but for Go
$ go get github.com/ahl5esoft/golang-underscore
$ go get -u github.com/ahl5esoft/golang-underscore
* FindLastIndex
* Sample
* more...
All
,AllBy
Any
,AnyBy
AsParallel
Chain
Clone
Each
Find
,FindBy
FindIndex
,FindIndexBy
First
Group
,GroupBy
IsArray
IsMatch
Keys
Map
,MapBy
Md5
ParseJson
Pluck
Property
,PropertyRV
Range
Reduce
Reject
,RejectBy
Select
,SelectBy
Size
Sort
,SortBy
Take
ToJson
Uniq
,UniqBy
UUID
Values
Arguments
source
- array or mappredicate
- func(element, index or key) bool
Return
- bool - all the values that pass a truth test
predicate
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{1, "two"},
TestModel{1, "three"},
}
ok := All(arr, func(r TestModel, _ int) bool {
return r.Id == 1
})
// ok == true
Arguments
source
- array or mapproperties
- map[string]interface{}
Return
- bool
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{1, "two"},
TestModel{1, "three"},
}
ok := AllBy(arr, nil)
// ok == true
ok = AllBy(arr, map[string]interface{}{
"name": "a",
})
// ok == false
ok = AllBy(arr, map[string]interface{}{
"id": 1,
})
// ok == true
Arguments
source
- array or mappredicate
- func(element or value, index or key) bool
Return
- bool - any of the values that pass a truth test
predicate
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{2, "two"},
TestModel{3, "three"},
}
ok := Any(arr, func(r TestModel, _ int) bool {
return r.Id == 0
})
// ok == false
Arguments
source
- array or mapproperties
- map[string]interface{}
Return
- bool
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{2, "two"},
TestModel{3, "three"},
}
ok := AnyBy(arr, map[string]interface{}{
"Id": 0,
})
// ok == false
ok = AnyBy(arr, map[string]interface{}{
"id": arr[0].Id,
"name": arr[0].Name,
})
// ok == true
Support
Each
Examples
arr := []int{ 1, 2, 3 }
Chain(arr).AsParallel().Each(func (n, i int) {
// code
})
Arguments
source
- array or map
Return
- interface{} - a wrapped object, wrapped objects until value is called
Examples
res, ok := Chain([]int{1, 2, 1, 4, 1, 3}).Uniq(nil).Group(func(n, _ int) string {
if n%2 == 0 {
return "even"
}
return "old"
}).Value().(map[string][]int)
// len(res) == 2 && ok == true
Return
- interface{}
Examples
arr := []int{1, 2, 3}
duplicate := Clone(arr)
ok := All(duplicate, func(n, i int) bool {
return arr[i] == n
})
// ok == true
Arguments
source
- array or mapiterator
- func(element or value, index or key)
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{1, "two"},
TestModel{1, "three"},
}
Each(arr, func (r TestModel, i int) {
// coding
})
Arguments
source
- array or mappredicate
- func(element or value, index or key) bool
Return
- interface{}
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{2, "two"},
TestModel{3, "three"},
}
item := Find(arr, func(r TestModel, _ int) bool {
return r.Id == 1
})
// item == arr[0]
Arguments
source
- array or mapproperties
- map[string]interface{}
Return
- interface{}
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{2, "two"},
TestModel{3, "three"},
}
item := FindBy(arr, map[string]interface{}{
"id": 2,
})
// item == arr[1]
Arguments
source
- array or mappredicate
- func(element or value, index or key) bool
Return
- int - index
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{1, "two"},
TestModel{1, "three"},
}
i := FindIndex(arr, func(r TestModel, _ int) bool {
return r.Name == arr[1].Name
})
// i == 1
Arguments
source
- array or mapproperties
- map[string]interface{}
Return
- int - index
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{2, "two"},
TestModel{3, "three"},
}
i := FindIndexBy(arr, map[string]interface{}{
"id": 1,
})
// i == 0
Arguments
source
- array or map
Return
- interface{}
Examples
arr := []int{ 1, 2, 3 }
v := First(arr)
n, ok := v.(int)
if !(ok && n == 1) {
//wrong
}
v = First(nil)
if v != nil {
//wrong
}
Arguments
source
- array or mapkeySelector
- func(element or value, index or key) anyType
Return
- interface{} - map[anyType][](element or value)
Examples
v := Group([]int{ 1, 2, 3, 4, 5 }, func (n, _ int) string {
if n % 2 == 0 {
return "even"
}
return "odd"
})
dict, ok := v.(map[string][]int)
if !(ok && len(dict["even"]) == 2) {
t.Error("wrong")
}
Arguments
source
- array or mapproperty
- property name
Return
- interface{} - map[property type][](element or value)
Examples
arr := []TestModel{
TestModel{ 1, "a" },
TestModel{ 2, "a" },
TestModel{ 3, "b" },
TestModel{ 4, "b" },
}
v := GroupBy(arr, "name")
dict, ok := v.(map[string][]TestModel)
if !(ok && len(dict) == 2) {
t.Error("wrong")
}
Arguments
source
- array or mapindexSelector
- func(element or value, index or key) anyType
Return
- interface{} - map[anyType](element or value)
Examples
v, _ := Index([]string{ "a", "b" }, func (item string, _ int) string {
return item
})
res, ok := v.(map[string]string)
if !(ok && res["a"] == "a") {
// wrong
}
Arguments
source
- array or mapproperty
- string
Return
- interface{} - map[propertyType](element or value)
Examples
arr := []TestModel{
TestModel{ 1, "a" },
TestModel{ 2, "a" },
TestModel{ 3, "b" },
TestModel{ 4, "b" },
}
res := IndexBy(arr, "Name")
dict, ok := res.(map[string]TestModel)
if !(ok && len(dict) == 2) {
// wrong
}
Arguments
element
- object
Return
- bool
Examples
if !IsArray([]int{}) {
// wrong
}
if IsArray(map[string]int{}) {
// wrong
}
Arguments
element
- objectproperties
- map[string]interface{}
Return
- bool
Examples
m := TestModel{ 1, "one" }
ok := IsMatch(nil, nil)
if ok {
// wrong
}
ok = IsMatch(m, nil)
if ok {
// wrong
}
ok = IsMatch(m, map[string]interface{}{
"id": m.Id,
"name": "a",
})
if ok {
// wrong
}
ok = IsMatch(m, map[string]interface{}{
"id": m.Id,
"name": m.Name,
})
if !ok {
// wrong
}
Arguments
source
- map
Return
- interface{} - []keyType
Examples
arr := []string{ "aa" }
v := Keys(arr)
if v != nil {
// wrong
}
dict := map[int]string{
1: "a",
2: "b",
3: "c",
4: "d",
}
v = Keys(dict)
res, ok := v.([]int)
if !(ok && len(res) == len(dict)) {
// wrong
}
Arguments
source
- array or mapselector
- func(element, index or key) anyType
Return
- interface{} - an array of anyType
Examples
arr := []string{ "11", "12", "13" }
v := Map(arr, func (s string, _ int) int {
n, _ := strconv.Atoi(s)
return n
})
res, ok := v.([]int)
if !(ok && len(res) == len(arr)) {
// wrong
}
Arguments
plaintext
- string
Return
- string - md5 string
Examples
if Md5("123456") != "e10adc3949ba59abbe56e057f20f883e" {
// wrong
}
Arguments
str
- json stringcontainer
- interface{}
Return
- error
Examples
str := `["a","b"]`
var arr []string
err := ParseJson(str, &arr)
if !(err == nil && len(arr) == 2) {
// wrong
}
Arguments
source
- array or mapproperty
- property name
Return
- interface{} - []propertyType
Examples
arr := []TestModel{
TestModel{ 1, "a" },
TestModel{ 2, "a" },
TestModel{ 3, "b" },
TestModel{ 4, "b" },
}
v := MapBy(arr, "name")
res, ok := v.([]string)
if !(ok && len(res) == 4) {
// wrong
}
Arguments
source
- arrayproperty
- string
Return
- interface{} - an array of property type
Examples
arr := []TestModel{
TestModel{ 1, "one" },
TestModel{ 2, "two" },
TestModel{ 3, "three" },
}
v := Pluck(arr, "name")
res, ok := v.([]string)
if !(ok && len(res) == len(arr)) {
// wrong
}
for i := 0; i < 3; i++ {
if res[i] != arr[i].Name {
// wrong
}
}
Arguments
name
- property name
Return
- func(interface{}) (interface{}, error)
Examples
item := TestModel{ 1, "one" }
getAge := Property("age")
_, err := getAge(item)
if err == nil {
// wrong
}
getName := Property("name")
name, err := getName(item)
if !(err == nil && name.(string) == item.Name) {
// wrong
}
Arguments
name
- property name
Return
- func(interface{}) (reflect.Value, error)
Examples
item := TestModel{ 1, "one" }
getAgeRV := PropertyRV("age")
_, err := getAgeRV(item)
if err == nil {
// wrong
}
getNameRV := PropertyRV("name")
nameRV, err := getNameRV(item)
if !(err == nil && nameRV.String() == item.Name) {
// wrong
}
Arguments
start
- intstop
- intstep
- int
Return
- []int
Examples
arr := Range(0, 0, 1)
if len(arr) != 0 {
// wrong
}
arr = Range(0, 10, 0)
if len(arr) != 0 {
// wrong
}
arr = Range(10, 0, 1)
if len(arr) != 0 {
// wrong
}
arr = Range(0, 2, 1)
if !(len(arr) == 2 && arr[0] == 0 && arr[1] == 1) {
// wrong
}
arr = Range(0, 3, 2)
if !(len(arr) == 2 && arr[0] == 0 && arr[1] == 2) {
// wrong
}
Arguments
source
- arrayiterator
- func(memo, element or value, key or index) memomemo
- anyType
Return
- interface{} - memo
Examples
v := Reduce([]int{ 1, 2 }, func (memo []int, n, _ int) []int {
memo = append(memo, n)
memo = append(memo, n + 10)
return memo
}, make([]int, 0))
res, ok := v.([]int)
if !(ok && len(res) == 4) {
// wrong
}
if !(res[0] == 1 && res[1] == 11 && res[2] == 2 && res[3] == 12) {
// wrong
}
Arguments
source
- array or mappredicate
- func(element or value, index or key) bool
Return
- interface{} - an array of all the values that without pass a truth test
predicate
Examples
arr := []int{ 1, 2, 3, 4 }
v := Reject(arr, func (n, i int) bool {
return n % 2 == 0
})
res, ok := v.([]int)
if !(ok && len(res) == 2) {
// wrong
}
if !(res[0] == 1 && res[1] == 3) {
// wrong
}
Arguments
source
- array or mapproperties
- map[string]interface{}
Return
- interface{} - an array of all the values that without pass a truth test
properties
Examples
arr := []TestModel{
TestModel{ 1, "one" },
TestModel{ 2, "two" },
TestModel{ 3, "three" },
}
v := RejectBy(arr, map[string]interface{}{
"Id": 1,
})
res, ok := v.([]TestModel)
if !(ok && len(res) == 2) {
// wrong
}
Arguments
source
- array or mappredicate
- func(element or value, index or key) bool
Return
- interface{} - an array of all the values that pass a truth test
predicate
Examples
arr := []int{ 1, 2, 3, 4 }
v := Select(arr, func (n, i int) bool {
return n % 2 == 0
})
res, ok := v.([]int)
if !(ok && len(res) == 2) {
t.Error("wrong length")
return
}
if !(res[0] == 2 && res[1] == 4) {
t.Error("wrong result")
}
Arguments
source
- array or mapproperties
- map[string]interface{}
Return
- interface{} - an array of all the values that pass a truth test
properties
Examples
arr := []TestModel{
TestModel{ 1, "one" },
TestModel{ 2, "two" },
TestModel{ 3, "three" },
}
v := SelectBy(arr, map[string]interface{}{
"Id": 1,
})
res, ok := v.([]TestModel)
if !(ok && len(res) == 1 && res[0] == arr[0]) {
// wrong
}
Arguments
source
- array or map
Return
- int
Examples
dict := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
if Size(dict) != len(dict) {
// wrong
}
Arguments
source
- array or mapselector
- func(element, key or index) anyType
Return
- interface{} - an array of
source
that sorted
Examples
arr := []int{ 1, 2, 3, 5 }
v := Sort([]int{ 5, 3, 2, 1 }, func (n, _ int) int {
return n
})
res, ok := v.([]int)
if !(ok && len(res) == len(arr)) {
// wrong
}
for i, n := range arr {
if res[i] != n {
// wrong
}
}
Arguments
source
- array or mapproperty
- string
Return
- interface{}
Examples
arr := []TestModel{
TestModel{ 3, "three" },
TestModel{ 1, "one" },
TestModel{ 2, "two" },
}
v := SortBy(arr, "id")
res, ok := v.([]TestModel)
if !(ok && len(res) == len(arr)) {
// wrong
}
if !(res[0].Id < res[1].Id && res[1].Id < res[2].Id) {
// wrong
}
Arguments
source
- array or mapcount
- int
Return
- interface{}
Examples
arr := []int{ 1, 2, 3 }
v := Take(arr, 1)
res, ok := v.([]int)
if !ok {
// wrong
}
if res[0] != 1 {
// wrong
}
Arguments
value
- interface{}, anyType
Return
- string, error
Examples
b := true
v, _ := ToJson(b)
if v != "true" {
// wrong
}
str := "a"
v, _ = ToJson(str)
if v != str {
// wrong
}
v, _ = ToJson(1)
if v != "1" {
// wrong
}
arr := []int{ 1, 2, 3 }
v, _ = ToJson(arr)
if v != "[1,2,3]" {
// wrong
}
obj := TestModel{ 1, "name" }
v, _ = ToJson(obj)
if v != `{"Id":1,"Name":"name"}` {
// wrong
}
Arguments
source
- arrayselector
- nil or func(element or value, index or key) anyType
Return
- interface{} - only the first occurence of each value is kept
Examples
v := Uniq([]int{ 1, 2, 1, 4, 1, 3 }, func (n, _ int) int {
return n % 2
})
res, ok := v.([]int)
if !(ok && len(res) == 2) {
// wrong
}
Arguments
source
- arrayproperty
- string
Return
- interface{}
Examples
arr := []TestModel{
TestModel{ 1, "one" },
TestModel{ 2, "one" },
TestModel{ 3, "one" },
}
v := UniqBy(arr, "Name")
res, ok := v.([]TestModel)
if !(ok && len(res) == 1) {
// wrong
}
Return
- string - uuid string
Examples
uuid := UUID()
//1a40272540e57d1c80e7b06042219d0c
Arguments
source
- map
Return
- interface{} - an array of
source
's values - error
Examples
dict := map[int]string{
1: "a",
2: "b",
3: "c",
4: "d",
}
v, _ := Values(dict)
res, ok := v.([]string)
if !(ok && len(res) == len(dict)) {
// wrong
}