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

Function editScript

packages/effect/src/TextDiff.ts:767–787  ·  view source on GitHub ↗
(
  a: ReadonlyArray<A>,
  b: ReadonlyArray<A>,
  equals: (i: number, j: number) => boolean,
  maxEdits?: number
)

Source from the content-addressed store, hash-verified

765}
766
767const editScript = <A>(
768 a: ReadonlyArray<A>,
769 b: ReadonlyArray<A>,
770 equals: (i: number, j: number) => boolean,
771 maxEdits?: number
772): Array<Edit<A>> => {
773 const blocks = matchingBlocks(a.length, b.length, equals, maxEdits)
774 // A final empty block flushes the trailing changes
775 blocks.push({ a: a.length, b: b.length, length: 0 })
776 const out: Array<Edit<A>> = []
777 let i = 0
778 let j = 0
779 for (const block of blocks) {
780 if (block.a > i) out.push({ _tag: "Delete", items: a.slice(i, block.a) })
781 if (block.b > j) out.push({ _tag: "Insert", items: b.slice(j, block.b) })
782 if (block.length > 0) out.push({ _tag: "Equal", items: a.slice(block.a, block.a + block.length) })
783 i = block.a + block.length
784 j = block.b + block.length
785 }
786 return out
787}
788
789/**
790 * Returns ordered, merged matching runs for sequences of lengths n and m,

Callers 2

diffWithFunction · 0.85
diffStringsFunction · 0.85

Calls 3

matchingBlocksFunction · 0.85
sliceMethod · 0.80
pushMethod · 0.65

Tested by

no test coverage detected