arrutil

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2022 License: MIT Imports: 8 Imported by: 60

Documentation

Overview

Package arrutil provides some util functions for array, slice

Index

Constants

View Source
const ErrElementNotFound = "element not found"

ErrElementNotFound is the error returned when the element is not found.

Variables

View Source
var ErrInvalidType = errors.New("the input param type is invalid")

ErrInvalidType error

Functions

func AnyToString added in v0.5.2

func AnyToString(arr interface{}) string

AnyToString simple and quickly convert any array, slice to string

func CloneSlice added in v0.5.3

func CloneSlice(data interface{}) interface{}

CloneSlice Clone a slice. data: the slice to clone. returns: the cloned slice.

func Contains added in v0.3.6

func Contains(arr, val interface{}) bool

Contains check array(strings, intXs, uintXs) should be contained the given value(int(X),string).

func ExceptWhile added in v0.5.3

func ExceptWhile(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 Excepts added in v0.5.3

func Excepts(first interface{}, second interface{}, fn Comparer) 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 Find added in v0.5.3

func Find(source interface{}, fn Predicate) (interface{}, error)

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 FindOrDefault added in v0.5.3

func FindOrDefault(source interface{}, fn Predicate, defaultValue interface{}) interface{}

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 FormatIndent added in v0.5.3

func FormatIndent(arr interface{}, indent string) string

FormatIndent array data to string.

func GetRandomOne added in v0.3.8

func GetRandomOne(arr interface{}) interface{}

GetRandomOne get random element from an array/slice

func HasValue added in v0.3.13

func HasValue(arr, val interface{}) bool

HasValue check array(strings, intXs, uintXs) should be contained the given value(int(X),string).

func InStrings added in v0.5.0

func InStrings(elem string, ss []string) bool

InStrings alias of StringsHas()

func Int64sHas added in v0.3.6

func Int64sHas(ints []int64, val int64) bool

Int64sHas check the []int64 contains the given value

func Intersects added in v0.5.3

func Intersects(first interface{}, second interface{}, fn Comparer) interface{}

Intersects Produces the 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 IntsHas added in v0.3.6

func IntsHas(ints []int, val int) bool

IntsHas check the []int contains the given value

func JoinSlice added in v0.5.0

func JoinSlice(sep string, arr ...interface{}) string

JoinSlice join []any slice to string.

func JoinStrings added in v0.4.2

func JoinStrings(sep string, ss ...string) string

JoinStrings alias of strings.Join

func MakeEmptySlice added in v0.5.3

func MakeEmptySlice(itemType reflect.Type) interface{}

MakeEmptySlice Create a new slice with the elements of the source that satisfy the predicate. itemType: the type of the elements in the source. returns: the new slice.

func MustToInt64s added in v0.3.13

func MustToInt64s(arr interface{}) []int64

MustToInt64s convert interface{}(allow: array,slice) to []int64

func MustToStrings added in v0.3.13

func MustToStrings(arr interface{}) []string

MustToStrings convert interface{}(allow: array,slice) to []string

func NotContains added in v0.3.6

func NotContains(arr, val interface{}) bool

NotContains check array(strings, ints, uints) should be not contains the given value.

func Reverse

func Reverse(ss []string)

Reverse string slice [site user info 0] -> [0 info user site]

func SliceToInt64s added in v0.3.13

func SliceToInt64s(arr []interface{}) []int64

SliceToInt64s convert []interface{} to []int64

func SliceToString added in v0.4.5

func SliceToString(arr ...interface{}) string

SliceToString convert []interface{} to string

func SliceToStrings added in v0.3.13

func SliceToStrings(arr []interface{}) []string

SliceToStrings convert []interface{} to []string

func StringsHas added in v0.3.6

func StringsHas(ss []string, val string) bool

StringsHas check the []string contains the given element

func StringsJoin added in v0.4.2

func StringsJoin(sep string, ss ...string) string

StringsJoin alias of strings.Join

func StringsRemove added in v0.1.4

func StringsRemove(ss []string, s string) []string

StringsRemove a value form a string slice

func StringsToInts added in v0.2.2

func StringsToInts(ss []string) (ints []int, err error)

StringsToInts string slice to int slice

func StringsToSlice added in v0.4.5

func StringsToSlice(ss []string) []interface{}

StringsToSlice convert []string to []interface{}

func TakeWhile added in v0.5.3

func TakeWhile(data interface{}, fn Predicate) interface{}

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 ToInt64s added in v0.3.13

func ToInt64s(arr interface{}) (ret []int64, err error)

ToInt64s convert interface{}(allow: array,slice) to []int64

func ToString added in v0.4.5

func ToString(arr []interface{}) string

ToString simple and quickly convert []interface{} to string

func ToStrings added in v0.3.13

func ToStrings(arr interface{}) (ret []string, err error)

ToStrings convert interface{}(allow: array,slice) to []string

func TrimStrings added in v0.2.2

func TrimStrings(ss []string, cutSet ...string) (ns []string)

TrimStrings trim string slice item.

Usage:

// output: [a, b, c]
ss = arrutil.TrimStrings([]string{",a", "b.", ",.c,"}, ",.")

func TwowaySearch added in v0.5.3

func TwowaySearch(data interface{}, item interface{}, fn Comparer) (int, error)

TwowaySearch Find specialized element in a slice forward and backward in the same time, should be more quicker. 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 Union added in v0.5.3

func Union(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.

Types

type ArrFormatter added in v0.5.3

type ArrFormatter struct {
	common.BaseFormatter
	// Prefix string for each element
	Prefix string
	// Indent string for format each element
	Indent string
	// ClosePrefix string for last "]"
	ClosePrefix string
}

ArrFormatter struct

func NewFormatter added in v0.5.3

func NewFormatter(arr interface{}) *ArrFormatter

NewFormatter instance

func (*ArrFormatter) Format added in v0.5.3

func (f *ArrFormatter) Format() string

Format to string

func (*ArrFormatter) FormatTo added in v0.5.3

func (f *ArrFormatter) FormatTo(w io.Writer)

FormatTo to custom buffer

func (*ArrFormatter) String added in v0.5.3

func (f *ArrFormatter) String() string

Format to string

func (*ArrFormatter) WithFn added in v0.5.3

func (f *ArrFormatter) WithFn(fn func(f *ArrFormatter)) *ArrFormatter

WithFn for config self

func (*ArrFormatter) WithIndent added in v0.5.3

func (f *ArrFormatter) WithIndent(indent string) *ArrFormatter

WithIndent string

type Comparer added in v0.5.3

type Comparer func(a, b interface{}) int

Comparer Function to compare two elements.

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 {
		typeOfA := reflect.TypeOf(a)
		if typeOfA.Kind() == reflect.Ptr {
			typeOfA = typeOfA.Elem()
		}

		typeOfB := reflect.TypeOf(b)
		if typeOfB.Kind() == reflect.Ptr {
			typeOfB = typeOfB.Elem()
		}

		if typeOfA != typeOfB {
			return -1
		}

		strA := ""
		strB := ""

		if val, ok := a.(string); ok {
			strA = val
		} else if val, ok := a.(*string); ok {
			strA = *val
		} else {
			return -1
		}

		if val, ok := b.(string); ok {
			strB = val
		} else if val, ok := b.(*string); ok {
			strB = *val
		} else {
			return -1
		}

		if strA == strB {
			return 0
		}
		return -1
	}

	// ReferenceEqualsComparer Comparer for strcut ptr. It will compare the struct by their ptr addr.
	// returns: 0 if equal, -1 if a != b
	ReferenceEqualsComparer Comparer = func(a, b interface{}) int {
		if a == b {
			return 0
		}
		return -1
	}

	// ElemTypeEqualsComparer Comparer for struct/value. It will compare the struct by their element type (reflect.Type.Elem()).
	// returns: 0 if same type, -1 if not.
	ElemTypeEqualsComparer Comparer = func(a, b interface{}) int {
		at := reflect.TypeOf(a)
		bt := reflect.TypeOf(b)
		if at.Kind() == reflect.Ptr {
			at = at.Elem()
		}

		if bt.Kind() == reflect.Ptr {
			bt = bt.Elem()
		}

		if at == bt {
			return 0
		}
		return -1
	}
)

type Ints added in v0.3.9

type Ints []int

Ints type

func (Ints) Has added in v0.3.9

func (is Ints) Has(i int) bool

Has given element

func (Ints) String added in v0.3.9

func (is Ints) String() string

String to string

type Predicate added in v0.5.3

type Predicate func(a interface{}) bool

Predicate Function to predicate a struct/value satisfies a condition.

type Strings added in v0.3.9

type Strings []string

Strings type

func (Strings) Has added in v0.3.9

func (ss Strings) Has(sub string) bool

Has given element

func (Strings) String added in v0.3.9

func (ss Strings) String() string

String to string

Jump to

Keyboard shortcuts

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