|
1
|
import { BigNumberInput, BigNumberRawValue, IBigNumber } from "@medusajs/types" |
|
2
|
import { BigNumber as BigNumberConstructor } from "bignumber.js" |
|
3
|
import { isBigNumber } from "../common/is-big-number" |
|
4
|
import { isDefined } from "../common/is-defined" |
|
5
|
import { isString } from "../common/is-string" |
|
6
|
|
|
7
|
type BigNumberJS = InstanceType<typeof BigNumberConstructor> |
|
8
|
const BigNumberJS = BigNumberConstructor |
|
9
|
export class BigNumber implements IBigNumber { |
|
10
|
static DEFAULT_PRECISION = 20 |
|
11
|
|
|
12
|
private numeric_: number |
|
13
|
private raw_?: BigNumberRawValue |
|
14
|
private bignumber_?: BigNumberJS |
|
15
|
|
|
16
|
constructor( |
|
17
|
rawValue: BigNumberInput | BigNumber, |
|
18
|
options?: { precision?: number } |
|
19
|
) { |
|
20
|
this.setRawValueOrThrow(rawValue, options) |
|
21
|
} |
|
22
|
|
|
23
|
setRawValueOrThrow( |
|
24
|
rawValue: BigNumberInput | BigNumber, |
|
25
|
{ precision }: { precision?: number } = {} |
|
26
|
) { |
|
27
|
precision ??= BigNumber.DEFAULT_PRECISION |
|
28
|
|
|
29
|
if (rawValue instanceof BigNumber) { |
|
30
|
Object.assign(this, rawValue) |
|
31
|
} else if (BigNumberJS.isBigNumber(rawValue)) { |
|
32
|
/** |
|
33
|
* Example: |
|
34
|
* const bnUnitValue = new BigNumberJS("10.99") |
|
35
|
* const unitValue = new BigNumber(bnUnitValue) |
|
36
|
*/ |
|
37
|
this.numeric_ = rawValue.toNumber() |
|
38
|
this.raw_ = { |
|
39
|
value: rawValue.toPrecision(precision), |
|
40
|
precision, |
|
41
|
} |
|
42
|
this.bignumber_ = rawValue |
|
43
|
} else if (isString(rawValue)) { |
|
44
|
/** |
|
45
|
* Example: const unitValue = "1234.1234" |
|
46
|
*/ |
|
47
|
const bigNum = new BigNumberJS(rawValue) |
|
48
|
|
|
49
|
this.numeric_ = bigNum.toNumber() |
|
50
|
this.raw_ = this.raw_ = { |
|
51
|
value: bigNum.toPrecision(precision), |
|
52
|
precision, |
|
53
|
} |
|
54
|
this.bignumber_ = bigNum |
|
55
|
} else if (isBigNumber(rawValue)) { |
|
56
|
/** |
|
57
|
* Example: const unitValue = { value: "1234.1234" } |
|
58
|
*/ |
|
59
|
const definedPrecision = rawValue.precision ?? precision |
|
60
|
const bigNum = new BigNumberJS(rawValue.value) |
|
61
|
this.numeric_ = bigNum.toNumber() |
|
62
|
this.raw_ = { |
|
63
|
...rawValue, |
|
64
|
precision: definedPrecision, |
|
65
|
} |
|
66
|
this.bignumber_ = bigNum |
|
67
|
} else if (typeof rawValue === `number` && !Number.isNaN(rawValue)) { |
|
68
|
/** |
|
69
|
* Example: const unitValue = 1234 |
|
70
|
*/ |
|
71
|
this.numeric_ = rawValue as number |
|
72
|
|
|
73
|
const bigNum = new BigNumberJS(rawValue as number) |
|
74
|
this.raw_ = { |
|
75
|
value: bigNum.toPrecision(precision), |
|
76
|
precision, |
|
77
|
} |
|
78
|
this.bignumber_ = bigNum |
|
79
|
} else { |
|
80
|
throw new Error( |
|
81
|
`Invalid BigNumber value: ${rawValue}. Should be one of: string, number, BigNumber (bignumber.js), BigNumberRawValue` |
|
82
|
) |
|
83
|
} |
|
84
|
} |
|
85
|
|
|
86
|
get numeric(): number { |
|
87
|
let value: number |
|
88
|
let raw = this.raw_ as BigNumberRawValue |
|
89
|
if (raw) { |
|
90
|
value = new BigNumberJS(raw.value).toNumber() |
|
91
|
} else { |
|
92
|
value = this.numeric_ |
|
93
|
} |
|
94
|
|
|
95
|
if (Math.abs(value) <= MEDUSA_EPSILON.numeric_) { |
|
96
|
return 0 |
|
97
|
} |
|
98
|
|
|
99
|
return value |
|
100
|
} |
|
101
|
|
|
102
|
set numeric(value: BigNumberInput) { |
|
103
|
const newValue = new BigNumber(value) |
|
104
|
this.numeric_ = newValue.numeric_ |
|
105
|
this.raw_ = newValue.raw_ |
|
106
|
this.bignumber_ = newValue.bignumber_ |
|
107
|
} |
|
108
|
|
|
109
|
get raw(): BigNumberRawValue | undefined { |
|
110
|
return this.raw_ |
|
111
|
} |
|
112
|
|
|
113
|
get bigNumber(): BigNumberJS | undefined { |
|
114
|
return this.bignumber_ |
|
115
|
} |
|
116
|
|
|
117
|
set raw(rawValue: BigNumberInput) { |
|
118
|
const newValue = new BigNumber(rawValue) |
|
119
|
this.numeric_ = newValue.numeric_ |
|
120
|
this.raw_ = newValue.raw_ |
|
121
|
this.bignumber_ = newValue.bignumber_ |
|
122
|
} |
|
123
|
|
|
124
|
toJSON(): number { |
|
125
|
const value = this.bignumber_ |
|
126
|
? this.bignumber_?.toNumber() |
|
127
|
: this.raw_ |
|
128
|
? new BigNumberJS(this.raw_.value).toNumber() |
|
129
|
: this.numeric_ |
|
130
|
|
|
131
|
if (Math.abs(value) <= MEDUSA_EPSILON.numeric_) { |
|
132
|
return 0 |
|
133
|
} |
|
134
|
|
|
135
|
return value |
|
136
|
} |
|
137
|
|
|
138
|
valueOf(): number { |
|
139
|
return this.numeric |
|
140
|
} |
|
141
|
|
|
142
|
/** |
|
143
|
* Explicit string coercion. Without this, `.toString()` falls through to |
|
144
|
* `Object.prototype.toString` and returns "[object Object]", silently |
|
145
|
* breaking callers such as `parseFloat(bn.toString())` (returns `NaN`). |
|
146
|
* This is the single source of truth for string coercion — the |
|
147
|
* `[Symbol.toPrimitive]` "string" hint delegates to it so the two stay |
|
148
|
* consistent. |
|
149
|
*/ |
|
150
|
toString(): string { |
|
151
|
return `${this.raw?.value ?? this.numeric}` |
|
152
|
} |
|
153
|
|
|
154
|
[Symbol.toPrimitive](hint) { |
|
155
|
if (hint === "string") { |
|
156
|
return this.toString() |
|
157
|
} |
|
158
|
|
|
159
|
return this.numeric |
|
160
|
} |
|
161
|
} |
|
162
|
|
|
163
|
export const MEDUSA_EPSILON = new BigNumber( |
|
164
|
process.env.MEDUSA_EPSILON || "0.0001" |
|
165
|
) |
|
166
|
|
|
167
|
export const MEDUSA_DEFAULT_CURRENCY_EPSILON = new BigNumber( |
|
168
|
process.env.MEDUSA_DEFAULT_CURRENCY_EPSILON || "0.01" |
|
169
|
) |
|
170
|
|
|
171
|
export const getEpsilonFromDecimalPrecision = (decimalDigits?: number) => { |
|
172
|
if (!isDefined(decimalDigits)) { |
|
173
|
return MEDUSA_DEFAULT_CURRENCY_EPSILON |
|
174
|
} |
|
175
|
|
|
176
|
const epsilon = new BigNumberJS(1).dividedBy( |
|
177
|
new BigNumberJS(10).pow(decimalDigits) |
|
178
|
) |
|
179
|
return new BigNumber(epsilon.toString()) |
|
180
|
} |