Browse Source

more convenient constructor

...allow inputing strings or buffers in the constructor.
patch-2
Ryan X. Charles 10 years ago
parent
commit
78ef76eb2f
  1. 9
      lib/base58check.js
  2. 5
      test/base58check.js

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

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

Loading…
Cancel
Save