5 Commits

4 changed files with 14 additions and 10 deletions

View File

@@ -1,6 +1,4 @@
# node-base58
[![Build Status](https://secure.travis-ci.org/jimeh/node-base58.png)](http://travis-ci.org/jimeh/node-base58)
# node-base58 [![Build Status](https://secure.travis-ci.org/jimeh/node-base58.png)](http://travis-ci.org/jimeh/node-base58)
A Base58 encoding and decoding library for [Node.js].

View File

@@ -1,6 +1,6 @@
{
"name": "base58",
"version": "0.0.1",
"version": "0.0.2",
"keywords": "base58",
"description": "Base58 encoding and decoding",
"licenses": [{

View File

@@ -1,10 +1,10 @@
class Base58
class Base58Builder
constructor: ->
@alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
@base = @alphabet.length
encode: (num) ->
throw new Error('Value passed is not a number.') if typeof num != 'number'
throw new Error('Value passed is not an integer.') unless /^\d+$/.test num
str = ''
while num >= @base
mod = num % @base
@@ -16,10 +16,10 @@ class Base58
num = 0
for char, index in str.split(//).reverse()
if (char_index = @alphabet.indexOf(char)) == -1
throw new Error('Value passed not a valid Base58 string.')
throw new Error('Value passed is not a valid Base58 string.')
num += char_index * Math.pow(@base, index)
num
# Export module
module.exports = new Base58()
module.exports = new Base58Builder()

View File

@@ -10,9 +10,15 @@ describe 'Base58', ->
for str, num of examples
Base58.encode(num).should.eql(str)
describe 'when passed a float', ->
it 'throws an error', ->
(-> Base58.encode(3.14)).should
.throw('Value passed is not an integer.')
describe 'when passed a non-number value', ->
it 'throws an error', ->
(-> Base58.encode('hi')).should.throw('Value passed is not a number.')
(-> Base58.encode('hi')).should
.throw('Value passed is not an integer.')
describe '.decode', ->
it 'decodes Base58 string to number', ->
@@ -22,4 +28,4 @@ describe 'Base58', ->
describe 'when passed a non-Base58 string', ->
it 'throws an error', ->
(-> Base58.decode('>_<')).should
.throw('Value passed not a valid Base58 string.')
.throw('Value passed is not a valid Base58 string.')