| 299 | } |
| 300 | |
| 301 | function deleteJsonPathValue(root: unknown, tokens: PathToken[]): void { |
| 302 | if (tokens.length === 0) { |
| 303 | throw new Error("JSON delete path must not be empty"); |
| 304 | } |
| 305 | let current = root as Record<string, unknown> | unknown[]; |
| 306 | for (let index = 0; index < tokens.length - 1; index++) { |
| 307 | const token = tokens[index]; |
| 308 | const next = |
| 309 | typeof token === "number" |
| 310 | ? (current as unknown[])[token] |
| 311 | : (current as Record<string, unknown>)[token]; |
| 312 | current = next as Record<string, unknown> | unknown[]; |
| 313 | if (current === undefined) { |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | const finalToken = tokens[tokens.length - 1]; |
| 318 | if (typeof finalToken === "number") { |
| 319 | if (Array.isArray(current) && finalToken < current.length) { |
| 320 | current.splice(finalToken, 1); |
| 321 | } |
| 322 | } else if (current && typeof current === "object") { |
| 323 | delete (current as Record<string, unknown>)[finalToken]; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | async function buildTreeNode( |
| 328 | path: string, |