1 | import Decimal from 'decimal.js'
|
2 | import { factory } from '../../utils/factory'
|
3 |
|
4 | const name = 'BigNumber'
|
5 | const dependencies = ['?on', 'config']
|
6 |
|
7 | export const createBigNumberClass = factory(name, dependencies, ({ on, config }) => {
|
8 | const BigNumber = Decimal.clone({ precision: config.precision })
|
9 |
|
10 | |
11 |
|
12 |
|
13 | BigNumber.prototype.type = 'BigNumber'
|
14 | BigNumber.prototype.isBigNumber = true
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | BigNumber.prototype.toJSON = function () {
|
23 | return {
|
24 | mathjs: 'BigNumber',
|
25 | value: this.toString()
|
26 | }
|
27 | }
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | BigNumber.fromJSON = function (json) {
|
36 | return new BigNumber(json.value)
|
37 | }
|
38 |
|
39 | if (on) {
|
40 |
|
41 | on('config', function (curr, prev) {
|
42 | if (curr.precision !== prev.precision) {
|
43 | BigNumber.config({ precision: curr.precision })
|
44 | }
|
45 | })
|
46 | }
|
47 |
|
48 | return BigNumber
|
49 | }, { isClass: true })
|