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; return buf;
}; };
bnjs.prototype._add = _bnjs.prototype.add; function decorate(name) {
bnjs.prototype['_' + name] = _bnjs.prototype[name];
bnjs.prototype.add = function(b) { var f = function(b) {
if (typeof b === 'number') if (typeof b === 'string')
b = b.toString(); b = new _bnjs(b);
if (typeof b === 'number' || typeof b === 'string') else if (typeof b === 'number')
b = new _bnjs(b); b = new _bnjs(b.toString());
return this._add(b); return this['_' + name](b);
};
bnjs.prototype[name] = f;
}; };
bnjs.prototype._sub = _bnjs.prototype.sub; decorate('add');
decorate('sub');
bnjs.prototype.sub = function(b) { decorate('mul');
if (typeof b === 'number') decorate('mod');
b = b.toString(); decorate('div');
if (typeof b === 'number' || typeof b === 'string') decorate('cmp');
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);
};
bnjs.prototype.gt = function(b) { bnjs.prototype.gt = function(b) {
if (typeof b === 'number') if (typeof b === 'string')
b = b.toString();
if (typeof b === 'number' || typeof b === 'string')
b = new _bnjs(b); b = new _bnjs(b);
else if (typeof b === 'number')
b = new _bnjs(b.toString());
return this.cmp(b) > 0; return this.cmp(b) > 0;
}; };
bnjs.prototype.lt = function(b) { bnjs.prototype.lt = function(b) {
if (typeof b === 'number') if (typeof b === 'string')
b = b.toString();
if (typeof b === 'number' || typeof b === 'string')
b = new _bnjs(b); b = new _bnjs(b);
else if (typeof b === 'number')
b = new _bnjs(b.toString());
return this.cmp(b) < 0; return this.cmp(b) < 0;
}; };