mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56fc2aabd6 | ||
|
|
0ddd52225b | ||
|
|
3919160e38 | ||
|
|
40ec5bc0f6 | ||
|
|
99faeccb05 | ||
|
|
ad777bc877 | ||
|
|
589ce7404f | ||
|
|
6ab4fca433 |
@@ -6,7 +6,7 @@
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||
[](https://codecov.io/gh/duke-git/lancet)
|
||||
@@ -391,6 +391,7 @@ func Filter(slice, function interface{}) interface{} //filter slice, function si
|
||||
func Find(slice, function interface{}) interface{} //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool .
|
||||
func IntSlice(slice interface{}) ([]int, error) //convert value to int slice
|
||||
func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice
|
||||
func Intersection(slices ...interface{}) interface{} //creates a slice of unique values that included by all slices.
|
||||
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //insert the element into slice at index.
|
||||
func Map(slice, function interface{}) interface{} //map lisce, function signature should be func(index int, value interface{}) interface{}
|
||||
func ReverseSlice(slice interface{}) //revere slice
|
||||
@@ -399,7 +400,9 @@ func SortByField(slice interface{}, field string, sortType ...string) error //so
|
||||
func Some(slice, function interface{}) bool //return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool
|
||||
func StringSlice(slice interface{}) []string //convert value to string slice
|
||||
func Unique(slice interface{}) interface{} //remove duplicate elements in slice
|
||||
func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index.
|
||||
func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values
|
||||
```
|
||||
|
||||
#### 10. strutil is for processing string
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||
[](https://codecov.io/gh/duke-git/lancet)
|
||||
@@ -392,6 +392,7 @@ func Find(slice, function interface{}) interface{} //查找slice中第一个符
|
||||
func Filter(slice, function interface{}) interface{} //过滤slice, 函数签名:func(index int, value interface{}) bool
|
||||
func IntSlice(slice interface{}) ([]int, error) //转成int切片
|
||||
func InterfaceSlice(slice interface{}) []interface{} //转成interface{}切片
|
||||
func Intersection(slices ...interface{}) interface{} //slice交集,去重
|
||||
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置插入value
|
||||
func Map(slice, function interface{}) interface{} //遍历切片, 函数签名:func(index int, value interface{}) interface{}
|
||||
func ReverseSlice(slice interface{}) //反转切片
|
||||
@@ -400,7 +401,9 @@ func Some(slice, function interface{}) bool //slice中任意一个元素都符
|
||||
func SortByField(slice interface{}, field string, sortType ...string) error //对struct切片进行排序
|
||||
func StringSlice(slice interface{}) []string //转为string切片
|
||||
func Unique(slice interface{}) interface{} //去重切片
|
||||
func Union(slices ...interface{}) interface{} //slice并集, 去重
|
||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value
|
||||
func Without(slice interface{}, values ...interface{}) interface{} //slice去除values
|
||||
```
|
||||
|
||||
#### 10. strutil字符串处理包
|
||||
|
||||
@@ -135,4 +135,4 @@ func TestReadFileByLine(t *testing.T) {
|
||||
if !reflect.DeepEqual(res, expected) {
|
||||
utils.LogFailedTestInfo(t, "ReadFileByLine", path, expected, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by MIT license
|
||||
|
||||
// Package function implements some functions for control the function execution and some is for functional programming.
|
||||
|
||||
package function
|
||||
|
||||
import (
|
||||
@@ -36,10 +35,10 @@ func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value {
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
// Fn is for curry function which is func(...interface{}) interface{}
|
||||
type Fn func(...interface{}) interface{}
|
||||
|
||||
// Curry make a curryed function
|
||||
// Curry make a curry function
|
||||
func (f Fn) Curry(i interface{}) func(...interface{}) interface{} {
|
||||
return func(values ...interface{}) interface{} {
|
||||
v := append([]interface{}{i}, values...)
|
||||
@@ -49,7 +48,7 @@ func (f Fn) Curry(i interface{}) func(...interface{}) interface{} {
|
||||
|
||||
// Compose compose the functions from right to left
|
||||
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{} {
|
||||
return func(s... interface{}) interface{} {
|
||||
return func(s ...interface{}) interface{} {
|
||||
f := fnList[0]
|
||||
restFn := fnList[1:]
|
||||
|
||||
@@ -61,7 +60,6 @@ func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) in
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Delay make the function execution after delayed time
|
||||
func Delay(delay time.Duration, fn interface{}, args ...interface{}) {
|
||||
time.Sleep(delay)
|
||||
|
||||
116
slice/slice.go
116
slice/slice.go
@@ -97,7 +97,7 @@ func Every(slice, function interface{}) bool {
|
||||
|
||||
elemType := sv.Type().Elem()
|
||||
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
|
||||
panic("Filter function must be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
}
|
||||
|
||||
var indexes []int
|
||||
@@ -119,7 +119,7 @@ func Some(slice, function interface{}) bool {
|
||||
|
||||
elemType := sv.Type().Elem()
|
||||
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
|
||||
panic("Filter function must be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
}
|
||||
|
||||
has := false
|
||||
@@ -141,7 +141,7 @@ func Filter(slice, function interface{}) interface{} {
|
||||
|
||||
elemType := sv.Type().Elem()
|
||||
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
|
||||
panic("Filter function must be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
}
|
||||
|
||||
var indexes []int
|
||||
@@ -167,7 +167,7 @@ func Find(slice, function interface{}) interface{} {
|
||||
|
||||
elemType := sv.Type().Elem()
|
||||
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
|
||||
panic("Filter function must be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
}
|
||||
|
||||
var index int
|
||||
@@ -189,7 +189,7 @@ func Map(slice, function interface{}) interface{} {
|
||||
|
||||
elemType := sv.Type().Elem()
|
||||
if checkSliceCallbackFuncSignature(fn, elemType, nil) {
|
||||
panic("Map function must be of type func(int, " + elemType.String() + ")" + elemType.String())
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + elemType.String())
|
||||
}
|
||||
|
||||
res := reflect.MakeSlice(sv.Type(), sv.Len(), sv.Len())
|
||||
@@ -215,7 +215,7 @@ func Reduce(slice, function, zero interface{}) interface{} {
|
||||
fn := functionValue(function)
|
||||
if checkSliceCallbackFuncSignature(fn, elementType, elementType, elementType) {
|
||||
t := elementType.String()
|
||||
panic("Reduce function must be of type func(int, " + t + ", " + t + ")" + t)
|
||||
panic("function param should be of type func(int, " + t + ", " + t + ")" + t)
|
||||
}
|
||||
|
||||
var params [3]reflect.Value
|
||||
@@ -379,22 +379,28 @@ func Unique(slice interface{}) interface{} {
|
||||
return slice
|
||||
}
|
||||
|
||||
var res []interface{}
|
||||
var temp []interface{}
|
||||
len := 0
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
v := sv.Index(i).Interface()
|
||||
flag := true
|
||||
for j := range res {
|
||||
if v == res[j] {
|
||||
flag = false
|
||||
skip := true
|
||||
for j := range temp {
|
||||
if v == temp[j] {
|
||||
skip = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
res = append(res, v)
|
||||
if skip {
|
||||
temp = append(temp, v)
|
||||
len++
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
res := reflect.MakeSlice(sv.Type(), len, len)
|
||||
for i := 0; i < len; i++ {
|
||||
res.Index(i).Set(reflect.ValueOf(temp[i]))
|
||||
}
|
||||
return res.Interface()
|
||||
|
||||
// if use map filter, the result slice element order is random, not same as origin slice
|
||||
//mp := make(map[interface{}]bool)
|
||||
@@ -411,6 +417,66 @@ func Unique(slice interface{}) interface{} {
|
||||
|
||||
}
|
||||
|
||||
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||
func Union(slices ...interface{}) interface{} {
|
||||
if len(slices) == 0 {
|
||||
return nil
|
||||
}
|
||||
// append all slices, then unique it
|
||||
var allSlice []interface{}
|
||||
len := 0
|
||||
for i := range slices {
|
||||
sv := sliceValue(slices[i])
|
||||
len += sv.Len()
|
||||
for j := 0; j < sv.Len(); j++ {
|
||||
v := sv.Index(j).Interface()
|
||||
allSlice = append(allSlice, v)
|
||||
}
|
||||
}
|
||||
|
||||
sv := sliceValue(slices[0])
|
||||
res := reflect.MakeSlice(sv.Type(), len, len)
|
||||
for i := 0; i < len; i++ {
|
||||
res.Index(i).Set(reflect.ValueOf(allSlice[i]))
|
||||
}
|
||||
|
||||
return Unique(res.Interface())
|
||||
}
|
||||
|
||||
// Intersection creates a slice of unique values that included by all slices.
|
||||
func Intersection(slices ...interface{}) interface{} {
|
||||
if len(slices) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
reduceFunc := func(index int, slice1, slice2 interface{}) interface{} {
|
||||
set := make([]interface{}, 0)
|
||||
hash := make(map[interface{}]bool)
|
||||
|
||||
sv1 := reflect.ValueOf(slice1)
|
||||
for i := 0; i < sv1.Len(); i++ {
|
||||
v := sv1.Index(i).Interface()
|
||||
hash[v] = true
|
||||
}
|
||||
|
||||
sv2 := reflect.ValueOf(slice2)
|
||||
for i := 0; i < sv2.Len(); i++ {
|
||||
el := sv2.Index(i).Interface()
|
||||
if _, found := hash[el]; found {
|
||||
set = append(set, el)
|
||||
}
|
||||
}
|
||||
res := reflect.MakeSlice(sv1.Type(), len(set), len(set))
|
||||
for i := 0; i < len(set); i++ {
|
||||
res.Index(i).Set(reflect.ValueOf(set[i]))
|
||||
}
|
||||
return res.Interface()
|
||||
}
|
||||
|
||||
res := Reduce(slices, reduceFunc, nil)
|
||||
return Union(res)
|
||||
}
|
||||
|
||||
// ReverseSlice return slice of element order is reversed to the given slice
|
||||
func ReverseSlice(slice interface{}) {
|
||||
v := sliceValue(slice)
|
||||
@@ -474,3 +540,25 @@ func SortByField(slice interface{}, field string, sortType ...string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Without creates a slice excluding all given values
|
||||
func Without(slice interface{}, values ...interface{}) interface{} {
|
||||
sv := sliceValue(slice)
|
||||
if sv.Len() == 0 {
|
||||
return slice
|
||||
}
|
||||
var indexes []int
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
v := sv.Index(i).Interface()
|
||||
if !Contain(values, v) {
|
||||
indexes = append(indexes, i)
|
||||
}
|
||||
}
|
||||
|
||||
res := reflect.MakeSlice(sv.Type(), len(indexes), len(indexes))
|
||||
for i := range indexes {
|
||||
res.Index(i).Set(sv.Index(indexes[i]))
|
||||
}
|
||||
|
||||
return res.Interface()
|
||||
}
|
||||
|
||||
@@ -395,7 +395,7 @@ func updateByIndex(t *testing.T, test interface{}, index int, value, expected in
|
||||
func TestUnique(t *testing.T) {
|
||||
t1 := []int{1, 2, 2, 3}
|
||||
e1 := []int{1, 2, 3}
|
||||
r1, _ := IntSlice(Unique(t1))
|
||||
r1 := Unique(t1)
|
||||
if !reflect.DeepEqual(r1, e1) {
|
||||
utils.LogFailedTestInfo(t, "Unique", t1, e1, r1)
|
||||
t.FailNow()
|
||||
@@ -403,13 +403,60 @@ func TestUnique(t *testing.T) {
|
||||
|
||||
t2 := []string{"a", "a", "b", "c"}
|
||||
e2 := []string{"a", "b", "c"}
|
||||
r2 := StringSlice(Unique(t2))
|
||||
r2 := Unique(t2)
|
||||
if !reflect.DeepEqual(r2, e2) {
|
||||
utils.LogFailedTestInfo(t, "Unique", t2, e2, r2)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnion(t *testing.T) {
|
||||
s1 := []int{1, 3, 4, 6}
|
||||
s2 := []int{1, 2, 5, 6}
|
||||
s3 := []int{0, 4, 5, 7}
|
||||
|
||||
expected1 := []int{1, 3, 4, 6, 2, 5, 0, 7}
|
||||
res1 := Union(s1, s2, s3)
|
||||
if !reflect.DeepEqual(res1, expected1) {
|
||||
utils.LogFailedTestInfo(t, "Union", s1, expected1, res1)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
expected2 := []int{1, 3, 4, 6}
|
||||
res2 := Union(s1)
|
||||
if !reflect.DeepEqual(res2, expected2) {
|
||||
utils.LogFailedTestInfo(t, "Union", s1, expected2, res2)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntersection(t *testing.T) {
|
||||
s1 := []int{1, 2, 2, 3}
|
||||
s2 := []int{1, 2, 3, 4}
|
||||
s3 := []int{0, 2, 3, 5, 6}
|
||||
s4 := []int{0, 5, 6}
|
||||
|
||||
expected := [][]int{
|
||||
{2, 3},
|
||||
{1, 2, 3},
|
||||
{1, 2, 3},
|
||||
{},
|
||||
}
|
||||
res := []interface{}{
|
||||
Intersection(s1, s2, s3),
|
||||
Intersection(s1, s2),
|
||||
Intersection(s1),
|
||||
Intersection(s1, s4),
|
||||
}
|
||||
for i := 0; i < len(res); i++ {
|
||||
if !reflect.DeepEqual(res[i], expected[i]) {
|
||||
utils.LogFailedTestInfo(t, "Intersection", "Intersection", expected[i], res[i])
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestReverseSlice(t *testing.T) {
|
||||
s1 := []int{1, 2, 3, 4, 5}
|
||||
e1 := []int{5, 4, 3, 2, 1}
|
||||
@@ -469,3 +516,14 @@ func TestSortByField(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestWithout(t *testing.T) {
|
||||
s := []int{1, 2, 3, 4, 5}
|
||||
expected := []int{3, 4, 5}
|
||||
res := Without(s, 1, 2)
|
||||
|
||||
if !reflect.DeepEqual(res, expected) {
|
||||
utils.LogFailedTestInfo(t, "Without", s, expected, res)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ package strutil
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// CamelCase covert string to camelCase string.
|
||||
@@ -40,18 +41,16 @@ func Capitalize(s string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
res := ""
|
||||
for i, v := range []rune(s) {
|
||||
out := make([]rune, len(s))
|
||||
for i, v := range s {
|
||||
if i == 0 {
|
||||
if v >= 97 && v <= 122 {
|
||||
v -= 32
|
||||
}
|
||||
res += string(v)
|
||||
out[i] = unicode.ToUpper(v)
|
||||
} else {
|
||||
res += strings.ToLower(string(v))
|
||||
out[i] = unicode.ToLower(v)
|
||||
}
|
||||
}
|
||||
return res
|
||||
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// LowerFirst converts the first character of string to lower case.
|
||||
|
||||
Reference in New Issue
Block a user