golodash

package module
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 13 Imported by: 0

README

go-lodash

A set of lodash-like utilities in Golang.

Installation

$ go get github.com/189/golodash

Documentation

Index

Examples

Constants

View Source
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

View Source
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

func CamelCase(str string) string

CamelCase converts string to camel case. eg."hello world" will be "helloWorld"

func Capitalize added in v0.1.10

func Capitalize(str string) string

Capitalize converts the first character of string to upper case and the remaining to lower case.

func DecimalsDiv added in v0.1.17

func DecimalsDiv(input string, rest ...string) (string, error)

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

func DecimalsDivRound(input string, divider string, precision int32) (string, error)

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

func DecimalsMult(input string, rest ...string) (string, error)

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

func DecimalsPlus(input string, rest ...string) (string, error)

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

func DecimalsPow(input string, d2 string) (string, error)
Example
v, _ := DecimalsPow("0.1", "2")
fmt.Println(v)
Output:

0.01

func DecimalsSub added in v0.1.17

func DecimalsSub(input string, rest ...string) (string, error)

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

func Ellipsis(str string, length int) string

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

func ForEach[T ~[]E, E any](items T, callback func(idx int, item E) bool)

Another forms for for loop instead of for .. range, loop will stop when callback with False result

func FormatDuration added in v0.1.18

func FormatDuration(d time.Duration, formatStr string) string

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 GetKeys

func GetKeys[K comparable, V any](obj map[K]V) []K

Retrive all the keys from a map

func GetValues

func GetValues[K comparable, V any](obj map[K]V) []V

Retrive all the values from a map

func GetZeroValue added in v0.1.10

func GetZeroValue[T any]() T

Get an empty value

func IsAlpha added in v0.1.12

func IsAlpha(s string) bool

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

func IsAlphaNumeric(s string) bool

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

func IsNumeric(s string) bool

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

func IsWhole(v float64) bool

IsWhole returns true if v is whole number

Example
v := IsWhole(3)
fmt.Println(v)
Output:

true

func KebabCase added in v0.1.10

func KebabCase(str string) string

KebabCase converts string to kebab case. eg. "hello world" will be "hello-world"

func Map

func Map[T any, U any](items []T, mapper func(T) U) []U

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

func PadEnd(s string, length int, fillStr string) string

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

func PadStart(s string, length int, fillStr string) string

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

func PascalCase(str string) string

PascalCase converts string to pascal case. eg."hello world" will be "HelloWorld"

func Pop added in v0.1.14

func Pop[T ~[]E, E any](s *T) (E, error)

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

func Reduce[T any, U any](items []T, initial U, reducer func(U, T) U) U

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

func RuneLength(str string) int

RuneLength is an alias to utf8.RuneCountInString which returns the number of runes in string.

func Shift added in v0.1.14

func Shift[T ~[]E, E any](s *T) (E, error)

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

func Splice[T ~[]E, E any](s *T, start int, removes int, items ...E) error

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 Substring added in v0.1.10

func Substring[T ~string](from T, offset int, length uint) T

Return Part of a string

func Try added in v0.1.9

func Try(callback func() error) (ok bool)

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]

func Validate added in v0.1.9

func Validate(ok bool, format string, args ...any) error

Create error and return if the condition is not met

func Words added in v0.1.10

func Words(str string) []string

Words splits string into an array of its words.

Types

type Predicate added in v0.1.6

type Predicate[T comparable] func(T) bool

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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