项目作者: miguelpragier

项目描述 :
GO Golang Utilities and helpers like validators and string formatters
高级语言: Go
项目地址: git://github.com/miguelpragier/handy.git
创建时间: 2018-06-13T13:10:07Z
项目社区:https://github.com/miguelpragier/handy

开源协议:MIT License

下载


GitHub GitHub
Go Report Card
Coverage Status Build Status Mentioned in Awesome Go
CircleCI
Go Version

Handy Go utilities

GO Golang Utilities and helpers like validators, sanitizers and string formatters

Requires GO v>=1.14

Dependencies

None, since v1.1.1


Documentation :green_book:
GoDocs


Quality :point_left:
GoCover

Functions

  1. // CheckEmail returns true if the given sequence is a valid email address
  2. // See https://tools.ietf.org/html/rfc2822#section-3.4.1 for details about email address anatomy
  3. func CheckEmail(email string) bool {}
  4. // CheckNewPassword Run some basic checks on new password strings, based on given options
  5. // This routine requires at least 4 (four) characters
  6. // Example requiring only basic minimum lenght: CheckNewPassword("lalala", "lalala", 10, CheckNewPasswordComplexityLowest)
  7. // Example requiring number and symbol: CheckNewPassword("lalala", "lalala", 10, CheckNewPasswordComplexityRequireNumber|CheckNewPasswordComplexityRequireSymbol)
  8. func CheckNewPassword(password, passwordConfirmation string, minimumLenght uint, flagComplexity uint8) uint8 {
  9. // StringHash simply generates a SHA256 hash from the given string
  10. func StringHash(s string) string {}
  11. // OnlyLetters returns only the letters from the given string, after strip all the rest ( numbers, spaces, etc. )
  12. func OnlyLetters(sequence string) string {}
  13. // OnlyDigits returns only the numbers from the given string, after strip all the rest ( letters, spaces, etc. )
  14. func OnlyDigits(sequence string) string {}
  15. // OnlyLettersAndNumbers returns only the letters and numbers from the given string, after strip all the rest, like spaces and special symbols.
  16. func OnlyLettersAndNumbers(sequence string) string {}
  17. // RandomInt returns a rondom integer within the given (inclusive) range
  18. func RandomInt(min, max int) int {}
  19. // StringAsFloat tries to convert a string to float, and if it can't, just returns zero
  20. // It's limited to one billion
  21. func StringAsFloat(s string, decimalSeparator, thousandsSeparator rune) float64 {}
  22. // StringAsInteger returns the integer value extracted from string, or zero
  23. func StringAsInteger(s string) int {}
  24. // Between checks if param n in between low and high integer params
  25. func Between(n, low, high int) bool {}
  26. // Tif is a simple implementation of the dear ternary IF operator
  27. func Tif(condition bool, tifThen, tifElse interface{}) interface{} {}
  28. // Truncate limits the length of a given string, trimming or not, according parameters
  29. func Truncate(s string, maxLen int, trim bool) string {}
  30. // Transform handles a string according given flags/parametrization, as follows:
  31. // Available Flags to be used alone or combined:
  32. // TransformNone - Does nothing. It's only for truncation.
  33. // TransformFlagTrim - Trim spaces before and after proccess the input
  34. // TransformFlagLowerCase - Change string case to lower
  35. // TransformFlagUpperCase - Change string case to upper
  36. // TransformFlagOnlyDigits - Filter/strip all but digits
  37. // TransformFlagOnlyLetters - Filter/strip all but letters
  38. // TransformFlagOnlyLettersAndDigits - Filter/strip all but numbers and letters. Removes spaces, punctuation and special symbols
  39. // TransformFlagHash - Apply handy.StringHash() routine to string
  40. func Transform(s string, maxLen int, transformFlags uint8) string {}
  41. // MatchesAny returns true if any of the given items matches ( equals ) the subject ( search parameter )
  42. func MatchesAny(search interface{}, items ...interface{}) bool {}
  43. // HasOnlyNumbers returns true if the sequence is entirely numeric
  44. func HasOnlyNumbers(sequence string) bool {}
  45. // HasOnlyNumbers returns true if the sequence is entirely composed by letters
  46. func HasOnlyLetters(sequence string) bool {}
  47. // TrimLen returns the runes count after trim the spaces
  48. func TrimLen(text string) int {}
  49. // CheckMinLen verifies if the rune-count is greater then or equal the given minimum
  50. // It returns true if the given string has length greater than or equal than minLength parameter
  51. func CheckMinLen(value string, minLength int) bool {}
  52. // IsNumericType checks if an interface's concrete type corresponds to some of golang native numeric types
  53. func IsNumericType(x interface{}) bool {}
  54. // Bit returns only uint8(0) or uint8(1).
  55. // 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)
  56. // If the interface is not a number, it returns 0 (zero)
  57. func Bit(x interface{}) uint8 {}
  58. // Boolean returns the bool version/interpretation of some value;
  59. // It receives an interface, and when this is a number, Boolean() returns flase of zero and true for different from zero.
  60. // If it's a string, try to find "1", "T", "TRUE" to return true.
  61. // Any other case returns false
  62. func Boolean(x interface{}) bool {}
  63. // Reverse returns the given string written backwards, with letters reversed.
  64. func Reverse(s string) string {}
  65. // CheckPersonName returns true if the name contains at least two words, one >= 3 chars and one >=2 chars.
  66. // I understand that this is a particular criteria, but this is the OpenSourceMagic, where you can change and adapt to your own specs.
  67. func CheckPersonName(name string, acceptEmpty bool) uint8 {}
  68. // InArray searches for "item" in "array" and returns true if it's found
  69. // This func resides here alone only because its long size.
  70. // TODO Embrace/comprise all native scalar/primitive types
  71. func InArray(array interface{}, item interface{}) bool {}
  72. // ArrayDifferenceAtoB returns the items from A that doesn't exist in B
  73. func ArrayDifferenceAtoB(a, b []int) []int {}
  74. // ArrayDifference returns all items that doesn't exist in both given arrays
  75. func ArrayDifference(a, b []int) []int {}
  76. /* DateTime Routines */
  77. // DateTimeAsString formats time.Time variables as strings, considering the format directive
  78. func DateTimeAsString(dt time.Time, format string) string {}
  79. // StringAsDateTime converts a date-time string using given format string and return it as time.Time
  80. func StringAsDateTime(s string, format string) time.Time {}
  81. // CheckDate validates a date using the given format
  82. func CheckDate(format, dateTime string) bool {
  83. // CheckDate returns true if given sequence is a valid date in format yyyymmdd
  84. // The function removes non-digit characteres like "yyyy/mm/dd" or "yyyy-mm-dd", filtering to "yyyymmdd"
  85. func CheckDateYMD(yyyymmdd string) bool {}
  86. // YMDasDateUTC returns a valid UTC time from the given yyymmdd-formatted sequence
  87. func YMDasDateUTC(yyyymmdd string, utc bool) (time.Time, error) {}
  88. // YMDasDate returns a valid time from the given yyymmdd-formatted sequence
  89. func YMDasDate(yyyymmdd string) (time.Time, error) {}
  90. // ElapsedMonths returns the number of elapsed months between two given dates
  91. func ElapsedMonths(from, to time.Time) int {}
  92. // ElapsedYears returns the number of elapsed years between two given dates
  93. func ElapsedYears(from, to time.Time) int {}
  94. // YearsAge returns the number of years past since a given date
  95. func YearsAge(birthdate time.Time) int {}
  96. /* Brazilian specific routines */
  97. // CheckCPF returns true if the given sequence is a valid cpf
  98. // CPF is the Brazilian TAXPayerID document for persons
  99. func CheckCPF(cpf string) bool {}
  100. // CheckCNPJ returns true if the cnpj is valid
  101. // Thanks to https://gopher.net.br/validacao-de-cpf-e-cnpj-em-go/
  102. // CNPJ is the Brazilian TAXPayerID document for companies
  103. func CheckCNPJ(cnpj string) bool {}
  104. // AmountAsWord receives an int64 e returns the value as its text representation
  105. // Today I have only the PT-BR text.
  106. // Ex: AmountAsWord(129) => "cento e vinte e nove"
  107. // Supports up to one trillion and does not add commas.
  108. func AmountAsWord(n int64) string {}
  109. /* Web/HTTP Specific Routines */
  110. // HTTPRequestAsString gets a parameter coming from a http request as string, truncated to maxLenght
  111. // Only maxLenght >= 1 is considered. Otherwise, it's ignored
  112. func HTTPRequestAsString(r *http.Request, key string, maxLenght int, transformOptions ...uint8) string {}
  113. // HTTPRequestAsInteger gets a parameter coming from a http request as an integer
  114. // It tries to guess if it's a signed/negative integer
  115. func HTTPRequestAsInteger(r *http.Request, key string) int {}
  116. // HTTPRequestAsFloat64 gets a parameter coming from a http request as float64 number
  117. // You have to inform the decimal separator symbol.
  118. // If decimalSeparator is period, engine considers thousandSeparator is comma, and vice-versa.
  119. func HTTPRequestAsFloat64(r *http.Request, key string, decimalSeparator rune) float64 {}
  120. /* Other Routines */
  121. // CheckPersonNameResult returns a meaningful message describing the code generated bu CheckPersonName
  122. // The routine considers the given idiom. The fallback is in english
  123. func CheckPersonNameResult(idiom string, r uint8) string {}
  124. // CheckNewPasswordResult returns a meaningful message describing the code generated bu CheckNewPassword()
  125. // The routine considers the given idiom. The fallback is in english
  126. func CheckNewPasswordResult(idiom string, r uint8) string {}