primitives

package
v0.0.0-...-94a580d Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package primitives is collection of utility functions to ease working with primitive types in go ─── ACKNOWLEDGEMENTS ─────────────────────────────────────────────────────────── github.com/hoempf/creader github.com/danilopolani/gosc github.com/mina86/unsafeConvert github.com/fmstephe/unsafeutil github.com/alecthomas/unsafeslice ────────────────────────────────────────────────────────────────────────────────

Index

Constants

View Source
const (

	// Ki ...
	Ki = 1 << (10 * iota)
	// Mi ...
	Mi
	// Gi ...
	Gi
	// Ti ...
	Ti
	// Pi ...
	Pi
	// Ei ...
	Ei
)

Binary prefixes for common use.

View Source
const (
	MaxUint = ^uint(0)
	MaxInt  = int(MaxUint >> 1)
)

consts -

Variables

View Source
var (
	// ErrByteSliceTooLarge ...
	ErrByteSliceTooLarge = stacktrace.NewError("ByteSlice: too large")
)

errors

Functions

func AllFloat

func AllFloat(s []float64, f func(float64) bool) bool

AllFloat returns true if all of the float64s in the slice satisfy the predicate f

func AllInt

func AllInt(s []int, f func(int) bool) bool

AllInt returns true if all of the ints in the slice satisfy the predicate f

func AllString

func AllString(s []string, f func(string) bool) bool

AllString returns true if all of the strings in the slice satisfy the predicate f

func AnyFloat

func AnyFloat(s []float64, f func(float64) bool) bool

AnyFloat returns true if one of the float64s in the slice satisfies the predicate f

func AnyInt

func AnyInt(s []int, f func(int) bool) bool

AnyInt returns true if one of the ints in the slice satisfies the predicate f

func AnyString

func AnyString(s []string, f func(string) bool) bool

AnyString returns true if one of the strings in the slice satisfies the predicate f

func ByteCountBinary

func ByteCountBinary(b int64) string

ByteCountBinary returns a string representaion of bytes with a base of 2

func ByteCountDecimal

func ByteCountDecimal(b int64) string

ByteCountDecimal returns a string represenation of bytes with a base of 10

func ByteSliceFromString

func ByteSliceFromString(str string) []byte

ByteSliceFromString converts a string into a slice of bytes without performing a copy. [NOTE] => This is an unsafe operation and may lead to problems if the bytes are changed.

func CutBytes

func CutBytes(a []byte, from, to int) []byte

CutBytes elements from slice for a given range

func Delete

func Delete(s interface{}, i int)

Delete an item from a slice

func DurationToInt

func DurationToInt(duration, unit time.Duration) int

DurationToInt -

func EqSlices

func EqSlices(a, b interface{}) bool

EqSlices returns true if two slices are equal (not in-depth, for that use reflect.DeepEqual)

func EveryFloat

func EveryFloat(s []float64, f func(float64) bool) bool

EveryFloat returns true if all of the float64s in the slice satisfy the predicate f

func EveryInt

func EveryInt(s []int, f func(int) bool) bool

EveryInt returns true if all of the ints in the slice satisfy the predicate f

func EveryString

func EveryString(s []string, f func(string) bool) bool

EveryString returns true if all of the strings in the slice satisfy the predicate f

func FileSizeStringToInt

func FileSizeStringToInt(s string) (int64, error)

FileSizeStringToInt ...

func FilterFloat

func FilterFloat(s []float64, f func(float64) bool) []float64

FilterFloat returns a new slice containing all float64s in the slice that satisfy the predicate f.

func FilterInt

func FilterInt(s []int, f func(int) bool) []int

FilterInt returns a new slice containing all ints in the slice that satisfy the predicate f.

func FilterString

func FilterString(s []string, f func(string) bool) []string

FilterString returns a new slice containing all strings in the slice that satisfy the predicate f.

func FromBase64

func FromBase64(s string) string

FromBase64 decodes a string from base64

func HasPrefix

func HasPrefix(s string, prefix string) bool

HasPrefix ...

func HasSuffix

func HasSuffix(s string, suffix string) bool

HasSuffix ...

func InSlice

func InSlice(v interface{}, s interface{}) bool

InSlice returns a boolean if the value is in the slice v = value to find s = slice

func IndentedJSON

func IndentedJSON(data interface{}) (*bytes.Buffer, error)

func IndentedYAML

func IndentedYAML(data interface{}) (*bytes.Buffer, error)

func Index

func Index(s interface{}, t interface{}) int

Index returns the index of an element in a slice or -1 if not found

func Indexi

func Indexi(s []string, t string) int

Indexi returns the index of a string in a slice or -1 if not found. Case Insentive.

func InsertBytes

func InsertBytes(a []byte, i int, b []byte) []byte

InsertBytes new slice at specified position

func InsertInStringSlice

func InsertInStringSlice(a []string, index int, value string) []string

func IntToFileSizeString

func IntToFileSizeString(size, unit int) string

GetSizeString ...

func IsBool

func IsBool(s string) bool

IsBool checks if a string is a boolean

func IsCreditCard

func IsCreditCard(s string) bool

IsCreditCard returns true if the provided string is a valid credit card

func IsEmail

func IsEmail(s string) bool

IsEmail returns true if the provided string is a valid email address

func IsFloat

func IsFloat(s string) bool

IsFloat checks if a string is a float number

func IsHexColor

func IsHexColor(s string) bool

IsHexColor returns true if the provided string is a valid HEX color

func IsIP

func IsIP(s string) bool

IsIP returns true if the provided string is a valid IPv4

func IsInt

func IsInt(s string) bool

IsInt checks if a string is an integer

func IsJSON

func IsJSON(s string) bool

IsJSON returns true if the provided string is a valid JSON document

func IsOnlyAlphaNumeric

func IsOnlyAlphaNumeric(s string) bool

IsOnlyAlphaNumeric returns true if the provided string is composed only by letters and numbers

func IsOnlyDigits

func IsOnlyDigits(s string) bool

IsOnlyDigits returns true if the provided string is composed only by numbers

func IsOnlyLetters

func IsOnlyLetters(s string) bool

IsOnlyLetters returns true if the provided string is composed only by letters

func IsRGB

func IsRGB(s string) bool

IsRGB returns true if the provided string is a valid RGB color

func IsStringEqual

func IsStringEqual(s1 string, s2 string) bool

IsStringEqual ...

func IsURL

func IsURL(s string) bool

IsURL returns true if the provided string is a valid url

func LowerFirst

func LowerFirst(s string) string

LowerFirst returns a string with the first character lowercased

func MakeByteSlice

func MakeByteSlice(n int) []byte

MakeByteSlice allocates a slice of size n. If the allocation fails, it panics with ErrTooLarge.

func MapFloat

func MapFloat(s []float64, f func(float64) float64) []float64

MapFloat a new slice containing the results of applying the function f to each float64 in the original slice.

func MapInt

func MapInt(s []int, f func(int) int) []int

MapInt a new slice containing the results of applying the function f to each int in the original slice.

func MapString

func MapString(s []string, f func(string) string) []string

MapString a new slice containing the results of applying the function f to each string in the original slice.

func NormalizePath

func NormalizePath(p string) string

func PathJoin

func PathJoin(elem ...string) string

PathJoin ...

func ReplaceBytes

func ReplaceBytes(a []byte, from, to int, new []byte) []byte

ReplaceBytes function unlike bytes.Replace allows you to specify range

func ReverseSort

func ReverseSort(s interface{})

ReverseSort is an alias of Rsort

func ReverseString

func ReverseString(s string) string

ReverseString returns the string reversed

func RoundUp

func RoundUp(v uint64) uint64

RoundUp takes a uint64 greater than 0 and rounds it up to the next power of 2.

func Rsort

func Rsort(s interface{})

Rsort reverses the order (desc) of a slice

func SomeFloat

func SomeFloat(s []float64, f func(float64) bool) bool

SomeFloat returns true if one of the float64s in the slice satisfies the predicate f

func SomeInt

func SomeInt(s []int, f func(int) bool) bool

SomeInt returns true if one of the ints in the slice satisfies the predicate f

func SomeString

func SomeString(s []string, f func(string) bool) bool

SomeString returns true if one of the strings in the slice satisfies the predicate f

func StringFromByteSlice

func StringFromByteSlice(bytes []byte) string

StringFromByteSlice converts a slice of bytes into a string without performing a copy. This is an unsafe operation and may lead to problems if the bytes passed as argument are changed while the string is used.

func ToBase64

func ToBase64(s string) string

ToBase64 encodes a string in base64

func ToCamelCase

func ToCamelCase(s string) string

ToCamelCase converts a string to camelCase

func ToInt

func ToInt(s string) int

ToInt returns an int from a string

func ToInt64

func ToInt64(s string) int64

ToInt64 returns an int64 from a string

func ToKebabCase

func ToKebabCase(s string) string

ToKebabCase converts a string to kebab-case

func ToPascalCase

func ToPascalCase(s string) string

ToPascalCase converts a string to PascalCase

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase converts a string to snake_case

func ToUint

func ToUint(s string) uint

ToUint returns a uint from a string

func Unique

func Unique() string

Unique a unique token based on current time and crypted in sha256

func UpperFirst

func UpperFirst(s string) string

UpperFirst returns a string with the first character uppercased

func Utoa

func Utoa(u uint) string

Utoa transforms a uint into a string

Types

This section is empty.

Jump to

Keyboard shortcuts

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