MCPcopy Create free account
hub / github.com/ScaleML/AgentSPEX / insertNestedItem

Function insertNestedItem

yaml-flow-editor/src/utils/flow-utils.ts:159–215  ·  view source on GitHub ↗
(
  currentData: any,
  pathParts: string[],
  targetIndex: number,
  newItem: FlowNodeData
)

Source from the content-addressed store, hash-verified

157
158// Insert item at specific path and index
159export const insertNestedItem = (
160 currentData: any,
161 pathParts: string[],
162 targetIndex: number,
163 newItem: FlowNodeData
164): any => {
165 if (pathParts.length === 0 || currentData == null) return currentData;
166
167 const [head, ...tail] = pathParts;
168
169 if (!isNaN(Number(head)) && Array.isArray(currentData)) {
170 const index = Number(head);
171 const item = currentData[index];
172 const updatedItem = insertNestedItem(item, tail, targetIndex, newItem);
173 const newData = [...currentData];
174 newData[index] = updatedItem;
175 return newData;
176 }
177
178 let realKey = head;
179 let isSwitchCase = false;
180
181 if (head === 'then') realKey = 'thenSteps';
182 else if (head === 'else') realKey = 'elseSteps';
183 else if (head === 'loop') realKey = 'loopSteps';
184 else if (head === '__default__') realKey = 'defaultSteps';
185 else {
186 if (currentData.cases && Array.isArray(currentData.cases[head])) {
187 isSwitchCase = true;
188 }
189 }
190
191 if (tail.length === 0) {
192 if (isSwitchCase) {
193 const newCases = { ...currentData.cases };
194 const targetArray = [...(newCases[head] || [])];
195 targetArray.splice(targetIndex, 0, newItem);
196 newCases[head] = targetArray;
197 return { ...currentData, cases: newCases };
198 } else {
199 const targetArray = [...(currentData[realKey] || [])];
200 targetArray.splice(targetIndex, 0, newItem);
201 return { ...currentData, [realKey]: targetArray };
202 }
203 }
204
205 if (isSwitchCase) {
206 const newCases = { ...currentData.cases };
207 const array = newCases[head] || [];
208 newCases[head] = insertNestedItem(array, tail, targetIndex, newItem);
209 return { ...currentData, cases: newCases };
210 } else {
211 const array = currentData[realKey] || [];
212 const newArray = insertNestedItem(array, tail, targetIndex, newItem);
213 return { ...currentData, [realKey]: newArray };
214 }
215};

Callers 6

App.tsxFile · 0.90
SwitchContainerNodeFunction · 0.90
ParallelNodeFunction · 0.90
WhileContainerNodeFunction · 0.90
ForEachContainerNodeFunction · 0.90
IfContainerNodeFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected