monjovi / golang-underscore

Helpfully Functional Go like underscore.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Underscore.go

like underscore.js, but for Go

Installation

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

Update

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

Lack

* FindLastIndex
* Sample
* more...

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

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
### AllBy(source, properties)

Arguments

  • source - array or map
  • properties - 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
### 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

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
### AnyBy(source, properties)

Arguments

  • source - array or map
  • properties - 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
### Chain(source).AsParallel()...

Support

  • Each

Examples

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

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
### Clone()

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
### Each(source, iterator)

Arguments

  • source - array or map
  • iterator - 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
})
### Find(source, predicate)

Arguments

  • source - array or map
  • predicate - 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]
### FindBy(source, properties)

Arguments

  • source - array or map
  • properties - 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]
### FindIndex(source, predicate)

Arguments

  • source - array or map
  • predicate - 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
### FindIndexBy(source, properties)

Arguments

  • source - array or map
  • properties - 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
### First(source)

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
}
### 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

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")
}
### GroupBy(source, property)

Arguments

  • source - array or map
  • property - 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")
}
### 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

v, _ := Index([]string{ "a", "b" }, func (item string, _ int) string {
	return item
})
res, ok := v.(map[string]string)
if !(ok && res["a"] == "a") {
	// wrong
}
### IndexBy(source, property)

Arguments

  • source - array or map
  • property - 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
}
### 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)
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
}
### Keys()

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
}
### Map(source, selector)

Arguments

  • source - array or map
  • selector - 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
}
### Md5(plaintext)

Arguments

  • plaintext - string

Return

  • string - md5 string

Examples

if Md5("123456") != "e10adc3949ba59abbe56e057f20f883e" {
	// wrong
}	
### ParseJson(str, container)

Arguments

  • str - json string
  • container - interface{}

Return

  • error

Examples

str := `["a","b"]`
var arr []string
err := ParseJson(str, &arr)
if !(err == nil && len(arr) == 2) {
	// wrong
}
### MapBy(source, property)

Arguments

  • source - array or map
  • property - 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
}
### Pluck(source, property)

Arguments

  • source - array
  • property - 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
	}
}
### Property(name)

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
}
### Property(name)

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
}
### Range(start, stop, step)

Arguments

  • start - int
  • stop - int
  • step - 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
}
### Reduce(source, iterator)

Arguments

  • source - array
  • iterator - func(memo, element or value, key or index) memo
  • memo - 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
}
### 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 }
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
}
### 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{
	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
}
### Select(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

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")
}
### SelectBy(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

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
}
### Size(source)

Arguments

  • source - array or map

Return

  • int

Examples

dict := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}
if Size(dict) != len(dict) {
	// wrong
}
### 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 := []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
	}
}
### SortBy(source, property)

Arguments

  • source - array or map
  • property - 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
}
### Take(source, count)

Arguments

  • source - array or map
  • count - 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
}
### ToJson(value)

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
}
### 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

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
}
### UniqBy(source, property)

Arguments

  • source - array
  • property - 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
}
### UUID()

Return

  • string - uuid string

Examples

uuid := UUID()
//1a40272540e57d1c80e7b06042219d0c
### Values(source)

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
}

About

Helpfully Functional Go like underscore.js

License:MIT License


Languages

Language:Go 100.0%