MCPcopy
hub / github.com/jestjs/jest / diffStrings

Function diffStrings

packages/jest-diff/src/diffStrings.ts:11–46  ·  view source on GitHub ↗
(a: string, b: string)

Source from the content-addressed store, hash-verified

9import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
10
11const diffStrings = (a: string, b: string): Array<Diff> => {
12 const isCommon = (aIndex: number, bIndex: number) => a[aIndex] === b[bIndex];
13
14 let aIndex = 0;
15 let bIndex = 0;
16 const diffs: Array<Diff> = [];
17
18 const foundSubsequence = (
19 nCommon: number,
20 aCommon: number,
21 bCommon: number,
22 ) => {
23 if (aIndex !== aCommon) {
24 diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex, aCommon)));
25 }
26 if (bIndex !== bCommon) {
27 diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex, bCommon)));
28 }
29
30 aIndex = aCommon + nCommon; // number of characters compared in a
31 bIndex = bCommon + nCommon; // number of characters compared in b
32 diffs.push(new Diff(DIFF_EQUAL, b.slice(bCommon, bIndex)));
33 };
34
35 diffSequences(a.length, b.length, isCommon, foundSubsequence);
36
37 // After the last common subsequence, push remaining change items.
38 if (aIndex !== a.length) {
39 diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex)));
40 }
41 if (bIndex !== b.length) {
42 diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex)));
43 }
44
45 return diffs;
46};
47
48export default diffStrings;

Callers 1

diffStringsRawFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected