module ShortUUID
Constants
- DEFAULT_BASE62
- VERSION
Public Class Methods
convert_alphabet_to_decimal(word, alphabet = DEFAULT_BASE62)
click to toggle source
# File lib/shortuuid.rb, line 28 def self.convert_alphabet_to_decimal(word, alphabet = DEFAULT_BASE62) num = 0 radix = alphabet.length word.chars.to_a.reverse.each_with_index do |char, index| num += alphabet.index(char) * (radix**index) end num end
convert_decimal_to_alphabet(decimal, alphabet = DEFAULT_BASE62)
click to toggle source
# File lib/shortuuid.rb, line 14 def self.convert_decimal_to_alphabet(decimal, alphabet = DEFAULT_BASE62) alphabet = alphabet.to_a radix = alphabet.length i = decimal.to_i out = [] return alphabet[0] if i.zero? loop do break if i.zero? out.unshift(alphabet[i % radix]) i /= radix end out.join end
decode(word, alphabet = DEFAULT_BASE62)
click to toggle source
# File lib/shortuuid.rb, line 41 def self.decode(word, alphabet = DEFAULT_BASE62) convert_alphabet_to_decimal(word, alphabet) end
encode(number, alphabet = DEFAULT_BASE62)
click to toggle source
# File lib/shortuuid.rb, line 37 def self.encode(number, alphabet = DEFAULT_BASE62) convert_decimal_to_alphabet(number, alphabet) end
expand(short_uuid, alphabet = DEFAULT_BASE62)
click to toggle source
# File lib/shortuuid.rb, line 45 def self.expand(short_uuid, alphabet = DEFAULT_BASE62) return nil unless short_uuid && !short_uuid.empty? decimal_value = convert_alphabet_to_decimal(short_uuid, alphabet) uuid = decimal_value.to_s(16).rjust(32, '0') [ uuid[0..7], uuid[8..11], uuid[12..15], uuid[16..19], uuid[20..31] ].join('-') end
shorten(uuid, alphabet = DEFAULT_BASE62)
click to toggle source
# File lib/shortuuid.rb, line 8 def self.shorten(uuid, alphabet = DEFAULT_BASE62) return nil unless uuid && !uuid.empty? decimal_value = uuid.split('-').join.to_i(16) convert_decimal_to_alphabet(decimal_value, alphabet) end