stringz

package
v0.15.8 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package stringz contains string functions similar in spirit to the stdlib strings package.

Index

Constants

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

Variables

This section is empty.

Functions

func ByteSized

func ByteSized(size int64, precision int, sep string) string

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

func FormatFloat(f float64) string

FormatFloat formats f. This method exists to provide a standard float formatting across the codebase.

func GenerateAlphaColName

func GenerateAlphaColName(n int, lower bool) string

GenerateAlphaColName returns an Excel-style column name for index n, starting with A, B, C... and continuing to AA, AB, AC, etc...

func InSlice

func InSlice(haystack []string, needle string) bool

InSlice returns true if the needle is present in the haystack.

func LineCount

func LineCount(r io.Reader, skipEmpty bool) int

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

func ParseBool(s string) (bool, error)

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 Plu

func Plu(s string, i int) string

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

func PrefixSlice(a []string, w string) []string

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

func RepeatJoin(s string, count int, sep string) string

RepeatJoin returns a string consisting of count copies of s separated by sep. For example:

stringz.RepeatJoin("?", 3, ", ") == "?, ?, ?"

func Reverse

func Reverse(input string) string

Reverse reverses the input string.

func SanitizeAlphaNumeric

func SanitizeAlphaNumeric(s string, r rune) string

SanitizeAlphaNumeric replaces any non-alphanumeric runes of s with r (which is typically underscore).

a#2%3.4_ --> a_2_3_4_

func SliceIndex

func SliceIndex(haystack []string, needle string) int

SliceIndex returns the index of needle in haystack, or -1.

func SprintJSON

func SprintJSON(value interface{}) string

func Surround

func Surround(s, w string) string

Surround returns s prefixed and suffixed with w.

func SurroundSlice

func SurroundSlice(a []string, w string) []string

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 UUID

func UUID() string

UUID returns a new UUID string.

func Uniq32

func Uniq32() string

Uniq32 returns a UUID-like string that only contains alphanumeric chars. The result has length 32.

func Uniq8

func Uniq8() string

Uniq8 returns a UUID-like string that only contains alphanumeric chars. The result has length 8.

func UniqSuffix

func UniqSuffix(s string) string

UniqSuffix returns s with a unique suffix.

func UniqTableName

func UniqTableName(tbl string) string

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.

Jump to

Keyboard shortcuts

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