|
| 1 | +import Diff, { DiffOptions } from './base'; |
| 2 | +import {generateOptions} from '../util/params'; |
| 3 | + |
| 4 | + |
| 5 | +class LineDiff extends Diff<string, string> { |
| 6 | + // public so it can be copied by jsonDiff |
| 7 | + public tokenize(value: string, options: DiffOptions<string>) { |
| 8 | + if(options.stripTrailingCr) { |
| 9 | + // remove one \r before \n to match GNU diff's --strip-trailing-cr behavior |
| 10 | + value = value.replace(/\r\n/g, '\n'); |
| 11 | + } |
| 12 | + |
| 13 | + let retLines = [], |
| 14 | + linesAndNewlines = value.split(/(\n|\r\n)/); |
| 15 | + |
| 16 | + // Ignore the final empty token that occurs if the string ends with a new line |
| 17 | + if (!linesAndNewlines[linesAndNewlines.length - 1]) { |
| 18 | + linesAndNewlines.pop(); |
| 19 | + } |
| 20 | + |
| 21 | + // Merge the content and line separators into single tokens |
| 22 | + for (let i = 0; i < linesAndNewlines.length; i++) { |
| 23 | + let line = linesAndNewlines[i]; |
| 24 | + |
| 25 | + if (i % 2 && !options.newlineIsToken) { |
| 26 | + retLines[retLines.length - 1] += line; |
| 27 | + } else { |
| 28 | + retLines.push(line); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return retLines; |
| 33 | + }; |
| 34 | + |
| 35 | + protected equals(left: string, right: string, options: DiffOptions<string>) { |
| 36 | + // If we're ignoring whitespace, we need to normalise lines by stripping |
| 37 | + // whitespace before checking equality. (This has an annoying interaction |
| 38 | + // with newlineIsToken that requires special handling: if newlines get their |
| 39 | + // own token, then we DON'T want to trim the *newline* tokens down to empty |
| 40 | + // strings, since this would cause us to treat whitespace-only line content |
| 41 | + // as equal to a separator between lines, which would be weird and |
| 42 | + // inconsistent with the documented behavior of the options.) |
| 43 | + if (options.ignoreWhitespace) { |
| 44 | + if (!options.newlineIsToken || !left.includes('\n')) { |
| 45 | + left = left.trim(); |
| 46 | + } |
| 47 | + if (!options.newlineIsToken || !right.includes('\n')) { |
| 48 | + right = right.trim(); |
| 49 | + } |
| 50 | + } else if (options.ignoreNewlineAtEof && !options.newlineIsToken) { |
| 51 | + if (left.endsWith('\n')) { |
| 52 | + left = left.slice(0, -1); |
| 53 | + } |
| 54 | + if (right.endsWith('\n')) { |
| 55 | + right = right.slice(0, -1); |
| 56 | + } |
| 57 | + } |
| 58 | + return super.equals(left, right, options); |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +export const lineDiff = new LineDiff(); |
| 63 | + |
| 64 | +export function diffLines(oldStr: string, newStr: string, options: DiffOptions<string>) { |
| 65 | + return lineDiff.diff(oldStr, newStr, options); |
| 66 | +} |
| 67 | + |
| 68 | +// Kept for backwards compatibility. This is a rather arbitrary wrapper method |
| 69 | +// that just calls `diffLines` with `ignoreWhitespace: true`. It's confusing to |
| 70 | +// have two ways to do exactly the same thing in the API, so we no longer |
| 71 | +// document this one (library users should explicitly use `diffLines` with |
| 72 | +// `ignoreWhitespace: true` instead) but we keep it around to maintain |
| 73 | +// compatibility with code that used old versions. |
| 74 | +export function diffTrimmedLines(oldStr, newStr, callback) { |
| 75 | + let options = generateOptions(callback, {ignoreWhitespace: true}); |
| 76 | + return lineDiff.diff(oldStr, newStr, options); |
| 77 | +} |
0 commit comments