mirror of
https://github.com/jimeh/node-base58.git
synced 2026-02-19 07:36:40 +00:00
26 lines
706 B
CoffeeScript
26 lines
706 B
CoffeeScript
class Base58Builder
|
|
constructor: ->
|
|
@alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
|
|
@base = @alphabet.length
|
|
|
|
encode: (num) ->
|
|
throw new Error('Value passed is not an integer.') unless /^\d+$/.test num
|
|
str = ''
|
|
while num >= @base
|
|
mod = num % @base
|
|
str = @alphabet[mod] + str
|
|
num = (num - mod)/@base
|
|
@alphabet[num] + str
|
|
|
|
decode: (str) ->
|
|
num = 0
|
|
for char, index in str.split(//).reverse()
|
|
if (char_index = @alphabet.indexOf(char)) == -1
|
|
throw new Error('Value passed is not a valid Base58 string.')
|
|
num += char_index * Math.pow(@base, index)
|
|
num
|
|
|
|
|
|
# Export module
|
|
module.exports = new Base58Builder()
|