stringo

package module
v0.0.0-...-47ddc5c Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2022 License: MIT Imports: 12 Imported by: 0

README

GitHub GitHub Go Report Card Go Version

StrinGO

GO Golang string utilities and helpers
Requires GO v>=1.14
No external dependencies


Documentation 📗
GoDocs


Quality 👈
GoCover

Documentation

Overview

Package stringo has utilities and helpers like validators, sanitizers and string formatters.

Index

Constants

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

func AsFloat64(s string, decimalSeparator string) float64

AsFloat64 tries to convert a string to float64, and if it can't, just returns zero

func AsInt

func AsInt(s string) int

AsInt tries to convert a string to int, and if it can't, just returns zero

func AsInt64

func AsInt64(s string) int64

AsInt64 tries to convert a string to int64, and if it can't, just returns zero

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

func DateReformat(d string, currentLayout, newLayout string) string

DateReformat gets a date string in a given currentFormat, and transform it according newFormat

func DateTimeAsString

func DateTimeAsString(dt time.Time, format string) string

DateTimeAsString formats time.Time variables as strings, considering the format directive

func DedupSpaces

func DedupSpaces(s string) string

DedupSpaces removes duplicated spaces, tabs and newLine characters I.E: Replaces two tabs for one single tab

func HasLetter

func HasLetter(s string) bool

HasLetter returns true if input string contains at least one letter

func HasNumber

func HasNumber(s string) bool

HasNumber returns true if input string contains at least one digit/number

func HasOnlyDigits

func HasOnlyDigits(sequence string) bool

HasOnlyDigits returns true if the input is entirely numeric HasOnlyDigits is an alias for HasOnlyNumbers()

func HasOnlyLetters

func HasOnlyLetters(sequence string) bool

HasOnlyLetters returns true if the input is entirely composed by letters

func HasOnlyNumbers

func HasOnlyNumbers(sequence string) bool

HasOnlyNumbers returns true if the input is entirely numeric

func HasSymbol

func HasSymbol(s string) bool

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 Initials

func Initials(sequence string) string

Initials returns the first and last words/names from the given input.

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

func OnlyDigits(sequence string) string

OnlyDigits returns only the numbers from the given string, after strip all the rest ( letters, spaces, etc. )

func OnlyLetters

func OnlyLetters(sequence string) string

OnlyLetters returns only the letters from the given string, after strip all the rest ( numbers, spaces, etc. )

func OnlyLettersAndNumbers

func OnlyLettersAndNumbers(sequence string) string

OnlyLettersAndNumbers returns only the letters and numbers from the given string, after strip all the rest, like spaces and special symbols.

func RandomInt

func RandomInt(min, max int) int

RandomInt returns a random integer within the given (inclusive) range

func RandomNumericString

func RandomNumericString(forbiddenDigits []int, lengthMin, lengthMax int) string

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

func RemoveDigits(sequence string) string

RemoveDigits returns the given string without digit/numeric runes

func ReplaceAll

func ReplaceAll(original string, replacementPairs ...string) string

ReplaceAll keeps replacing until there's no more occurrences to replace.

func Reshape

func Reshape(format, sequence string) string

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

func ReshapePH(placeholder rune, format, sequence string) string

ReshapePH formats a string according positional directives given with an arbitrary placeholder It's the core of Reshape

func Reverse

func Reverse(s string) string

Reverse returns the given string written backwards, with letters reversed.

func RuneHasSymbol

func RuneHasSymbol(ru rune) bool

RuneHasSymbol returns true if the given rune contains a symbol

func Sha256Hash

func Sha256Hash(s string) string

Sha256Hash simply generates a SHA256 hash from the given string In case of error, return ""

func StrContainsEmail

func StrContainsEmail(seq string) bool

StrContainsEmail returns true if given string contains an email address

func Title

func Title(s string) string

Title tries to return the given word capitalized

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 TrimLen

func TrimLen(text string) int

TrimLen returns the runes count after trim the spaces

func Truncate

func Truncate(s string, maxLen int, trim bool) string

Truncate limits the length of a given string, trimming or not, according parameters

func ValidateEmail

func ValidateEmail(email string) bool

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 ChkResult

type ChkResult int

func CheckStr

func CheckStr(seq string, minLen, maxLen uint, rules ChkRule) ChkResult

CheckStr validates a string according given complexity rules CheckStr first evaluates "Deny" rules, and then "Require" rules. minLen=0 means there's no minimum length maxLen=0 means there's no maximum length

type ChkRule

type ChkRule uint

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
)

Jump to

Keyboard shortcuts

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