|
1
|
/** |
|
2
|
* Recursively converts BigNumber instances to plain numbers, while preserving |
|
3
|
* Dates and object/array structure. |
|
4
|
* |
|
5
|
* Retrieving or creating orders/carts computes totals, so numeric fields come |
|
6
|
* back as BigNumber instances rather than primitives. Use this in test |
|
7
|
* assertions (e.g. `expect(normalizeBigNumbers(order)).toEqual(...)`) to compare |
|
8
|
* against plain-number expectations. Dates are left intact so matchers like |
|
9
|
* `expect.any(Date)` keep working. |
|
10
|
*/ |
|
11
|
export function normalizeBigNumbers(value: any): any { |
|
12
|
if (Array.isArray(value)) { |
|
13
|
return value.map(normalizeBigNumbers) |
|
14
|
} |
|
15
|
|
|
16
|
if (value && typeof value === "object" && !(value instanceof Date)) { |
|
17
|
// Duck-type the framework BigNumber (has `numeric_` and `raw_`). |
|
18
|
if ("numeric_" in value && "raw_" in value) { |
|
19
|
return Number(value) |
|
20
|
} |
|
21
|
|
|
22
|
return Object.fromEntries( |
|
23
|
Object.keys(value).map((key) => [key, normalizeBigNumbers(value[key])]) |
|
24
|
) |
|
25
|
} |
|
26
|
|
|
27
|
return value |
|
28
|
} |