Documentation ¶
Index ¶
- Constants
- Variables
- func CamelCase(str string) string
- func Capitalize(str string) string
- func DecimalsDiv(input string, rest ...string) (string, error)
- func DecimalsDivRound(input string, divider string, precision int32) (string, error)
- func DecimalsMult(input string, rest ...string) (string, error)
- func DecimalsPlus(input string, rest ...string) (string, error)
- func DecimalsPow(input string, d2 string) (string, error)
- func DecimalsSub(input string, rest ...string) (string, error)
- func Ellipsis(str string, length int) string
- func Every[T ~[]E, E comparable](items T, fn Predicate[E]) bool
- func Filter[T ~[]E, E comparable](items T, fn Predicate[E]) T
- func Flat[T ~[]E, E any](items []T) []E
- func ForEach[T ~[]E, E any](items T, callback func(idx int, item E) bool)
- func FormatDuration(d time.Duration, formatStr string) string
- func GetKeys[K comparable, V any](obj map[K]V) []K
- func GetValues[K comparable, V any](obj map[K]V) []V
- func GetZeroValue[T any]() T
- func IsAlpha(s string) bool
- func IsAlphaNumeric(s string) bool
- func IsArray(o interface{}) bool
- func IsBool(o interface{}) bool
- func IsChannel(o interface{}) bool
- func IsNumeric(s string) bool
- func IsSlice(o interface{}) bool
- func IsTime(o interface{}) bool
- func IsWhole(v float64) bool
- func KebabCase(str string) string
- func Map[T any, U any](items []T, mapper func(T) U) []U
- func PadEnd(s string, length int, fillStr string) string
- func PadStart(s string, length int, fillStr string) string
- func PascalCase(str string) string
- func Pop[T ~[]E, E any](s *T) (E, error)
- func Push[T ~[]E, E any](s *T, items ...E)
- func Reduce[T any, U any](items []T, initial U, reducer func(U, T) U) U
- func RuneLength(str string) int
- func Shift[T ~[]E, E any](s *T) (E, error)
- func Some[T ~[]E, E comparable](items T, fn Predicate[E]) bool
- func Splice[T ~[]E, E any](s *T, start int, removes int, items ...E) error
- func Substring[T ~string](from T, offset int, length uint) T
- func Try(callback func() error) (ok bool)
- func Unshift[T ~[]E, E any](s *T, items ...E) T
- func Validate(ok bool, format string, args ...any) error
- func Words(str string) []string
- type Predicate
Examples ¶
Constants ¶
const ( Int string = "^(?:[-+]?(?:0|[1-9][0-9]*))$" Float string = "^(?:[-+]?(?:[0-9]+))?(?:\\.[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$" Hexadecimal string = "^[0-9a-fA-F]+$" DataURI string = "^data:.+\\/(.+);base64$" WinPath string = `^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$` UnixPath string = `^((?:\/[a-zA-Z0-9\.\:]+(?:_[a-zA-Z0-9\:\.]+)*(?:\-[\:a-zA-Z0-9\.]+)*)+\/?)$` Semver string = "" /* 183-byte string literal not displayed */ URL string = `` /* 322-byte string literal not displayed */ )
Variables ¶
var ( RegDataURI = regexp.MustCompile(DataURI) RegURL = regexp.MustCompile(URL) RegInt = regexp.MustCompile(Int) RegFloat = regexp.MustCompile(Float) RegSemver = regexp.MustCompile(Semver) )
Functions ¶
func CamelCase ¶ added in v0.1.10
CamelCase converts string to camel case. eg."hello world" will be "helloWorld"
func Capitalize ¶ added in v0.1.10
Capitalize converts the first character of string to upper case and the remaining to lower case.
func DecimalsDiv ¶ added in v0.1.17
DecimalsDiv return the value of multiple parameters multiplied sequentially
Example ¶
v, _ := DecimalsDiv("100", "0.2", "5") fmt.Println(v)
Output: 100
func DecimalsDivRound ¶ added in v0.1.17
DecimalsDivRound rounds the decimal to places decimal places. If places < 0, it will round the integer part to the nearest 10^(-places).
Example ¶
v, _ := DecimalsDivRound("0.006", "10", 3) fmt.Println(v)
Output: 0.001
func DecimalsMult ¶ added in v0.1.17
DecimalsMult return the product of mutiple big numbers
Example ¶
v, _ := DecimalsMult("0.001", "10", "1") fmt.Println(v)
Output: 0.01
func DecimalsPlus ¶ added in v0.1.17
DecimalsPlus return the value of multiple parameters added sequentially, DecimalsPlus("0.1", "0.2") = 0.3,
Example ¶
v, _ := DecimalsPlus("0.1", "0.2", "0.3") fmt.Println(v)
Output: 0.6
func DecimalsPow ¶ added in v0.1.17
Example ¶
v, _ := DecimalsPow("0.1", "2") fmt.Println(v)
Output: 0.01
func DecimalsSub ¶ added in v0.1.17
DecimalsSub return the value of multiple parameters subtracted sequentially. DecimalsSub("0.3", "0.2", "0.05") = 0.05,
Example ¶
v, _ := DecimalsSub("1", "0.2", "0.3") fmt.Println(v)
Output: 0.5
func Ellipsis ¶ added in v0.1.10
Ellipsis trims and truncates a string to a specified length and appends an ellipsis if truncated.
func Every ¶ added in v0.1.6
func Every[T ~[]E, E comparable](items T, fn Predicate[E]) bool
Return true only if all Predicate(item) == true
func Filter ¶
func Filter[T ~[]E, E comparable](items T, fn Predicate[E]) T
Filter the slice object
Example ¶
nums := []int32{1, 2, 3, 4, 5} rst := Filter(nums, func(item int32) bool { return item > 4 }) fmt.Printf("%v", rst)
Output: [5]
func Flat ¶ added in v0.1.8
func Flat[T ~[]E, E any](items []T) []E
Flat returns an array with a single level deep.
Example ¶
items := [][]int{ {1, 2, 3}, {1}, {4, 5, 6}, } rst := Flat(items) fmt.Printf("%v", rst)
Output: [1 2 3 1 4 5 6]
func ForEach ¶ added in v0.1.6
Another forms for for loop instead of for .. range, loop will stop when callback with False result
func FormatDuration ¶ added in v0.1.18
FormatDuration formats a time range into a specified expression, such as MM:DD HH:mm:ss.
Example ¶
d := time.Second*3 + time.Hour*2 + time.Minute*21 str := FormatDuration(d, "HH:mm:ss") fmt.Println(str)
Output: 02:21:03
func GetValues ¶
func GetValues[K comparable, V any](obj map[K]V) []V
Retrive all the values from a map
func IsAlpha ¶ added in v0.1.12
IsAlpha check if the string contains only letters (a-zA-Z). Empty string is valid.
Example ¶
ret := IsAlpha("basdcf") fmt.Println(ret) // Output // true
Output:
func IsAlphaNumeric ¶ added in v0.1.13
IsAlphanumeric check if the string contains only letters and numbers. Empty string is valid.
Example ¶
rst := IsAlphaNumeric("abcd13") fmt.Println(rst)
Output: true
func IsArray ¶ added in v0.1.11
func IsArray(o interface{}) bool
IsArray will retrun true when type of the o is array
func IsBool ¶ added in v0.1.11
func IsBool(o interface{}) bool
IsBool will return true when type of the o is boolean
func IsChannel ¶ added in v0.1.11
func IsChannel(o interface{}) bool
IsChannel will return true when type of the o is channel
func IsNumeric ¶ added in v0.1.13
IsNumeric check if the string contains only numbers. Empty string is valid.
Example ¶
rst := IsNumeric("1234") fmt.Println(rst)
Output: true
func IsSlice ¶ added in v0.1.11
func IsSlice(o interface{}) bool
IsSlice will retrun true when type of the o is slice
func IsTime ¶ added in v0.1.11
func IsTime(o interface{}) bool
IsDate will return true when type of the data is time.Time
func IsWhole ¶ added in v0.1.13
IsWhole returns true if v is whole number
Example ¶
v := IsWhole(3) fmt.Println(v)
Output: true
func KebabCase ¶ added in v0.1.10
KebabCase converts string to kebab case. eg. "hello world" will be "hello-world"
func Map ¶
Iterate over each item in a slice sequentially
Example ¶
nums := []int32{1, 2, 3, 4, 5} rst := Map(nums, func(item int32) int32 { return item * 2 }) fmt.Printf("%v", rst)
Output: [2 4 6 8 10]
func PadEnd ¶ added in v0.1.19
PadEnd pads the right side of a string with characters until it meets the specified length
Example ¶
rst := PadEnd("x", 5, "0") fmt.Println(rst)
Output: x0000
func PadStart ¶ added in v0.1.19
PadStart pads the left side of a string with characters until it meets the specified length
Example ¶
rst := PadStart("x", 5, "0") fmt.Println(rst)
Output: 0000x
func PascalCase ¶ added in v0.1.10
PascalCase converts string to pascal case. eg."hello world" will be "HelloWorld"
func Pop ¶ added in v0.1.14
Pop removes and returns the last element from a slice
Example ¶
items := []int{ 1, 2, 3, } p, _ := Pop(&items) fmt.Printf("%v, %v", p, items)
Output: 3, [1 2]
func Push ¶ added in v0.1.14
func Push[T ~[]E, E any](s *T, items ...E)
Append insert new item to the end of the slice
Example ¶
items := []int{ 1, 2, 3, } Push(&items, 4, 5) fmt.Printf("%v", items)
Output: [1 2 3 4 5]
func Reduce ¶
Iterate over each item in the slice sequentially, passing the result of the previous iteration to the next iteration's function execution
Example ¶
nums := []int32{1, 2, 3, 4, 5} total := Reduce(nums, 0, func(prev int32, now int32) int32 { return prev + now }) fmt.Printf("%v", total)
Output: 15
func RuneLength ¶ added in v0.1.10
RuneLength is an alias to utf8.RuneCountInString which returns the number of runes in string.
func Shift ¶ added in v0.1.14
Shift remove and returns the first element from a slice
Example ¶
items := []int{ 1, 2, 3, } f, _ := Shift(&items) fmt.Printf("%v, %v", f, items)
Output: 1, [2 3]
func Some ¶ added in v0.1.6
func Some[T ~[]E, E comparable](items T, fn Predicate[E]) bool
Return true if any Predicate(item) == true
func Splice ¶ added in v0.1.14
Splice modifies the slice by removing and/or adding elements
Example ¶
items := []int{ 1, 2, 7, 8, 5, } Splice(&items, 2, 2, []int{3, 4}...) expected := []int{ 1, 2, 3, 4, 5, } fmt.Printf("%v", expected)
Output: [1 2 3 4 5]
func Try ¶ added in v0.1.9
Try call function, return false in case of error.
Example ¶
b := Try(func() error { panic("ho no, don't panic here") }) fmt.Println(b)
Output: false
func Unshift ¶ added in v0.1.15
func Unshift[T ~[]E, E any](s *T, items ...E) T
Splice add the slice to the front of slice
Example ¶
items := []int{1, 2, 7, 8, 5} Unshift(&items, 0) fmt.Println(items)
Output: [0 1 2 7 8 5]
Types ¶
type Predicate ¶ added in v0.1.6
type Predicate[T comparable] func(T) bool