MCPcopy
hub / github.com/reduxjs/redux-devtools / map2tree

Function map2tree

packages/map2tree/src/index.ts:45–101  ·  view source on GitHub ↗
(
  // eslint-disable-next-line @typescript-eslint/ban-types
  root: unknown,
  options: { key?: string; pushMethod?: 'push' | 'unshift' } = {},
  tree: Node = { name: options.key || 'state', children: [] }
  // eslint-disable-next-line @typescript-eslint/ban-types
)

Source from the content-addressed store, hash-verified

43}
44
45export function map2tree(
46 // eslint-disable-next-line @typescript-eslint/ban-types
47 root: unknown,
48 options: { key?: string; pushMethod?: 'push' | 'unshift' } = {},
49 tree: Node = { name: options.key || 'state', children: [] }
50 // eslint-disable-next-line @typescript-eslint/ban-types
51): Node | {} {
52 // eslint-disable-next-line @typescript-eslint/ban-types
53 if (!isPlainObject(root) && root && !(root as { toJS: () => {} }).toJS) {
54 return {};
55 }
56
57 const { key: rootNodeKey = 'state', pushMethod = 'push' } = options;
58 const currentNode = getNode(tree, rootNodeKey);
59
60 if (currentNode === null) {
61 return {};
62 }
63
64 mapValues(
65 // eslint-disable-next-line @typescript-eslint/ban-types
66 root && (root as { toJS: () => {} }).toJS
67 ? // eslint-disable-next-line @typescript-eslint/ban-types
68 (root as { toJS: () => {} }).toJS()
69 : // eslint-disable-next-line @typescript-eslint/ban-types
70 (root as {}),
71 // eslint-disable-next-line @typescript-eslint/ban-types
72 (maybeImmutable: { toJS?: () => {} }, key) => {
73 const value =
74 maybeImmutable && maybeImmutable.toJS
75 ? maybeImmutable.toJS()
76 : maybeImmutable;
77 const newNode: Node = { name: key };
78
79 if (isArray(value)) {
80 newNode.children = [];
81
82 for (let i = 0; i < value.length; i++) {
83 newNode.children[pushMethod]({
84 name: `${key}[${i}]`,
85 [isPlainObject(value[i]) ? 'object' : 'value']: value[i],
86 });
87 }
88 } else if (isPlainObject(value)) {
89 newNode.children = [];
90 } else {
91 newNode.value = value;
92 }
93
94 currentNode.children![pushMethod](newNode);
95
96 map2tree(value, { key, pushMethod }, tree);
97 }
98 );
99
100 return tree;
101}

Callers 2

map2tree.spec.tsFile · 0.90
tree.tsFile · 0.90

Calls 2

getNodeFunction · 0.85
isArrayFunction · 0.50

Tested by

no test coverage detected