Skip to content

Ruby implementation of Nanoid, secure URL-friendly unique ID generator

License

Notifications You must be signed in to change notification settings

radeno/nanoid.rb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nanoid.rb

This is ReadMe for version 2.0, if you looking for version compatible with NanoID 1.x look in branch v1.x.

A tiny, secure URL-friendly unique string ID generator.

Ruby implementation of original NanoID https://github.com/ai/nanoid

Installation

Into Gemfile / gems.rb add:

gem 'nanoid'

Usage

require 'nanoid'

Nanoid.generate

Options

parameter type description
size integer Length of generated string
alphabet string Collection of characters used for generated string
non_secure boolean Switch to use non-secure generated string. Do not use if you don't need

Default options:

Size: 21 character length

Alphabet: _-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Non secure: false

Examples

Simple generate unique string

require 'nanoid'

Nanoid.generate
# => "MVNBLMwrcdbKjpo73qsmN"

Change length of generated string

require 'nanoid'

Nanoid.generate(size: 10)
# => "zRTPzok801"

Change alphabet of generated string

require 'nanoid'

Nanoid.generate(alphabet: '1234567890abcdef')
# => "84e292c6aabbbf50bf087"

Change size and alphabet of generated string

require 'nanoid'

Nanoid.generate(size: 14, alphabet: '1234567890abcdef')
# => "c5db3470fadf94"

Use non-secure generator

require 'nanoid'

Nanoid.generate(non_secure: true)
# => "-BX3dH02BmRX9Al4drpIb"

About

Ruby implementation of Nanoid, secure URL-friendly unique ID generator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages