Class: Firefly::Base62

Inherits:
Object
  • Object
show all
Defined in:
lib/tmin/base62.rb

Constant Summary collapse

CHARS =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split('')
BASE =
62

Class Method Summary collapse

Class Method Details

.decode(str) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/tmin/base62.rb', line 18

def self.decode(str)
  str = str.split('').reverse
  total = 0
  str.each_with_index do |v,k|
    total += (CHARS.index(v) * (BASE ** k))
  end
  total
end

.encode(value) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/tmin/base62.rb', line 8

def self.encode(value)
  s = []
  while value >= BASE
    value, rem = value.divmod(BASE)
    s << CHARS[rem]
  end
  s << CHARS[value]
  s.reverse.join("")
end