Skip to content

Commit 4189aff

Browse files
line.js -> line.ts
1 parent 9b4fc4f commit 4189aff

File tree

5 files changed

+94
-72
lines changed

5 files changed

+94
-72
lines changed

src/diff/base.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ export interface DiffOptions<ValueT> {
2626

2727
// diffWords only:
2828
intlSegmenter?: Intl.Segmenter,
29+
30+
// diffLines only:
31+
stripTrailingCr?: boolean,
32+
newlineIsToken?: boolean,
33+
ignoreNewlineAtEof?: boolean,
34+
ignoreWhitespace?: boolean, // TODO: This is SORT OF supported by diffWords. What to do?
2935
}
3036

3137
/**

src/diff/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class JsonDiff extends Diff<string, string> {
1717
};
1818

1919
protected equals(left: string, right: string, options: DiffOptions<string>) {
20-
return super.equals.call(jsonDiff, left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'), options);
20+
return super.equals(left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'), options);
2121
};
2222
}
2323

src/diff/line.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/diff/line.ts

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

src/diff/word.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class WordDiff extends Diff<string, string> {
141141

142142
export const wordDiff = new WordDiff();
143143

144-
export function diffWords(oldStr, newStr, options) {
144+
export function diffWords(oldStr: string, newStr: string, options) {
145145
// This option has never been documented and never will be (it's clearer to
146146
// just call `diffWordsWithSpace` directly if you need that behavior), but
147147
// has existed in jsdiff for a long time, so we retain support for it here

0 commit comments

Comments
 (0)