Official Elm port of Sqids. Generate short YouTube-looking IDs from numbers.
MIT License
Sqids (pronounced "squids") is a small library that lets you generate unique IDs from numbers. It is good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups.
Features:
Good for:
Not good for:
Install with
elm install sqids/sqids-elm
Simple encode & decode:
import Sqids
Sqids.encode [ 1, 2, 3 ]
--> (Ok "86Rf07")
Sqids.decode "86Rf07"
--> (Ok [ 1, 2, 3 ])
If IDs are too short, you can pad them to a certain length:
import Sqids
import Sqids.Context
context : Sqids.Context.Context
context =
case
Sqids.Context.from
{ alphabet = Sqids.Context.defaultAlphabet
, minLength = 10
, blockList = Sqids.Context.defaultBlockList
}
of
Ok ok ->
ok
Err err ->
Debug.todo <| Debug.toString err
Sqids.encodeWith context [ 1, 2, 3 ]
--> (Ok "86Rf07xd4z")
Sqids.decodeWith context "86Rf07xd4z"
--> (Ok [ 1, 2, 3 ])
Create unique IDs by shuffling the alphabet:
import Sqids
import Sqids.Context
context : Sqids.Context.Context
context =
case
Sqids.Context.new
|> Sqids.Context.withAlphabet "k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt"
|> Sqids.Context.build
of
Ok ok ->
ok
Err err ->
Debug.todo <| Debug.toString err
Sqids.encodeWith context [ 1, 2, 3 ]
--> (Ok "XRKUdQ")
Sqids.decodeWith context "XRKUdQ"
--> (Ok [ 1, 2, 3 ])
You need the Elm compiler and an elm test runner (e.g. elm-test or elm-test-rs).
An easy way to get started is with node.js and elm-tooling.
# Install dependencies
npm install
npx elm-tooling install
# Ensure that no unused code exists or that the documentation comments are correct
npx elm-review --fix
# Run tests in watch mode
npx elm-test-rs --watch
Note: Running elm-review also creates the test file tests/DocumentationCodeSnippetTest.elm
to check the code snippets in the README.md file and also the documentation comments in source code.
@todo add the elm-tooling-action GitHub action.