more convenient Base58 constructor

This commit is contained in:
Ryan X. Charles 2014-09-17 14:26:19 -07:00
parent ac85264a28
commit 40ea68a3ff
2 changed files with 13 additions and 1 deletions

View File

@ -3,8 +3,15 @@ var bs58 = require('bs58');
var Base58 = function Base58(obj) {
if (!(this instanceof Base58))
return new Base58(obj);
if (obj)
if (Buffer.isBuffer(obj)) {
var buf = obj;
this.fromBuffer(buf);
} else if (typeof obj === 'string') {
var str = obj;
this.fromString(str);
} else if (obj) {
this.set(obj);
}
};
Base58.prototype.set = function(obj) {

View File

@ -15,6 +15,11 @@ describe('Base58', function() {
should.exist(b58);
});
it('should allow this handy syntax', function() {
Base58(buf).toString().should.equal(enc);
Base58(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
});
describe('#set', function() {
it('should set a blank buffer', function() {