Ensure encode throws an error if receiving anything else than a integer

This commit is contained in:
2012-04-19 23:09:10 +01:00
parent 04918a627c
commit 0dbaf8676d
2 changed files with 8 additions and 2 deletions

View File

@@ -4,7 +4,7 @@ class Base58Builder
@base = @alphabet.length @base = @alphabet.length
encode: (num) -> 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 = '' str = ''
while num >= @base while num >= @base
mod = num % @base mod = num % @base

View File

@@ -10,9 +10,15 @@ describe 'Base58', ->
for str, num of examples for str, num of examples
Base58.encode(num).should.eql(str) 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', -> describe 'when passed a non-number value', ->
it 'throws an error', -> 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', -> describe '.decode', ->
it 'decodes Base58 string to number', -> it 'decodes Base58 string to number', ->