Skip to content

Commit

Permalink
refactored based on reddit feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
aliuygur committed Sep 21, 2016
1 parent 2149756 commit de96a27
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 252 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ go:
- 1.4
- 1.5
- 1.6
- 1.7
- tip
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ Micro check library in Golang.

## installation

`go get gopkg.in/alioygur/is.v0`
`go get gopkg.in/alioygur/is.v1`

## usage

```go
package main

import "gopkg.in/alioygur/is.v0"
import "gopkg.in/alioygur/is.v1"
import "log"

func main() {
email := "[email protected]"

is.Email(email) // true
is.Email("[email protected]") // true
is.Numeric("") // false
is.UTFNumeric("") // true
}
Expand Down
95 changes: 13 additions & 82 deletions is.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Alpha(s string) bool {
return true
}

//IsUTFLetter check if the string contains only unicode letter characters.
//UTFLetter check if the string contains only unicode letter characters.
//Similar to IsAlpha but for all languages. Empty string is valid.
func UTFLetter(str string) bool {
for _, v := range str {
Expand Down Expand Up @@ -136,34 +136,14 @@ func UTFNumeric(s string) bool {
return true
}

// Negative returns true if value < 0
func Negative(value float64) bool {
return value < 0
}

// Positive returns true if value > 0
func Positive(value float64) bool {
return value > 0
}

// NonNegative returns true if value >= 0
func NonNegative(value float64) bool {
return value >= 0
}

// NonPositive returns true if value <= 0
func NonPositive(value float64) bool {
return value <= 0
}

// Whole returns true if value is whole number
func Whole(value float64) bool {
return math.Abs(math.Remainder(value, 1)) == 0
}

// Natural returns true if value is natural number (positive and whole)
func Natural(value float64) bool {
return Whole(value) && Positive(value)
return Whole(value) && value > 0
}

// UTFDigit check if the string contains only unicode radix-10 decimal digits. Empty string is valid.
Expand Down Expand Up @@ -194,23 +174,23 @@ func RGBcolor(str string) bool {

// LowerCase check if the string is lowercase. Empty string is valid.
func LowerCase(str string) bool {
if NullString(str) {
if len(str) == 0 {
return true
}
return str == strings.ToLower(str)
}

// UpperCase check if the string is uppercase. Empty string is valid.
func UpperCase(str string) bool {
if NullString(str) {
if len(str) == 0 {
return true
}
return str == strings.ToUpper(str)
}

// Int check if the string is an integer. Empty string is valid.
func Int(str string) bool {
if NullString(str) {
if len(str) == 0 {
return true
}
_, err := strconv.Atoi(str)
Expand All @@ -224,32 +204,6 @@ func Float(str string) bool {
return err == nil
}

// DivisibleBy 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).
func DivisibleBy(str, num string) bool {
f, _ := toFloat(str)
p := int64(f)
q, _ := toInt(num)
if q == 0 {
return false
}
return (p == 0) || (p%q == 0)
}

// NullString check if the string is null.
func NullString(str string) bool {
return len(str) == 0
}

// Null check if the variable is null.
func Null(obj interface{}) bool {
if obj != nil {
return false
}
return true
}

// ByteLength check if the string's length (in bytes) falls in a range.
func ByteLength(str string, min, max int) bool {
return len(str) >= min && len(str) <= max
Expand Down Expand Up @@ -371,7 +325,7 @@ func Multibyte(s string) bool {
}
}

return NullString(s)
return len(s) == 0
}

// ASCII check if the string contains ASCII chars only. Empty string is valid.
Expand All @@ -396,31 +350,31 @@ func PrintableASCII(s string) bool {

// FullWidth check if the string contains any full-width chars. Empty string is valid.
func FullWidth(str string) bool {
if NullString(str) {
if len(str) == 0 {
return true
}
return rxFullWidth.MatchString(str)
}

// HalfWidth check if the string contains any half-width chars. Empty string is valid.
func HalfWidth(str string) bool {
if NullString(str) {
if len(str) == 0 {
return true
}
return rxHalfWidth.MatchString(str)
}

// VariableWidth check if the string contains a mixture of full and half-width chars. Empty string is valid.
func VariableWidth(str string) bool {
if NullString(str) {
if len(str) == 0 {
return true
}
return rxHalfWidth.MatchString(str) && rxFullWidth.MatchString(str)
}

// Base64 check if a string is base64 encoded.
func Base64(s string) bool {
if NullString(s) {
if len(s) == 0 {
return false
}
_, err := base64.StdEncoding.DecodeString(s)
Expand Down Expand Up @@ -557,33 +511,10 @@ func Semver(str string) bool {
return rxSemver.MatchString(str)
}

// Matches check if string matches the pattern (pattern is regular expression)
// In case of error return false
func Matches(str, pattern string) bool {
match, _ := regexp.MatchString(pattern, str)
return match
}

// StringMatches checks if a string matches a given pattern.
func StringMatches(s string, params ...string) bool {
if len(params) == 1 {
pattern := params[0]
return Matches(s, pattern)
}
return false
}

// StringLength check string's length (including multi byte strings)
func StringLength(str string, params ...string) bool {

if len(params) == 2 {
strLength := utf8.RuneCountInString(str)
min, _ := toInt(params[0])
max, _ := toInt(params[1])
return strLength >= int(min) && strLength <= int(max)
}

return false
func StringLength(str string, min int, max int) bool {
slen := utf8.RuneCountInString(str)
return slen >= min && slen <= max
}

//Exists returns whether the given file or directory exists or not
Expand Down
Loading

0 comments on commit de96a27

Please sign in to comment.