Sqids (pronounced "squids") is a small library that lets you generate YouTube-looking IDs from numbers. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups.
Install Sqids via (on the Julia REPL):
julia> ] # enter Pkg REPL-mode
pkg> add Sqids
Start using Sqids:
using Sqids
Simple encode & decode:
config = Sqids.configure()
id = Sqids.encode(config, [1, 2, 3]) #> "86Rf07"
numbers = Sqids.decode(config, id) #> [1, 2, 3]
Randomize IDs by providing a custom alphabet:
config = Sqids.configure(alphabet="FxnXM1kBN6cuhsAvjW3Co7l2RePyY8DwaU04Tzt9fHQrqSVKdpimLGIJOgb5ZE")
id = Sqids.encode(config, [1, 2, 3]) #> "B4aajs"
numbers = Sqids.decode(config, id) #> [1, 2, 3]
Enforce a minimum length for IDs:
config = Sqids.configure(minLength=10)
id = Sqids.encode(config, [1, 2, 3]) #> "86Rf07xd4z"
numbers = Sqids.decode(config, id) #> [1, 2, 3]
Prevent specific words from appearing anywhere in the auto-generated IDs:
config = Sqids.configure(blocklist=["word1","word2"])
id = Sqids.encode(config, [1, 2, 3]) #> "86Rf07"
numbers = Sqids.decode(config, id) #> [1, 2, 3]
If strict=false
is set when configuring, it enables handling of limitless values using Int128
or BigInt
, integer types larger than 64 bits.
config = Sqids.configure(strict=false) # not-strict mode
id = Sqids.encode(config, Int128[9223372036854775808]) #> "pXFNc5r689z6"
numbers = Sqids.decode(config, id) #> Int128[9223372036854775808]
Note that while this setting allows for automatic type selection of the decoded value, it may cause type instability and minor performance slowdowns.
- Do not encode sensitive data. These IDs can be easily decoded.
- Default blocklist is auto-enabled. It's configured for the most common profanity words. Create your own custom list by using the
blocklist
parameter, or pass an empty array to allow all words. - Read more at https://sqids.org/julia