Home

Awesome

Sqids OCaml

OCaml-CI Build Status

Sqids (pronounced "squids") is a small library that lets you generate unique IDs from numbers. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups.

Features:

šŸ§° Use-cases

Good for:

Not good for:

šŸš€ Getting started

Install:

opam install sqids

Add to dune-project:

(sqids (= 0.1.0))

Add to dune for your target:

(libraries sqids)

Try in utop:

# require "sqids";;
let s = Sqids.make () in
Sqids.encode s [1; 2; 3]

šŸ‘©ā€šŸ’» Examples

Simple encode & decode:

Note šŸš§ Because of the algorithm's design, multiple IDs can decode back into the same sequence of numbers. If it's important to your design that IDs are canonical, you have to manually re-encode decoded numbers and check that the generated ID matches.

Enforce a minimum length for IDs:

let sqids = Sqids.make ~min_length:10 () in
let id = Sqids.encode sqids [1; 2; 3] in (* "86Rf07xd4z" *)
let numbers = Sqids.decode sqids id in (* [1; 2; 3] *)

Randomize IDs by providing a custom alphabet:

let sqids = Sqids.make ~alphabet:"FxnXM1kBN6cuhsAvjW3Co7l2RePyY8DwaU04Tzt9fHQrqSVKdpimLGIJOgb5ZE" () in
let id = Sqids.encode sqids [1; 2; 3] in (* "B4aajs" *)
let numbers = Sqids.decode sqids id in (* [1; 2; 3] *)

Prevent specific words from appearing anywhere in the auto-generated IDs:

let sqids = Sqids.make ~blocklist:["86Rf07"] () in
let id = Sqids.encode sqids [1; 2; 3] in (* "se8ojk" *)
let numbers = Sqids.decode sqids id in (* [1; 2; 3] *)

šŸ“ License

MIT