Sha256: f6da83660645e1db1740cdbe7d0c9113141bcf08e538f64439f468a2d75f3ca7

Contents?: true

Size: 532 Bytes

Versions: 18

Compression:

Stored size: 532 Bytes

Contents

module Firefly
  class Base62
  
    CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split('')
    BASE = 62
  
    def self.encode(value)
      s = []
      while value >= BASE
        value, rem = value.divmod(BASE)
        s << CHARS[rem]
      end
      s << CHARS[value]
      s.reverse.join("")
    end

    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
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
firefly-1.5.0 lib/firefly/base62.rb
firefly-1.4.1 lib/firefly/base62.rb
firefly-1.4.0 lib/firefly/base62.rb
firefly-1.3.1 lib/firefly/base62.rb
firefly-1.3.0 lib/firefly/base62.rb
firefly-1.2.2 lib/firefly/base62.rb
firefly-1.2.0 lib/firefly/base62.rb
firefly-1.1.0 lib/firefly/base62.rb
firefly-1.0.1 lib/firefly/base62.rb
firefly-0.4.5 lib/firefly/base62.rb
firefly-0.4.4 lib/firefly/base62.rb
firefly-0.4.3 lib/firefly/base62.rb
firefly-0.4.2 lib/firefly/base62.rb
firefly-0.4.1 lib/firefly/base62.rb
firefly-0.4.0.1 lib/firefly/base62.rb
firefly-0.4.0 lib/firefly/base62.rb
firefly-0.3.1 lib/firefly/base62.rb
firefly-0.3.0 lib/firefly/base62.rb