UNPKG

divvy-lib-value

Version:

Classes for dealing with Divvy amount values

73 lines (59 loc) 2.37 kB
'use strict'; var _get = require('babel-runtime/helpers/get')['default']; var _inherits = require('babel-runtime/helpers/inherits')['default']; var _createClass = require('babel-runtime/helpers/create-class')['default']; var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default']; var Value = require('./value').Value; var XDVValue = require('./xdvvalue').XDVValue; var GlobalBigNumber = require('bignumber.js'); var BigNumber = GlobalBigNumber.another({ ROUNDING_MODE: GlobalBigNumber.ROUND_HALF_UP, DECIMAL_PLACES: 40 }); var divvyUnits = new BigNumber(1e6); var IOUValue = (function (_Value) { _inherits(IOUValue, _Value); function IOUValue(value) { var roundingMode = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; var base = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; _classCallCheck(this, IOUValue); _get(Object.getPrototypeOf(IOUValue.prototype), 'constructor', this).call(this, new BigNumber(value, base).toDigits(16, roundingMode)); } _createClass(IOUValue, [{ key: 'multiply', value: function multiply(multiplicand) { if (multiplicand instanceof XDVValue) { return _get(Object.getPrototypeOf(IOUValue.prototype), 'multiply', this).call(this, new IOUValue(multiplicand._value.times(divvyUnits))); } return _get(Object.getPrototypeOf(IOUValue.prototype), 'multiply', this).call(this, multiplicand); } }, { key: 'divide', value: function divide(divisor) { if (divisor instanceof XDVValue) { return _get(Object.getPrototypeOf(IOUValue.prototype), 'divide', this).call(this, new IOUValue(divisor._value.times(divvyUnits))); } return _get(Object.getPrototypeOf(IOUValue.prototype), 'divide', this).call(this, divisor); } }, { key: 'negate', value: function negate() { return new IOUValue(this._value.neg()); } }, { key: '_canonicalize', value: function _canonicalize(value) { if (value.isNaN()) { throw new Error('Invalid result'); } return new IOUValue(value.toPrecision(16)); } }, { key: 'equals', value: function equals(comparator) { return comparator instanceof IOUValue && this._value.equals(comparator._value); } }]); return IOUValue; })(Value); exports.IOUValue = IOUValue;