sqids / sqids-javascript

Official JavaScript port of Sqids. Generate short unique IDs from numbers.
https://sqids.org/javascript
MIT License
659 stars 14 forks source link

Create a function to check if the const encoded is valid #9

Closed brunoorosco closed 1 year ago

brunoorosco commented 1 year ago

As an update, it is worth inserting a function that can validate the value being decoded. If this value is not created with the minLenght and Alphabet, it could return an empty value, or length == 0. This way we could receive n values , but only decode information that respects standards.

4kimov commented 1 year ago

Thanks for the suggestion.

I'm assuming you mean something like this?

const isValid = (id) => sqids.encode(sqids.decode(id)) == id;

I've considered suggesting it as part of the spec as a convenience method, but it doesn't handle all use-cases well (for example if user adds minLength param later on, their initially generated / shorter IDs would be considered invalid). That's why I wanted to give it some time and see how the library is used among projects, to see if people would appreciate this convenience method.