strutil

package
v0.3.27 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyTrimmedStringIntMap added in v0.3.23

func CopyTrimmedStringIntMap(input map[string]int) map[string]int

CopyTrimmedStringIntMap returns a defensive copy of a string→int map with whitespace trimmed from all keys.

func DeduplicateStrings added in v0.1.19

func DeduplicateStrings(input []string, sorted bool) []string

DeduplicateStrings returns a new slice with whitespace-trimmed, empty, and duplicate entries removed from input. When sorted is true the result is sorted in ascending order. The relative order of first-seen entries is preserved when sorted is false.

func DeepCloneJSON added in v0.3.21

func DeepCloneJSON(v interface{}) interface{}

DeepCloneJSON creates a deep copy of a JSON-compatible value.

func FormatDuration added in v0.2.25

func FormatDuration(d time.Duration) string

FormatDuration formats a duration for display like the debug npm package. It provides granular formatting from nanoseconds to hours.

func FormatFutureTime added in v0.3.7

func FormatFutureTime(t time.Time) string

FormatFutureTime returns a human-readable representation of a future time, combining an RFC3339 timestamp with a relative countdown (e.g. "2026-05-03T12:00:00Z (in 5.0m)"). Returns "unknown" when t is the zero value.

func GetStringFromMap added in v0.3.23

func GetStringFromMap(m map[string]interface{}, keys ...string) string

GetStringFromMap returns the first non-empty string value found for any of the given keys in m. For each key, the value must be present, typed as string, and non-empty to be returned. Returns an empty string when no matching non-empty string value is found, when the map is nil, or when no keys are provided.

With a single key the behaviour is equivalent to `v, _ := m[key].(string)`:

GetStringFromMap(m, "owner")

With multiple keys the function returns the first non-empty match, which is useful for maps that may use either snake_case or camelCase field names:

GetStringFromMap(m, "html_url", "htmlUrl")

func RandomBytes added in v0.3.21

func RandomBytes(n int) ([]byte, error)

RandomBytes returns n cryptographically random bytes.

func RandomHex added in v0.2.18

func RandomHex(n int) (string, error)

RandomHex returns a hex-encoded string of n cryptographically random bytes. The returned string has length 2*n.

func RandomHexWithFallback added in v0.3.2

func RandomHexWithFallback(n int) string

RandomHexWithFallback returns a hex-encoded string of n random bytes. On the normal path it returns the same output as RandomHex(n) — a string of length 2*n containing cryptographically random hex characters. If crypto/rand is unavailable, it falls back to a hex-encoded pid+nanosecond value that is unique within a single process run. The fallback is non-cryptographic and should only arise in unusual runtime environments; it always produces a 32-character hex string (16 bytes), regardless of n. For the typical call site (n == 16) the fallback output length matches the normal output length.

func SessionSuffix added in v0.3.27

func SessionSuffix(sessionID string) string

SessionSuffix returns a formatted session suffix for log messages. Returns " for session '<sessionID>'" when sessionID is non-empty, or "" otherwise.

func Truncate

func Truncate(s string, maxLen int) string

Truncate truncates a string to the specified maximum length. If the string is longer than maxLen, it's truncated and "..." is appended. If maxLen is 0, returns "..." for non-empty strings, empty string for empty strings. If maxLen is negative, the original string is returned.

func TruncateRunes added in v0.2.18

func TruncateRunes(s string, maxRunes int) string

TruncateRunes truncates s to at most maxRunes Unicode code points (runes). Unlike Truncate, which counts bytes, TruncateRunes is safe for non-ASCII content (e.g. emoji, CJK characters). If maxRunes is 0 or negative, returns an empty string.

func TruncateWithSuffix

func TruncateWithSuffix(s string, maxLen int, suffix string) string

TruncateWithSuffix truncates a string to the specified maximum length with a custom suffix. If the string is longer than maxLen, it's truncated and suffix is appended. If maxLen is 0 or negative, the original string is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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