UNPKG

divvy-lib-value

Version:

Classes for dealing with Divvy amount values

73 lines (59 loc) 2.28 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 GlobalBigNumber = require('bignumber.js'); var BigNumber = GlobalBigNumber.another({ ROUNDING_MODE: GlobalBigNumber.ROUND_HALF_UP, DECIMAL_PLACES: 40 }); var Value = require('./value').Value; var divvyUnits = new BigNumber(1e6); var XDVValue = (function (_Value) { _inherits(XDVValue, _Value); function XDVValue(value) { _classCallCheck(this, XDVValue); _get(Object.getPrototypeOf(XDVValue.prototype), 'constructor', this).call(this, value); if (this._value.dp() > 6) { throw new Error('Value has more than 6 digits of precision past the decimal point, ' + 'an IOUValue may be being cast to an XDVValue'); } } _createClass(XDVValue, [{ key: 'multiply', value: function multiply(multiplicand) { if (multiplicand instanceof XDVValue) { return _get(Object.getPrototypeOf(XDVValue.prototype), 'multiply', this).call(this, new XDVValue(multiplicand._value.times(divvyUnits))); } return _get(Object.getPrototypeOf(XDVValue.prototype), 'multiply', this).call(this, multiplicand); } }, { key: 'divide', value: function divide(divisor) { if (divisor instanceof XDVValue) { return _get(Object.getPrototypeOf(XDVValue.prototype), 'divide', this).call(this, new XDVValue(divisor._value.times(divvyUnits))); } return _get(Object.getPrototypeOf(XDVValue.prototype), 'divide', this).call(this, divisor); } }, { key: 'negate', value: function negate() { return new XDVValue(this._value.neg()); } }, { key: '_canonicalize', value: function _canonicalize(value) { if (value.isNaN()) { throw new Error('Invalid result'); } return new XDVValue(value.round(6, BigNumber.ROUND_DOWN)); } }, { key: 'equals', value: function equals(comparator) { return comparator instanceof XDVValue && this._value.equals(comparator._value); } }]); return XDVValue; })(Value); exports.XDVValue = XDVValue;