diff --git a/lib/bn.js b/lib/bn.js index a4eeea0..c70db49 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -18,6 +18,12 @@ var reversebuf = function(buf, nbuf) { } }; +bnjs.prototype.fromString = function(str) { + var bn = bnjs(str); + bn.copy(this); + return this; +}; + bnjs.fromBuffer = function(buf, opts) { if (typeof opts !== 'undefined' && opts.endian === 'little') { var nbuf = new Buffer(buf.length); diff --git a/test/bn.js b/test/bn.js index 8a7d458..dbf8d5e 100644 --- a/test/bn.js +++ b/test/bn.js @@ -66,7 +66,23 @@ describe('BN', function() { }); - describe('#fromBuffer', function() { + describe('#fromString', function() { + + it('should make BN from a string', function() { + BN().fromString('5').toString().should.equal('5'); + }); + + }); + + describe('#toString', function() { + + it('should make a string', function() { + BN(5).toString().should.equal('5'); + }); + + }); + + describe('@fromBuffer', function() { it('should work with big endian', function() { var bn = BN.fromBuffer(new Buffer('0001', 'hex'), {endian: 'big'}); @@ -83,11 +99,15 @@ describe('BN', function() { bn.toString().should.equal('1'); }); + }); + + describe('#fromBuffer', function() { + it('should work as a prototype method', function() { var bn = BN().fromBuffer(new Buffer('0100', 'hex'), {size: 2, endian: 'little'}); bn.toString().should.equal('1'); }); - + }); describe('#toBuffer', function() {