UNPKG

2.38 kBJavaScriptView Raw
1
2
3'use strict';
4
5var _get = require('babel-runtime/helpers/get')['default'];
6
7var _inherits = require('babel-runtime/helpers/inherits')['default'];
8
9var _createClass = require('babel-runtime/helpers/create-class')['default'];
10
11var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];
12
13var Value = require('./value').Value;
14var XPAValue = require('./xpavalue').XPAValue;
15var GlobalBigNumber = require('bignumber.js');
16var BigNumber = GlobalBigNumber.another({
17 ROUNDING_MODE: GlobalBigNumber.ROUND_HALF_UP,
18 DECIMAL_PLACES: 40
19});
20var bluzelleUnits = new BigNumber(1e6);
21
22var IOUValue = (function (_Value) {
23 _inherits(IOUValue, _Value);
24
25 function IOUValue(value) {
26 var roundingMode = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
27 var base = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
28
29 _classCallCheck(this, IOUValue);
30
31 _get(Object.getPrototypeOf(IOUValue.prototype), 'constructor', this).call(this, new BigNumber(value, base).toDigits(16, roundingMode));
32 }
33
34 _createClass(IOUValue, [{
35 key: 'multiply',
36 value: function multiply(multiplicand) {
37 if (multiplicand instanceof XPAValue) {
38 return _get(Object.getPrototypeOf(IOUValue.prototype), 'multiply', this).call(this, new IOUValue(multiplicand._value.times(bluzelleUnits)));
39 }
40 return _get(Object.getPrototypeOf(IOUValue.prototype), 'multiply', this).call(this, multiplicand);
41 }
42 }, {
43 key: 'divide',
44 value: function divide(divisor) {
45 if (divisor instanceof XPAValue) {
46 return _get(Object.getPrototypeOf(IOUValue.prototype), 'divide', this).call(this, new IOUValue(divisor._value.times(bluzelleUnits)));
47 }
48 return _get(Object.getPrototypeOf(IOUValue.prototype), 'divide', this).call(this, divisor);
49 }
50 }, {
51 key: 'negate',
52 value: function negate() {
53 return new IOUValue(this._value.neg());
54 }
55 }, {
56 key: '_canonicalize',
57 value: function _canonicalize(value) {
58 if (value.isNaN()) {
59 throw new Error('Invalid result');
60 }
61 return new IOUValue(value.toPrecision(16));
62 }
63 }, {
64 key: 'equals',
65 value: function equals(comparator) {
66 return comparator instanceof IOUValue && this._value.equals(comparator._value);
67 }
68 }]);
69
70 return IOUValue;
71})(Value);
72
73exports.IOUValue = IOUValue;
\No newline at end of file