diff --git a/lib/browser/Bignum.js b/lib/browser/Bignum.js index ff00b03..7ada158 100644 --- a/lib/browser/Bignum.js +++ b/lib/browser/Bignum.js @@ -73,79 +73,38 @@ bnjs.prototype.toBuffer = function(opts) { return buf; }; -bnjs.prototype._add = _bnjs.prototype.add; - -bnjs.prototype.add = function(b) { - if (typeof b === 'number') - b = b.toString(); - if (typeof b === 'number' || typeof b === 'string') - b = new _bnjs(b); - return this._add(b); +function decorate(name) { + bnjs.prototype['_' + name] = _bnjs.prototype[name]; + var f = function(b) { + if (typeof b === 'string') + b = new _bnjs(b); + else if (typeof b === 'number') + b = new _bnjs(b.toString()); + return this['_' + name](b); + }; + bnjs.prototype[name] = f; }; -bnjs.prototype._sub = _bnjs.prototype.sub; - -bnjs.prototype.sub = function(b) { - if (typeof b === 'number') - b = b.toString(); - if (typeof b === 'number' || typeof b === 'string') - b = new _bnjs(b); - return this._sub(b); -}; - -bnjs.prototype._mul = _bnjs.prototype.mul; - -bnjs.prototype.mul = function(b) { - if (typeof b === 'number') - b = b.toString(); - if (typeof b === 'number' || typeof b === 'string') - b = new _bnjs(b); - return this._mul(b); -}; - -bnjs.prototype._mod = _bnjs.prototype.mod; - -bnjs.prototype.mod = function(b) { - if (typeof b === 'number') - b = b.toString(); - if (typeof b === 'number' || typeof b === 'string') - b = new _bnjs(b); - return this._mod(b); -}; - -bnjs.prototype._div = _bnjs.prototype.div; - -bnjs.prototype.div = function(b) { - if (typeof b === 'number') - b = b.toString(); - if (typeof b === 'number' || typeof b === 'string') - b = new _bnjs(b); - return this._div(b); -}; - -bnjs.prototype._cmp = _bnjs.prototype.cmp; - -bnjs.prototype.cmp = function(b) { - if (typeof b === 'number') - b = b.toString(); - if (typeof b === 'number' || typeof b === 'string') - b = new _bnjs(b); - return this._cmp(b); -}; +decorate('add'); +decorate('sub'); +decorate('mul'); +decorate('mod'); +decorate('div'); +decorate('cmp'); bnjs.prototype.gt = function(b) { - if (typeof b === 'number') - b = b.toString(); - if (typeof b === 'number' || typeof b === 'string') + if (typeof b === 'string') b = new _bnjs(b); + else if (typeof b === 'number') + b = new _bnjs(b.toString()); return this.cmp(b) > 0; }; bnjs.prototype.lt = function(b) { - if (typeof b === 'number') - b = b.toString(); - if (typeof b === 'number' || typeof b === 'string') + if (typeof b === 'string') b = new _bnjs(b); + else if (typeof b === 'number') + b = new _bnjs(b.toString()); return this.cmp(b) < 0; };