add fromString method for library consistency
This commit is contained in:
parent
6494ca5076
commit
aee8547093
|
@ -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);
|
||||||
|
|
22
test/bn.js
22
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() {
|
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');
|
||||||
|
|
Loading…
Reference in New Issue