Documentation ¶
Overview ¶
Package stringz contains string functions similar in spirit to the stdlib strings package.
Index ¶
- Constants
- func ByteSized(size int64, precision int, sep string) string
- func DateUTC(t time.Time) string
- func FormatFloat(f float64) string
- func GenerateAlphaColName(n int, lower bool) string
- func InSlice(haystack []string, needle string) bool
- func LineCount(r io.Reader, skipEmpty bool) int
- func ParseBool(s string) (bool, error)
- func ParseDateOrTimestampUTC(s string) (time.Time, error)
- func ParseLocalDate(s string) (time.Time, error)
- func ParseTimestampUTC(s string) (time.Time, error)
- func ParseUTCDate(s string) (time.Time, error)
- func Plu(s string, i int) string
- func PrefixSlice(a []string, w string) []string
- func RepeatJoin(s string, count int, sep string) string
- func Reverse(input string) string
- func SanitizeAlphaNumeric(s string, r rune) string
- func SliceIndex(haystack []string, needle string) int
- func SprintJSON(value any) string
- func Surround(s, w string) string
- func SurroundSlice(a []string, w string) []string
- func TimestampToDate(s string) string
- func TimestampToRFC3339(s string) string
- func TimestampUTC(t time.Time) string
- func TrimLen(s string, maxLen int) string
- func UUID() string
- func Uniq32() string
- func Uniq8() string
- func UniqN(length int) string
- func UniqPrefix(s string) string
- func UniqSuffix(s string) string
- func UniqTableName(tbl string) string
Constants ¶
const ( // DateFormat is the layout for dates (without a time component), such as 2006-01-02. DateFormat = "2006-01-02" // TimeFormat is the layout for 24-hour time (without a date component), such as 15:04:05. TimeFormat = "15:04:05" // DatetimeFormat is the layout for a date/time timestamp. DatetimeFormat = time.RFC3339Nano )
const ( // RFC3339Milli is an RFC3339 format with millisecond precision. RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" // RFC3339MilliZulu is the same as RFC3339Milli, but in zulu time. RFC3339MilliZulu = "2006-01-02T15:04:05.000Z" // RFC3339Zulu is an RFC3339 format, in Zulu time. RFC3339Zulu = "2006-01-02T15:04:05Z" // ISO8601 is similar to RFC3339Milli, but doesn't have the colon // in the timezone offset. ISO8601 = "2006-01-02T15:04:05.000Z0700" // DateOnly is a date-only format. DateOnly = "2006-01-02" )
const Redacted = "xxxxx"
Redacted is the "xxxxx" string used for redacted values, such as passwords.
Variables ¶
This section is empty.
Functions ¶
func ByteSized ¶
ByteSized returns a human-readable byte size, e.g. "2.1 MB", "3.0 TB", etc. TODO: replace this usage with "github.com/c2h5oh/datasize"
func FormatFloat ¶
FormatFloat formats f. This method exists to provide a standard float formatting across the codebase.
func GenerateAlphaColName ¶
GenerateAlphaColName returns an Excel-style column name for index n, starting with A, B, C... and continuing to AA, AB, AC, etc...
func LineCount ¶
LineCount returns the number of lines in r. If skipEmpty is true, empty lines are skipped (a whitespace-only line is not considered empty). If r is nil or any error occurs, -1 is returned.
func ParseBool ¶
ParseBool is an expansion of strconv.ParseBool that also accepts variants of "yes" and "no" (which are bool representations returned by some data sources).
func ParseDateOrTimestampUTC ¶ added in v0.21.0
ParseDateOrTimestampUTC attempts to parse s as either a date (see ParseUTCDate), or timestamp (see ParseTimestampUTC). The returned time is in UTC.
func ParseLocalDate ¶ added in v0.21.0
ParseLocalDate accepts a date string s, returning the local midnight time of that date. Arg s must in format "2006-01-02".
func ParseTimestampUTC ¶ added in v0.21.0
ParseTimestampUTC is the counterpart of TimestampUTC. It attempts to parse s first in RFC3339Milli, then time.RFC3339 format, falling back to the subtly different ISO8601 format.
func ParseUTCDate ¶ added in v0.21.0
ParseUTCDate accepts a date string s, returning the UTC midnight time of that date. Arg s must in format "2006-01-02".
func Plu ¶
Plu handles the most common (English language) case of pluralization. With arg s being "row(s) col(s)", Plu returns "row col" if arg i is 1, otherwise returns "rows cols".
func PrefixSlice ¶
PrefixSlice returns a new slice with each element of a prefixed with w, unless a is nil, in which case nil is returned.
func RepeatJoin ¶
RepeatJoin returns a string consisting of count copies of s separated by sep. For example:
stringz.RepeatJoin("?", 3, ", ") == "?, ?, ?"
func SanitizeAlphaNumeric ¶
SanitizeAlphaNumeric replaces any non-alphanumeric runes of s with r (which is typically underscore).
a#2%3.4_ --> a_2_3_4_
func SliceIndex ¶
SliceIndex returns the index of needle in haystack, or -1.
func SprintJSON ¶
func SurroundSlice ¶
SurroundSlice returns a new slice with each element of a prefixed and suffixed with w, unless a is nil, in which case nil is returned.
func TimestampToDate ¶ added in v0.21.0
TimestampToDate takes a RFC3339Milli, ISO8601 or RFC3339 timestamp, and returns just the date component. On error, the empty string is returned.
func TimestampToRFC3339 ¶ added in v0.21.0
TimestampToRFC3339 takes a RFC3339Milli, ISO8601 or RFC3339 timestamp, and returns RFC3339. That is, the milliseconds are dropped. On error, the empty string is returned.
func TimestampUTC ¶ added in v0.21.0
TimestampUTC returns the RFC3339Milli representation of t in UTC.
func Uniq32 ¶
func Uniq32() string
Uniq32 returns a UUID-like string that only contains alphanumeric chars. The result has length 32. The first element is guaranteed to be a letter.
func Uniq8 ¶
func Uniq8() string
Uniq8 returns a UUID-like string that only contains alphanumeric chars. The result has length 8. The first element is guaranteed to be a letter.
func UniqN ¶ added in v0.16.0
UniqN returns a uniq string of length n. The first element is guaranteed to be a letter.
func UniqPrefix ¶ added in v0.16.0
UniqPrefix returns s with a unique prefix.
func UniqTableName ¶
UniqTableName returns a new lower-case table name based on tbl, with a unique suffix, and a maximum length of 63. This value of 63 is chosen because it's less than the maximum table name length for Postgres, SQL Server, SQLite and MySQL.
Types ¶
This section is empty.