( aLines: string[], bLines: string[], )
| 20 | * {@link https://github.com/jonTrent/PatienceDiff} |
| 21 | */ |
| 22 | export function patienceDiff( |
| 23 | aLines: string[], |
| 24 | bLines: string[], |
| 25 | ): { |
| 26 | lines: { |
| 27 | line: string; |
| 28 | aIndex: number; |
| 29 | bIndex: number; |
| 30 | }[]; |
| 31 | lineCountDeleted: number; |
| 32 | lineCountInserted: number; |
| 33 | } { |
| 34 | /** |
| 35 | * Finds all unique values in lines[start...end], inclusive. This |
| 36 | * function is used in preparation for determining the longest common |
| 37 | * subsequence. |
| 38 | * |
| 39 | * @param lines - The array to search |
| 40 | * @param start - The starting index (inclusive) |
| 41 | * @param end - The ending index (inclusive) |
| 42 | * @returns A map of the unique lines to their index |
| 43 | */ |
| 44 | function findUnique(lines: string[], start: number, end: number) { |
| 45 | const lineMap = new Map<string, {count: number; index: number}>(); |
| 46 | for (let i = start; i <= end; i++) { |
| 47 | const line = lines[i]; |
| 48 | const data = lineMap.get(line); |
| 49 | if (data) { |
| 50 | data.count++; |
| 51 | data.index = i; |
| 52 | } else { |
| 53 | lineMap.set(line, {count: 1, index: i}); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | const newMap = new Map<string, number>(); |
| 58 | for (const [key, value] of lineMap) { |
| 59 | if (value.count === 1) { |
| 60 | newMap.set(key, value.index); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return newMap; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Finds all the unique common entries between aArray[aStart...aEnd] and |
| 69 | * bArray[bStart...bEnd], inclusive. This function uses findUnique to pare |
| 70 | * down the aArray and bArray ranges first, before then walking the |
| 71 | * comparison between the two arrays. |
| 72 | * |
| 73 | * |
| 74 | * @param aArray - The original array |
| 75 | * @param aStart - The start of the original array to search |
| 76 | * @param aEnd - The end of the original array to search, inclusive |
| 77 | * @param bArray - The new array |
| 78 | * @param bStart - the start of the new array to search |
| 79 | * @param bEnd - The end of the new array to search, inclusive |
no test coverage detected