speps/go-hashids

Fix for #28 just broke MinLength

Closed this issue · 4 comments

This should not fail, and it never used to, but now it does.
The hashid with min length 20 fails to decode the encoded string of length 30.

func TestHashids(t *testing.T) {

	// MinLength 30
	hData30 := hashids.NewData()
	hData30.Salt = "blarg123"
	hData30.MinLength = 30
	h30, err := hashids.NewWithData(hData30)
	if err != nil {
		t.Error(err)
	}

	encoded30, err := h30.EncodeInt64([]int64{4})
	if err != nil {
		t.Error(err)
	}
	decoded30, err := h30.DecodeInt64WithError(encoded30)
	if err != nil {
		t.Error(err)
	}
	if len(decoded30) != 1 || decoded30[0] != []int64{4}[0] {
		t.Error("Expected: ", []int64{4}, "; Got: ", decoded30)
	}

	// Same hashid, but minLength 20
	hData20 := hashids.NewData()
	hData20.Salt = "blarg123"
	hData20.MinLength = 20
	h20, err := hashids.NewWithData(hData20)
	if err != nil {
		t.Error(err)
	}

	// Should still be able to decode, as the only change is MinLength
	decoded20, err := h20.DecodeInt64WithError(encoded30)
	if err != nil {
		t.Error(err)
	}
	if len(decoded20) != 1 || decoded20[0] != []int64{4}[0] {
		t.Error("Expected: ", []int64{4}, "; Got: ", decoded20)
	}
}
speps commented

Do you have JSFiddle that shows that it works there? You can edit the demo from the official site : https://codepen.io/ivanakimov/pen/bNmExm

A PR would be very welcome as I don't have much time to work on it, the code isn't too complicated to understand there are reference implementations to check.

https://codepen.io/anon/pen/zzNrOp suggests that with the JS one, this behavior doesn't exist / Decoding with a minlength 4 an id encoded with a minlength 8 doesn't work

Interesting. Well then, MinLength is very poorly named...

speps commented

So... the JS version does the as the Go version for this case? Should I close this?