MCPcopy Index your code
hub / github.com/modelcontextprotocol/inspector / updateArray

Function updateArray

client/src/utils/jsonUtils.ts:142–184  ·  view source on GitHub ↗

* Updates an array at a specific path

(
  array: JsonValue[],
  path: string[],
  value: JsonValue,
)

Source from the content-addressed store, hash-verified

140 * Updates an array at a specific path
141 */
142function updateArray(
143 array: JsonValue[],
144 path: string[],
145 value: JsonValue,
146): JsonValue[] {
147 const [index, ...restPath] = path;
148 const arrayIndex = Number(index);
149
150 if (isNaN(arrayIndex)) {
151 console.error(`Invalid array index: ${index}`);
152 return array;
153 }
154
155 if (arrayIndex < 0) {
156 console.error(`Array index out of bounds: ${arrayIndex} < 0`);
157 return array;
158 }
159
160 let newArray: JsonValue[] = [];
161 for (let i = 0; i < array.length; i++) {
162 newArray[i] = i in array ? array[i] : null;
163 }
164
165 if (arrayIndex >= newArray.length) {
166 const extendedArray: JsonValue[] = new Array(arrayIndex).fill(null);
167 // Copy over the existing elements (now guaranteed to be dense)
168 for (let i = 0; i < newArray.length; i++) {
169 extendedArray[i] = newArray[i];
170 }
171 newArray = extendedArray;
172 }
173
174 if (restPath.length === 0) {
175 newArray[arrayIndex] = value;
176 } else {
177 newArray[arrayIndex] = updateValueAtPath(
178 newArray[arrayIndex],
179 restPath,
180 value,
181 );
182 }
183 return newArray;
184}
185
186/**
187 * Updates an object at a specific path

Callers 1

updateValueAtPathFunction · 0.85

Calls 1

updateValueAtPathFunction · 0.85

Tested by

no test coverage detected