MCPcopy Create free account
hub / github.com/RobPruzan/react-scratch / findNodeOrThrow

Function findNodeOrThrow

src/utils.ts:75–105  ·  view source on GitHub ↗
(
  eq: (node: T) => boolean,
  tree: T
)

Source from the content-addressed store, hash-verified

73
74
75export const findNodeOrThrow = <T extends { id: string; childNodes: Array<T> }>(
76 eq: (node: T) => boolean,
77 tree: T
78): T => {
79 const aux = (viewNode: T): T | undefined => {
80 for (const node of viewNode.childNodes) {
81 if (eq(node)) {
82 return node;
83 }
84
85 const res = aux(node);
86
87 if (res) {
88 return res;
89 }
90 }
91 };
92
93 if (eq(tree)) {
94 return tree;
95 }
96
97 const result = aux(tree);
98
99 if (!result) {
100 throw new Error(
101 "detached node or wrong id:" + "\n\n" + JSON.stringify(tree)
102 );
103 }
104 return result;
105};
106
107export const findNode = <T extends { id: string; childNodes: Array<T> }, R>(
108 eq: (node: T) => boolean,

Callers 1

findNodeFunction · 0.85

Calls 1

auxFunction · 0.70

Tested by

no test coverage detected