### diff_core Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md The core diffing algorithm. It computes differences between two sequences given their starting indices and lengths, using a provided equality comparison function. ```APIDOC ## diff_core ### Description Computes a sequence of differences between two subsequences. The output is a sequence of quadruples `[sx, ex, sy, ey]`, representing ranges to delete from the first sequence and insert from the second. ### Method `Generator<[number, number, number, number]>` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **i** (number) - Required - The starting index of the first sequence. - **N** (number) - Required - The length of the first sequence slice to consider. - **j** (number) - Required - The starting index of the second sequence. - **M** (number) - Required - The length of the second sequence slice to consider. - **eq** (function) - Required - A callback function `(i: number, j: number) => boolean` that returns true if the elements at the given indices are equal. ### Request Example ```javascript // Example usage within a larger context // Assuming xs and ys are indexable and eq is defined const diffGenerator = diff_core(0, xs.length, 0, ys.length, eq); ``` ### Response #### Success Response (Generator Output) - **[sx, ex, sy, ey]** (Array) - A quadruple representing a diff operation. `[sx, ex)` is the range to delete from the first sequence, and `[sy, ey)` is the range to insert from the second sequence. Simple deletions occur when `sy === ey`, and simple insertions occur when `sx === ex`. #### Response Example ```json // Example output from the generator [0, 5, 0, 3] // Delete 5 elements from xs starting at index 0, insert 3 elements from ys starting at index 0 [7, 7, 10, 12] // Insert 2 elements from ys starting at index 10 (no deletion from xs) ``` ``` -------------------------------- ### Longest Common Subsequence (lcs) Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md The `lcs` function computes the longest common subsequence between two indexable sequences. It internally uses the `diff` function and processes its output to return triples representing the start indices and length of common substrings. ```typescript declare function lcs(xs: T, ys: T, eq?: (i: number, j: number) => boolean): Generator<[number, number, number]>; ``` -------------------------------- ### Patch Application (applyPatch) Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md The `applyPatch` function takes a source sequence and a patch generated by `calcPatch` to reconstruct the target sequence. It outputs a stream of chunks, alternating between the source sequence and the patch data, enabling memory-efficient stream processing. ```typescript declare function applyPatch(xs: T, patch: Iterable<[number, number, T]>): Generator; ``` -------------------------------- ### diff Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md A convenient wrapper around `diff_core` that automatically handles common affixes and calculates necessary parameters. ```APIDOC ## diff ### Description Calculates the differences between two indexable sequences `xs` and `ys`. This function optimizes by first checking for common prefixes and suffixes before calling the core diffing algorithm. ### Method `Generator<[number, number, number, number]>` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **xs** (Indexable) - Required - The first indexable sequence (string, array, TypedArray, GenericIndexable). - **ys** (Indexable) - Required - The second indexable sequence. - **eq** (function) - Optional - A callback function `(i: number, j: number) => boolean` for custom element comparison. If not provided, default equality is used. ### Request Example ```javascript const xs = "abcdefg"; const ys = "abXdefYg"; const diffGenerator = diff(xs, ys); for (const [sx, ex, sy, ey] of diffGenerator) { console.log(`Delete [${sx}, ${ex}), Insert [${sy}, ${ey})`); } ``` ### Response #### Success Response (Generator Output) - **[sx, ex, sy, ey]** (Array) - A quadruple representing a diff operation. `[sx, ex)` is the range to delete from `xs`, and `[sy, ey)` is the range to insert from `ys`. #### Response Example ```json // Example output for diff("abcdefg", "abXdefYg") [2, 3, 2, 3] // Delete 'c' at index 2, insert 'X' at index 2 [6, 7, 6, 7] // Delete 'g' at index 6, insert 'Y' at index 6 ``` ``` -------------------------------- ### Core Diffing Algorithm (diff_core) Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md The `diff_core` function is the heart of the library, calculating differences between two sequences based on provided lengths and an equality comparison function. It yields ranges to delete from the first sequence and insert from the second. Direct sequence access is not required, enhancing efficiency and flexibility. ```typescript declare function diff_core(i: number, N: number, j: number, M: number, eq: (i: number, j: number) => boolean): Generator<[number, number, number, number]>; ``` -------------------------------- ### calcPatch Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md Generates a patch that can transform one sequence into another. ```APIDOC ## calcPatch ### Description Calculates a patch required to transform sequence `xs` into sequence `ys`. The patch consists of operations specifying deletions and insertions. ### Method `Generator<[number, number, T]>` where `T` is the type of `xs` and `ys`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **xs** (Sliceable) - Required - The source sequence. - **ys** (Sliceable) - Required - The target sequence. - **eq** (function) - Optional - A callback function `(i: number, j: number) => boolean` for custom element comparison. ### Request Example ```javascript const xs = "the quick brown fox"; const ys = "the slow brown cat"; const patchGenerator = calcPatch(xs, ys); const patch = Array.from(patchGenerator); console.log(patch); ``` ### Response #### Success Response (Generator Output) - **[sx, ex, slice]** (Array) - A triple representing a patch operation. `sx` is the starting index in `xs` to be replaced, `ex` is the ending index in `xs` to be replaced, and `slice` is the data from `ys` to be inserted. Pure insertions occur when `sx === ex`. #### Response Example ```json // Example output for calcPatch("the quick brown fox", "the slow brown cat") [4, 9, "slow"] // Replace "quick" (indices 4-9) with "slow" [16, 19, "cat"] // Replace "fox" (indices 16-19) with "cat" ``` ``` -------------------------------- ### Patch Calculation (calcPatch) Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md The `calcPatch` function generates a patch to transform one sequence into another. It wraps the `diff` function and replaces deletion ranges with corresponding slices from the target sequence. This allows for reconstituting the target sequence from the source sequence and the patch. ```typescript declare function calcPatch(xs: T, ys: T, eq?: (i: number, j: number) => boolean): Generator<[number, number, T]>; ``` -------------------------------- ### applyPatch Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md Applies a generated patch to a sequence to reconstruct the target sequence. ```APIDOC ## applyPatch ### Description Applies a patch (generated by `calcPatch`) to a source sequence `xs` to reconstruct the target sequence. The output is a generator yielding chunks of the reconstructed sequence. ### Method `Generator` where `T` is the type of `xs`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **xs** (Sliceable) - Required - The source sequence. - **patch** (Iterable<[number, number, T]>) - Required - The patch data generated by `calcPatch`. ### Request Example ```javascript const xs = "the quick brown fox"; const patch = [[4, 9, "slow"], [16, 19, "cat"]]; // Example patch const reconstructedGenerator = applyPatch(xs, patch); let result = ""; for (const chunk of reconstructedGenerator) { result += chunk; } console.log(result); // Output: "the slow brown cat" ``` ### Response #### Success Response (Generator Output) - **chunk** (T) - A chunk of the reconstructed sequence. The generator yields alternating chunks from the original sequence `xs` and the insertion data from the `patch`. #### Response Example ```json // Example output chunks for applyPatch("the quick brown fox", patch) "the " "slow" " brown " "cat" ``` ``` -------------------------------- ### Sequence Diffing (diff) Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md The `diff` function is a convenient wrapper around `diff_core`. It automatically handles common prefix and suffix calculations, reducing overhead. It also determines the necessary parameters (i, N, j, M, eq) for `diff_core` if not explicitly provided. ```typescript declare function diff(xs: T, ys: T, eq?: (i: number, j: number) => boolean): Generator<[number, number, number, number]>; ``` -------------------------------- ### Apply Patch with fast-myers-diff (JavaScript) Source: https://context7.com/gliese1337/fast-myers-diff/llms.txt Applies a patch generated by `calcPatch` to reconstruct a target sequence. Returns a generator of chunks for streaming, avoiding upfront memory allocation. Requires `fast-myers-diff`. ```javascript import { calcPatch, applyPatch } from 'fast-myers-diff'; // Create and apply a patch const source = "Hello World"; const target = "Hello Beautiful World"; // Generate patch const patch = [...calcPatch(source, target)]; console.log("Generated patch:", patch); // Output: Generated patch: [ [ 5, 5, ' Beautiful' ] ] // Apply patch to reconstruct target const chunks = [...applyPatch(source, patch)]; const reconstructed = chunks.join(''); console.log(`Reconstructed: "${reconstructed}"`); console.log(`Matches target: ${reconstructed === target}`); // Output: // Reconstructed: "Hello Beautiful World" // Matches target: true // Streaming large file processing function* processLargeFile(original, patch) { for (const chunk of applyPatch(original, patch)) { yield chunk; // Process chunk by chunk without loading entire result } } ``` -------------------------------- ### Generate Patch Operations with calcPatch (JavaScript/TypeScript) Source: https://context7.com/gliese1337/fast-myers-diff/llms.txt Generates patch operations to transform a source sequence into a target sequence. It returns tuples `[deleteStart, deleteEnd, insertSlice]` where `insertSlice` is the content from the target to be inserted. This function works with both strings and arrays. ```javascript import { calcPatch } from 'fast-myers-diff'; // Generate a patch for strings const original = "The quick brown fox"; const modified = "The slow brown dog"; const patches = [...calcPatch(original, modified)]; console.log("Patches to apply:"); for (const [delStart, delEnd, insert] of patches) { console.log(` At ${delStart}: delete ${delEnd - delStart} chars, insert "${insert}"`); } // Output: // Patches to apply: // At 4: delete 5 chars, insert "slow" // At 16: delete 3 chars, insert "dog" // Works with arrays too const arrOriginal = ['apple', 'banana', 'cherry']; const arrModified = ['apple', 'blueberry', 'cherry', 'date']; for (const [delStart, delEnd, insert] of calcPatch(arrOriginal, arrModified)) { console.log(`Array patch: delete [${delStart},${delEnd}), insert [${insert.join(', ')}]`); } // Output: // Array patch: delete [1,2), insert [blueberry] // Array patch: delete [3,3), insert [date] ``` -------------------------------- ### calcSlices Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md Calculates slices representing the differences or similarities between two sequences. ```APIDOC ## calcSlices ### Description Compares two sequences `xs` and `ys` and generates slices indicating whether a segment is common (0), deleted from `xs` (-1), or inserted into `ys` (1). ### Method `Generator<[number | 1 | 0 | -1, S]>` where `S` is the type of `xs` and `ys`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **xs** (S) - Required - The first sequence. - **ys** (S) - Required - The second sequence. - **eq** (Comparator) - Optional - A comparator function for custom element comparison. ### Request Example ```javascript const xs = [1, 2, 3, 4]; const ys = [1, 3, 4, 5]; const slicesGenerator = calcSlices(xs, ys); for (const [type, slice] of slicesGenerator) { console.log(`Type: ${type}, Slice: ${slice}`); } ``` ### Response #### Success Response (Generator Output) - **[type, slice]** (Array) - A pair where `type` is -1 (deletion), 0 (common), or 1 (insertion), and `slice` is the corresponding segment from `xs` or `ys`. #### Response Example ```json // Example output for calcSlices([1, 2, 3, 4], [1, 3, 4, 5]) [0, [1]] // Common: [1] [-1, [2]] // Deleted from xs: [2] [0, [3, 4]] // Common: [3, 4] [1, [5]] // Inserted into ys: [5] ``` ``` -------------------------------- ### Sequence Slicing Calculation (calcSlices) Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md The `calcSlices` function determines how to transform one sequence into another by identifying commonalities and differences. It returns a generator yielding tuples indicating whether a section is common (0), needs to be deleted from the source (-1), or needs to be inserted into the source (1), along with the relevant slice. ```typescript declare function calcSlices>(xs: S, ys: S, eq?: Comparator): Generator<[-1 | 0 | 1, S]>; ``` -------------------------------- ### Perform Low-Level Diff with Custom Equality using diff_core (JavaScript/TypeScript) Source: https://context7.com/gliese1337/fast-myers-diff/llms.txt The low-level core diff function that operates on abstract indices with a custom equality callback. This function is useful when full control over comparison logic is needed or when working with non-standard data structures that are not directly indexable. ```javascript import { diff_core } from 'fast-myers-diff'; // Compare two segments starting at different offsets const data1 = "prefix_ABC_suffix"; const data2 = "other_AXC_end"; // Compare only the middle portions: "ABC" (indices 7-10) vs "AXC" (indices 6-9) const eq = (i, j) => data1[i] === data2[j]; for (const [sx, ex, sy, ey] of diff_core(7, 3, 6, 3, eq)) { console.log(`Diff: delete [${sx}, ${ex}), insert [${sy}, ${ey})`); console.log(` "${data1.slice(sx, ex)}" -> "${data2.slice(sy, ey)}"`); } // Output: // Diff: delete [8, 9), insert [7, 8) // "B" -> "X" ``` -------------------------------- ### Compute Differences Between Sequences with diff (JavaScript/TypeScript) Source: https://context7.com/gliese1337/fast-myers-diff/llms.txt The main entry point for computing differences between two sequences (strings or arrays). It returns a generator yielding differences as ranges to delete from the source and insert from the target. It automatically handles common prefix/suffix elimination for performance. ```javascript import { diff } from 'fast-myers-diff'; // Basic string diff const source = "hello world"; const target = "hello there world"; for (const [delStart, delEnd, insStart, insEnd] of diff(source, target)) { console.log(`Delete [${delStart}, ${delEnd}): "${source.slice(delStart, delEnd)}"`); console.log(`Insert [${insStart}, ${insEnd}): "${target.slice(insStart, insEnd)}"`); } // Output: // Delete [5, 5): "" // Insert [5, 11): " there" // Array diff with custom equality const arr1 = [{ id: 1, val: 'a' }, { id: 2, val: 'b' }, { id: 3, val: 'c' }]; const arr2 = [{ id: 1, val: 'a' }, { id: 4, val: 'd' }, { id: 3, val: 'c' }]; const eq = (i, j) => arr1[i].id === arr2[j].id; for (const [sx, ex, sy, ey] of diff(arr1, arr2, eq)) { console.log(`Replace items ${sx}-${ex} with items ${sy}-${ey}`); } // Output: Replace items 1-2 with items 1-2 ``` -------------------------------- ### lcs Source: https://github.com/gliese1337/fast-myers-diff/blob/master/README.md Computes the Longest Common Subsequence (LCS) between two indexable sequences. ```APIDOC ## lcs ### Description Calculates the Longest Common Subsequence (LCS) between two indexable sequences `xs` and `ys`. It returns a generator yielding triples representing common substrings. ### Method `Generator<[number, number, number]>` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **xs** (Indexable) - Required - The first indexable sequence. - **ys** (Indexable) - Required - The second indexable sequence. - **eq** (function) - Optional - A callback function `(i: number, j: number) => boolean` for custom element comparison. ### Request Example ```javascript const xs = [1, 2, 3, 4, 5]; const ys = [1, 3, 4, 6]; const lcsGenerator = lcs(xs, ys); for (const [sx, sy, l] of lcsGenerator) { console.log(`Common substring starts at xs[${sx}], ys[${sy}] with length ${l}`); } ``` ### Response #### Success Response (Generator Output) - **[sx, sy, l]** (Array) - A triple representing a common substring. `sx` is the starting index in `xs`, `sy` is the starting index in `ys`, and `l` is the length of the common substring. #### Response Example ```json // Example output for lcs([1, 2, 3, 4, 5], [1, 3, 4, 6]) [0, 0, 1] // Common substring "1" starting at xs[0] and ys[0] [2, 1, 2] // Common substring "3, 4" starting at xs[2] and ys[1] ``` ``` -------------------------------- ### Calculate Diff Slices with fast-myers-diff (JavaScript) Source: https://context7.com/gliese1337/fast-myers-diff/llms.txt Produces a breakdown of two sequences into labeled segments for diff visualization. Returns tuples `[type, slice]` where type indicates deletion (-1), common (0), or insertion (1). Requires `fast-myers-diff`. ```javascript import { calcSlices } from 'fast-myers-diff'; // Generate diff visualization data const oldText = "The quick fox jumps"; const newText = "The slow fox leaps"; console.log("Diff breakdown:"); for (const [type, slice] of calcSlices(oldText, newText)) { const label = type === -1 ? 'DELETE' : type === 1 ? 'INSERT' : 'KEEP'; console.log(` [${label}] "${slice}"`); } // Output: // Diff breakdown: // [KEEP] "The " // [DELETE] "quick" // [INSERT] "slow" // [KEEP] " fox " // [DELETE] "jumps" // [INSERT] "leaps" // Build HTML diff output function renderDiff(oldStr, newStr) { const parts = []; for (const [type, slice] of calcSlices(oldStr, newStr)) { if (type === -1) parts.push(`${slice}`); else if (type === 1) parts.push(`${slice}`); else parts.push(slice); } return parts.join(''); } console.log(renderDiff("Hello World", "Hello There World")); // Output: Hello There World ``` -------------------------------- ### Compute Longest Common Subsequence with lcs (JavaScript/TypeScript) Source: https://context7.com/gliese1337/fast-myers-diff/llms.txt Computes the Longest Common Subsequence (LCS) between two sequences. It returns a generator yielding triples `[sx, sy, length]` representing aligned common segments. This is useful for identifying shared parts between sequences. ```javascript import { lcs } from 'fast-myers-diff'; // Find common subsequences between strings const source = "ABCDEFG"; const target = "ACDEG"; const commonParts = []; for (const [sourceIdx, targetIdx, length] of lcs(source, target)) { const segment = source.slice(sourceIdx, sourceIdx + length); commonParts.push(segment); console.log(`Common: "${segment}" at source[${sourceIdx}], target[${targetIdx}], length=${length}`); } // Output: // Common: "A" at source[0], target[0], length=1 // Common: "CDE" at source[2], target[1], length=3 // Common: "G" at source[6], target[4], length=1 console.log(`LCS: "${commonParts.join('')}"`); // Output: LCS: "ACDEG" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.