Skip to content

Type Editor


Type Editor / @type-editor/changeset / myers-diff/run-myers-diff / runMyersDiff

Function: runMyersDiff()

ts
function runMyersDiff<T>(
  tokensA,
  tokensB,
  range,
  compareTokens,
  changeRange,
): Change<any>[];

Defined in: myers-diff/run-myers-diff.ts:40

Run Myers' diff algorithm to find the optimal sequence of changes.

Myers' algorithm uses dynamic programming to find the shortest edit script between two sequences. It explores diagonals in the edit graph, maintaining a frontier of the furthest reaching paths.

See: https://neil.fraser.name/writing/diff/myers.pdf See: https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
tokensAT[]The first token sequence.
tokensBT[]The second token sequence.
rangeTrimmedRangeThe trimmed range to process.
compareTokens(a, b) => booleanThe function to compare tokens for equality.
changeRangeChangeThe original change range for creating result changes.

Returns

Change<any>[]

An array of changes, or null if the algorithm exceeded MAX_DIFF_SIZE.