Categorygithub.com/smarkm/golang-underscore
modulepackage
1.0.0
Repository: https://github.com/smarkm/golang-underscore.git
Documentation: pkg.go.dev

# README

                  /\ \                                                       
 __  __    ___    \_\ \     __   _ __   ____    ___    ___   _ __    __	         __     ___
/\ \/\ \ /' _ `\  /'_  \  /'__`\/\  __\/ ,__\  / ___\ / __`\/\  __\/'__`\      /'_ `\  / __`\
\ \ \_\ \/\ \/\ \/\ \ \ \/\  __/\ \ \//\__, `\/\ \__//\ \ \ \ \ \//\  __/  __ /\ \L\ \/\ \L\ \
 \ \____/\ \_\ \_\ \___,_\ \____\\ \_\\/\____/\ \____\ \____/\ \_\\ \____\/\_\\ \____ \ \____/
  \/___/  \/_/\/_/\/__,_ /\/____/ \/_/ \/___/  \/____/\/___/  \/_/ \/____/\/_/ \/___L\ \/___/
                                                                                 /\____/
                                                                                 \_/__/

Underscore.go GoDoc Go Report Card

like underscore.js, but for Go

Installation

$ go get github.com/ahl5esoft/golang-underscore

Update

$ go get -u github.com/ahl5esoft/golang-underscore

Lack

  • more...

Suggest

always using `chain`

Documentation

API

All(source, predicate)

Arguments

  • source - array or map
  • predicate - func(element, index or key) bool

Return

  • bool - all the values that pass a truth test predicate

Examples

src := []testModel{
	{ID: 1, Name: "one"},
	{ID: 1, Name: "two"},
	{ID: 1, Name: "three"},
}
ok := All(src, func(r testModel, _ int) bool {
	return r.Id == 1
})
// ok == true

AllBy(source, properties)

Arguments

  • source - array or map
  • properties - map[string]interface{}

Return

  • bool

Examples

src := []testModel{
	{ID: 1, Name: "one"},
	{ID: 1, Name: "two"},
	{ID: 1, Name: "three"},
}
ok := AllBy(src, nil)
// ok == true

ok = AllBy(src, map[string]interface{}{
	"name": "a",
})
// ok == false

ok = AllBy(src, map[string]interface{}{
	"id": 1,
})
// ok == true

Any(source, predicate)

Arguments

  • source - array or map
  • predicate - func(element or value, index or key) bool

Return

  • bool - any of the values that pass a truth test predicate

Examples

src := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
ok := Any(src, func(r testModel, _ int) bool {
	return r.Id == 0
})
// ok == false

AnyBy(source, properties)

Arguments

  • source - array or map
  • properties - map[string]interface{}

Return

  • bool

Examples

src := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
ok := AnyBy(src, map[string]interface{}{
	"Id": 0,
})
// ok == false

ok = AnyBy(src, map[string]interface{}{
	"id":   src[0].Id,
	"name": src[0].Name,
})
// ok == true

Chain(source).AsParallel()...

Support

  • Each
  • Object

Examples

arr := []int{ 1, 2, 3 }
Chain(arr).AsParallel().Each(func (n, i int) {
	// code
})

Chain(source)

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

Clone()

Return

  • interface{}

Examples

arr := []int{1, 2, 3}
var duplicate []int
Chain(arr).Clone().Value(&duplicate)
// or
duplicate := Clone(arr)
ok := All(duplicate, func(n, i int) bool {
	return arr[i] == n
})
// ok == true

Each(source, iterator)

Arguments

  • source - array or map
  • iterator - func(element or value, index or key)

Examples

src := []testModel{
	{ID: 1, Name: "one"},
	{ID: 1, Name: "two"},
	{ID: 1, Name: "three"},
}
Each(src, func (r testModel, i int) {
	// coding
})

Find(source, predicate)

Arguments

  • source - array or map
  • predicate - func(element or value, index or key) bool

Return

  • interface{} -- source elem

Examples

src := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
var item testModel
Chain(src).Find(func(r testModel, _ int) bool {
	return r.ID == 1
}).Value(&item)
// or
item := Find(src, func(r testModel, _ int) bool {
	return r.ID == 1
})
// item == arr[0]

FindBy(source, properties)

Arguments

  • source - array or map
  • properties - map[string]interface{}

Return

  • interface{} - source elem

Examples

arr := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
var item testModel
Chain(arr).FindBy(map[string]interface{}{
	"id": 2,
}).Value(&item)
// or
item := FindBy(arr, map[string]interface{}{
	"id": 2,
})
// item == arr[1]

FindIndex(source, predicate)

Arguments

  • source - array or map
  • predicate - func(element or value, index or key) bool

Return

  • int - index

Examples

arr := []testModel{
	{ID: 1, Name: "one"},
	{ID: 1, Name: "two"},
	{ID: 1, Name: "three"},
}
i := FindIndex(arr, func(r testModel, _ int) bool {
	return r.Name == arr[1].Name
})
// i == 1

FindIndexBy(source, properties)

Arguments

  • source - array or map
  • properties - map[string]interface{}

Return

  • int - index

Examples

arr := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
i := FindIndexBy(arr, map[string]interface{}{
	"id": 1,
})
// i == 0

First(source)

Arguments

  • source - array or map

Return

  • interface{}

Examples

arr := []int{ 1, 2, 3 }
var res int
Chain(arr).First().Value(&res)
// or
res := First(arr).(int)
// res = 1

res := First(nil) 
// res == nil

Group(source, keySelector)

Arguments

  • source - array or map
  • keySelector - func(element or value, index or key) anyType

Return

  • interface{} - map[anyType][](element or value)

Examples

src := []int{ 1, 2, 3, 4, 5 }
var res map[string][]int
Chain(src).Group(func (n, _ int) string {
	if n % 2 == 0 {
		return "even"
	}
	return "odd"
}).Value(&res)
// or
res := Group(src, func (n, _ int) string {
	if n % 2 == 0 {
		return "even"
	}
	return "odd"
}).(map[string][]int)
// res = map[odd:[1 3 5] even:[2 4]]

GroupBy(source, property)

Arguments

  • source - array or map
  • property - property name

Return

  • interface{} - map[property type][](element or value)

Examples

arr := []testModel{
	{ID: 1, Name: "a"},
	{ID: 2, Name: "a"},
	{ID: 3, Name: "b"},
	{ID: 4, Name: "b"},
}
var res map[string][]testModel
Chain(arr).GroupBy("name").Value(&res)
// or
res := GroupBy(arr, "name").(map[string][]testModel)
// res = map[a:[{{0} 1 a} {{0} 2 a}] b:[{{0} 3 b} {{0} 4 b}]]

Index(source, indexSelector)

Arguments

  • source - array or map
  • indexSelector - func(element or value, index or key) anyType

Return

  • interface{} - map[anyType](element or value)

Examples

src := []string{ "a", "b" }
var res map[string]string
Chain(src).Index(func (r string, _ int) string {
	return r
}).Value(&res)
// or
res := Index(src, func (r string, _ int) string {
	return r
}).(map[string]string)
// res = map[a:a b:b]

IndexBy(source, property)

Arguments

  • source - array or map
  • property - string

Return

  • interface{} - map[propertyType](element or value)

Examples

arr := []testModel{
	{ID: 1, Name: "a"},
	{ID: 2, Name: "a"},
	{ID: 3, Name: "b"},
	{ID: 4, Name: "b"},
}
var res map[int]testModel
Chain(arr).IndexBy("id").Value(&res)
// or
res := IndexBy(arr, "id").(map[int]testModel)
// res = map[1:{{0} 1 a} 2:{{0} 2 a} 3:{{0} 3 b} 4:{{0} 4 b}]

IsArray(element)

Arguments

  • element - object

Return

  • bool

Examples

if !IsArray([]int{}) {
	// wrong
}

if IsArray(map[string]int{}) {
	// wrong
}

IsMatch(element, properties)

Arguments

  • element - object
  • properties - map[string]interface{}

Return

  • bool

Examples

m := testModel{ 1, "one" }
ok := IsMatch(nil, nil)
// ok = false

ok = IsMatch(m, nil)
// ok = false

ok = IsMatch(m, map[string]interface{}{
	"id": m.Id,
	"name": "a",
})
// ok = false

ok = IsMatch(m, map[string]interface{}{
	"id": m.Id,
	"name": m.Name,
})
// ok = true

Keys()

Arguments

  • source - map

Return

  • interface{} - []keyType

Examples

arr := []string{ "aa" }
v := Keys(arr)
// v = nil

dict := map[int]string{	
	1: "a",
	2: "b",
	3: "c",
	4: "d",
}
var res []int
Chain(dict).Keys().Value(&res)
// or
res := Keys(dict).([]int)
// res = [1 2 3 4]

Last(source)

Arguments

  • source - array or map

Return

  • interface{} - last element of source

Examples

arr := []int{1, 2, 3}
var res int
chain(arr).Last().Value(&res)
// or
res := Last(arr).(int)
// res = 3

dict := map[string]string{
	"a": "aa",
	"b": "bb",
}
var str string
Chain(dict).Last().Value(&str)
// or
str := Last(dict).(string)
// res = "aa" or "bb"

Map(source, selector)

Arguments

  • source - array or map
  • selector - func(element, index or key) anyType

Return

  • interface{} - an slice of property value

Examples

arr := []string{ "11", "12", "13" }
var res []int
Chain(arr).Map(func (s string, _ int) int {
	n, _ := strconv.Atoi(s)
	return n
}).Value(&res)
// or
res := Map(arr, func (s string, _ int) int {
	n, _ := strconv.Atoi(s)
	return n
}).([]int)
// res = [11 12 13]

MapBy(source, property)

Arguments

  • source - array
  • property - string

Return

  • interface{} - an slice of property value

Examples

arr := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
var res []string
Chain(arr).MapBy("name").Value(&res)
// or
res := MapBy(arr, "name").([]string)
// res = [one two three]

MapMany(source, selector)

Arguments

  • source - array or map
  • selector - func(element, index or key) anyType with array or slice

Return

  • interface{} - an slice of property elem value

Examples

src := []int{1, 2}
MapMany(src, func(r, _ int) int {
	return r // will panic because `r` is not array or slice
})
// or
Chain(src).MapMany(func(r, _ int) int {
	return r // will panic because `r` is not array or slice
})

var res []int
Chain(src).MapMany(func(r, _ int) []int {
	var temp []int
	Range(0, r, 1).Map(func(_, _ int) int {
		return r
	}).Value(&temp)
	return temp
}).Value(&res)
// or
res := MapMany(src, func(r, _ int) []int {
	var temp []int
	Range(0, r, 1).Map(func(_, _ int) int {
		return r
	}).Value(&temp)
	return temp
}).([]int)
// res = [1 2 2]

MapManyBy(source, property)

Arguments

  • source - array
  • property - string

Return

  • interface{} - an slice of propery elem value

Examples

src := []mapManyTestModel{
	{Slice: []string{"a", "b"}},
	{Slice: []string{"c", "d"}},
}
Chain(src).MapManyBy("Str") // will panic because `Str` property value is not array or slice
// or
MapManyBy(src, "Str") // will panic because `Str` property value is not array or slice

var res []string
Chain(src).MapManyBy("Slice").Value(&res)
// or
res := MapManyBy(src, "Slice").([]string)
// res = [a b c d]

Md5(plaintext)

Arguments

  • plaintext - string

Return

  • string - md5 string

Examples

if Md5("123456") != "e10adc3949ba59abbe56e057f20f883e" {
	// wrong
}	

Object(arr)

Arguments

  • arr - array

Return

  • map, error

Examples

arr := []interface{}{
	[]interface{}{"a", 1},
	[]interface{}{"b", 2},
}
var res map[string]int
Chain(arr).Object().Value(&res)
// or
res := Object(arr).(map[string]int)
// res = map[b:2 a:1] or map[a:1 b:2]

Property(name)

Arguments

  • name - property name

Return

  • func(interface{}) (interface{}, error)

Examples

item := testModel{ 1, "one" }

getAge := Property("age")
_, err := getAge(item)
// err != nil

getName := Property("name")
name, err := getName(item)
// name = "one"

Property(name)

Arguments

  • name - property name

Return

  • func(interface{}) (reflect.Value, error)

Examples

item := testModel{ 1, "one" }

getAgeRV := PropertyRV("age")
_, err := getAgeRV(item)
// err != nil

getNameRV := PropertyRV("name")
nameRV, err := getNameRV(item)
// nameRV = reflect.ValueOf("one")

Range(start, stop, step)

Arguments

  • start - int
  • stop - int
  • step - int

Return

  • IQuery - a wrapped object, wrapped objects until value is called

Examples

var res []int
Range(0, 0, 1).Value(&res)
// res = []

var res []int
Range(0, 10, 0).Value(&res)
// res = []

var res []int
Range(10, 0, 1).Value(&res)
// res = []

var res []int
Range(0, 2, 1).Value(&res)
// res = [0 1]

var res []int
Range(0, 3, 2).Value(&res)
// res = [0 2]

Reduce(source, iterator)

Arguments

  • source - array
  • iterator - func(memo, element or value, key or index) memo
  • memo - anyType

Return

  • interface{} - memo

Examples

var res []int
Chain([]int{1, 2}).Reduce(func(memo []int, n, _ int) []int {
	memo = append(memo, n)
	memo = append(memo, n+10)
	return memo
}, make([]int, 0)).Value(&res)
// or
res := Reduce([]int{1, 2}, func(memo []int, n, _ int) []int {
	memo = append(memo, n)
	memo = append(memo, n+10)
	return memo
}, make([]int, 0)).([]int)
// res = [1 11 2 12]

Reject(source, predicate)

Arguments

  • source - array or map
  • predicate - 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}
var res []int
Chain(arr).Reject(func(n, i int) bool {
	return n%2 == 0
}).Value(&res)
// or
res := Reject(arr, func(n, i int) bool {
	return n%2 == 0
}).([]int)
// res = [1 3]

RejectBy(source, properties)

Arguments

  • source - array or map
  • properties - map[string]interface{}

Return

  • interface{} - an array of all the values that without pass a truth test properties

Examples

arr := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
var res []testModel
Chain(arr).RejectBy(map[string]interface{}{
	"Id": 1,
}).Value(&res)
// or
res := RejectBy(arr, map[string]interface{}{
	"Id": 1,
}).([]testModel)
// res = [{{0} 2 two} {{0} 3 three}]

Reverse(source, selector)

Arguments

  • source - array or map
  • selector - func(element, key or index) anyType

Return

  • interface{} - an array of source that reversed

Examples

arr := []testModel{
	{ID: 2, Name: "two"},
	{ID: 1, Name: "one"},
	{ID: 3, Name: "three"},
}
var res []testModel
Chain(arr).Reverse(func(n testModel, _ int) int {
	return n.ID
}).Value(&res)
// or
res := Reverse(arr, func(n testModel, _ int) int {
	return n.ID
}).([]testModel)
// res = [{{0} 3 three} {{0} 2 two} {{0} 1 one}]

ReverseBy(source, selector)

Arguments

  • source - array or map
  • property - string

Return

  • interface{} - an array of source that reversed

Examples

arr := []testModel{
	{ID: 2, Name: "two"},
	{ID: 1, Name: "one"},
	{ID: 3, Name: "three"},
}
var res []testModel
Chain(arr).ReverseBy("id").Value(&res)
// or
res := ReverseBy(arr, "id").([]testModel)
// res = [{{0} 3 three} {{0} 2 two} {{0} 1 one}]

Size(source)

Arguments

  • source - array or map

Return

  • int

Examples

dict := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}
l := Size(dict)
// l = 3

Sort(source, selector)

Arguments

  • source - array or map
  • selector - func(element, key or index) anyType

Return

  • interface{} - an array of source that sorted

Examples

arr := []testModel{
	{ID: 2, Name: "two"},
	{ID: 1, Name: "one"},
	{ID: 3, Name: "three"},
}
var res []testModel
Chain(arr).Sort(func(n testModel, _ int) int {
	return n.ID
}).Value(&res)
// or
res := Sort(arr, func(n testModel, _ int) int {
	return n.ID
}).([]testModel)
// res = [{{0} 1 one} {{0} 2 two} {{0} 3 three}]

SortBy(source, property)

Arguments

  • source - array or map
  • property - string

Return

  • interface{}

Examples

arr := []testModel{
	{ID: 2, Name: "two"},
	{ID: 1, Name: "one"},
	{ID: 3, Name: "three"},
}
var res []testModel
Chain(arr).SortBy("id").Value(&res)
// or
res := SortBy(arr, "id").([]testModel)
// res = [{{0} 1 one} {{0} 2 two} {{0} 3 three}]

Take(source, count)

Arguments

  • source - array or map
  • count - int

Return

  • interface{}

Examples

arr := []int{1, 2, 3}
var res []int
Chain(arr).Take(1).Value(&res)
// or
res := Take(arr, 1).([]int)
// res = [1]

Uniq(source, selector)

Arguments

  • source - array
  • selector - nil or func(element or value, index or key) anyType

Return

  • interface{} - only the first occurence of each value is kept

Examples

arr := []int{1, 2, 1, 4, 1, 3}
var res []int
Chain(arr).Uniq(func(n, _ int) int {
	return n % 2
}).Value(&res)
// or
res := Uniq(arr, func(n, _ int) int {
	return n % 2
}).([]int)
// res = [1 2]

UniqBy(source, property)

Arguments

  • source - array
  • property - string

Return

  • interface{}

Examples

arr := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "one"},
	{ID: 3, Name: "one"},
}
var res []testModel
Chain(arr).UniqBy("name").Value(&res)
// or
res := UniqBy(arr, "Name").([]testModel)
// res = [{{0} 1 one}]

UUID()

Return

  • string - uuid string

Examples

uuid := UUID()
//1a40272540e57d1c80e7b06042219d0c

Value(result)

Examples

arr := []int{1, 2, 1, 4, 1, 3}
var res map[string][]int
Chain(arr).Uniq(nil).Group(func(n, _ int) string {
	if n%2 == 0 {
		return "even"
	}

	return "old"
}).Value(&res)
// res = map[old:[1 3] even:[2 4]]

Values(source)

Arguments

  • source - map

Return

  • interface{} - an array of source's values

Examples

src := map[int]string{
	1: "a",
	2: "b",
	3: "c",
	4: "d",
}
var res []string
Chain(src).Values().Value(&res)
// or
res := Values(src).([]string)
// res = [a b c d]

Where(source, predicate)

Arguments

  • source - array or map
  • predicate - func(element or value, index or key) bool

Return

  • interface{} - an array of all the values that pass a truth test predicate

Examples

src := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
var res []testModel
Chain(src).Where(func(r testModel, i int) bool {
	return r.ID%2 == 0
}).Value(&res)
// or
res := Where(src, func(r testModel, i int) bool {
	return r.ID%2 == 0
}).([]testModel)
// res = [{{0} 2 two}]

WhereBy(source, properties)

Arguments

  • source - array or map
  • properties - map[string]interface{}

Return

  • interface{} - an array of all the values that pass a truth test properties

Examples

src := []testModel{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	{ID: 3, Name: "three"},
}
var res []testModel
Chain(src).WhereBy(map[string]interface{}{
	"name": "one",
}).Value(&res)
// or
res := WhereBy(src, map[string]interface{}{
	"name": "one",
}).([]testModel
// res = [{{0} 1 one}]

# Functions

All of the values in the `source` pass the `predicate` truth test.
AllBy will stop traversing the `source` if a false element is found.
Any is if any of the values in the `source` pass the `predicate` truth test.
AnyBy will stop traversing the `source` if a true element is found.
Chain will cause all future method calls to return wrapped objects.
Clone will create a deep-copied clone of the `src`.
Each will iterate over a list of elements.
Find is 根据断言获取元素.
FindBy is 根据属性值获取元素.
FindIndex is 根据断言函数获取下标.
FindIndexBy is 根据字典获取下标.
FindLastIndex gets the last index of the argument.
First is 获取第一个元素.
Group is 分组.
GroupBy is 根据某个属性分组.
Index is 转化为indexSelector筛选出的值为key的map.
IndexBy is 转化为property值的map.
IsArray is 判断是否数组或者切片.
IsMatch is 对象中的属性名与属性值都与map的key和value相同.
Keys is 获取map的所有key.
Last is 最后元素.
Map 映射.
MapBy is 从source中取出所有property.
MapMany is 将序列的每个元素投影到一个序列,并将结果序列合并为一个序列.
MapManyBy is 将序列的每个元素的property值(值必须是一个序列)合并为一个序列.
Md5 is 字符串转md5.
Object 将二维数组转化为字典.
Property is 获取属性函数.
PropertyRV is 获取reflect.Value.
Range is 生成区间数据.
Reduce is 聚合.
Reject is 排除.
RejectBy is 根据属性排除.
Reverse is 倒序.
ReverseBy is 根据属性倒序.
Size is 数组或字典的长度.
Sort is 排序.
SortBy is 根据属性排序.
Take is 获取从0开始的n个元素.
ToRealValue is 将反射值转为真实类型的值.
Uniq is 去重.
UniqBy is 根据某个属性去重.
UUID is 生成UUID.
Values is 字典的所有value.
Where is 获取所有满足条件.
WhereBy is 获取所有满足条件.

# Interfaces

IQuery is interface.

# Type aliases

GetProeprtyRVFunc is get property reflect.Value func.