underscore

package module
v0.0.0-...-95ae506 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2019 License: MIT Imports: 8 Imported by: 0

README

                  /\ \                                                       
 __  __    ___    \_\ \     __   _ __   ____    ___    ___   _ __    __	         __     ___
/\ \/\ \ /' _ `\  /'_  \  /'__`\/\  __\/ ,__\  / ___\ / __`\/\  __\/'__`\      /'_ `\  / __`\
\ \ \_\ \/\ \/\ \/\ \ \ \/\  __/\ \ \//\__, `\/\ \__//\ \ \ \ \ \//\  __/  __ /\ \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

* to be continued...

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
  • 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}
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, match)

Arguments

  • source - array or map
  • predicate - func(element or value, index or key) bool
  • match - 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]
FindBy(source, properties, match)

Arguments

  • source - array or map
  • properties - 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]
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, first)

Arguments

  • source - array or map
  • first - first element of source

Examples

arr := []int{ 1, 2, 3 }
var n int
First(arr, &n)
if n != 1 {
	//wrong
}
Group(source, keySelector, result)

Arguments

  • source - array or map
  • keySelector - func(element or value, index or key) anyType
  • result - 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")
}
GroupBy(source, property, result)

Arguments

  • source - array or map
  • property - property name
  • result - 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")
}
Index(source, indexSelector, result)

Arguments

  • source - array or map
  • indexSelector - func(element or value, index or key) anyType
  • result - 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
}
IndexBy(source, property, result)

Arguments

  • source - array or map
  • property - string
  • result - 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
}
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(keys)

Arguments

  • source - map
  • keys - []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
}
Last(source, last)

Arguments

  • source - array or map
  • last - last element of source

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

Arguments

  • source - array or map
  • selector - func(element, index or key) anyType
  • result - 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
}
MapBy(source, property, result)

Arguments

  • source - array
  • property - string
  • result - 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
	}
}
Md5(plaintext)

Arguments

  • plaintext - string

Return

  • string - md5 string

Examples

if Md5("123456") != "e10adc3949ba59abbe56e057f20f883e" {
	// wrong
}	
Object(arr, result)

Arguments

  • arr - array
  • result - 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
}
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

  • 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
}
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, result)

Arguments

  • source - array or map
  • predicate - func(element or value, index or key) bool
  • result - 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, result)

Arguments

  • source - array or map
  • properties - map[string]interface{}
  • result - 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
}
Reverse(source, selector, result)

Arguments

  • source - array or map
  • selector - func(element, key or index) anyType
  • result - an array of source 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
}
ReverseBy(source, selector, result)

Arguments

  • source - array or map
  • property - string
  • result - an array of source 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
}
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, result)

Arguments

  • source - array or map
  • selector - func(element, key or index) anyType
  • result - an array of source 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
}
SortBy(source, property, result)

Arguments

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

Arguments

  • source - array or map
  • count - int
  • result - array

Examples

arr := []int{1, 2, 3}
res := make([]int, 0)
Take(arr, 1, &res)
if len(res) != 1 || res[0] != 1 {
	// 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
Value()

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
}
Values(source, values)

Arguments

  • source - map
  • values - an array of source'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
}
Where(source, predicate, result)

Arguments

  • source - array or map
  • predicate - func(element or value, index or key) bool
  • result - an array of all the values that pass a truth test predicate

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
}
WhereBy(source, properties, result)

Arguments

  • source - array or map
  • properties - map[string]interface{}
  • result - an array of all the values that pass a truth test properties

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(source, predicate interface{}) bool

All of the values in the `source` pass the `predicate` truth test

func AllBy

func AllBy(source interface{}, properties map[string]interface{}) bool

AllBy will stop traversing the `source` if a false element is found

func Any

func Any(source, predicate interface{}) bool

Any is if any of the values in the `source` pass the `predicate` truth test

func AnyBy

func AnyBy(source interface{}, properties map[string]interface{}) bool

AnyBy will stop traversing the `source` if a true element is found

func Clone

func Clone(src, dst interface{})

Clone will create a deep-copied clone of the `src`

func Each

func Each(source, iterator interface{})

Each will iterate over a list of elements

func Find

func Find(source, predicate, match interface{})

Find is 根据断言获取元素

func FindBy

func FindBy(source interface{}, properties map[string]interface{}, match interface{})

FindBy is 根据属性值获取元素

func FindIndex

func FindIndex(source, predicate interface{}) int

FindIndex is 根据断言函数获取下标

func FindIndexBy

func FindIndexBy(source interface{}, properties map[string]interface{}) int

FindIndexBy is 根据字典获取下标

func FindLastIndex

func FindLastIndex(source interface{}) int

FindLastIndex gets the last index of the argument

func First

func First(source, first interface{})

First is 获取第一个元素

func Group

func Group(source, keySelector, result interface{})

Group is 分组

func GroupBy

func GroupBy(source interface{}, property string, result interface{})

GroupBy is 根据某个属性分组

func Index

func Index(source, indexSelector, result interface{})

Index is 转化为indexSelector筛选出的值为key的map

func IndexBy

func IndexBy(source interface{}, property string, res interface{})

IndexBy is 转化为property值的map

func IsArray

func IsArray(v interface{}) bool

IsArray is 判断是否数组或者切片

func IsMatch

func IsMatch(item interface{}, properties map[string]interface{}) bool

IsMatch is 对象中的属性名与属性值都与map的key和value相同

func Keys

func Keys(source, keys interface{})

Keys is 获取map的所有key

func Last

func Last(source, last interface{})

Last is 最后元素

func Map

func Map(source, selector, result interface{})

Map 映射

func MapBy

func MapBy(source interface{}, property string, result interface{})

MapBy is 从source中取出所有property

func Md5

func Md5(plaintext string) string

Md5 is 字符串转md5

func Object

func Object(source, result interface{})

Object 将二维数组转化为字典

func Property

func Property(name string) func(interface{}) interface{}

Property is 获取属性函数

func Reduce

func Reduce(source, iterator, memo interface{}) interface{}

Reduce is 聚合

func Reject

func Reject(source, predicate, result interface{})

Reject is 排除

func RejectBy

func RejectBy(source interface{}, properties map[string]interface{}, result interface{})

RejectBy is 根据属性排除

func Reverse

func Reverse(source, selector, result interface{})

Reverse is 倒序

func ReverseBy

func ReverseBy(source interface{}, property string, result interface{})

ReverseBy is 根据属性倒序

func Size

func Size(source interface{}) int

Size is 数组或字典的长度

func Sort

func Sort(source, selector, result interface{})

Sort is 排序

func SortBy

func SortBy(source interface{}, property string, result interface{})

SortBy is 根据属性排序

func Take

func Take(source interface{}, count int, result interface{})

Take is 获取从0开始的n个元素

func ToRealValue

func ToRealValue(rv reflect.Value) interface{}

ToRealValue is 将反射值转为真实类型的值

func UUID

func UUID() string

UUID is 生成UUID

func Uniq

func Uniq(source, selector, result interface{})

Uniq is 去重

func UniqBy

func UniqBy(source interface{}, property string, result interface{})

UniqBy is 根据某个属性去重

func Values

func Values(source, values interface{})

Values is 字典的所有value

func Where

func Where(source, predicate, result interface{})

Where is 获取所有满足条件

func WhereBy

func WhereBy(source interface{}, properties map[string]interface{}, result interface{})

WhereBy is 获取所有满足条件

Types

type GetProeprtyRVFunc

type GetProeprtyRVFunc func(interface{}) reflect.Value

GetProeprtyRVFunc is get property reflect.Value func

func PropertyRV

func PropertyRV(name string) GetProeprtyRVFunc

PropertyRV is 获取reflect.Value

type IQuery

type IQuery interface {
	All(interface{}) bool
	AllBy(map[string]interface{}) bool
	Any(interface{}) bool
	AnyBy(map[string]interface{}) bool
	AsParallel() IQuery
	Clone() IQuery
	Each(interface{}) IQuery
	Find(interface{}) IQuery
	FindBy(map[string]interface{}) IQuery
	FindIndex(interface{}) int
	FindIndexBy(map[string]interface{}) int
	FindLastIndex() IQuery
	First() IQuery
	Group(interface{}) IQuery
	GroupBy(string) IQuery
	Index(interface{}) IQuery
	IndexBy(string) IQuery
	Keys() IQuery
	Last() IQuery
	Map(interface{}) IQuery
	MapBy(string) IQuery
	Object() IQuery
	Reduce(interface{}, interface{}) IQuery
	Reject(interface{}) IQuery
	RejectBy(map[string]interface{}) IQuery
	Reverse(interface{}) IQuery
	ReverseBy(string) IQuery
	Size() int
	Sort(interface{}) IQuery
	SortBy(string) IQuery
	Take(int) IQuery
	Uniq(interface{}) IQuery
	UniqBy(string) IQuery
	Value(v interface{})
	Values() IQuery
	Where(interface{}) IQuery
	WhereBy(map[string]interface{}) IQuery
}

IQuery is interface

func Chain

func Chain(source interface{}) IQuery

Chain will cause all future method calls to return wrapped objects

func Range

func Range(start, stop, step int) IQuery

Range is 生成区间数据

Jump to

Keyboard shortcuts

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