sliceutil

package
v1.11.4 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: MIT Imports: 6 Imported by: 73

Documentation

Overview

Utilities for converting, manipulating, and iterating over slices

Index

Constants

This section is empty.

Variables

View Source
var RelaxedEqualityCompare = func(_ int, first interface{}, second interface{}) bool {
	if v, err := stringutil.RelaxedEqual(first, second); err == nil && v == true {
		return true
	}

	return false
}
View Source
var Stop = utils.Stop
View Source
var StrictEqualityCompare = func(_ int, first interface{}, second interface{}) bool {
	if first == second {
		return true
	}

	return false
}

Functions

func Append added in v1.5.53

func Append(in interface{}, values ...interface{}) []interface{}

Returns a copy of the given slicified value with the given additional values appended.

func At

func At(in interface{}, index int) (interface{}, bool)

Returns the element in the given indexable value at the given index. If the index is present, the second return value will be true. If the index is not present, or the given input is not indexable, the second return value will be false.

func Autotype

func Autotype(in interface{}) []interface{}

Returns a copy of the given slice with each element's value passed to stringutil.Autotype

func Chunks

func Chunks(in interface{}, size int) [][]interface{}

Divide the given slice into chunks of (at most) a given length

func Compact

func Compact(in interface{}) []interface{}

Removes all elements from the given interface slice that are "empty", which is defined as being nil, a nil or zero-length array, chan, map, slice, or string.

The zero values of any other type are not considered empty and will remain in the return value.

func CompactString

func CompactString(in []string) []string

Removes all zero-length strings from the given string slice, returning a new slice with the values removed.

func Contains

func Contains(in interface{}, value interface{}, comparators ...CompareFunc) bool

Return whether the given slice contains the given value. If a comparator is provided, it will be used to compare the elements.

func ContainsAllStrings

func ContainsAllStrings(list []string, elems ...string) bool

Returns whether the given string slice contains all of the following strings.

func ContainsAnyString

func ContainsAnyString(list []string, elems ...string) bool

Returns whether the given string slice contains any of the following strings.

func ContainsString

func ContainsString(list []string, elem string) bool

Returns whether the given string slice contains a given string.

func Difference added in v1.7.51

func Difference(first interface{}, second interface{}) []interface{}

Return the slice that results from removing elements in second from the first.

func Each

func Each(slice interface{}, iterFn IterationFunc) error

Iterate through each element of the given array or slice, calling iterFn exactly once for each element. Otherwise, call iterFn one time with the given input as the argument.

func First

func First(in interface{}) interface{}

Returns the first element from the given slice, array or string; or nil.

func FirstNonZero added in v1.9.8

func FirstNonZero(inputs ...interface{}) interface{}

Returns the first element in the given inputs that is not that type's zero value. All input values are flattened into a single array, so variadic elements can contain scalar or array values.

func Flatten

func Flatten(in interface{}) []interface{}

Returns the given slice as a single-level flattened array.

func Get

func Get(in interface{}, index int) interface{}

Returns the nth element from the given slice, array or string; or nil.

func Intersect

func Intersect(a interface{}, b interface{}) []interface{}

Return the intersection of two slices.

func IntersectStrings

func IntersectStrings(a []string, b []string) []string

Return the intersection of two string slices.

func Last

func Last(in interface{}) interface{}

Returns the last element from the given slice, array or string; or nil.

func Len

func Len(in interface{}) int

Returns the length of the given slice, array, or string.

func Map

func Map(in interface{}, fn MapFunc) []interface{}

Returns a copy of the given slice with each element modified by the a given function.

func MapString

func MapString(in interface{}, fn MapStringFunc) []string

Returns a copy of the given slice with each element modified by the a given function, then converted to a string.

func Or

func Or(in ...interface{}) interface{}

Returns the first item that is not the zero value for that value's type.

func OrString

func OrString(in ...string) string

Returns the first item that is not a zero-length string.

func Rest

func Rest(in interface{}) []interface{}

Returns the all but the first element from the given slice, array or string; or nil.

func Slice added in v1.6.30

func Slice(slice interface{}, from int, to int) []interface{}

Returns a new slice with only the specified subset of items included. In addition to the normal slice index rules in Golang, negative indices are also supported. If a negative index is given for the from and/or to values, the index will be treated as being relative to the end of the given slice. For example:

Slice([]interface{}{1,2,3,4,5}, -5, -1)  // returns []interface{}{1, 2, 3, 4, 5}
Slice([]interface{}{1,2,3,4,5}, -2, -1)  // returns []interface{}{4, 5}
Slice([]interface{}{1,2,3,4,5}, -1, -1)  // returns []interface{}{5}
Slice([]interface{}{1,2,3,4,5}, -4, -2)  // returns []interface{}{2, 3, 4}

func Sliceify

func Sliceify(in interface{}) []interface{}

Takes some input value and returns it as a slice.

func StringSlice added in v1.6.30

func StringSlice(slice interface{}, from int, to int) []string

Same as slice, but returns strings.

func Stringify

func Stringify(in interface{}) []string

Converts all elements of the given interface slice to strings using the "%v" format string via the fmt package.

func TrimSpace added in v1.7.13

func TrimSpace(in interface{}) []string

Trims the whitespace from each element in the given array.

func Unique

func Unique(in interface{}) []interface{}

Returns a new slice with only unique elements from the given interface included.

func UniqueStrings

func UniqueStrings(in interface{}) []string

Returns a new slice with only unique string elements from the given interface included.

Types

type CompareFunc

type CompareFunc func(i int, first interface{}, second interface{}) bool // {}

type IterationFunc

type IterationFunc = utils.IterationFunc

type MapFunc

type MapFunc func(i int, value interface{}) interface{}

type MapStringFunc

type MapStringFunc func(i int, value string) string

Jump to

Keyboard shortcuts

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