more convenient constructor

...allow inputing strings or buffers in the constructor.
This commit is contained in:
Ryan X. Charles 2014-09-17 14:29:53 -07:00
parent 40ea68a3ff
commit 78ef76eb2f
2 changed files with 13 additions and 1 deletions

View File

@ -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) {

View File

@ -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() {