Skip to content

Commit

Permalink
doc: update top and some sub pkg README docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 16, 2022
1 parent 9027fde commit 87c962d
Show file tree
Hide file tree
Showing 25 changed files with 555 additions and 89 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [`stdutil`](./stdutil) Provide some commonly std util functions.
- [`structs`](./structs) Provide some extends util functions for struct. eg: tag parse, struct data init
- [`strutil`](./strutil) String util functions. eg: bytes, check, convert, encode, format and more
- [textscan](strutil/textscan) Implemented a parser that quickly scans and analyzes text content. It can be used to parse INI, Properties and other formats
- [`sysutil`](./sysutil) System util functions. eg: sysenv, exec, user, process
- [clipboard](./sysutil/clipboard) Provide a simple clipboard read and write operations.
- [cmdr](./sysutil/cmdr) Provide for quick build and run a cmd, batch run multi cmd tasks
Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Go一些常用的工具函数实现、增强、收集和整理
- [`stdutil`](./stdutil) 提供一些常用的 std util 函数。
- [`structs`](./structs) 为 struct 提供一些扩展 util 函数。 eg: tag parse, struct data
- [`strutil`](./strutil) string 相关操作的函数工具包. eg: bytes, check, convert, encode, format and more
- [textscan](strutil/textscan) 实现了一个快速扫描和分析文本内容的解析器. 可用于解析 INI, Properties 等格式内容
- [`sysutil`](./sysutil) system 相关操作的函数工具包. eg: sysenv, exec, user, process
- [clipboard](./sysutil/clipboard) 提供简单的剪贴板读写操作工具库
- [cmdr](./sysutil/cmdr) 提供快速构建和运行一个cmd,批量运行多个cmd任务
Expand Down
24 changes: 24 additions & 0 deletions arrutil/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ go get github.com/gookit/goutil/arrutil

- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/arrutil)

## Functions API

```go

```

## Code Check & Testing

```bash
gofmt -w -l ./
golint ./...
```

**Testing**:

```shell
go test -v ./cliutil/...
```

**Test limit by regexp**:

```shell
go test -v -run ^TestSetByKeys ./cliutil/...
```

## Refers

Expand Down
6 changes: 6 additions & 0 deletions arrutil/any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package arrutil

// alias of interface{}
//
// TIP: cannot add `go:build !go1.18` in file head, that require the go.mod set `go 1.18`
type any = interface{}
6 changes: 3 additions & 3 deletions arrutil/arrutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func TrimStrings(ss []string, cutSet ...string) []string {
}

// GetRandomOne get random element from an array/slice
func GetRandomOne(arr interface{}) interface{} { return RandomOne(arr) }
func GetRandomOne(arr any) interface{} { return RandomOne(arr) }

// RandomOne get random element from an array/slice
func RandomOne(arr interface{}) interface{} {
func RandomOne(arr any) interface{} {
rv := reflect.ValueOf(arr)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
return arr
Expand All @@ -107,7 +107,7 @@ func RandomOne(arr interface{}) interface{} {
}

// Unique value in the given array, slice.
func Unique(arr interface{}) interface{} {
func Unique(arr any) interface{} {
rv := reflect.ValueOf(arr)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
return arr
Expand Down
6 changes: 3 additions & 3 deletions arrutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func StringsHas(ss []string, val string) bool {
}

// HasValue check array(strings, intXs, uintXs) should be contained the given value(int(X),string).
func HasValue(arr, val interface{}) bool {
func HasValue(arr, val any) bool {
return Contains(arr, val)
}

// Contains check array(strings, intXs, uintXs) should be contained the given value(int(X),string).
func Contains(arr, val interface{}) bool {
func Contains(arr, val any) bool {
if val == nil || arr == nil {
return false
}
Expand Down Expand Up @@ -82,6 +82,6 @@ func Contains(arr, val interface{}) bool {
}

// NotContains check array(strings, ints, uints) should be not contains the given value.
func NotContains(arr, val interface{}) bool {
func NotContains(arr, val any) bool {
return !Contains(arr, val)
}
86 changes: 43 additions & 43 deletions arrutil/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
const ErrElementNotFound = "element not found"

// Comparer Function to compare two elements.
type Comparer func(a, b interface{}) int
type Comparer func(a, b any) int

// Predicate Function to predicate a struct/value satisfies a condition.
type Predicate func(a interface{}) bool
type Predicate func(a any) bool

var (
// StringEqualsComparer Comparer for string. It will compare the string by their value.
// returns: 0 if equal, -1 if a != b
StringEqualsComparer Comparer = func(a, b interface{}) int {
StringEqualsComparer Comparer = func(a, b any) int {
typeOfA := reflect.TypeOf(a)
if typeOfA.Kind() == reflect.Ptr {
typeOfA = typeOfA.Elem()
Expand Down Expand Up @@ -88,11 +88,11 @@ var (

// TwowaySearch Find specialized element in a slice forward and backward in the same time, should be more quickly.
//
// data: the slice to search in. MUST BE A SLICE.
// item: the element to search.
// fn: the comparer function.
// return: the index of the element, or -1 if not found.
func TwowaySearch(data interface{}, item interface{}, fn Comparer) (int, error) {
// data: the slice to search in. MUST BE A SLICE.
// item: the element to search.
// fn: the comparer function.
// return: the index of the element, or -1 if not found.
func TwowaySearch(data any, item any, fn Comparer) (int, error) {
if data == nil {
return -1, errors.New("collections.TwowaySearch: data is nil")
}
Expand Down Expand Up @@ -150,9 +150,9 @@ func MakeEmptySlice(itemType reflect.Type) interface{} {

// CloneSlice Clone a slice.
//
// data: the slice to clone.
// returns: the cloned slice.
func CloneSlice(data interface{}) interface{} {
// data: the slice to clone.
// returns: the cloned slice.
func CloneSlice(data any) interface{} {
typeOfData := reflect.TypeOf(data)
if typeOfData.Kind() != reflect.Slice {
panic("collections.CloneSlice: data must be a slice")
Expand All @@ -162,11 +162,11 @@ func CloneSlice(data interface{}) interface{} {

// Excepts Produces the set difference of two slice according to a comparer function.
//
// first: the first slice. MUST BE A SLICE.
// second: the second slice. MUST BE A SLICE.
// fn: the comparer function.
// returns: the difference of the two slices.
func Excepts(first interface{}, second interface{}, fn Comparer) interface{} {
// first: the first slice. MUST BE A SLICE.
// second: the second slice. MUST BE A SLICE.
// fn: the comparer function.
// returns: the difference of the two slices.
func Excepts(first, second any, fn Comparer) interface{} {
typeOfFirst := reflect.TypeOf(first)
if typeOfFirst.Kind() != reflect.Slice {
panic("collections.Excepts: first must be a slice")
Expand Down Expand Up @@ -199,11 +199,11 @@ func Excepts(first interface{}, second interface{}, fn Comparer) interface{} {

// Intersects Produces to intersect of two slice according to a comparer function.
//
// first: the first slice. MUST BE A SLICE.
// second: the second slice. MUST BE A SLICE.
// fn: the comparer function.
// returns: the intersect of the two slices.
func Intersects(first interface{}, second interface{}, fn Comparer) interface{} {
// first: the first slice. MUST BE A SLICE.
// second: the second slice. MUST BE A SLICE.
// fn: the comparer function.
// returns: the intersect of the two slices.
func Intersects(first any, second any, fn Comparer) interface{} {
typeOfFirst := reflect.TypeOf(first)
if typeOfFirst.Kind() != reflect.Slice {
panic("collections.Intersects: first must be a slice")
Expand Down Expand Up @@ -236,11 +236,11 @@ func Intersects(first interface{}, second interface{}, fn Comparer) interface{}

// Union Produces the set union of two slice according to a comparer function
//
// first: the first slice. MUST BE A SLICE.
// second: the second slice. MUST BE A SLICE.
// fn: the comparer function.
// returns: the union of the two slices.
func Union(first interface{}, second interface{}, fn Comparer) interface{} {
// first: the first slice. MUST BE A SLICE.
// second: the second slice. MUST BE A SLICE.
// fn: the comparer function.
// returns: the union of the two slices.
func Union(first, second any, fn Comparer) interface{} {
excepts := Excepts(second, first, fn)

typeOfFirst := reflect.TypeOf(first)
Expand All @@ -259,10 +259,10 @@ func Union(first interface{}, second interface{}, fn Comparer) interface{} {

// Find Produces the struct/value of a slice according to a predicate function.
//
// source: the slice. MUST BE A SLICE.
// fn: the predicate function.
// returns: the struct/value of the slice.
func Find(source interface{}, fn Predicate) (interface{}, error) {
// source: the slice. MUST BE A SLICE.
// fn: the predicate function.
// returns: the struct/value of the slice.
func Find(source any, fn Predicate) (interface{}, error) {
aType := reflect.TypeOf(source)
if aType.Kind() != reflect.Slice {
panic("collections.Find: source must be a slice")
Expand All @@ -285,11 +285,11 @@ func Find(source interface{}, fn Predicate) (interface{}, error) {
// FindOrDefault Produce the struct/value f a slice to a predicate function,
// Produce default value when predicate function not found.
//
// source: the slice. MUST BE A SLICE.
// fn: the predicate function.
// defaultValue: the default value.
// returns: the struct/value of the slice.
func FindOrDefault(source interface{}, fn Predicate, defaultValue interface{}) interface{} {
// source: the slice. MUST BE A SLICE.
// fn: the predicate function.
// defaultValue: the default value.
// returns: the struct/value of the slice.
func FindOrDefault(source any, fn Predicate, defaultValue any) interface{} {
item, err := Find(source, fn)
if err != nil {
if err.Error() == ErrElementNotFound {
Expand All @@ -302,10 +302,10 @@ func FindOrDefault(source interface{}, fn Predicate, defaultValue interface{}) i
// TakeWhile Produce the set of a slice according to a predicate function,
// Produce empty slice when predicate function not matched.
//
// data: the slice. MUST BE A SLICE.
// fn: the predicate function.
// returns: the set of the slice.
func TakeWhile(data interface{}, fn Predicate) interface{} {
// data: the slice. MUST BE A SLICE.
// fn: the predicate function.
// returns: the set of the slice.
func TakeWhile(data any, fn Predicate) interface{} {
aType := reflect.TypeOf(data)
if aType.Kind() != reflect.Slice {
panic("collections.TakeWhile: data must be a slice")
Expand All @@ -327,10 +327,10 @@ func TakeWhile(data interface{}, fn Predicate) interface{} {
// ExceptWhile Produce the set of a slice except with a predicate function,
// Produce original slice when predicate function not match.
//
// data: the slice. MUST BE A SLICE.
// fn: the predicate function.
// returns: the set of the slice.
func ExceptWhile(data interface{}, fn Predicate) interface{} {
// data: the slice. MUST BE A SLICE.
// fn: the predicate function.
// returns: the set of the slice.
func ExceptWhile(data any, fn Predicate) interface{} {
aType := reflect.TypeOf(data)
if aType.Kind() != reflect.Slice {
panic("collections.ExceptWhile: data must be a slice")
Expand Down
28 changes: 14 additions & 14 deletions arrutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func StringsToInts(ss []string) (ints []int, err error) {
}

// MustToStrings convert interface{}(allow: array,slice) to []string
func MustToStrings(arr interface{}) []string {
func MustToStrings(arr any) []string {
ret, _ := ToStrings(arr)
return ret
}
Expand All @@ -60,7 +60,7 @@ func StringsToSlice(ss []string) []interface{} {
*************************************************************/

// ToInt64s convert interface{}(allow: array,slice) to []int64
func ToInt64s(arr interface{}) (ret []int64, err error) {
func ToInt64s(arr any) (ret []int64, err error) {
rv := reflect.ValueOf(arr)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
err = ErrInvalidType
Expand All @@ -79,13 +79,13 @@ func ToInt64s(arr interface{}) (ret []int64, err error) {
}

// MustToInt64s convert interface{}(allow: array,slice) to []int64
func MustToInt64s(arr interface{}) []int64 {
func MustToInt64s(arr any) []int64 {
ret, _ := ToInt64s(arr)
return ret
}

// SliceToInt64s convert []interface{} to []int64
func SliceToInt64s(arr []interface{}) []int64 {
// SliceToInt64s convert []any to []int64
func SliceToInt64s(arr []any) []int64 {
i64s := make([]int64, len(arr))
for i, v := range arr {
i64s[i] = mathutil.MustInt64(v)
Expand All @@ -94,7 +94,7 @@ func SliceToInt64s(arr []interface{}) []int64 {
}

// ToStrings convert interface{}(allow: array,slice) to []string
func ToStrings(arr interface{}) (ret []string, err error) {
func ToStrings(arr any) (ret []string, err error) {
rv := reflect.ValueOf(arr)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
err = ErrInvalidType
Expand All @@ -112,8 +112,8 @@ func ToStrings(arr interface{}) (ret []string, err error) {
return
}

// SliceToStrings convert []interface{} to []string
func SliceToStrings(arr []interface{}) []string {
// SliceToStrings convert []any to []string
func SliceToStrings(arr []any) []string {
ss := make([]string, len(arr))
for i, v := range arr {
ss[i] = strutil.MustString(v)
Expand All @@ -122,16 +122,16 @@ func SliceToStrings(arr []interface{}) []string {
}

// AnyToString simple and quickly convert any array, slice to string
func AnyToString(arr interface{}) string {
func AnyToString(arr any) string {
return NewFormatter(arr).Format()
}

// SliceToString convert []interface{} to string
func SliceToString(arr ...interface{}) string { return ToString(arr) }
// SliceToString convert []any to string
func SliceToString(arr ...any) string { return ToString(arr) }

// ToString simple and quickly convert []interface{} to string
func ToString(arr []interface{}) string {
// like fmt.Println([]interface{}(nil))
func ToString(arr []any) string {
// like fmt.Println([]any(nil))
if arr == nil {
return "[]"
}
Expand All @@ -151,7 +151,7 @@ func ToString(arr []interface{}) string {
}

// JoinSlice join []any slice to string.
func JoinSlice(sep string, arr ...interface{}) string {
func JoinSlice(sep string, arr ...any) string {
if arr == nil {
return ""
}
Expand Down
4 changes: 2 additions & 2 deletions arrutil/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ArrFormatter struct {
}

// NewFormatter instance
func NewFormatter(arr interface{}) *ArrFormatter {
func NewFormatter(arr any) *ArrFormatter {
f := &ArrFormatter{}
f.Src = arr

Expand Down Expand Up @@ -120,6 +120,6 @@ func (f *ArrFormatter) doFormat() {
}

// FormatIndent array data to string.
func FormatIndent(arr interface{}, indent string) string {
func FormatIndent(arr any, indent string) string {
return NewFormatter(arr).WithIndent(indent).Format()
}
Loading

0 comments on commit 87c962d

Please sign in to comment.