Documentation ¶
Overview ¶
Package stringo has utilities and helpers like validators, sanitizers and string formatters.
Index ¶
- Constants
- func AsFloat64(s string, decimalSeparator string) float64
- func AsInt(s string) int
- func AsInt64(s string) int64
- func CheckNewPassword(password, passwordConfirmation string, minimumlength uint, ...) uint8
- func DateReformat(d string, currentLayout, newLayout string) string
- func DateTimeAsString(dt time.Time, format string) string
- func DedupSpaces(s string) string
- func HasLetter(s string) bool
- func HasNumber(s string) bool
- func HasOnlyDigits(sequence string) bool
- func HasOnlyLetters(sequence string) bool
- func HasOnlyNumbers(sequence string) bool
- func HasSymbol(s string) bool
- func Initials(sequence string) string
- func NameFirst(name string, transformFlags TransformFlag) string
- func NameFirstAndLast(name string, transformFlags TransformFlag) string
- func OnlyDigits(sequence string) string
- func OnlyLetters(sequence string) string
- func OnlyLettersAndNumbers(sequence string) string
- func RandomInt(min, max int) int
- func RandomNumericString(forbiddenDigits []int, lengthMin, lengthMax int) string
- func RandomString(minLen, maxLen int, allowUnicode, allowNumbers, allowSymbols, allowSpaces bool) string
- func RemoveDigits(sequence string) string
- func ReplaceAll(original string, replacementPairs ...string) string
- func Reshape(format, sequence string) string
- func ReshapePH(placeholder rune, format, sequence string) string
- func Reverse(s string) string
- func RuneHasSymbol(ru rune) bool
- func Sha256Hash(s string) string
- func StrContainsEmail(seq string) bool
- func Title(s string) string
- func Transform(s string, maxLen int, transformFlags TransformFlag) string
- func TransformSerially(s string, maxLen int, transformFlags ...TransformFlag) string
- func TrimLen(text string) int
- func Truncate(s string, maxLen int, trim bool) string
- func ValidateEmail(email string) bool
- type ChkPersonNameResult
- type ChkResult
- type ChkRule
- type TransformFlag
Constants ¶
const ( // ChkAllowEmpty allows empty string "" ChkAllowEmpty ChkRule = 1 // ChkDenySpaces forbids spaces, tabs, new lines and carriage return ChkDenySpaces ChkRule = 2 // ChkDenyNumbers forbids digits/numbers ChkDenyNumbers ChkRule = 4 // ChkDenyLetters forbids letters ChkDenyLetters ChkRule = 8 // ChkDenySymbols forbids symbols. if it's not a number, letter or space, is considered a symbol ChkDenySymbols ChkRule = 16 // ChkDenyMoreThanOneWord forbids more than one word ChkDenyMoreThanOneWord ChkRule = 32 // ChkDenyUpperCase forbids uppercase letters ChkDenyUpperCase ChkRule = 64 // ChkDenyLowercase forbids lowercase letters ChkDenyLowercase ChkRule = 128 // ChkDenyUnicode forbids non-ASCII characters ChkDenyUnicode ChkRule = 256 // ChkRequireNumbers demands at least 1 number within string ChkRequireNumbers ChkRule = 512 // ChkRequireLetters demands at least 1 letter within string ChkRequireLetters ChkRule = 1024 // ChkRequireSymbols demands at least 1 symbol within string. if it's not a number, letter or space, is considered a symbol ChkRequireSymbols ChkRule = 2048 // ChkRequireMoreThanOneWord demands at least 2 words in given string input ChkRequireMoreThanOneWord ChkRule = 4096 // ChkRequireUpperCase demands at least 1 uppercase letter within string ChkRequireUpperCase ChkRule = 8192 // ChkRequireLowercase demands at least 1 lowercase letter within string ChkRequireLowercase ChkRule = 16384 // ChkOk means "alright" ChkOk ChkResult = 0 // ChkEmptyDenied is self explained ChkEmptyDenied ChkResult = -1 // ChkTooShort is self explained ChkTooShort ChkResult = -2 // ChkTooLong is self explained ChkTooLong ChkResult = -4 // ChkSpaceDenied is self explained ChkSpaceDenied ChkResult = -5 // ChkNumbersDenied is self explained ChkNumbersDenied ChkResult = -6 // ChkLettersDenied is self explained ChkLettersDenied ChkResult = -7 // ChkSymbolsDenied is self explained ChkSymbolsDenied ChkResult = -8 // ChkMoreThanOneWordDenied is self explained ChkMoreThanOneWordDenied ChkResult = -9 // ChkUpperCaseDenied is self explained ChkUpperCaseDenied ChkResult = -10 // ChkLowercaseDenied is self explained ChkLowercaseDenied ChkResult = -11 // ChkUnicodeDenied is self explained ChkUnicodeDenied ChkResult = -12 // ChkNumbersNotFound is self explained ChkNumbersNotFound ChkResult = -13 // ChkLettersNotFound is self explained ChkLettersNotFound ChkResult = -14 // ChkSymbolsNotFound is self explained ChkSymbolsNotFound ChkResult = -15 // ChkMoreThanOneWordNotFound is self explained ChkMoreThanOneWordNotFound ChkResult = -16 // ChkUpperCaseNotFound is self explained ChkUpperCaseNotFound ChkResult = -17 // ChkLowercaseNotFound is self explained ChkLowercaseNotFound ChkResult = -18 )
const ( // CheckNewPasswordResultOK Means the checking ran alright CheckNewPasswordResultOK = 0 // CheckNewPasswordResultDivergent Password is different from confirmation CheckNewPasswordResultDivergent = 1 // CheckNewPasswordResultTooShort Password is too short CheckNewPasswordResultTooShort = 2 // CheckNewPasswordResultTooSimple Given string doesn't satisfy complexity rules CheckNewPasswordResultTooSimple = 3 // CheckNewPasswordComplexityLowest There's no rules besides the minimum length // >>> This flag turns all others off <<< CheckNewPasswordComplexityLowest = 1 // CheckNewPasswordComplexityRequireLetter At least one letter is required in order to approve password CheckNewPasswordComplexityRequireLetter = 2 // CheckNewPasswordComplexityRequireUpperCase At least one uppercase letter is required in order to approve password. // Only works if CheckNewPasswordComplexityRequireLetter is included/activated CheckNewPasswordComplexityRequireUpperCase = 4 // CheckNewPasswordComplexityRequireNumber At least one number is required in order to approve password CheckNewPasswordComplexityRequireNumber = 8 // CheckNewPasswordComplexityRequireSpace The password must contain at least one space CheckNewPasswordComplexityRequireSpace = 16 // CheckNewPasswordComplexityRequireSymbol User have to include at least one special character, like # or - CheckNewPasswordComplexityRequireSymbol = 32 )
Variables ¶
This section is empty.
Functions ¶
func CheckNewPassword ¶
func CheckNewPassword(password, passwordConfirmation string, minimumlength uint, flagComplexity uint8) uint8
CheckNewPassword Run some basic checks on new password strings, based on given options This routine requires at least 4 (four) characters Example requiring only basic minimum length: CheckNewPassword("lalala", "lalala", 10, CheckNewPasswordComplexityLowest) Example requiring number and symbol: CheckNewPassword("lalala", "lalala", 10, CheckNewPasswordComplexityRequireNumber|CheckNewPasswordComplexityRequireSymbol)
func DateReformat ¶
DateReformat gets a date string in a given currentFormat, and transform it according newFormat
func DateTimeAsString ¶
DateTimeAsString formats time.Time variables as strings, considering the format directive
func DedupSpaces ¶
DedupSpaces removes duplicated spaces, tabs and newLine characters I.E: Replaces two tabs for one single tab
func HasOnlyDigits ¶
HasOnlyDigits returns true if the input is entirely numeric HasOnlyDigits is an alias for HasOnlyNumbers()
func HasOnlyLetters ¶
HasOnlyLetters returns true if the input is entirely composed by letters
func HasOnlyNumbers ¶
HasOnlyNumbers returns true if the input is entirely numeric
func HasSymbol ¶
HasSymbol returns true if input string contains at least one symbol If rune is not a space, letter nor a number, it's considered a symbol
func NameFirst ¶
func NameFirst(name string, transformFlags TransformFlag) string
NameFirst returns the first word/name from the given input, optionally transformed by transformFlags Example: handy.NameFirst("friedrich wilhelm nietzsche", handy.TransformTitleCase) // returns "Friedrich"
func NameFirstAndLast ¶
func NameFirstAndLast(name string, transformFlags TransformFlag) string
NameFirstAndLast returns the first and last words/names from the given input, optionally transformed by transformFlags Example: handy.NameFirstAndLast("friedrich wilhelm nietzsche", handy.TransformTitleCase) // returns "Friedrich Nietzsche"
func OnlyDigits ¶
OnlyDigits returns only the numbers from the given string, after strip all the rest ( letters, spaces, etc. )
func OnlyLetters ¶
OnlyLetters returns only the letters from the given string, after strip all the rest ( numbers, spaces, etc. )
func OnlyLettersAndNumbers ¶
OnlyLettersAndNumbers returns only the letters and numbers from the given string, after strip all the rest, like spaces and special symbols.
func RandomNumericString ¶
RandomNumericString returns a string with length between given lengthMin and lengthMax Any digit within forbiddenDigits param will be ignored Example: https://play.golang.org/p/phF-y9ZsUIP
func RandomString ¶
func RandomString(minLen, maxLen int, allowUnicode, allowNumbers, allowSymbols, allowSpaces bool) string
RandomString generates a string sequence based on given params/rules
func RemoveDigits ¶
RemoveDigits returns the given string without digit/numeric runes
func ReplaceAll ¶
ReplaceAll keeps replacing until there's no more occurrences to replace.
func Reshape ¶
Reshape formats a string according "hash" positional directives Example: Reshape("#####-###","98765432") returns "98765-432" Example: Reshape("####.####-####/##","98765432101234") returns "9876.5432-1012/34" It tries to adapt the format to the sequence when the sizes don't match Example: Reshape("###.###.##","9876") returns "987.6" Example: Reshape("###.###","123456789") returns "123.456789" Run: https://play.golang.org/p/hlCyVZehTn3
func ReshapePH ¶
ReshapePH formats a string according positional directives given with an arbitrary placeholder It's the core of Reshape
func RuneHasSymbol ¶
RuneHasSymbol returns true if the given rune contains a symbol
func Sha256Hash ¶
Sha256Hash simply generates a SHA256 hash from the given string In case of error, return ""
func StrContainsEmail ¶
StrContainsEmail returns true if given string contains an email address
func Transform ¶
func Transform(s string, maxLen int, transformFlags TransformFlag) string
Transform handles a string according given flags/parametrization, as follows: The transformations are made in arbitrary order, what can result in unexpected output. If the order matters, use TransformSerially instead. If maxLen==0, truncation is skipped The last operations are, by order, truncation and trimming.
func TransformSerially ¶
func TransformSerially(s string, maxLen int, transformFlags ...TransformFlag) string
TransformSerially reformat given string according parameters, in the order these params were sent Example: TransformSerially("uh lalah 123", 4, TransformOnlyDigits,TransformHash,TransformUpperCase)
First remove non-digits, then hashes string and after make it all uppercase.
If maxLen==0, truncation is skipped Truncation is the last operation
func ValidateEmail ¶
ValidateEmail returns true if the given input is a valid email address Observe that ValidateEmail doesn't trim nor sanitize string before check See https://tools.ietf.org/html/rfc2822#section-3.4.1 for details about email address anatomy
Types ¶
type ChkPersonNameResult ¶
type ChkPersonNameResult uint8
const ( // ChkPersonNameOK means the name was validated ChkPersonNameOK ChkPersonNameResult = 0 // ChkPersonNamePolluted The routine only accepts letters, single quotes and spaces ChkPersonNamePolluted ChkPersonNameResult = 1 // ChkPersonNameTooFewWords The function requires at least 2 words ChkPersonNameTooFewWords ChkPersonNameResult = 2 // ChkPersonNameTooShort the sum of all characters must be >= 6 ChkPersonNameTooShort ChkPersonNameResult = 3 // ChkPersonNameTooSimple The name rule requires that at least one word ChkPersonNameTooSimple ChkPersonNameResult = 4 )
func ChkPersonName ¶
func ChkPersonName(name string, acceptEmpty bool) ChkPersonNameResult
ChkPersonName returns true if the name contains at least two words, one >= 3 chars and one >=2 chars. I understand that this is a particular criteria, but this is the OpenSourceMagic, where you can change and adapt to your own specs.
type TransformFlag ¶
type TransformFlag uint
const ( // TransformNone No transformations are ordered. Only constraints maximum length // TransformNone turns all other flags OFF. TransformNone TransformFlag = 1 // TransformTrim Trim spaces before and after process the input // TransformTrim Trims the string, removing leading and trailing spaces TransformTrim TransformFlag = 2 // TransformLowerCase Makes the string lowercase // If case transformation flags are combined, the last one remains, considering the following order: TransformTitleCase, TransformLowerCase and TransformUpperCase. TransformLowerCase TransformFlag = 4 // TransformUpperCase Makes the string uppercase // If case transformation flags are combined, the last one remains, considering the following order: TransformTitleCase, TransformLowerCase and TransformUpperCase. TransformUpperCase TransformFlag = 8 // TransformOnlyDigits Removes all non-numeric characters TransformOnlyDigits TransformFlag = 16 // TransformOnlyLetters Removes all non-letter characters TransformOnlyLetters TransformFlag = 32 // TransformOnlyLettersAndDigits Leaves only letters and numbers TransformOnlyLettersAndDigits TransformFlag = 64 // TransformHash After process all other flags, applies SHA256 hashing on string for output // The routine applies handy.Sha256Hash() on given string TransformHash TransformFlag = 128 // TransformTitleCase Makes the string uppercase // If case transformation flags are combined, the last one remains, considering the following order: TransformTitleCase, TransformLowerCase and TransformUpperCase. TransformTitleCase TransformFlag = 256 // TransformRemoveDigits Removes all digit characters, without to touch on any other // If combined with TransformOnlyLettersAndDigits, TransformOnlyDigits or TransformOnlyLetters, it's ineffective TransformRemoveDigits TransformFlag = 512 )