use a decorate function to optimize bundle filesize

This commit is contained in:
Ryan X. Charles 2014-07-10 18:27:15 -07:00
parent 32cf5aa941
commit 80bba1cf81
1 changed files with 22 additions and 63 deletions

View File

@ -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;
};