Skip to content

Commit cb03e0a

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

File tree

5 files changed

+85
-72
lines changed

5 files changed

+85
-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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

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)