/\ \
__ __ ___ \_\ \ __ _ __ ____ ___ ___ _ __ __ __ ___
/\ \/\ \ /' _ `\ /'_ \ /'__`\/\ __\/ ,__\ / ___\ / __`\/\ __\/'__`\ /'_ `\ / __`\
\ \ \_\ \/\ \/\ \/\ \ \ \/\ __/\ \ \//\__, `\/\ \__//\ \ \ \ \ \//\ __/ __ /\ \L\ \/\ \L\ \
\ \____/\ \_\ \_\ \___,_\ \____\\ \_\\/\____/\ \____\ \____/\ \_\\ \____\/\_\\ \____ \ \____/
\/___/ \/_/\/_/\/__,_ /\/____/ \/_/ \/___/ \/____/\/___/ \/_/ \/____/\/_/ \/___L\ \/___/
/\____/
\_/__/
==========================================
like underscore.js, but for Go
$ go get github.com/ahl5esoft/golang-underscore
$ go get -u github.com/ahl5esoft/golang-underscore
* to be continued...
All
,AllBy
Any
,AnyBy
AsParallel
Chain
Clone
Each
Find
,FindBy
FindIndex
,FindIndexBy
First
Group
,GroupBy
IsArray
IsMatch
Keys
Last
Map
,MapBy
Md5
Object
Property
,PropertyRV
Range
Reduce
Reject
,RejectBy
Reverse
,ReverseBy
Size
Sort
,SortBy
Take
Uniq
,UniqBy
UUID
Value
Values
Where
,WhereBy
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
Object
Examples
arr := []int{ 1, 2, 3 }
Chain(arr).AsParallel().Each(func (n, i int) {
// code
})
Arguments
source
- array or map
Return
- IQuery - a wrapped object, wrapped objects until value is called
Examples
res := make(map[string][]int)
Chain([]int{1, 2, 1, 4, 1, 3}).Uniq(nil).Group(func(n, _ int) string {
if n%2 == 0 {
return "even"
}
return "old"
}).Value(&res)
// 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) boolmatch
- result
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{2, "two"},
TestModel{3, "three"},
}
var item TestModel
Find(arr, func(r TestModel, _ int) bool {
return r.Id == 1
}, &item)
// item == arr[0]
Arguments
source
- array or mapproperties
- map[string]interface{}match
- result
Examples
arr := []TestModel{
TestModel{1, "one"},
TestModel{2, "two"},
TestModel{3, "three"},
}
var item TestModel
FindBy(arr, map[string]interface{}{
"id": 2,
}, &item)
// 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 mapfirst
- first element ofsource
Examples
arr := []int{ 1, 2, 3 }
var n int
First(arr, &n)
if n != 1 {
//wrong
}
Arguments
source
- array or mapkeySelector
- func(element or value, index or key) anyTyperesult
- map[anyType][](element or value)
Examples
dic := make(map[string][]int)
Group([]int{1, 2, 3, 4, 5}, func(n, _ int) string {
if n%2 == 0 {
return "even"
}
return "odd"
}, &dic)
if len(dic["even"]) != 2 {
t.Error("wrong")
}
Arguments
source
- array or mapproperty
- property nameresult
- map[property type][](element or value)
Examples
arr := []TestModel{
TestModel{ID: 1, Name: "a"},
TestModel{ID: 2, Name: "a"},
TestModel{ID: 3, Name: "b"},
TestModel{ID: 4, Name: "b"},
}
dic := make(map[string][]TestModel)
GroupBy(arr, "name", &dic)
if len(dic) != 2 {
t.Error("wrong")
}
Arguments
source
- array or mapindexSelector
- func(element or value, index or key) anyTyperesult
- map[anyType](element or value)
Examples
res := make(map[string]string)
Index([]string{"a", "b"}, func(r string, _ int) string {
return r
}, &res)
if res["a"] != "a" {
// error
}
Arguments
source
- array or mapproperty
- stringresult
- map[propertyType](element or value)
Examples
arr := []TestModel{
TestModel{ID: 1, Name: "a"},
TestModel{ID: 2, Name: "a"},
TestModel{ID: 3, Name: "b"},
TestModel{ID: 4, Name: "b"},
}
res := make(map[string]TestModel)
IndexBy(arr, "Name", &res)
if len(res) != 2 {
// error
}
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
- mapkeys
- []keyType
Examples
dict := map[int]string{
1: "a",
2: "b",
3: "c",
4: "d",
}
res := make([]int, 0)
Keys(dict, &res)
if len(res) != len(dict) {
// wrong
}
Arguments
source
- array or maplast
- last element ofsource
Examples
arr := []int{1, 2, 3}
var n int
Last(arr, &n)
if n != 3 {
// wrong
}
dict := map[string]string{
"a": "aa",
"b": "bb",
}
var str string
Last(dict, &str)
if !(str == "aa" || str == "bb") {
// wrong
}
Arguments
source
- array or mapselector
- func(element, index or key) anyTyperesult
- an array of anyType
Examples
arr := []string{"11", "12", "13"}
res := make([]int, 0)
Map(arr, func(s string, _ int) int {
n, _ := strconv.Atoi(s)
return n
}, &res)
if len(res) != len(arr) {
// wrong
}
Arguments
source
- arrayproperty
- stringresult
- an array of property type
Examples
arr := []TestModel{
TestModel{ID: 1, Name: "one"},
TestModel{ID: 2, Name: "two"},
TestModel{ID: 3, Name: "three"},
}
res := make([]string, 0)
MapBy(arr, "name", &res)
if len(res) != len(arr) {
// wrong
}
for i := 0; i < 3; i++ {
if res[i] != arr[i].Name {
// wrong
}
}
Arguments
plaintext
- string
Return
- string - md5 string
Examples
if Md5("123456") != "e10adc3949ba59abbe56e057f20f883e" {
// wrong
}
Arguments
arr
- arrayresult
- map
Examples
arr := []interface{}{
[]interface{}{"a", 1},
[]interface{}{"b", 2},
}
dic := make(map[string]int)
Object(arr, &dic)
if len(dic) != 2 {
// wrong
}
if v, ok := dic["a"]; !(ok && v == 1) {
// wrong
}
if v, ok := dic["b"]; !(ok && v == 2) {
// 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
- IQuery - a wrapped object, wrapped objects until value is called
Examples
arr := make([]int, 0)
Range(0, 0, 1).Value(&arr)
if len(arr) != 0 {
// wrong
}
Range(0, 10, 0).Value(&arr)
if len(arr) != 0 {
// wrong
}
Range(10, 0, 1).Value(&arr)
if len(arr) != 0 {
// wrong
}
Range(0, 2, 1).Value(&arr)
if !(len(arr) == 2 && arr[0] == 0 && arr[1] == 1) {
// wrong
}
Range(0, 3, 2).Value(&arr)
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) boolresult
- an array of all the values that without pass a truth testpredicate
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{}result
- an array of all the values that without pass a truth testproperties
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 mapselector
- func(element, key or index) anyTyperesult
- an array ofsource
that reversed
Examples
arr := []TestModel{
TestModel{ID: 2, Name: "two"},
TestModel{ID: 1, Name: "one"},
TestModel{ID: 3, Name: "three"},
}
res := make([]TestModel, 0)
Reverse(arr, func(n TestModel, _ int) int {
return n.ID
}, &res)
if len(res) != len(arr) {
// error
}
if !(res[0].ID == 3 && res[1].ID == 2 && res[2].ID == 1) {
// error
}
Arguments
source
- array or mapproperty
- stringresult
- an array ofsource
that reversed
Examples
arr := []TestModel{
TestModel{ID: 2, Name: "two"},
TestModel{ID: 1, Name: "one"},
TestModel{ID: 3, Name: "three"},
}
res := make([]TestModel, 0)
ReverseBy(arr, "id", &res)
if len(res) != len(arr) {
// error
}
if !(res[0].ID == 3 && res[1].ID == 2 && res[2].ID == 1) {
// error
}
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) anyTyperesult
- an array ofsource
that sorted
Examples
arr := []TestModel{
TestModel{ID: 2, Name: "two"},
TestModel{ID: 1, Name: "one"},
TestModel{ID: 3, Name: "three"},
}
res := make([]TestModel, 0)
Sort(arr, func(n TestModel, _ int) int {
return n.ID
}, &res)
if len(res) != len(arr) {
// error
}
if !(res[0].ID == 1 && res[1].ID == 2 && res[2].ID == 3) {
// error
}
Arguments
source
- array or mapproperty
- stringresult
- an array ofsource
that sorted
Examples
arr := []TestModel{
TestModel{ID: 2, Name: "two"},
TestModel{ID: 1, Name: "one"},
TestModel{ID: 3, Name: "three"},
}
res := make([]TestModel, 0)
SortBy(arr, "id", &res)
if len(res) != len(arr) {
// error
}
if !(res[0].ID < res[1].ID && res[1].ID < res[2].ID) {
// error
}
Arguments
source
- array or mapcount
- intresult
- array
Examples
arr := []int{1, 2, 3}
res := make([]int, 0)
Take(arr, 1, &res)
if len(res) != 1 || res[0] != 1 {
// 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
Return
- interface{} - Chain final result
Examples
res := make(map[string][]int)
Chain([]int{1, 2, 1, 4, 1, 3}).Uniq(nil).Group(func(n, _ int) string {
if n%2 == 0 {
return "even"
}
return "old"
}).Value(&res)
if len(res) == 2 {
// wrong
}
Arguments
source
- mapvalues
- an array ofsource
's values
Examples
dict := map[int]string{
1: "a",
2: "b",
3: "c",
4: "d",
}
res := make([]string, 0)
Values(dict, &res)
if len(res) != len(dict) {
// wrong
}
Arguments
source
- array or mappredicate
- func(element or value, index or key) boolresult
- an array of all the values that pass a truth testpredicate
Examples
arr := []TestModel{
TestModel{ID: 1, Name: "one"},
TestModel{ID: 2, Name: "two"},
TestModel{ID: 3, Name: "three"},
TestModel{ID: 4, Name: "three"},
}
res := make([]TestModel, 0)
Where(arr, func(r TestModel, i int) bool {
return r.ID%2 == 0
}, &res)
if !(len(res) == 2 && res[0].ID == 2 && res[1].ID == 4) {
// wrong
}
Arguments
source
- array or mapproperties
- map[string]interface{}result
- an array of all the values that pass a truth testproperties
Examples
arr := []TestModel{
TestModel{ID: 1, Name: "one"},
TestModel{ID: 2, Name: "one"},
TestModel{ID: 3, Name: "three"},
TestModel{ID: 4, Name: "three"},
}
res := make([]TestModel, 0)
WhereBy(arr, map[string]interface{}{
"Name": "one",
}, &res)
if !(len(res) == 2 && res[0] == arr[0] && res[1] == arr[1]) {
// wrong
}