All Projects → sudhirj → shortuuid.rb

sudhirj / shortuuid.rb

Licence: MIT License
Convert UUIDs & numbers into space efficient and URL-safe Base62 strings, or any other alphabet.

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to shortuuid.rb

Xid.Net
Ported from https://github.com/rs/xid - A fast, low allocation, zero config, thread-safe 12 byte UUID generator based on the Mongo Object Id algorithm.
Stars: ✭ 17 (-55.26%)
Mutual labels:  uuid
secrets.clj
A library designed to generate cryptographically strong random numbers.
Stars: ✭ 64 (+68.42%)
Mutual labels:  uuid
vue-uuid
Add UUID to Vue instance.
Stars: ✭ 55 (+44.74%)
Mutual labels:  uuid
ksuid-go
K-Sortable globally Unique ID
Stars: ✭ 15 (-60.53%)
Mutual labels:  uuid
micell
A collection of functions for front-end development
Stars: ✭ 16 (-57.89%)
Mutual labels:  uuid
jquery-datatables
Jquery datatables ruby gems for assets pipeline
Stars: ✭ 73 (+92.11%)
Mutual labels:  rubygem
circleci
CircleCI REST API Ruby Gem
Stars: ✭ 93 (+144.74%)
Mutual labels:  rubygem
cyksuid
Fast Python implementation of KSUID (K-Sortable Globally Unique IDs) using Cython
Stars: ✭ 29 (-23.68%)
Mutual labels:  uuid
user
A domain layer providing basic user management
Stars: ✭ 14 (-63.16%)
Mutual labels:  uuid
workbook
simple framework for containing spreadsheet like data
Stars: ✭ 13 (-65.79%)
Mutual labels:  rubygem
Hunspell
Ruby wrapper for the famous spell checker library hunspell.
Stars: ✭ 34 (-10.53%)
Mutual labels:  rubygem
git-status-all
Get the status of all git repositories in a directory
Stars: ✭ 32 (-15.79%)
Mutual labels:  rubygem
gettext i18n rails js
Extends gettext_i18n_rails making your .PO files available to client side javascript as JSON
Stars: ✭ 28 (-26.32%)
Mutual labels:  rubygem
php-tools
Some code snippets that are often used in PHP
Stars: ✭ 25 (-34.21%)
Mutual labels:  uuid
twitch-chat-visualizer
A Node.js Project. Would you like to see your chat stream with a custom design? This is for you!
Stars: ✭ 14 (-63.16%)
Mutual labels:  uuid
eav-bundle
A Symfony bundle for basic EAV management
Stars: ✭ 19 (-50%)
Mutual labels:  uuid
ulid-creator
A Java library for generating Universally Unique Lexicographically Sortable Identifiers (ULID)
Stars: ✭ 38 (+0%)
Mutual labels:  uuid
nlp-pure
Natural language processing algorithms implemented in pure Ruby with minimal dependencies
Stars: ✭ 19 (-50%)
Mutual labels:  rubygem
benchmark-malloc
Trace memory allocations and collect stats
Stars: ✭ 18 (-52.63%)
Mutual labels:  rubygem
uuix
A tiny (<1KB) and fast UUID (v4) generator for Crystal
Stars: ✭ 17 (-55.26%)
Mutual labels:  uuid

ShortUUID

Encode and decode UUIDs into any alphabet. Can either be used to improve space efficiency by using any alphabet with more than 16 characters, or any other alphabet for fun or profit.

The default alphabet is a URL safe base62, as defined by

DEFAULT_BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.chars

Usage

irb(main):001:0> id = SecureRandom.uuid
=> "4890586e-32a5-4f9c-a000-2a2bb68eb1ce"

irb(main):003:0> short_id = ShortUUID.shorten id
=> "2CvPdpytrcURpSLoPxYb30"

irb(main):005:0> ShortUUID.expand short_id
=> "4890586e-32a5-4f9c-a000-2a2bb68eb1ce"

irb(main):007:0> vowel_id = ShortUUID.shorten id, "AEIOU".chars
=> "EOOIAUUEOUUIOIIEUUUAOOUIOOUUAOIIIEAUAOUAAAOEOOEOAUAUUEE"

irb(main):009:0> ShortUUID.expand vowel_id, "AEIOU".chars
=> "4890586e-32a5-4f9c-a000-2a2bb68eb1ce"

ShortUUID also works for numerical IDs:

irb(main):006:0> t = Time.now.to_i
=> 1545502666
irb(main):007:0> ShortUUID.encode t
=> "1galqM"

irb(main):008:0> t2 = Time.now.to_i
=> 1545502688
irb(main):009:0> ShortUUID.encode t2
=> "1galqi"

irb(main):010:0> t < t2
=> true
irb(main):011:0> ShortUUID.encode(t) < ShortUUID.encode(t2)
=> true

And the default alphabet is lexicographically sortable, if the encoded strings are the same length:

irb(main):013:0> a = 42
=> 42
irb(main):014:0> b = rand(9999999999999999)
=> 6674476916560393
irb(main):015:0> a < b
=> true
irb(main):016:0> ShortUUID.encode(a) < ShortUUID.encode(b)
=> false
irb(main):018:0> ShortUUID.encode(a).rjust(10, '0') < ShortUUID.encode(b).rjust(10, '0')
=> true
irb(main):021:0> [a, b].map{|x| ShortUUID::encode(x)}
=> ["g", "UZHuMM7pZ"]
irb(main):022:0> [a, b].map{|x| ShortUUID::encode(x).rjust(10, '0')}
=> ["000000000g", "0UZHuMM7pZ"]

Installation

Add this line to your application's Gemfile:

gem 'shortuuid'

And then execute:

$ bundle

Or install it yourself as:

$ gem install shortuuid

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sudhirj/shortuuid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].