Browse Source

add fromString method for library consistency

patch-2
Ryan X. Charles 10 years ago
parent
commit
aee8547093
  1. 6
      lib/bn.js
  2. 24
      test/bn.js

6
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);

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

Loading…
Cancel
Save