mirror of
https://github.com/jimeh/node-base58.git
synced 2026-02-19 07:36:40 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 950311347c | |||
| be04fd0d49 | |||
| 33ae17b282 | |||
| b45ca698b1 | |||
| f7e5594dc1 | |||
| 2235cafbc0 | |||
| 0dbaf8676d | |||
| 04918a627c | |||
| 8b81397bf3 |
@@ -1,6 +1,4 @@
|
||||
# node-base58
|
||||
|
||||
[](http://travis-ci.org/jimeh/node-base58)
|
||||
# node-base58 [](http://travis-ci.org/jimeh/node-base58)
|
||||
|
||||
A Base58 encoding and decoding library for [Node.js].
|
||||
|
||||
@@ -13,7 +11,7 @@ for short URLs among other things. Flickr is one the biggest sites that makes
|
||||
use of it for short photo URLs.
|
||||
|
||||
For example `6857269519` becomes `brXijP` when Base58 encoded, and hence the
|
||||
Flickr short URL is: [`http://flic.kr/p/brXijP`](http://flic.kr/p/brXijP)
|
||||
Flickr short URL is: `http://flic.kr/p/brXijP`
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "base58",
|
||||
"version": "0.0.1",
|
||||
"version": "0.1.0",
|
||||
"keywords": "base58",
|
||||
"description": "Base58 encoding and decoding",
|
||||
"licenses": [{
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
"repository" : {
|
||||
"type" : "git",
|
||||
"url" : "http://github.com/jimeh/node-base58.git"
|
||||
"url" : "https://github.com/jimeh/node-base58.git"
|
||||
},
|
||||
|
||||
"main": "./lib/base58",
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
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
|
||||
num = parseInt(num) unless typeof num == 'number'
|
||||
str = ''
|
||||
while num >= @base
|
||||
mod = num % @base
|
||||
@@ -16,10 +17,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()
|
||||
|
||||
@@ -10,9 +10,20 @@ describe 'Base58', ->
|
||||
for str, num of examples
|
||||
Base58.encode(num).should.eql(str)
|
||||
|
||||
describe 'when passed a string only containing numbers', ->
|
||||
it 'encodes string after first converting it to an integer', ->
|
||||
for str, num of examples
|
||||
Base58.encode(num.toString()).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 +33,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.')
|
||||
|
||||
Reference in New Issue
Block a user