Skip to content
A unique string ID generator for Clojure and ClojureScript
Clojure
Branch: master
Clone or download
Latest commit 3a3085c Oct 10, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Update Circle CI config: bump lein version to 2.8.1 Jul 6, 2018
src/nano_id Replace ~ with - in default alphabet Oct 9, 2019
test/nano_id Replace ~ with - in default alphabet Oct 9, 2019
.gitignore Bump version to 0.10.0 Oct 9, 2019
LICENSE Init Jun 26, 2018
README.md Update readme Oct 10, 2019
package.json Bump version to 0.10.0 Oct 9, 2019
project.clj Bump version to 0.10.0 Oct 9, 2019

README.md

nano-id

A tiny, secure, URL-friendly unique string ID generator for both Clojure and ClojureScript.

Clojars Project cljdoc badge CircleCI

Secure

nano-id uses SecureRandom (Clojure) and crypto (ClojureScript) to generate cryptographically strong random IDs with a proper distribution of characters.

Compact

nano-id generates compact IDs with just 21 characters. By using a larger alphabet than UUID, nano-id can generate a greater number of unique IDs, when compared to UUID, with fewer characters (21 versus 36).

URL-Friendly

nano-id uses URL-friendly characters [A-Za-z0-9_-]. Perfect for unique identifiers in web applications.

Usage

Normal

Add to your project.clj: [nano-id "0.10.0"].

The default implementation uses 64-character alphabet and generates 21-character IDs.

user=> (require '[nano-id.core :refer [nano-id]])
nil

user=> (nano-id)
"NOBHihn110UuXbF2JiKxT"

But you can pass the size

user=> (nano-id 10)
"N2g6IlJP0l"

Custom alphabet or random number generator

Also you can provide your own alphabet as follow

user=> (require '[nano-id.custom :refer [generate]])
nil

user=> (def my-nano-id (generate "abc"))
#'user/my-nano-id

user=> (my-nano-id 10)
"abcbabacba"

Or your custom random number generator, for example

(let [alphabet "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
      time-gen (fn [n] (take n (iterate #(unsigned-bit-shift-right % 6)
                                        (quot (System/currentTimeMillis) 1000))))
      time-id  (generate alphabet time-gen)]
  (time-id 6))
"1RJu2O"

This encodes current time using a lexicographical alphabet.

Tools

Copyright and license

Code copyright 2018 nano-id authors and Andrey Sitnik. Code released under the MIT License.

Based on the original nanoid for JavaScript by Andrey Sitnik.

You can’t perform that action at this time.