nanoid
nanoid.cr
Crystal implementation of original NanoID
Installation
Add this to your application's shard.yml:
dependencies:
  nanoid:
    github: mamantoha/nanoid.cr
Usage
require "nanoid"
Nanoid.generate uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID with 21 characters.
Nanoid.generate
# => 3gFI8yZxcfXsXGhB0036l
If you want to reduce ID length (and increase collisions probability), you can pass the length as an argument.
Nanoid.generate(size: 8, alphabet: "1234567890abcdef")
# => 86984b57
Non-secure API is also available.
Nanoid.generate(secure: false)
# => no0MmiInNcBm9jR2_3sGt
Nanoid.generate(size: 10, alphabet: "1234567890abcdef", secure: false)
# => 2793b2351c
Benchmark
crystal run --release bench/benchmark.cr
Crystal 1.1.0-dev [e7b46c407] (2021-03-30)
LLVM: 10.0.0
Default target: x86_64-unknown-linux-gnu
    Nanoid.simple_generate(21)   2.01  (498.25ms) (± 5.03%)  76.3MB/op   3.11× slower
   Nanoid.complex_generate(21)   1.02  (983.07ms) (±11.12%)  91.6MB/op   6.13× slower
Nanoid.non_secure_generate(21)   6.24  (160.31ms) (± 5.53%)  45.8MB/op        fastest
                       UUID v4   2.17  (459.78ms) (±22.74%)    0.0B/op   2.87× slower
    Nanoid.simple_generate(16)   1.50  (665.50ms) (±29.37%)  61.3MB/op   5.62× slower
   Nanoid.complex_generate(16)   1.53  (652.78ms) (±14.02%)  61.1MB/op   5.51× slower
Nanoid.non_secure_generate(16)   8.44  (118.41ms) (± 4.69%)  30.5MB/op        fastest
                          UUID   1.77  (566.09ms) (±41.97%)    0.0B/op   4.78× slower
Development
Clone this repository and install dependencies:
shards install
Run tests:
crystal spec
Contributing
- Fork it (https://github.com/mamantoha/nanoid.cr/fork)
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- mamantoha Anton Maminov - creator, maintainer
License
Copyright: 2018-2021 Anton Maminov (anton.maminov@gmail.com)
This library is distributed under the MIT license. Please see the LICENSE file.