mirror of
https://github.com/jimeh/node-base58.git
synced 2026-02-19 07:36:40 +00:00
Ensure encode throws an error if receiving anything else than a integer
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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', ->
|
||||||
|
|||||||
Reference in New Issue
Block a user