This project was created by a developer from Ukraine.

Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured.

The death toll keeps climbing. It's a genocide. We need your help. Let's fight back against the Russian regime.

Support Ukraine

#StandWithUkraine

This project was created by a developer from Ukraine.

Russia has invaded Ukraine and already killed tens of thousands of civilians,

with many more raped or tortured. It's a genocide. We need your help.

Support Ukraine

#StandWithUkraine

nanoid.cr v0.7.0

Crystal implementation of Nanoid, secure URL-friendly unique ID generator.

nanoid.cr

Codacy Badge Crystal CI GitHub release Docs License

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)   1.93  (517.44ms) (± 8.59%)  76.3MB/op        fastest
Nanoid.complex_generate(21)   1.05  (950.32ms) (± 3.96%)  91.6MB/op   1.84× slower
                    UUID v4   1.59  (628.64ms) (±14.80%)   168MB/op   1.21× slower
 Nanoid.simple_generate(36) 992.75m (  1.01s ) (±19.92%)  107MB/op   1.51× slower
Nanoid.complex_generate(36) 796.97m (  1.25s ) (±20.35%)  122MB/op   1.89× slower
                    UUID v4   1.50  (664.97ms) (±16.80%)  168MB/op        fastest

Development

Clone this repository and install dependencies:

shards install

Run tests:

crystal spec

Contributing

  1. Fork it (https://github.com/mamantoha/nanoid.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. 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.

Owner
github statistic
  • 35
  • 1
  • 2
  • 0
  • 11 months ago
  • January 3, 2018
License

MIT License

Links
Synced at

Wed, 29 Jun 2022 02:33:36 GMT