upboatact•

this is exactly the reason not to use such libraries without looking at the source, it implements left and righttrim with regexps and other such nonsense

go is famous for not masking the complexity behind its operations

17
dchapes•

Indeed this suffers from the "lets use regexp for all the things!" syndrome.

As an example, I benchmarked their IsASCII which uses a pre-compiled "^[\x00-\x7F]+$" regexp vs the obvious loop:

for i := 0; i < len(s); i++ {
	if s[i] >= utf8.RuneSelf {
		return false
	}
}
return true

and it was ~47629 ns/op for regexp vs ~884 ns/op for the simple loop (with a ~1500 byte string).

10
cathalgarvey•

Make a PR?

-2
schumacherfm•

... and full of interface{}

8
seriouslulz•

Go noob here, what's wrong with that?

2
JokerSp3•
// ToInt convert the input string to an integer, or 0 if the input is not an integer.
func ToInt(str string) (int64, error) {
	res, err := strconv.ParseInt(str, 0, 64)
	if err != nil {
		res = 0
	}
	return res, err
}

All this does is the equivalent of "return strconv.ParseInt(str, 0, 64)" because the 0 value of res is 0 anyway.

All numeric conversion functions actually do nothing but wrap functionality already in the std library.

Abs() is not just a wrapper of math.Abs, it is a complete rewrite of the function in a significantly slower way? Why rewrite a stdlib function if it gives no improvement in functionality and no improvement in performance?

13
uzimonkey•

I don't see the point of most of this. What's the point of IsNegative? What's wrong with < 0? IsWhole is probably a bad idea to begin with given the nature of floating point numbers, and is something that's only really useful in JS where you only have floating point numbers. You cannot assume that the result of any calculation is going to be whole. While in practice 5.*2. == 10., you should not be making these assumptions with floating point calculations. I don't know if that result is whole, I shouldn't care if it's whole, if my code relies on it being whole it's just taunting the floating point gods because one day something is going to change and the result isn't going to be whole. This, again, is something that only even makes sense in Javascript where you have to do things like use floating point values for array indices (WTF Javascript), but I don't see the point of it here.

A number of these convert strings to []byte and run them through regular expressions, this is a bad idea especially when you think you're "sanitizing" input. Taking a multi-byte character apart and shoving it into a byte slice, replacing or removing some bytes and then converting back to string is bound to end in disaster and, depending on what you're sanitizing, is a potential vulnerability depending on whether the character classes used in the regexp are inclusive or exclusive. There are string variants in the regexp package, use them.

Why are functions to validate a credit card number and an ISBN in the same package? What does one have to do with the other? And again, IsISBN is using byte slices with regexp presumably in a function specifically made to validate user input. This is a bad idea.

IsJSON just wraps json.Unmarshal. Why not just use json.Unmarshal? That pretty much sums up my opinion of 80% of the functions here, come to think of it.

Why is IsMultibyte second-guessing Golang's UTF code? I'm not sure if there'a a solution in the standard library, but a better guess would be to convert then string to []rune and use utf8.RuneLen != 1.

And things like Each are just really out of place. How often do I have a []interface{}? Usually only when I'm passing it through to a *printf function. If I want to iterate over that, what is wrong with for range? I see where this is going, I'm coming from Ruby where you're doing things like this all the time, but it doesn't fit with Go.

13
Redundancy_•

The library that this was based on was semi-interesting due to struct-tag based validation. A significant amount of this seems to come from that, which makes me wonder why the MIT license isn't reproduced.

1
cbarrick•

It would be nice if the Is functions worked with Readers; I find myself using Readers a lot more in Go where strings made sense in JavaScript. And there's no Unicode support, which is something Go is really good at. Perhaps if the state machines were written by hand instead of regexps, they could be made to work with readers and Unicode, and maybe faster.

I really don't like the Each function. It's a range loop but adds the overhead of indirection. I don't think there is a use for it.

4
tv64738•

For those who have not been infected with Javascript, this is referring to https://lodash.com/

2
Fwippy•

A shame that this doesn't work as you'd hope:

import (
    _ "github.com/aliygur/godash"
)
2
sethammons•

what do you expect? Per the spec, _ imports only get "side effects". Are you saying that you want these functions available without namespace (a terrible, terrible idea)? You can do that with . "github...." if I recall correctly.

3
Fwippy•

Lodash is an alternative to Underscore. Both provide functionality under the _ prefix - like _.reduce(...).

Tldr, a joke.

2
sethammons•

woosh Gotcha. Underscore.

2
RevMen•

I love lodash/underscore. I'll be taking a look at this for sure.

1
nerdwaller•

Are there plans to add the string functions? Mostly stuff like Snake, Pascal, Kebab and Title? I don't see much in the way of this there yet (forgive me if I missed it since I'm on mobile).

-1