codekingpro/portable-devtools
115k
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 if (typeof b !== "function" && b !== null)
11 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12 extendStatics(d, b);
13 function __() { this.constructor = d; }
14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 };
16})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.lineDiff = void 0;
19exports.diffLines = diffLines;
20exports.diffTrimmedLines = diffTrimmedLines;
21exports.tokenize = tokenize;
22var base_js_1 = require("./base.js");
23var params_js_1 = require("../util/params.js");
24var LineDiff = /** @class */ (function (_super) {
25 __extends(LineDiff, _super);
26 function LineDiff() {
27 var _this = _super !== null && _super.apply(this, arguments) || this;
28 _this.tokenize = tokenize;
29 return _this;
30 }
31 LineDiff.prototype.equals = function (left, right, options) {
32 // If we're ignoring whitespace, we need to normalise lines by stripping
33 // whitespace before checking equality. (This has an annoying interaction
34 // with newlineIsToken that requires special handling: if newlines get their
35 // own token, then we DON'T want to trim the *newline* tokens down to empty
36 // strings, since this would cause us to treat whitespace-only line content
37 // as equal to a separator between lines, which would be weird and
38 // inconsistent with the documented behavior of the options.)
39 if (options.ignoreWhitespace) {
40 if (!options.newlineIsToken || !left.includes('\n')) {
41 left = left.trim();
42 }
43 if (!options.newlineIsToken || !right.includes('\n')) {
44 right = right.trim();
45 }
46 }
47 else if (options.ignoreNewlineAtEof && !options.newlineIsToken) {
48 if (left.endsWith('\n')) {
49 left = left.slice(0, -1);
50 }
51 if (right.endsWith('\n')) {
52 right = right.slice(0, -1);
53 }
54 }
55 return _super.prototype.equals.call(this, left, right, options);
56 };
57 return LineDiff;
58}(base_js_1.default));
59exports.lineDiff = new LineDiff();
60function diffLines(oldStr, newStr, options) {
61 return exports.lineDiff.diff(oldStr, newStr, options);
62}
63function diffTrimmedLines(oldStr, newStr, options) {
64 options = (0, params_js_1.generateOptions)(options, { ignoreWhitespace: true });
65 return exports.lineDiff.diff(oldStr, newStr, options);
66}
67// Exported standalone so it can be used from jsonDiff too.
68function tokenize(value, options) {
69 if (options.stripTrailingCr) {
70 // remove one \r before \n to match GNU diff's --strip-trailing-cr behavior
71 value = value.replace(/\r\n/g, '\n');
72 }
73 var retLines = [], linesAndNewlines = value.split(/(\n|\r\n)/);
74 // Ignore the final empty token that occurs if the string ends with a new line
75 if (!linesAndNewlines[linesAndNewlines.length - 1]) {
76 linesAndNewlines.pop();
77 }
78 // Merge the content and line separators into single tokens
79 for (var i = 0; i < linesAndNewlines.length; i++) {
80 var line = linesAndNewlines[i];
81 if (i % 2 && !options.newlineIsToken) {
82 retLines[retLines.length - 1] += line;
83 }
84 else {
85 retLines.push(line);
86 }
87 }
88 return retLines;
89}
90 