UNPKG

3.45 kBJavaScriptView Raw
1
2
3'use strict';
4
5var _createClass = require('babel-runtime/helpers/create-class')['default'];
6
7var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];
8
9var GlobalBigNumber = require('bignumber.js');
10
11var BigNumber = GlobalBigNumber.another({
12 ROUNDING_MODE: GlobalBigNumber.ROUND_HALF_UP,
13 DECIMAL_PLACES: 40
14});
15
16var assert = require('assert');
17
18var Value = (function () {
19 function Value(value) {
20 _classCallCheck(this, Value);
21
22 if (this.constructor === 'Value') {
23 throw new Error('Cannot instantiate Value directly, it is an abstract base class');
24 }
25 this._value = new BigNumber(value);
26 }
27
28 _createClass(Value, [{
29 key: 'abs',
30 value: function abs() {
31 var result = this._value.abs();
32 return this._canonicalize(result);
33 }
34 }, {
35 key: 'add',
36 value: function add(addend) {
37 assert(this.constructor === addend.constructor);
38 var result = this._value.plus(addend._value);
39 return this._canonicalize(result);
40 }
41 }, {
42 key: 'subtract',
43 value: function subtract(subtrahend) {
44 assert(this.constructor === subtrahend.constructor);
45 var result = this._value.minus(subtrahend._value);
46 return this._canonicalize(result);
47 }
48 }, {
49 key: 'multiply',
50 value: function multiply(multiplicand) {
51 var result = this._value.times(multiplicand._value);
52 return this._canonicalize(result);
53 }
54 }, {
55 key: 'divide',
56 value: function divide(divisor) {
57 if (divisor.isZero()) {
58 throw new Error('divide by zero');
59 }
60 var result = this._value.dividedBy(divisor._value);
61 return this._canonicalize(result);
62 }
63 }, {
64 key: 'invert',
65 value: function invert() {
66 var result = new BigNumber(this._value).toPower(-1);
67 return this._canonicalize(result);
68 }
69 }, {
70 key: 'round',
71 value: function round(decimalPlaces, roundingMode) {
72 var result = this._value.round(decimalPlaces, roundingMode);
73 return this._canonicalize(result);
74 }
75 }, {
76 key: 'toFixed',
77 value: function toFixed(decimalPlaces, roundingMode) {
78 return this._value.toFixed(decimalPlaces, roundingMode);
79 }
80 }, {
81 key: 'getExponent',
82 value: function getExponent() {
83 return this._value.e;
84 }
85 }, {
86 key: 'isNaN',
87 value: function isNaN() {
88 return this._value.isNaN();
89 }
90 }, {
91 key: 'isZero',
92 value: function isZero() {
93 return this._value.isZero();
94 }
95 }, {
96 key: 'isNegative',
97 value: function isNegative() {
98 return this._value.isNegative();
99 }
100 }, {
101 key: 'toString',
102 value: function toString() {
103 return this._value.toString();
104 }
105 }, {
106 key: 'greaterThan',
107 value: function greaterThan(comparator) {
108 assert(this.constructor === comparator.constructor);
109 return this._value.greaterThan(comparator._value);
110 }
111 }, {
112 key: 'lessThan',
113 value: function lessThan(comparator) {
114 assert(this.constructor === comparator.constructor);
115 return this._value.lessThan(comparator._value);
116 }
117 }, {
118 key: 'comparedTo',
119 value: function comparedTo(comparator) {
120 assert(this.constructor === comparator.constructor);
121 return this._value.comparedTo(comparator._value);
122 }
123 }], [{
124 key: 'getBNRoundDown',
125 value: function getBNRoundDown() {
126 return BigNumber.ROUND_DOWN;
127 }
128 }]);
129
130 return Value;
131})();
132
133exports.Value = Value;
\No newline at end of file