add fromString method for library consistency

This commit is contained in:
Ryan X. Charles 2014-09-17 15:32:14 -07:00
parent 6494ca5076
commit aee8547093
2 changed files with 28 additions and 2 deletions

View File

@ -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) { bnjs.fromBuffer = function(buf, opts) {
if (typeof opts !== 'undefined' && opts.endian === 'little') { if (typeof opts !== 'undefined' && opts.endian === 'little') {
var nbuf = new Buffer(buf.length); var nbuf = new Buffer(buf.length);

View File

@ -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() { it('should work with big endian', function() {
var bn = BN.fromBuffer(new Buffer('0001', 'hex'), {endian: 'big'}); var bn = BN.fromBuffer(new Buffer('0001', 'hex'), {endian: 'big'});
@ -83,6 +99,10 @@ describe('BN', function() {
bn.toString().should.equal('1'); bn.toString().should.equal('1');
}); });
});
describe('#fromBuffer', function() {
it('should work as a prototype method', function() { it('should work as a prototype method', function() {
var bn = BN().fromBuffer(new Buffer('0100', 'hex'), {size: 2, endian: 'little'}); var bn = BN().fromBuffer(new Buffer('0100', 'hex'), {size: 2, endian: 'little'});
bn.toString().should.equal('1'); bn.toString().should.equal('1');