1 import { BigNumber } from "./big-number"
2
3 export function transformPropertiesToBigNumber(
4 obj,
5 {
6 prefix = "raw_",
7 include = [],
8 exclude = [],
9 }: {
10 prefix?: string
11 include?: string[]
12 exclude?: string[]
13 } = {}
14 ) {
15 const stack = [{ current: obj, path: "" }]
16
17 while (stack.length > 0) {
18 const { current, path } = stack.pop()!
19
20 if (
21 current == null ||
22 typeof current !== "object" ||
23 current instanceof BigNumber
24 ) {
25 continue
26 }
27
28 if (Array.isArray(current)) {
29 current.forEach((element, index) =>
30 stack.push({ current: element, path })
31 )
32 } else {
33 for (const key of Object.keys(current)) {
34 const value = current[key]
35 const currentPath = path ? `${path}.${key}` : key
36
37 if (value != null && !exclude.includes(currentPath)) {
38 if (key.startsWith(prefix)) {
39 const newKey = key.replace(prefix, "")
40
41 const newPath = path ? `${path}.${newKey}` : newKey
42 if (!exclude.includes(newPath)) {
43 current[newKey] = new BigNumber(value)
44 continue
45 }
46 } else if (include.includes(currentPath)) {
47 current[key] = new BigNumber(value)
48 continue
49 }
50 }
51
52 stack.push({ current: value, path: currentPath })
53 }
54 }
55 }
56 }