godash

package module
v0.0.0-...-af6b3da Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2016 License: MIT Imports: 12 Imported by: 18

README

!!!THIS PROJECT IS NOT MAINTAINED ANYMORE!!!

You can use https://github.com/alioygur/is package instead of this.

godash

Build Status GoDoc Go Report Card

A utility library in Golang inspired by lodash

installation

go get gopkg.in/alioygur/godash.v0

Contribute

we are waiting your contribution

  • Report problems
  • Add/Suggest new features/recipes
  • Improve/fix documentation

Many thanks to our contributors: contributors

Is* (collection of checking)

An Example;

func IsEmail(str string) bool
func ExampleIsEmail() {
	fmt.Println(IsEmail("jhon@example.com"))
	fmt.Println(IsEmail("invalid.com"))
	fmt.Println(IsEmail(`very.(),:;<>[]".VERY."very@\ "very".unusual@strange.example.com`))
	// Output:
	// true
	// false
	// true
}

Full list of Is* functions;

func IsASCII(s string) bool
func IsAlpha(s string) bool
func IsAlphanumeric(s string) bool
func IsBase64(s string) bool
func IsByteLength(str string, min, max int) bool
func IsCreditCard(str string) bool
func IsDNSName(str string) bool
func IsDataURI(str string) bool
func IsDialString(str string) bool
func IsDivisibleBy(str, num string) bool
func IsEmail(s string) bool
func IsFilePath(str string) (bool, int)
func IsFloat(str string) bool
func IsFullWidth(str string) bool
func IsHalfWidth(str string) bool
func IsHexadecimal(str string) bool
func IsHexcolor(str string) bool
func IsIP(str string) bool
func IsIPv4(str string) bool
func IsIPv6(str string) bool
func IsISBN(str string, version int) bool
func IsISBN10(str string) bool
func IsISBN13(str string) bool
func IsISO3166Alpha2(str string) bool
func IsISO3166Alpha3(str string) bool
func IsInRange(value, left, right float64) bool
func IsInt(str string) bool
func IsJSON(str string) bool
func IsLatitude(str string) bool
func IsLongitude(str string) bool
func IsLowerCase(str string) bool
func IsMAC(str string) bool
func IsMatches(str, pattern string) bool
func IsMongoID(str string) bool
func IsMultibyte(s string) bool
func IsNatural(value float64) bool
func IsNegative(value float64) bool
func IsNonNegative(value float64) bool
func IsNonPositive(value float64) bool
func IsNull(str string) bool
func IsNumeric(s string) bool
func IsPort(str string) bool
func IsPositive(value float64) bool
func IsPrintableASCII(s string) bool
func IsRGBcolor(str string) bool
func IsRequestURI(rawurl string) bool
func IsRequestURL(rawurl string) bool
func IsSSN(str string) bool
func IsSemver(str string) bool
func IsStringLength(str string, params ...string) bool
func IsStringMatches(s string, params ...string) bool
func IsURL(str string) bool
func IsUTFDigit(s string) bool
func IsUTFLetter(str string) bool
func IsUTFLetterNumeric(s string) bool
func IsUTFNumeric(s string) bool
func IsUUID(str string) bool
func IsUUIDv3(str string) bool
func IsUUIDv4(str string) bool
func IsUUIDv5(str string) bool
func IsUpperCase(str string) bool
func IsVariableWidth(str string) bool
func IsWhole(value float64) bool

To* (collection of converting)

Examples;

func ExampleToBoolean() {
	fmt.Println(ToBoolean("True"))
	fmt.Println(ToBoolean("true"))
	fmt.Println(ToBoolean("1"))
	fmt.Println(ToBoolean("False"))
	fmt.Println(ToBoolean("false"))
	fmt.Println(ToBoolean("0"))
	// Output:
	// true <nil>
	// true <nil>
	// true <nil>
	// false <nil>
	// false <nil>
	// false <nil>
}

func ExampleToCamelCase() {
	fmt.Println(ToCamelCase("camel case"))
	fmt.Println(ToCamelCase("  camel case  "))
	fmt.Println(ToCamelCase("!!!camel case===="))
	fmt.Println(ToCamelCase("camel-case"))
	fmt.Println(ToCamelCase("camel_case"))
	// Output:
	// CamelCase
	// CamelCase
	// CamelCase
	// CamelCase
	// CamelCase
}

func ExampleToSnakeCase() {
	fmt.Println(ToSnakeCase("SnakeCase"))
	fmt.Println(ToSnakeCase("snake case"))
	fmt.Println(ToSnakeCase("  snake case  "))
	fmt.Println(ToSnakeCase("!!!snake case===="))
	fmt.Println(ToSnakeCase("snake-case"))
	fmt.Println(ToSnakeCase("snake_case"))
	// Output:
	// snake_case
	// snake_case
	// snake_case
	// snake_case
	// snake_case
	// snake_case
}

Full list of To* functions;

func ToBoolean(str string) (bool, error)
func ToCamelCase(s string) string
func ToFloat(str string) (float64, error)
func ToInt(str string) (int64, error)
func ToJSON(obj interface{}) (string, error)
func ToSnakeCase(str string) string
func ToString(obj interface{}) string

for more documentation godoc

Thanks & Authors

I use code/got inspiration from these excellent libraries:

  • asaskevich/govalidator [Go] Package of validators and sanitizers for strings, numerics, slices and structs
  • lodash/lodash A modern JavaScript utility library delivering modularity, performance, & extras.

Documentation

Overview

Package godash is utility library in Golang inspired by lodash

Index

Examples

Constants

View Source
const (
	// Email        string = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
	CreditCard string = "" /* 151-byte string literal not displayed */
	ISBN10     string = "^(?:[0-9]{9}X|[0-9]{10})$"
	ISBN13     string = "^(?:[0-9]{13})$"
	UUID3      string = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
	UUID4      string = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
	UUID5      string = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
	UUID       string = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
	// Alpha        string = "^[a-zA-Z]+$"
	// Alphanumeric string = "^[a-zA-Z0-9]+$"
	// Numeric      string = "^[-+]?[0-9]+$"
	Int         string = "^(?:[-+]?(?:0|[1-9][0-9]*))$"
	Float       string = "^(?:[-+]?(?:[0-9]+))?(?:\\.[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$"
	Hexadecimal string = "^[0-9a-fA-F]+$"
	Hexcolor    string = "^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
	RGBcolor    string = "" /* 157-byte string literal not displayed */
	// ASCII          string = "^[\x00-\x7F]+$"
	Multibyte string = "[^\x00-\x7F]"
	FullWidth string = "[^\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]"
	HalfWidth string = "[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]"
	// Base64         string = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
	// PrintableASCII string = "^[\x20-\x7E]+$"
	DataURI   string = "^data:.+\\/(.+);base64$"
	Latitude  string = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
	Longitude string = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
	DNSName   string = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62}){1}(.[a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62})*$`
	URL       string = `` /* 322-byte string literal not displayed */
	SSN       string = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
	WinPath   string = `^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`
	UnixPath  string = `^((?:\/[a-zA-Z0-9\.\:]+(?:_[a-zA-Z0-9\:\.]+)*(?:\-[\:a-zA-Z0-9\.]+)*)+\/?)$`
	Semver    string = "" /* 183-byte string literal not displayed */
	Cameling  string = `[\p{L}\p{N}]+`
)

Basic regular expressions for validating strings

View Source
const (
	// Unknown is unresolved OS type
	Unknown = iota
	// Win is Windows type
	Win
	// Unix is *nix OS types
	Unix
)

Used by IsFilePath func

Variables

View Source
var ISO3166List = []ISO3166Entry{}/* 249 elements not displayed */

ISO3166List based on https://www.iso.org/obp/ui/#search/code/ Code Type "Officially Assigned Codes"

Functions

func IsASCII

func IsASCII(s string) bool

IsASCII check if the string contains ASCII chars only. Empty string is valid.

func IsAlpha

func IsAlpha(s string) bool

IsAlpha check if the string contains only letters (a-zA-Z). Empty string is valid.

func IsAlphanumeric

func IsAlphanumeric(s string) bool

IsAlphanumeric check if the string contains only letters and numbers. Empty string is valid.

func IsBase64

func IsBase64(s string) bool

IsBase64 check if a string is base64 encoded.

func IsByteLength

func IsByteLength(str string, min, max int) bool

IsByteLength check if the string's length (in bytes) falls in a range.

func IsCreditCard

func IsCreditCard(str string) bool

IsCreditCard check if the string is a credit card.

func IsDNSName

func IsDNSName(str string) bool

IsDNSName will validate the given string as a DNS name

func IsDataURI

func IsDataURI(str string) bool

IsDataURI checks if a string is base64 encoded data URI such as an image

func IsDialString

func IsDialString(str string) bool

IsDialString validates the given string for usage with the various Dial() functions

func IsDivisibleBy

func IsDivisibleBy(str, num string) bool

IsDivisibleBy check if the string is a number that's divisible by another. If second argument is not valid integer or zero, it's return false. Otherwise, if first argument is not valid integer or zero, it's return true (Invalid string converts to zero).

Example

This small example illustrate how to work with IsDivisibleBy function.

println("1024 is divisible by 64: ", IsDivisibleBy("1024", "64"))
Output:

func IsEmail

func IsEmail(s string) bool

IsEmail is a constraint to do a simple validation for email addresses, it only check if the string contains "@" and that it is not in the first or last character of the string https://en.wikipedia.org/wiki/Email_address#Valid_email_addresses

Example
fmt.Println(IsEmail("jhon@example.com"))
fmt.Println(IsEmail("invalid.com"))
fmt.Println(IsEmail(`very.(),:;<>[]".VERY."very@\ "very".unusual@strange.example.com`))
Output:

true
false
true

func IsFilePath

func IsFilePath(str string) (bool, int)

IsFilePath check is a string is Win or Unix file path and returns it's type.

func IsFloat

func IsFloat(str string) bool

IsFloat check if the string is a float.

func IsFullWidth

func IsFullWidth(str string) bool

IsFullWidth check if the string contains any full-width chars. Empty string is valid.

func IsHalfWidth

func IsHalfWidth(str string) bool

IsHalfWidth check if the string contains any half-width chars. Empty string is valid.

func IsHexadecimal

func IsHexadecimal(str string) bool

IsHexadecimal check if the string is a hexadecimal number.

func IsHexcolor

func IsHexcolor(str string) bool

IsHexcolor check if the string is a hexadecimal color.

func IsIP

func IsIP(str string) bool

IsIP checks if a string is either IP version 4 or 6.

func IsIPv4

func IsIPv4(str string) bool

IsIPv4 check if the string is an IP version 4.

func IsIPv6

func IsIPv6(str string) bool

IsIPv6 check if the string is an IP version 6.

func IsISBN

func IsISBN(str string, version int) bool

IsISBN check if the string is an ISBN (version 10 or 13). If version value is not equal to 10 or 13, it will be check both variants.

func IsISBN10

func IsISBN10(str string) bool

IsISBN10 check if the string is an ISBN version 10.

func IsISBN13

func IsISBN13(str string) bool

IsISBN13 check if the string is an ISBN version 13.

func IsISO3166Alpha2

func IsISO3166Alpha2(str string) bool

IsISO3166Alpha2 checks if a string is valid two-letter country code

func IsISO3166Alpha3

func IsISO3166Alpha3(str string) bool

IsISO3166Alpha3 checks if a string is valid three-letter country code

func IsInRange

func IsInRange(value, left, right float64) bool

IsInRange returns true if value lies between left and right border

func IsInt

func IsInt(str string) bool

IsInt check if the string is an integer. Empty string is valid.

func IsJSON

func IsJSON(str string) bool

IsJSON check if the string is valid JSON (note: uses json.Unmarshal).

func IsLatitude

func IsLatitude(str string) bool

IsLatitude check if a string is valid latitude.

func IsLongitude

func IsLongitude(str string) bool

IsLongitude check if a string is valid longitude.

func IsLowerCase

func IsLowerCase(str string) bool

IsLowerCase check if the string is lowercase. Empty string is valid.

func IsMAC

func IsMAC(str string) bool

IsMAC check if a string is valid MAC address. Possible MAC formats: 01:23:45:67:89:ab 01:23:45:67:89:ab:cd:ef 01-23-45-67-89-ab 01-23-45-67-89-ab-cd-ef 0123.4567.89ab 0123.4567.89ab.cdef

func IsMatches

func IsMatches(str, pattern string) bool

IsMatches check if string matches the pattern (pattern is regular expression) In case of error return false

func IsMongoID

func IsMongoID(str string) bool

IsMongoID check if the string is a valid hex-encoded representation of a MongoDB ObjectId.

func IsMultibyte

func IsMultibyte(s string) bool

IsMultibyte check if the string contains one or more multibyte chars. Empty string is valid.

func IsNatural

func IsNatural(value float64) bool

IsNatural returns true if value is natural number (positive and whole)

func IsNegative

func IsNegative(value float64) bool

IsNegative returns true if value < 0

func IsNonNegative

func IsNonNegative(value float64) bool

IsNonNegative returns true if value >= 0

func IsNonPositive

func IsNonPositive(value float64) bool

IsNonPositive returns true if value <= 0

func IsNull

func IsNull(str string) bool

IsNull check if the string is null.

func IsNumeric

func IsNumeric(s string) bool

IsNumeric check if the string contains only numbers. Empty string is valid.

func IsPort

func IsPort(str string) bool

IsPort checks if a string represents a valid port

func IsPositive

func IsPositive(value float64) bool

IsPositive returns true if value > 0

func IsPrintableASCII

func IsPrintableASCII(s string) bool

IsPrintableASCII check if the string contains printable ASCII chars only. Empty string is valid.

func IsRGBcolor

func IsRGBcolor(str string) bool

IsRGBcolor check if the string is a valid RGB color in form rgb(RRR, GGG, BBB).

func IsRequestURI

func IsRequestURI(rawurl string) bool

IsRequestURI check if the string rawurl, assuming it was recieved in an HTTP request, is an absolute URI or an absolute path.

func IsRequestURL

func IsRequestURL(rawurl string) bool

IsRequestURL check if the string rawurl, assuming it was recieved in an HTTP request, is a valid URL confirm to RFC 3986

func IsSSN

func IsSSN(str string) bool

IsSSN will validate the given string as a U.S. Social Security Number

func IsSemver

func IsSemver(str string) bool

IsSemver check if string is valid semantic version

func IsStringLength

func IsStringLength(str string, params ...string) bool

IsStringLength check string's length (including multi byte strings)

func IsStringMatches

func IsStringMatches(s string, params ...string) bool

IsStringMatches checks if a string matches a given pattern.

func IsURL

func IsURL(str string) bool

IsURL check if the string is an URL.

func IsUTFDigit

func IsUTFDigit(s string) bool

IsUTFDigit check if the string contains only unicode radix-10 decimal digits. Empty string is valid.

func IsUTFLetter

func IsUTFLetter(str string) bool

IsUTFLetter check if the string contains only unicode letter characters. Similar to IsAlpha but for all languages. Empty string is valid.

func IsUTFLetterNumeric

func IsUTFLetterNumeric(s string) bool

IsUTFLetterNumeric check if the string contains only unicode letters and numbers. Empty string is valid.

func IsUTFNumeric

func IsUTFNumeric(s string) bool

IsUTFNumeric check if the string contains only unicode numbers of any kind. Numbers can be 0-9 but also Fractions ¾,Roman Ⅸ and Hangzhou 〩. Empty string is valid.

func IsUUID

func IsUUID(str string) bool

IsUUID check if the string is a UUID (version 3, 4 or 5).

func IsUUIDv3

func IsUUIDv3(str string) bool

IsUUIDv3 check if the string is a UUID version 3.

func IsUUIDv4

func IsUUIDv4(str string) bool

IsUUIDv4 check if the string is a UUID version 4.

func IsUUIDv5

func IsUUIDv5(str string) bool

IsUUIDv5 check if the string is a UUID version 5.

func IsUpperCase

func IsUpperCase(str string) bool

IsUpperCase check if the string is uppercase. Empty string is valid.

func IsVariableWidth

func IsVariableWidth(str string) bool

IsVariableWidth check if the string contains a mixture of full and half-width chars. Empty string is valid.

func IsWhole

func IsWhole(value float64) bool

IsWhole returns true if value is whole number

func ToBoolean

func ToBoolean(str string) (bool, error)

ToBoolean convert the input string to a boolean.

Example
fmt.Println(ToBoolean("True"))
fmt.Println(ToBoolean("true"))
fmt.Println(ToBoolean("1"))
fmt.Println(ToBoolean("False"))
fmt.Println(ToBoolean("false"))
fmt.Println(ToBoolean("0"))
Output:

true <nil>
true <nil>
true <nil>
false <nil>
false <nil>
false <nil>

func ToCamelCase

func ToCamelCase(s string) string

ToCamelCase converts from underscore separated form to camel case form.

Example
fmt.Println(ToCamelCase("camel case"))
fmt.Println(ToCamelCase("  camel case  "))
fmt.Println(ToCamelCase("!!!camel case===="))
fmt.Println(ToCamelCase("camel-case"))
fmt.Println(ToCamelCase("camel_case"))
Output:

CamelCase
CamelCase
CamelCase
CamelCase
CamelCase

func ToFloat

func ToFloat(str string) (float64, error)

ToFloat convert the input string to a float, or 0.0 if the input is not a float.

func ToInt

func ToInt(str string) (int64, error)

ToInt convert the input string to an integer, or 0 if the input is not an integer.

func ToJSON

func ToJSON(obj interface{}) (string, error)

ToJSON convert the input to a valid JSON string

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase converts from camel case form to underscore separated form.

Example
fmt.Println(ToSnakeCase("SnakeCase"))
fmt.Println(ToSnakeCase("snake case"))
fmt.Println(ToSnakeCase("  snake case  "))
fmt.Println(ToSnakeCase("!!!snake case===="))
fmt.Println(ToSnakeCase("snake-case"))
fmt.Println(ToSnakeCase("snake_case"))
Output:

snake_case
snake_case
snake_case
snake_case
snake_case
snake_case

func ToString

func ToString(obj interface{}) string

ToString convert the input to a string.

Types

type ISO3166Entry

type ISO3166Entry struct {
	EnglishShortName string
	FrenchShortName  string
	Alpha2Code       string
	Alpha3Code       string
	Numeric          string
}

ISO3166Entry stores country codes

Jump to

Keyboard shortcuts

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