Package handy is a toolbelt with utilities and helpers like validators, sanitizers and string formatters. There are routines to filter strings, convert between types, validate passwords with custom rules, easily format dates and much more.
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 aprove password CheckNewPasswordComplexityRequireLetter = 2 // CheckNewPasswordComplexityRequireUpperCase At least one uppercase letter is required in order to aprove password. // Only works if CheckNewPasswordComplexityRequireLetter is included/activated CheckNewPasswordComplexityRequireUpperCase = 4 // CheckNewPasswordComplexityRequireNumber At least one number is required in order to aprove 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 )
const ( // TransformNone No transformations are ordered. Only constraints maximum length // TransformNone turns all other flags OFF. TransformNone = uint8(1) // TransformFlagTrim Trim spaces before and after process the input // TransformFlagTrim Trims the string, removing leading and trailing spaces TransformFlagTrim = uint8(2) // TransformFlagLowerCase Makes the string lowercase // If it's combined with TransformFlagUpperCase, only uppercase remains, once is executed later. TransformFlagLowerCase = uint8(4) // TransformFlagUpperCase Makes the string uppercase // If it's combined with TransformFlagLowerCase, only uppercase remains, once it's executed later. TransformFlagUpperCase = uint8(8) // TransformFlagOnlyDigits Removes all non-numeric characters TransformFlagOnlyDigits = uint8(16) // TransformFlagOnlyLetters Removes all non-letter characters TransformFlagOnlyLetters = uint8(32) // TransformFlagOnlyLettersAndDigits Leaves only letters and numbers TransformFlagOnlyLettersAndDigits = uint8(64) // TransformFlagHash After process all other flags, applies SHA256 hashing on string for output // The routine applies handy.StringHash() on given string TransformFlagHash = uint8(128) )
const ( // CheckPersonNameResultOK means the name was validated CheckPersonNameResultOK = 0 // CheckPersonNameResultPolluted The routine only accepts letters, single quotes and spaces CheckPersonNameResultPolluted = 1 // CheckPersonNameResultTooFewWords The funcion requires at least 2 words CheckPersonNameResultTooFewWords = 2 // CheckPersonNameResultTooShort the sum of all characters must be >= 6 CheckPersonNameResultTooShort = 3 // CheckPersonNameResultTooSimple The name rule requires that at least one word CheckPersonNameResultTooSimple = 4 )
AmountAsWord receives an int64 e returns the value as its text representation Today I have only the PT-BR text. Ex: AmountAsWord(129) => "cento e vinte e nove" Supports up to one trillion and does not add commas.
ArrayDifference returns all items that doesn't exist in both given arrays
ArrayDifferenceAtoB returns the items from A that doesn't exist in B
Between checks if param n in between low and high integer params
Bit returns only uint8(0) or uint8(1). It receives an interface, and when it's a number, and when this number is 0 (zero) it returns 0. Otherwise it returns 1 (one) If the interface is not a number, it returns 0 (zero)
Boolean returns the bool version/interpretation of some value; It receives an interface, and when this is a number, Boolean() returns flase of zero and true for different from zero. If it's a string, try to find "1", "T", "TRUE" to return true. Any other case returns false
CheckCNPJ returns true if the cnpj is valid Thanks to https://gopher.net.br/validacao-de-cpf-e-cnpj-em-go/ CNPJ is the Brazilian TAXPayerID document for companies
CheckCPF returns true if the given sequence is a valid cpf CPF is the Brazilian TAXPayerID document for persons
CheckDate validates a date using the given format
CheckDateYMD returns true if given sequence is a valid date in format yyyymmdd The function removes non-digit characters like "yyyy/mm/dd" or "yyyy-mm-dd", filtering to "yyyymmdd"
CheckEmail returns true if the given sequence is a valid email address See https://tools.ietf.org/html/rfc2822#section-3.4.1 for details about email address anatomy
CheckMinLen verifies if the rune-count is greater then or equal the given minimum It returns true if the given string has length greater than or equal than minLength parameter
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)
CheckNewPasswordResult returns a meaningful message describing the code generated bu CheckNewPassword() The routine considers the given idiom. The fallback is in english
CheckPersonName 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.
CheckPersonNameResult returns a meaningful message describing the code generated bu CheckPersonName The routine considers the given idiom. The fallback is in english
CheckPhone returns true if a given sequence has between 9 and 14 digits
DateTimeAsString formats time.Time variables as strings, considering the format directive
ElapsedMonths returns the number of elapsed months between two given dates
ElapsedYears returns the number of elapsed years between two given dates
HTTPJSONBodyToStruct decode json to a given anatomically compatible struct
HTTPRequestAsFloat64 gets a parameter coming from a http request as float64 number You have to inform the decimal separator symbol. If decimalSeparator is period, engine considers thousandSeparator is comma, and vice-versa.
HTTPRequestAsInteger gets a parameter coming from a http request as an integer It tries to guess if it's a signed/negative integer
HTTPRequestAsString gets a parameter coming from a http request as string, truncated to maxLength Only maxLength >= 1 is considered. Otherwise, it's ignored
HasOnlyLetters returns true if the sequence is entirely composed by letters
HasOnlyNumbers returns true if the sequence is entirely numeric
InArray searches for "item" in "array" and returns true if it's found This func resides here alone only because its long size. TODO Embrace/comprise all native scalar/primitive types
IsNumericType checks if an interface's concrete type corresponds to some of golang native numeric types
MatchesAny returns true if any of the given items matches ( equals ) the subject ( search parameter )
MonthLastDay returns the last day of month, considering the year for cover february in leap years
NowAsString formats time.Now() as string, considering the format directive
OnlyDigits returns only the numbers from the given string, after strip all the rest ( letters, spaces, etc. )
OnlyLetters returns only the letters from the given string, after strip all the rest ( numbers, spaces, etc. )
OnlyLettersAndNumbers returns only the letters and numbers from the given string, after strip all the rest, like spaces and special symbols.
RandomInt returns a random integer within the given (inclusive) range
RandomReseed restarts the randonSeeder and returns a random integer within the given (inclusive) range
Reverse returns the given string written backwards, with letters reversed.
RuneHasSymbol returns true if the given rune contains a symbol
StringAsDateTime formats time.Time variables as strings, considering the format directive
StringAsFloat tries to convert a string to float, and if it can't, just returns zero It's limited to one billion
StringAsInteger returns the integer value extracted from string, or zero
StringHash simply generates a SHA256 hash from the given string In case of error, return ""
StringReplaceAll keeps replacing until there's no more ocurrences to replace.
Tif is a simple implementation of the dear ternary IF operator
Today returns today's date at zero hours, minutes, seconds, etc. It returns a time and a yyyy-mm-dd formated string
Todayf returns today's date at zero hours, minutes, seconds, etc. It returns a time and a custom formated string
Transform handles a string according given flags/parametrization, as follows: The transformations are made in arbitrary order, what can result in unexpected output. It the sequence matters, use TransformSerially instead. If maxLen==0, truncation is skipped The last operations are, by order, truncation and trimming.
TransformSerially reformat given string according parameters, in the order these params were sent Example: TransformSerially("uh lalah 123", 4, TransformFlagOnlyDigits,TransformFlagHash,TransformFlagUpperCase)
First remove non-digits, then hashes string and after make it all uppercase.
If maxLen==0, truncation is skipped Truncation is the last operation
TrimLen returns the runes count after trim the spaces
Truncate limits the length of a given string, trimming or not, according parameters
YMD returns today's date tokenized as year, month and day of month
YMDasDate returns a valid time from the given yyymmdd-formatted sequence
YMDasDateUTC returns a valid UTC time from the given yyymmdd-formatted sequence
YearsAge returns the number of years past since a given date