mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 04:05:52 +00:00
[CHORE] Add Amount#invert mathematical utility function.
This commit is contained in:
@@ -159,6 +159,29 @@ Amount.prototype.add = function (v) {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn this amount into its inverse.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
Amount.prototype._invert = function () {
|
||||||
|
this._value = consts.bi_1e32.divide(this._value);
|
||||||
|
this._offset = -32 - this._offset;
|
||||||
|
this.canonicalize();
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the inverse of this amount.
|
||||||
|
*
|
||||||
|
* @return {Amount} New Amount object with same currency and issuer, but the
|
||||||
|
* inverse of the value.
|
||||||
|
*/
|
||||||
|
Amount.prototype.invert = function () {
|
||||||
|
return this.copy()._invert();
|
||||||
|
};
|
||||||
|
|
||||||
Amount.prototype.canonicalize = function () {
|
Amount.prototype.canonicalize = function () {
|
||||||
if (!(this._value instanceof BigInteger)) {
|
if (!(this._value instanceof BigInteger)) {
|
||||||
// NaN.
|
// NaN.
|
||||||
|
|||||||
@@ -512,4 +512,16 @@ describe('Amount', function() {
|
|||||||
assert.strictEqual(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').ratio_human(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'), {reference_date: 443845330 + 31535000}).to_text_full(), '201.0049931765529/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
assert.strictEqual(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').ratio_human(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'), {reference_date: 443845330 + 31535000}).to_text_full(), '201.0049931765529/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('_invert', function() {
|
||||||
|
it('Invert 1', function () {
|
||||||
|
assert.strictEqual(Amount.from_json('1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').invert().to_text_full(), '1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||||
|
});
|
||||||
|
it('Invert 20', function () {
|
||||||
|
assert.strictEqual(Amount.from_json('20/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').invert().to_text_full(), '0.05/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||||
|
});
|
||||||
|
it('Invert 0.02', function () {
|
||||||
|
assert.strictEqual(Amount.from_json('0.02/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').invert().to_text_full(), '50/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user