go-dash
is a modern Go library based on reflect.
Generic helpers rely on reflect, be careful, this code runs exclusively at runtime, so you must have a good test suite.
go-dash
may look like lodash in JavaScript because I write the go-dash base on lodash, but it will have a version upgrade and have its own roadmap.
We always write the code for the go-dash with the coverage of unit tests equal to 100 percent.
Lodash is a popular and widely used JavaScript utility library that provides a comprehensive set of functions to work with arrays, objects, strings, functions, and more. It aims to simplify and streamline common programming tasks and data manipulation in JavaScript applications.
Because it is based on lodash and I write it with Golang, I decided to give it a name that is go-dash
. I hope everyone who can contribute to the go-dash
will help make it more popular, flexible, and stable.
go get github.com/warriors-vn/go-dash
You can import go-dash
using a basic statement:
import "github.com/warriors-vn/go-dash"
These examples will be based on the following data model:
type User struct {
Name string
Age int
}
There are some examples about go-dash. You can find all function about: array, collection, date, math,lang, number, string in the library.
Some examples:
The function returns the new filtered slice and an error if any occurs.Filter([]int{1, 2, 3, 4}, func(n int) bool {
return n%2 == 0
}) // []int{2, 4}
Filter([]string{"a", "b", "c"}, func(n string) bool {
return n > "a"
}) // []string{"b", "c"}
Filter([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool {
return n.Age%2 == 0
}) // []*User{{Name: "Kakalot", Age: 26}, {Name: "Trunk", Age: 10}}
Find([]int{1, 2, 3, 4}, func(n int) bool {
return n%2 == 0
}) // 2
Find([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool {
return n.Age%2 == 0
}) // &User{Name: "Kakalot", Age: 26}
Chunk([]int{1, 2, 3}, 1) // [][]int{{1}, {2}, {3}}
Chunk([]string{"a", "b", "c", "d"}, 3) // [][]string{{"a", "b", "c"}, {"d"}}
Chunk([]interface{}{1, "2", true}, 1) // [][]interface{}{{1}, {"2"}, {true}}
Intersection([]int{1, 2, 3}, []int{1}) // []int{1}
Intersection([]string{"1.1", "2.2", "3.3"}, []string{"1.1", "3.3"}) // []string{"1.1", "3.3"}
Difference([]int{2, 1}, []int{2, 3}) // []int{1}
Difference([]string{"2", "1", "2", "2", "5"}, []string{"2", "3"}) // []string{"1", "5"}
IndexOf([]string{"1", "2", "b", "3", "c", "b"}, "b", 2) // 2
IndexOf([]interface{}{1.1, "2.2", 3.3}, 2.2, 0) // -1
IndexOf([]float32{1.1, 2.2, 3.3}, float32(2.2), -6) // 1
Every([]int{1, 2, 3, 4}, func(n int) bool {
return n%2 == 0
}) // false
Every([]string{"d", "b", "c"}, func(n string) bool {
return n > "a"
}) // true
Every([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool {
return n.Age%2 == 0
}) // false
Partition([]int{1, 2, 3, 4}, func(n int) bool {
return n%2 == 0
}) // [][]int{{2, 4}, {1, 3}}
Partition([]string{"a", "b", "c"}, func(n string) bool {
return n > "a"
}) // [][]string{{"b", "c"}, {"a"}}
Drop([]int{1, 2, 3}, 2) // []int{3}
Drop([]string{"1", "2", "3"}, 2) // []string{"3"}
Drop([]interface{}{1, "2", true}, 2) // []interface{}{true}
Initial([]int{1, 2, 3}) // []int{1, 2}
Initial([]string{"a", "b", "c"}) // []string{"a", "b"}
Initial([]interface{}{"true", 1, 1.1, true, int32(123), false, true}) // []interface{}{"true", 1, 1.1, true, int32(123), false}
Tail([]int{1, 2, 3, 3, 2, 1}) // []int{2, 3, 3, 2, 1}
Tail([]string{"1.1", "2.2", "3.3", "3.3", "2.2", "1.1"}) // []string{"2.2", "3.3", "3.3", "2.2", "1.1"}
Tail([]interface{}{false, "true", true, 1, 2.2}) // []interface{}{"true", true, 1, 2.2}
Reverse([]int{1, 2, 3}) // []int{3, 2, 1}
Reverse([]string{"1.1", "2.2", "3.3"}) // []string{"3.3", "2.2", "1.1"}
Reverse([]interface{}{"true", true, 1, 2.2, false}) // []interface{}{false, 2.2, 1, true, "true"}
FindIndex([]int64{1, 2, 3, 8, 7, 6, 5, 9}, int64(8)) // 3
FindIndex([]string{"a", "b", "c"}, "b") // 1
FindIndex([]*User{
{
Name: "warriors",
Age: 20,
},
{
Name: "god",
Age: 10,
},
{
Name: "vegeta",
Age: 15,
},
}, &User{
Name: "vegeta",
Age: 15,
}) // 2
PullAt([]float64{1.1, 2.2, 3.3}, []int{0, 1, 2}) // []float64{1.1, 2.2, 3.3}
PullAt([]bool{true, true, false, true, false}, []int{0, 1, 3, 4}) // []bool{true, true, true, false}
PullAt([]interface{}{"true", 1, false, true, 1.1, "false"}, []int{0, 1, 3, 4, 5}) // []interface{}{"true", 1, true, 1.1, "false"}
Without([]int{2, 1, 2, 3}, 1, 2) // []int{3}
Without([]string{"2", "1", "2", "3"}, "1", 2) // []string{"2", "2", "3"}
Without([]interface{}{"true", true, false, 1, 2.2, 3.3}, 2.2, false) // []interface{}{"true", true, 1, 3.3}
Join([]int{1, 2, 3}, "-") // "1-2-3"
Join([]bool{true, false}, "-") // "true-false"
Join([]interface{}{"1", false, 1.1, 2.2, 2}, "-") // "1-false-1.1-2.2-2"
Xor([]int{4, 4, 2, 1}, []int{2, 3, 4}) // []int{1, 3}
Xor([]float64{2, 1}, 3, []float64{2, 3}, []float64{2}) // []float64{1, 3}
Xor([]string{"2", "1"}, 3, []string{"2", "3"}, []string{"2"}) // []string{"1", "3"}
CamelCase("Foo Bar") // fooBar
CamelCase("--foo-bar--") // fooBar
CamelCase("@a98@#$,.23237@#$hello-world") // a9823237HelloWorld
Unescape("fred, barney, & pebbles") // fred, barney, & pebbles
Unescape("fred < barney") // fred < barney
Unescape("fred > barney") // fred > barney
Lte(1, 2) // true
Lte(int64(1), int64(2)) // true
Lte(3.2, 2.2) // false
Don't hesitate to participate in the discussion to enhance the generic helpers implementations.
- Ping me on instagram @tuanelnino9 or Facebook or Linkedin Tuan Nguyen Van DMs, mentions, whatever, send email :))
- Fork the project
- Fix open issues or request new features
Don't hesitate :))
- Tuan Nguyen Van