From 78ef76eb2fe1f4503c2c79895ec4c9417a539ce8 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 17 Sep 2014 14:29:53 -0700 Subject: [PATCH] more convenient constructor ...allow inputing strings or buffers in the constructor. --- lib/base58check.js | 9 ++++++++- test/base58check.js | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/base58check.js b/lib/base58check.js index 9bc78ee19..7b5fb954b 100644 --- a/lib/base58check.js +++ b/lib/base58check.js @@ -4,8 +4,15 @@ var sha256sha256 = require('./hash').sha256sha256; var Base58Check = function Base58Check(obj) { if (!(this instanceof Base58Check)) return new Base58Check(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); + } }; Base58Check.prototype.set = function(obj) { diff --git a/test/base58check.js b/test/base58check.js index 635198cf7..bc7081fac 100644 --- a/test/base58check.js +++ b/test/base58check.js @@ -16,6 +16,11 @@ describe('Base58Check', function() { should.exist(b58); }); + it('should allow this handy syntax', function() { + Base58Check(buf).toString().should.equal(enc); + Base58Check(enc).toBuffer().toString('hex').should.equal(buf.toString('hex')); + }); + describe('#set', function() { it('should set a buf', function() {