Documentation
¶
Index ¶
- func ContainsInt(s []int, e int) bool
- func ContainsStr(s []string, e string) bool
- func GenerateID(n int) string
- func GenerateKey(n int) string
- func GenerateRandomNumber(n int) string
- func GenerateToken(email string) string
- func GetRandomItem[T any](slice []T) T
- func IsE164(number string) bool
- func MustJSON(obj any) string
- func NormalizeDomainName(domain string) string
- func NormalizePhone(rawNumber, region string) (string, error)
- func ParseUnixTimestamp(timestamp int64) *time.Time
- func Round(num float64) int
- func SnakeCase(str string) string
- func ToFixed(num float64, precision int) float64
- func ToSliceAny[T any](objs []T) []any
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContainsInt ¶
ContainsInt checks if a slice of integers contains a specific integer.
This function iterates over the slice and returns true if the specified integer is found.
Parameters:
- s: The slice of integers to search.
- e: The integer to look for.
Returns:
- A boolean value indicating whether the integer is found in the slice.
func ContainsStr ¶
ContainsStr checks if a slice of strings contains a specific string.
This function iterates over the slice and returns true if the specified string is found.
Parameters:
- s: The slice of strings to search.
- e: The string to look for.
Returns:
- A boolean value indicating whether the string is found in the slice.
func GenerateID ¶
GenerateID generates a unique ID of the specified length.
This function uses the `gonanoid` library to create a random string consisting of characters from the predefined `alphabet`.
Parameters:
- n: The length of the ID to be generated.
Returns:
- A string representing the generated ID. If an error occurs during generation, an empty string is returned.
func GenerateKey ¶
GenerateKey generates a random alphanumeric string of the specified length.
This function uses a seeded random number generator to create a string consisting of characters from the `letterRunes` set.
Parameters:
- n: The length of the key to be generated.
Returns:
- A string containing the randomly generated key.
func GenerateRandomNumber ¶
GenerateRandomNumber generates a random numeric string of the specified length.
This function uses a seeded random number generator to create a string consisting of characters from the `numberRunes` set.
Parameters:
- n: The length of the numeric string to be generated.
Returns:
- A string containing the randomly generated numeric value.
func GenerateToken ¶
GenerateToken generates a unique token based on the provided email string.
This function first hashes the email using bcrypt with the default cost. If an error occurs during the hashing process, the program logs the error and exits. The resulting bcrypt hash is then further hashed using the MD5 algorithm, and the final token is returned as a hexadecimal-encoded string.
Parameters:
- email: The email string to be used for generating the token.
Returns:
- A string representing the generated token in hexadecimal format.
func GetRandomItem ¶
func GetRandomItem[T any](slice []T) T
GetRandomItem selects a random item from a slice.
This function seeds the random number generator with the current time and generates a random index to pick an element from the input slice.
Type Parameters:
- T: The type of elements in the input slice.
Parameters:
- slice: A slice of type `T` from which a random item will be selected.
Returns:
- An element of type `T` randomly selected from the input slice.
func MustJSON ¶
MustJSON converts an object to its JSON string representation.
This function marshals the given object into a JSON string. If an error occurs during the marshaling process, it is ignored, and the resulting string is returned.
Parameters:
- obj: The object to be marshaled into JSON.
Returns:
- A string containing the JSON representation of the object.
func NormalizeDomainName ¶
NormalizeDomainName normalizes a domain name by replacing dots with underscores and trimming any leading or trailing whitespace.
Parameters:
- domain: The domain name to be normalized.
Returns:
- A string representing the normalized domain name.
func NormalizePhone ¶ added in v0.14.0
NormalizePhone formats a phone number to E.164 if needed
func ParseUnixTimestamp ¶
ParseUnixTimestamp converts a Unix timestamp to a *time.Time object.
This function handles both second-based and millisecond-based Unix timestamps. If the input timestamp is 0, the function returns nil.
Parameters:
- timestamp: The Unix timestamp to be parsed. It can be in seconds or milliseconds.
Returns:
- A pointer to a time.Time object representing the parsed timestamp, or nil if the input is 0.
func Round ¶
Round rounds a floating-point number to the nearest integer.
This function uses `math.Copysign` to handle rounding for both positive and negative numbers.
Parameters:
- num: The floating-point number to be rounded.
Returns:
- An integer representing the rounded value.
func SnakeCase ¶
SnakeCase converts a camel case string to snake case.
This function uses regular expressions to identify camel case patterns and replaces them with underscores to create a snake case string.
Parameters:
- str: The input string in camel case format.
Returns:
- A string converted to snake case.
func ToFixed ¶
ToFixed rounds a floating-point number to a specified number of decimal places.
This function multiplies the input number by 10 raised to the power of the specified precision, rounds it to the nearest integer, and then divides it back to achieve the desired precision.
Parameters:
- num: The floating-point number to be rounded.
- precision: The number of decimal places to round to.
Returns:
- A floating-point number rounded to the specified precision.
func ToSliceAny ¶
ToSliceAny converts a slice of any type to a slice of empty interface values.
This function iterates over the input slice and appends each element to a new slice of type `[]any`.
Type Parameters:
- T: The type of elements in the input slice.
Parameters:
- objs: A slice of type `T` to be converted.
Returns:
- A slice of type `[]any` containing the elements of the input slice.
Types ¶
This section is empty.