UNPKG

ripple-lib-value

Version:

Classes for dealing with XRP Ledger amount values

90 lines 3.02 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bignumber_js_1 = __importDefault(require("bignumber.js")); const assert_1 = __importDefault(require("assert")); const IOUNumber = bignumber_js_1.default.clone({ ROUNDING_MODE: bignumber_js_1.default.ROUND_HALF_UP, DECIMAL_PLACES: 40 }); class Value { constructor(value) { if (new.target === Value) { throw new Error('Cannot instantiate Value directly, it is an abstract base class'); } this._value = new IOUNumber(value); } static getBNRoundDown() { return bignumber_js_1.default.ROUND_DOWN; } abs() { const result = this._value.abs(); return this._canonicalize(result); } add(addend) { assert_1.default(this.constructor === addend.constructor); const result = this._value.plus(addend._value); return this._canonicalize(result); } subtract(subtrahend) { assert_1.default(this.constructor === subtrahend.constructor); const result = this._value.minus(subtrahend._value); return this._canonicalize(result); } multiply(multiplicand) { const result = this._value.times(multiplicand._value); return this._canonicalize(result); } divide(divisor) { if (divisor.isZero()) { throw new Error('divide by zero'); } const result = this._value.dividedBy(divisor._value); return this._canonicalize(result); } invert() { const result = (new IOUNumber(this._value)).exponentiatedBy(-1); return this._canonicalize(result); } round(decimalPlaces, roundingMode) { const result = this._value.decimalPlaces(decimalPlaces, roundingMode); return this._canonicalize(result); } toFixed(decimalPlaces, roundingMode) { return this._value.toFixed(decimalPlaces, roundingMode); } getExponent() { return this._value.e; } isNaN() { return this._value.isNaN(); } isZero() { return this._value.isZero(); } isNegative() { return this._value.isNegative(); } toString() { return this._value.toString(); } greaterThan(comparator) { assert_1.default(this.constructor === comparator.constructor); return this._value.isGreaterThan(comparator._value); } lessThan(comparator) { assert_1.default(this.constructor === comparator.constructor); return this._value.isLessThan(comparator._value); } comparedTo(comparator) { assert_1.default(this.constructor === comparator.constructor); return this._value.comparedTo(comparator._value); } _canonicalize(value) { throw new Error('Subclasses must implement _canonicalize'); } } exports.Value = Value; //# sourceMappingURL=value.js.map