MCPcopy Create free account
hub / github.com/Effect-TS/effect / resolveParent

Function resolveParent

packages/effect/src/JsonPatch.ts:362–395  ·  view source on GitHub ↗
(
  doc: Schema.Json,
  pointer: string
)

Source from the content-addressed store, hash-verified

360// Walk to the parent of `pointer`, recording the path.
361// Returns null if the parent path cannot be resolved.
362function resolveParent(
363 doc: Schema.Json,
364 pointer: string
365): { readonly stack: ReadonlyArray<StackEntry>; readonly parent: unknown; readonly lastToken: string } | null {
366 const tokens = tokenize(pointer)
367 if (tokens.length === 0) return null // caller handles root
368
369 const lastToken = tokens[tokens.length - 1]
370 const stack: Array<StackEntry> = []
371 let cur: unknown = doc
372
373 for (let i = 0; i < tokens.length - 1; i++) {
374 const token = tokens[i]
375
376 if (Array.isArray(cur)) {
377 const idx = toIndex(token)
378 if (idx >= cur.length) return null
379 stack.push({ container: cur, token: idx })
380 cur = cur[idx]
381 continue
382 }
383
384 if (isJsonObject(cur)) {
385 if (!Object.hasOwn(cur, token)) return null
386 stack.push({ container: cur, token })
387 cur = cur[token]
388 continue
389 }
390
391 return null
392 }
393
394 return { stack, parent: cur, lastToken }
395}
396
397// Rebuild the document by writing `newParent` back through `stack`.
398function rebuildFromStack(stack: ReadonlyArray<StackEntry>, newParent: Schema.Json): Schema.Json {

Callers 1

applyOperationFunction · 0.85

Calls 4

toIndexFunction · 0.85
pushMethod · 0.80
tokenizeFunction · 0.70
isJsonObjectFunction · 0.70

Tested by

no test coverage detected