Generating short unique string identifiers from numbers with sqids-elm
2024-08-06 (Last updated on 2024-09-01)
I ported the sqids library to Elm, which generates short unique string IDs from numbers.
This is good for link shortening, fast & URL-safe ID generation and also storing relations between multiple numbers.
Interesting parts of the implementation
It has a shuffle algorithm that does not need random and always produces the same output given the same input.
It is designed for imperative languages though, requiring many swaps in an array.
I exposed two constructors: One that expects a configuration record, and one that follows the builder pattern.
Working that out was fun.
Sqids.Context.from
{ alphabet = Sqids.Context.defaultAlphabet
, minLength = 0
, blockList = Sqids.Context.defaultBlockList
}
Sqids.Context.new
|> Sqids.Context.withAlphabet "abc123"
|> Sqids.Context.withMinLength 1
|> Sqids.Context.withBlockList [ "block", "disallowed" ]
|> Sqids.Context.build
Releasing a package was also a nice experience
To aid with creating a new Elm package, Dillon Kearns created two awesome resources: The elm-package-starter template and the idiomatic Elm package guide.
After looking at those, two more things were great to have:
- elm-doc-preview is an awesome tool to preview the documentation. It is the main reason why I did not not create patch versions 1.0.1 to 1.0.10 (or even more) because I would not have been satisfied with the visuals on the package site. It is especially useful to look at the generated order of the document on the page and for the code snippets as part of the documentation.
- And lue-birds elm-review rule to verify the code examples both on the README and inside the documentation was great! It saved me a lot of copy-paste and wrong documentation.
More info about the sqids package (same as README):
Features:
- Encode multiple numbers - generate short IDs from one or several non-negative numbers
- Quick decoding - easily decode IDs back into numbers
- Unique IDs - generate unique IDs by shuffling the alphabet once
- ID padding - provide minimum length to make IDs more uniform
- URL safe - auto-generated IDs do not contain common profanity
- Randomized output - Sequential input provides nonconsecutive IDs
- Many implementations - Support for 40+ programming languages
Good for:
- Generating IDs for public URLs (eg: link shortening)
- Generating IDs for internal systems (eg: event tracking)
- Decoding for quicker database lookups (eg: by primary keys)
Not good for:
- Sensitive data (this is not an encryption library)
- User IDs (can be decoded revealing user count)
Note: Sqids is the success of hashids, which already had a nice package.