feat: Complete zCode CLI X with Telegram bot integration

- Add full Telegram bot functionality with Z.AI API integration
- Implement 4 tools: Bash, FileEdit, WebSearch, Git
- Add 3 agents: Code Reviewer, Architect, DevOps Engineer
- Add 6 skills for common coding tasks
- Add systemd service file for 24/7 operation
- Add nginx configuration for HTTPS webhook
- Add comprehensive documentation
- Implement WebSocket server for real-time updates
- Add logging system with Winston
- Add environment validation

🤖 zCode CLI X - Agentic coder with Z.AI + Telegram integration
This commit is contained in:
admin
2026-05-05 09:01:26 +00:00
Unverified
parent 4a7035dd92
commit 875c7f9b91
24688 changed files with 3224957 additions and 221 deletions

View File

@@ -0,0 +1,19 @@
import Diff from './base.js';
import type { ChangeObject, DiffArraysOptionsNonabortable, CallbackOptionNonabortable, DiffArraysOptionsAbortable, DiffCallbackNonabortable, CallbackOptionAbortable } from '../types.js';
declare class ArrayDiff<T> extends Diff<T, Array<T>> {
tokenize(value: Array<T>): T[];
join(value: Array<T>): T[];
removeEmpty(value: Array<T>): T[];
}
export declare const arrayDiff: ArrayDiff<unknown>;
/**
* diffs two arrays of tokens, comparing each item for strict equality (===).
* @returns a list of change objects.
*/
export declare function diffArrays<T>(oldArr: T[], newArr: T[], options: DiffCallbackNonabortable<T[]>): undefined;
export declare function diffArrays<T>(oldArr: T[], newArr: T[], options: DiffArraysOptionsAbortable<T> & CallbackOptionAbortable<T[]>): undefined;
export declare function diffArrays<T>(oldArr: T[], newArr: T[], options: DiffArraysOptionsNonabortable<T> & CallbackOptionNonabortable<T[]>): undefined;
export declare function diffArrays<T>(oldArr: T[], newArr: T[], options: DiffArraysOptionsAbortable<T>): ChangeObject<T[]>[] | undefined;
export declare function diffArrays<T>(oldArr: T[], newArr: T[], options?: DiffArraysOptionsNonabortable<T>): ChangeObject<T[]>[];
export {};
//# sourceMappingURL=array.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../src/diff/array.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAK,EAAC,YAAY,EAAE,6BAA6B,EAAE,0BAA0B,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,uBAAuB,EAAC,MAAM,aAAa,CAAC;AAExL,cAAM,SAAS,CAAC,CAAC,CAAE,SAAQ,IAAI,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IAC1C,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IAIxB,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IAIpB,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;CAG5B;AAED,eAAO,MAAM,SAAS,oBAAkB,CAAC;AAEzC;;;GAGG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC1B,MAAM,EAAE,CAAC,EAAE,EACX,MAAM,EAAE,CAAC,EAAE,EACX,OAAO,EAAE,wBAAwB,CAAC,CAAC,EAAE,CAAC,GACrC,SAAS,CAAC;AACb,wBAAgB,UAAU,CAAC,CAAC,EAC1B,MAAM,EAAE,CAAC,EAAE,EACX,MAAM,EAAE,CAAC,EAAE,EACX,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC,GAAG,uBAAuB,CAAC,CAAC,EAAE,CAAC,GACpE,SAAS,CAAA;AACZ,wBAAgB,UAAU,CAAC,CAAC,EAC1B,MAAM,EAAE,CAAC,EAAE,EACX,MAAM,EAAE,CAAC,EAAE,EACX,OAAO,EAAE,6BAA6B,CAAC,CAAC,CAAC,GAAG,0BAA0B,CAAC,CAAC,EAAE,CAAC,GAC1E,SAAS,CAAA;AACZ,wBAAgB,UAAU,CAAC,CAAC,EAC1B,MAAM,EAAE,CAAC,EAAE,EACX,MAAM,EAAE,CAAC,EAAE,EACX,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC,GACrC,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,SAAS,CAAA;AAClC,wBAAgB,UAAU,CAAC,CAAC,EAC1B,MAAM,EAAE,CAAC,EAAE,EACX,MAAM,EAAE,CAAC,EAAE,EACX,OAAO,CAAC,EAAE,6BAA6B,CAAC,CAAC,CAAC,GACzC,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAA"}

View File

@@ -0,0 +1,40 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.arrayDiff = void 0;
exports.diffArrays = diffArrays;
var base_js_1 = require("./base.js");
var ArrayDiff = /** @class */ (function (_super) {
__extends(ArrayDiff, _super);
function ArrayDiff() {
return _super !== null && _super.apply(this, arguments) || this;
}
ArrayDiff.prototype.tokenize = function (value) {
return value.slice();
};
ArrayDiff.prototype.join = function (value) {
return value;
};
ArrayDiff.prototype.removeEmpty = function (value) {
return value;
};
return ArrayDiff;
}(base_js_1.default));
exports.arrayDiff = new ArrayDiff();
function diffArrays(oldArr, newArr, options) {
return exports.arrayDiff.diff(oldArr, newArr, options);
}

View File

@@ -0,0 +1,20 @@
import type { ChangeObject, AllDiffOptions, AbortableDiffOptions, DiffCallbackNonabortable, CallbackOptionAbortable, CallbackOptionNonabortable } from '../types.js';
export default class Diff<TokenT, ValueT extends Iterable<TokenT> = Iterable<TokenT>, InputValueT = ValueT> {
diff(oldStr: InputValueT, newStr: InputValueT, options: DiffCallbackNonabortable<ValueT>): undefined;
diff(oldStr: InputValueT, newStr: InputValueT, options: AllDiffOptions & AbortableDiffOptions & CallbackOptionAbortable<ValueT>): undefined;
diff(oldStr: InputValueT, newStr: InputValueT, options: AllDiffOptions & CallbackOptionNonabortable<ValueT>): undefined;
diff(oldStr: InputValueT, newStr: InputValueT, options: AllDiffOptions & AbortableDiffOptions): ChangeObject<ValueT>[] | undefined;
diff(oldStr: InputValueT, newStr: InputValueT, options?: AllDiffOptions): ChangeObject<ValueT>[];
private diffWithOptionsObj;
private addToPath;
private extractCommon;
equals(left: TokenT, right: TokenT, options: AllDiffOptions): boolean;
removeEmpty(array: TokenT[]): TokenT[];
castInput(value: InputValueT, options: AllDiffOptions): ValueT;
tokenize(value: ValueT, options: AllDiffOptions): TokenT[];
join(chars: TokenT[]): ValueT;
postProcess(changeObjects: ChangeObject<ValueT>[], options: AllDiffOptions): ChangeObject<ValueT>[];
get useLongestToken(): boolean;
private buildValues;
}
//# sourceMappingURL=base.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/diff/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAE,cAAc,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,0BAA0B,EAA4D,MAAM,aAAa,CAAC;AAuB9N,MAAM,CAAC,OAAO,OAAO,IAAI,CACvB,MAAM,EACN,MAAM,SAAS,QAAQ,CAAC,MAAM,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,EAClD,WAAW,GAAG,MAAM;IAEpB,IAAI,CACF,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS;IACZ,IAAI,CACF,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,cAAc,GAAG,oBAAoB,GAAG,uBAAuB,CAAC,MAAM,CAAC,GAC/E,SAAS;IACZ,IAAI,CACF,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,cAAc,GAAG,0BAA0B,CAAC,MAAM,CAAC,GAC3D,SAAS;IACZ,IAAI,CACF,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,cAAc,GAAG,oBAAoB,GAC7C,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS;IACrC,IAAI,CACF,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,EACnB,OAAO,CAAC,EAAE,cAAc,GACvB,YAAY,CAAC,MAAM,CAAC,EAAE;IAwBzB,OAAO,CAAC,kBAAkB;IA0I1B,OAAO,CAAC,SAAS;IAqBjB,OAAO,CAAC,aAAa;IA8BrB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO;IASrE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE;IAWtC,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,GAAG,MAAM;IAK9D,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,MAAM,EAAE;IAI1D,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM;IAQ7B,WAAW,CACT,aAAa,EAAE,YAAY,CAAC,MAAM,CAAC,EAAE,EAErC,OAAO,EAAE,cAAc,GACtB,YAAY,CAAC,MAAM,CAAC,EAAE;IAIzB,IAAI,eAAe,IAAI,OAAO,CAE7B;IAED,OAAO,CAAC,WAAW;CAkDpB"}

View File

@@ -0,0 +1,265 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Diff = /** @class */ (function () {
function Diff() {
}
Diff.prototype.diff = function (oldStr, newStr,
// Type below is not accurate/complete - see above for full possibilities - but it compiles
options) {
if (options === void 0) { options = {}; }
var callback;
if (typeof options === 'function') {
callback = options;
options = {};
}
else if ('callback' in options) {
callback = options.callback;
}
// Allow subclasses to massage the input prior to running
var oldString = this.castInput(oldStr, options);
var newString = this.castInput(newStr, options);
var oldTokens = this.removeEmpty(this.tokenize(oldString, options));
var newTokens = this.removeEmpty(this.tokenize(newString, options));
return this.diffWithOptionsObj(oldTokens, newTokens, options, callback);
};
Diff.prototype.diffWithOptionsObj = function (oldTokens, newTokens, options, callback) {
var _this = this;
var _a;
var done = function (value) {
value = _this.postProcess(value, options);
if (callback) {
setTimeout(function () { callback(value); }, 0);
return undefined;
}
else {
return value;
}
};
var newLen = newTokens.length, oldLen = oldTokens.length;
var editLength = 1;
var maxEditLength = newLen + oldLen;
if (options.maxEditLength != null) {
maxEditLength = Math.min(maxEditLength, options.maxEditLength);
}
var maxExecutionTime = (_a = options.timeout) !== null && _a !== void 0 ? _a : Infinity;
var abortAfterTimestamp = Date.now() + maxExecutionTime;
var bestPath = [{ oldPos: -1, lastComponent: undefined }];
// Seed editLength = 0, i.e. the content starts with the same values
var newPos = this.extractCommon(bestPath[0], newTokens, oldTokens, 0, options);
if (bestPath[0].oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
// Identity per the equality and tokenizer
return done(this.buildValues(bestPath[0].lastComponent, newTokens, oldTokens));
}
// Once we hit the right edge of the edit graph on some diagonal k, we can
// definitely reach the end of the edit graph in no more than k edits, so
// there's no point in considering any moves to diagonal k+1 any more (from
// which we're guaranteed to need at least k+1 more edits).
// Similarly, once we've reached the bottom of the edit graph, there's no
// point considering moves to lower diagonals.
// We record this fact by setting minDiagonalToConsider and
// maxDiagonalToConsider to some finite value once we've hit the edge of
// the edit graph.
// This optimization is not faithful to the original algorithm presented in
// Myers's paper, which instead pointlessly extends D-paths off the end of
// the edit graph - see page 7 of Myers's paper which notes this point
// explicitly and illustrates it with a diagram. This has major performance
// implications for some common scenarios. For instance, to compute a diff
// where the new text simply appends d characters on the end of the
// original text of length n, the true Myers algorithm will take O(n+d^2)
// time while this optimization needs only O(n+d) time.
var minDiagonalToConsider = -Infinity, maxDiagonalToConsider = Infinity;
// Main worker method. checks all permutations of a given edit length for acceptance.
var execEditLength = function () {
for (var diagonalPath = Math.max(minDiagonalToConsider, -editLength); diagonalPath <= Math.min(maxDiagonalToConsider, editLength); diagonalPath += 2) {
var basePath = void 0;
var removePath = bestPath[diagonalPath - 1], addPath = bestPath[diagonalPath + 1];
if (removePath) {
// No one else is going to attempt to use this value, clear it
// @ts-expect-error - perf optimisation. This type-violating value will never be read.
bestPath[diagonalPath - 1] = undefined;
}
var canAdd = false;
if (addPath) {
// what newPos will be after we do an insertion:
var addPathNewPos = addPath.oldPos - diagonalPath;
canAdd = addPath && 0 <= addPathNewPos && addPathNewPos < newLen;
}
var canRemove = removePath && removePath.oldPos + 1 < oldLen;
if (!canAdd && !canRemove) {
// If this path is a terminal then prune
// @ts-expect-error - perf optimisation. This type-violating value will never be read.
bestPath[diagonalPath] = undefined;
continue;
}
// Select the diagonal that we want to branch from. We select the prior
// path whose position in the old string is the farthest from the origin
// and does not pass the bounds of the diff graph
if (!canRemove || (canAdd && removePath.oldPos < addPath.oldPos)) {
basePath = _this.addToPath(addPath, true, false, 0, options);
}
else {
basePath = _this.addToPath(removePath, false, true, 1, options);
}
newPos = _this.extractCommon(basePath, newTokens, oldTokens, diagonalPath, options);
if (basePath.oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
// If we have hit the end of both strings, then we are done
return done(_this.buildValues(basePath.lastComponent, newTokens, oldTokens)) || true;
}
else {
bestPath[diagonalPath] = basePath;
if (basePath.oldPos + 1 >= oldLen) {
maxDiagonalToConsider = Math.min(maxDiagonalToConsider, diagonalPath - 1);
}
if (newPos + 1 >= newLen) {
minDiagonalToConsider = Math.max(minDiagonalToConsider, diagonalPath + 1);
}
}
}
editLength++;
};
// Performs the length of edit iteration. Is a bit fugly as this has to support the
// sync and async mode which is never fun. Loops over execEditLength until a value
// is produced, or until the edit length exceeds options.maxEditLength (if given),
// in which case it will return undefined.
if (callback) {
(function exec() {
setTimeout(function () {
if (editLength > maxEditLength || Date.now() > abortAfterTimestamp) {
return callback(undefined);
}
if (!execEditLength()) {
exec();
}
}, 0);
}());
}
else {
while (editLength <= maxEditLength && Date.now() <= abortAfterTimestamp) {
var ret = execEditLength();
if (ret) {
return ret;
}
}
}
};
Diff.prototype.addToPath = function (path, added, removed, oldPosInc, options) {
var last = path.lastComponent;
if (last && !options.oneChangePerToken && last.added === added && last.removed === removed) {
return {
oldPos: path.oldPos + oldPosInc,
lastComponent: { count: last.count + 1, added: added, removed: removed, previousComponent: last.previousComponent }
};
}
else {
return {
oldPos: path.oldPos + oldPosInc,
lastComponent: { count: 1, added: added, removed: removed, previousComponent: last }
};
}
};
Diff.prototype.extractCommon = function (basePath, newTokens, oldTokens, diagonalPath, options) {
var newLen = newTokens.length, oldLen = oldTokens.length;
var oldPos = basePath.oldPos, newPos = oldPos - diagonalPath, commonCount = 0;
while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(oldTokens[oldPos + 1], newTokens[newPos + 1], options)) {
newPos++;
oldPos++;
commonCount++;
if (options.oneChangePerToken) {
basePath.lastComponent = { count: 1, previousComponent: basePath.lastComponent, added: false, removed: false };
}
}
if (commonCount && !options.oneChangePerToken) {
basePath.lastComponent = { count: commonCount, previousComponent: basePath.lastComponent, added: false, removed: false };
}
basePath.oldPos = oldPos;
return newPos;
};
Diff.prototype.equals = function (left, right, options) {
if (options.comparator) {
return options.comparator(left, right);
}
else {
return left === right
|| (!!options.ignoreCase && left.toLowerCase() === right.toLowerCase());
}
};
Diff.prototype.removeEmpty = function (array) {
var ret = [];
for (var i = 0; i < array.length; i++) {
if (array[i]) {
ret.push(array[i]);
}
}
return ret;
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Diff.prototype.castInput = function (value, options) {
return value;
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Diff.prototype.tokenize = function (value, options) {
return Array.from(value);
};
Diff.prototype.join = function (chars) {
// Assumes ValueT is string, which is the case for most subclasses.
// When it's false, e.g. in diffArrays, this method needs to be overridden (e.g. with a no-op)
// Yes, the casts are verbose and ugly, because this pattern - of having the base class SORT OF
// assume tokens and values are strings, but not completely - is weird and janky.
return chars.join('');
};
Diff.prototype.postProcess = function (changeObjects,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options) {
return changeObjects;
};
Object.defineProperty(Diff.prototype, "useLongestToken", {
get: function () {
return false;
},
enumerable: false,
configurable: true
});
Diff.prototype.buildValues = function (lastComponent, newTokens, oldTokens) {
// First we convert our linked list of components in reverse order to an
// array in the right order:
var components = [];
var nextComponent;
while (lastComponent) {
components.push(lastComponent);
nextComponent = lastComponent.previousComponent;
delete lastComponent.previousComponent;
lastComponent = nextComponent;
}
components.reverse();
var componentLen = components.length;
var componentPos = 0, newPos = 0, oldPos = 0;
for (; componentPos < componentLen; componentPos++) {
var component = components[componentPos];
if (!component.removed) {
if (!component.added && this.useLongestToken) {
var value = newTokens.slice(newPos, newPos + component.count);
value = value.map(function (value, i) {
var oldValue = oldTokens[oldPos + i];
return oldValue.length > value.length ? oldValue : value;
});
component.value = this.join(value);
}
else {
component.value = this.join(newTokens.slice(newPos, newPos + component.count));
}
newPos += component.count;
// Common case
if (!component.added) {
oldPos += component.count;
}
}
else {
component.value = this.join(oldTokens.slice(oldPos, oldPos + component.count));
oldPos += component.count;
}
}
return components;
};
return Diff;
}());
exports.default = Diff;

View File

@@ -0,0 +1,19 @@
import Diff from './base.js';
import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffCharsOptionsAbortable, DiffCharsOptionsNonabortable } from '../types.js';
declare class CharacterDiff extends Diff<string, string> {
}
export declare const characterDiff: CharacterDiff;
/**
* diffs two blocks of text, treating each character as a token.
*
* ("Characters" here means Unicode code points - the elements you get when you loop over a string with a `for ... of ...` loop.)
*
* @returns a list of change objects.
*/
export declare function diffChars(oldStr: string, newStr: string, options: DiffCallbackNonabortable<string>): undefined;
export declare function diffChars(oldStr: string, newStr: string, options: DiffCharsOptionsAbortable & CallbackOptionAbortable<string>): undefined;
export declare function diffChars(oldStr: string, newStr: string, options: DiffCharsOptionsNonabortable & CallbackOptionNonabortable<string>): undefined;
export declare function diffChars(oldStr: string, newStr: string, options: DiffCharsOptionsAbortable): ChangeObject<string>[] | undefined;
export declare function diffChars(oldStr: string, newStr: string, options?: DiffCharsOptionsNonabortable): ChangeObject<string>[];
export {};
//# sourceMappingURL=character.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"character.d.ts","sourceRoot":"","sources":["../../src/diff/character.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,yBAAyB,EAAE,4BAA4B,EAAC,MAAM,aAAa,CAAC;AAEvL,cAAM,aAAc,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;CAAG;AAEnD,eAAO,MAAM,aAAa,eAAsB,CAAC;AAEjD;;;;;;GAMG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS,CAAC;AACb,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GAAG,uBAAuB,CAAC,MAAM,CAAC,GACnE,SAAS,CAAA;AACZ,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,4BAA4B,GAAG,0BAA0B,CAAC,MAAM,CAAC,GACzE,SAAS,CAAA;AACZ,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GACjC,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAA;AACrC,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,4BAA4B,GACrC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAA"}

View File

@@ -0,0 +1,31 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.characterDiff = void 0;
exports.diffChars = diffChars;
var base_js_1 = require("./base.js");
var CharacterDiff = /** @class */ (function (_super) {
__extends(CharacterDiff, _super);
function CharacterDiff() {
return _super !== null && _super.apply(this, arguments) || this;
}
return CharacterDiff;
}(base_js_1.default));
exports.characterDiff = new CharacterDiff();
function diffChars(oldStr, newStr, options) {
return exports.characterDiff.diff(oldStr, newStr, options);
}

View File

@@ -0,0 +1,18 @@
import Diff from './base.js';
import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffCssOptionsAbortable, DiffCssOptionsNonabortable } from '../types.js';
declare class CssDiff extends Diff<string, string> {
tokenize(value: string): string[];
}
export declare const cssDiff: CssDiff;
/**
* diffs two blocks of text, comparing CSS tokens.
*
* @returns a list of change objects.
*/
export declare function diffCss(oldStr: string, newStr: string, options: DiffCallbackNonabortable<string>): undefined;
export declare function diffCss(oldStr: string, newStr: string, options: DiffCssOptionsAbortable & CallbackOptionAbortable<string>): undefined;
export declare function diffCss(oldStr: string, newStr: string, options: DiffCssOptionsNonabortable & CallbackOptionNonabortable<string>): undefined;
export declare function diffCss(oldStr: string, newStr: string, options: DiffCssOptionsAbortable): ChangeObject<string>[] | undefined;
export declare function diffCss(oldStr: string, newStr: string, options?: DiffCssOptionsNonabortable): ChangeObject<string>[];
export {};
//# sourceMappingURL=css.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"css.d.ts","sourceRoot":"","sources":["../../src/diff/css.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,0BAA0B,EAAC,MAAM,aAAa,CAAC;AAEnL,cAAM,OAAQ,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IACxC,QAAQ,CAAC,KAAK,EAAE,MAAM;CAGvB;AAED,eAAO,MAAM,OAAO,SAAgB,CAAC;AAErC;;;;GAIG;AACH,wBAAgB,OAAO,CACrB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS,CAAC;AACb,wBAAgB,OAAO,CACrB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,uBAAuB,GAAG,uBAAuB,CAAC,MAAM,CAAC,GACjE,SAAS,CAAA;AACZ,wBAAgB,OAAO,CACrB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,0BAA0B,GAAG,0BAA0B,CAAC,MAAM,CAAC,GACvE,SAAS,CAAA;AACZ,wBAAgB,OAAO,CACrB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,uBAAuB,GAC/B,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAA;AACrC,wBAAgB,OAAO,CACrB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,0BAA0B,GACnC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAA"}

View File

@@ -0,0 +1,34 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.cssDiff = void 0;
exports.diffCss = diffCss;
var base_js_1 = require("./base.js");
var CssDiff = /** @class */ (function (_super) {
__extends(CssDiff, _super);
function CssDiff() {
return _super !== null && _super.apply(this, arguments) || this;
}
CssDiff.prototype.tokenize = function (value) {
return value.split(/([{}:;,]|\s+)/);
};
return CssDiff;
}(base_js_1.default));
exports.cssDiff = new CssDiff();
function diffCss(oldStr, newStr, options) {
return exports.cssDiff.diff(oldStr, newStr, options);
}

View File

@@ -0,0 +1,24 @@
import Diff from './base.js';
import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffJsonOptionsAbortable, DiffJsonOptionsNonabortable } from '../types.js';
import { tokenize } from './line.js';
declare class JsonDiff extends Diff<string, string, string | object> {
get useLongestToken(): boolean;
tokenize: typeof tokenize;
castInput(value: string | object, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable): string;
equals(left: string, right: string, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable): boolean;
}
export declare const jsonDiff: JsonDiff;
/**
* diffs two JSON-serializable objects by first serializing them to prettily-formatted JSON and then treating each line of the JSON as a token.
* Object properties are ordered alphabetically in the serialized JSON, so the order of properties in the objects being compared doesn't affect the result.
*
* @returns a list of change objects.
*/
export declare function diffJson(oldStr: string | object, newStr: string | object, options: DiffCallbackNonabortable<string>): undefined;
export declare function diffJson(oldStr: string | object, newStr: string | object, options: DiffJsonOptionsAbortable & CallbackOptionAbortable<string>): undefined;
export declare function diffJson(oldStr: string | object, newStr: string | object, options: DiffJsonOptionsNonabortable & CallbackOptionNonabortable<string>): undefined;
export declare function diffJson(oldStr: string | object, newStr: string | object, options: DiffJsonOptionsAbortable): ChangeObject<string>[] | undefined;
export declare function diffJson(oldStr: string | object, newStr: string | object, options?: DiffJsonOptionsNonabortable): ChangeObject<string>[];
export declare function canonicalize(obj: any, stack: Array<any> | null, replacementStack: Array<any> | null, replacer: (k: string, v: any) => any, key?: string): any;
export {};
//# sourceMappingURL=json.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/diff/json.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,2BAA2B,EAAC,MAAM,aAAa,CAAC;AACrL,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAErC,cAAM,QAAS,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1D,IAAI,eAAe,YAIlB;IAED,QAAQ,kBAAY;IAEpB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,EAAE,2BAA2B,GAAG,wBAAwB;IAMjG,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,2BAA2B,GAAG,wBAAwB;CAGpG;AAED,eAAO,MAAM,QAAQ,UAAiB,CAAC;AAEvC;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS,CAAC;AACb,wBAAgB,QAAQ,CACtB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,OAAO,EAAE,wBAAwB,GAAG,uBAAuB,CAAC,MAAM,CAAC,GAClE,SAAS,CAAA;AACZ,wBAAgB,QAAQ,CACtB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,OAAO,EAAE,2BAA2B,GAAG,0BAA0B,CAAC,MAAM,CAAC,GACxE,SAAS,CAAA;AACZ,wBAAgB,QAAQ,CACtB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,OAAO,EAAE,wBAAwB,GAChC,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAA;AACrC,wBAAgB,QAAQ,CACtB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,OAAO,CAAC,EAAE,2BAA2B,GACpC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAA;AAQzB,wBAAgB,YAAY,CAC1B,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,gBAAgB,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,EAC7D,QAAQ,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,GAAG,KAAK,GAAG,EACpC,GAAG,CAAC,EAAE,MAAM,OA0Db"}

View File

@@ -0,0 +1,105 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsonDiff = void 0;
exports.diffJson = diffJson;
exports.canonicalize = canonicalize;
var base_js_1 = require("./base.js");
var line_js_1 = require("./line.js");
var JsonDiff = /** @class */ (function (_super) {
__extends(JsonDiff, _super);
function JsonDiff() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.tokenize = line_js_1.tokenize;
return _this;
}
Object.defineProperty(JsonDiff.prototype, "useLongestToken", {
get: function () {
// Discriminate between two lines of pretty-printed, serialized JSON where one of them has a
// dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output:
return true;
},
enumerable: false,
configurable: true
});
JsonDiff.prototype.castInput = function (value, options) {
var undefinedReplacement = options.undefinedReplacement, _a = options.stringifyReplacer, stringifyReplacer = _a === void 0 ? function (k, v) { return typeof v === 'undefined' ? undefinedReplacement : v; } : _a;
return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), null, ' ');
};
JsonDiff.prototype.equals = function (left, right, options) {
return _super.prototype.equals.call(this, left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'), options);
};
return JsonDiff;
}(base_js_1.default));
exports.jsonDiff = new JsonDiff();
function diffJson(oldStr, newStr, options) {
return exports.jsonDiff.diff(oldStr, newStr, options);
}
// This function handles the presence of circular references by bailing out when encountering an
// object that is already on the "stack" of items being processed. Accepts an optional replacer
function canonicalize(obj, stack, replacementStack, replacer, key) {
stack = stack || [];
replacementStack = replacementStack || [];
if (replacer) {
obj = replacer(key === undefined ? '' : key, obj);
}
var i;
for (i = 0; i < stack.length; i += 1) {
if (stack[i] === obj) {
return replacementStack[i];
}
}
var canonicalizedObj;
if ('[object Array]' === Object.prototype.toString.call(obj)) {
stack.push(obj);
canonicalizedObj = new Array(obj.length);
replacementStack.push(canonicalizedObj);
for (i = 0; i < obj.length; i += 1) {
canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack, replacer, String(i));
}
stack.pop();
replacementStack.pop();
return canonicalizedObj;
}
if (obj && obj.toJSON) {
obj = obj.toJSON();
}
if (typeof obj === 'object' && obj !== null) {
stack.push(obj);
canonicalizedObj = {};
replacementStack.push(canonicalizedObj);
var sortedKeys = [];
var key_1;
for (key_1 in obj) {
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(obj, key_1)) {
sortedKeys.push(key_1);
}
}
sortedKeys.sort();
for (i = 0; i < sortedKeys.length; i += 1) {
key_1 = sortedKeys[i];
canonicalizedObj[key_1] = canonicalize(obj[key_1], stack, replacementStack, replacer, key_1);
}
stack.pop();
replacementStack.pop();
}
else {
canonicalizedObj = obj;
}
return canonicalizedObj;
}

View File

@@ -0,0 +1,24 @@
import Diff from './base.js';
import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffLinesOptionsAbortable, DiffLinesOptionsNonabortable } from '../types.js';
declare class LineDiff extends Diff<string, string> {
tokenize: typeof tokenize;
equals(left: string, right: string, options: DiffLinesOptionsAbortable | DiffLinesOptionsNonabortable): boolean;
}
export declare const lineDiff: LineDiff;
/**
* diffs two blocks of text, treating each line as a token.
* @returns a list of change objects.
*/
export declare function diffLines(oldStr: string, newStr: string, options: DiffCallbackNonabortable<string>): undefined;
export declare function diffLines(oldStr: string, newStr: string, options: DiffLinesOptionsAbortable & CallbackOptionAbortable<string>): undefined;
export declare function diffLines(oldStr: string, newStr: string, options: DiffLinesOptionsNonabortable & CallbackOptionNonabortable<string>): undefined;
export declare function diffLines(oldStr: string, newStr: string, options: DiffLinesOptionsAbortable): ChangeObject<string>[] | undefined;
export declare function diffLines(oldStr: string, newStr: string, options?: DiffLinesOptionsNonabortable): ChangeObject<string>[];
export declare function diffTrimmedLines(oldStr: string, newStr: string, options: DiffCallbackNonabortable<string>): undefined;
export declare function diffTrimmedLines(oldStr: string, newStr: string, options: DiffLinesOptionsAbortable & CallbackOptionAbortable<string>): undefined;
export declare function diffTrimmedLines(oldStr: string, newStr: string, options: DiffLinesOptionsNonabortable & CallbackOptionNonabortable<string>): undefined;
export declare function diffTrimmedLines(oldStr: string, newStr: string, options: DiffLinesOptionsAbortable): ChangeObject<string>[] | undefined;
export declare function diffTrimmedLines(oldStr: string, newStr: string, options?: DiffLinesOptionsNonabortable): ChangeObject<string>[];
export declare function tokenize(value: string, options: DiffLinesOptionsAbortable | DiffLinesOptionsNonabortable): string[];
export {};
//# sourceMappingURL=line.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"line.d.ts","sourceRoot":"","sources":["../../src/diff/line.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,yBAAyB,EAAE,4BAA4B,EAAC,MAAM,aAAa,CAAC;AAGvL,cAAM,QAAS,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IACzC,QAAQ,kBAAY;IAEpB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,yBAAyB,GAAG,4BAA4B;CAyBtG;AAED,eAAO,MAAM,QAAQ,UAAiB,CAAC;AAEvC;;;GAGG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS,CAAC;AACb,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GAAG,uBAAuB,CAAC,MAAM,CAAC,GACnE,SAAS,CAAA;AACZ,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,4BAA4B,GAAG,0BAA0B,CAAC,MAAM,CAAC,GACzE,SAAS,CAAA;AACZ,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GACjC,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAA;AACrC,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,4BAA4B,GACrC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAA;AAWzB,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS,CAAC;AACb,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GAAG,uBAAuB,CAAC,MAAM,CAAC,GACnE,SAAS,CAAA;AACZ,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,4BAA4B,GAAG,0BAA0B,CAAC,MAAM,CAAC,GACzE,SAAS,CAAA;AACZ,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GACjC,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAA;AACrC,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,4BAA4B,GACrC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAA;AAOzB,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,yBAAyB,GAAG,4BAA4B,YA0BxG"}

View File

@@ -0,0 +1,89 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.lineDiff = void 0;
exports.diffLines = diffLines;
exports.diffTrimmedLines = diffTrimmedLines;
exports.tokenize = tokenize;
var base_js_1 = require("./base.js");
var params_js_1 = require("../util/params.js");
var LineDiff = /** @class */ (function (_super) {
__extends(LineDiff, _super);
function LineDiff() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.tokenize = tokenize;
return _this;
}
LineDiff.prototype.equals = function (left, right, options) {
// If we're ignoring whitespace, we need to normalise lines by stripping
// whitespace before checking equality. (This has an annoying interaction
// with newlineIsToken that requires special handling: if newlines get their
// own token, then we DON'T want to trim the *newline* tokens down to empty
// strings, since this would cause us to treat whitespace-only line content
// as equal to a separator between lines, which would be weird and
// inconsistent with the documented behavior of the options.)
if (options.ignoreWhitespace) {
if (!options.newlineIsToken || !left.includes('\n')) {
left = left.trim();
}
if (!options.newlineIsToken || !right.includes('\n')) {
right = right.trim();
}
}
else if (options.ignoreNewlineAtEof && !options.newlineIsToken) {
if (left.endsWith('\n')) {
left = left.slice(0, -1);
}
if (right.endsWith('\n')) {
right = right.slice(0, -1);
}
}
return _super.prototype.equals.call(this, left, right, options);
};
return LineDiff;
}(base_js_1.default));
exports.lineDiff = new LineDiff();
function diffLines(oldStr, newStr, options) {
return exports.lineDiff.diff(oldStr, newStr, options);
}
function diffTrimmedLines(oldStr, newStr, options) {
options = (0, params_js_1.generateOptions)(options, { ignoreWhitespace: true });
return exports.lineDiff.diff(oldStr, newStr, options);
}
// Exported standalone so it can be used from jsonDiff too.
function tokenize(value, options) {
if (options.stripTrailingCr) {
// remove one \r before \n to match GNU diff's --strip-trailing-cr behavior
value = value.replace(/\r\n/g, '\n');
}
var retLines = [], linesAndNewlines = value.split(/(\n|\r\n)/);
// Ignore the final empty token that occurs if the string ends with a new line
if (!linesAndNewlines[linesAndNewlines.length - 1]) {
linesAndNewlines.pop();
}
// Merge the content and line separators into single tokens
for (var i = 0; i < linesAndNewlines.length; i++) {
var line = linesAndNewlines[i];
if (i % 2 && !options.newlineIsToken) {
retLines[retLines.length - 1] += line;
}
else {
retLines.push(line);
}
}
return retLines;
}

View File

@@ -0,0 +1,21 @@
import Diff from './base.js';
import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffSentencesOptionsAbortable, DiffSentencesOptionsNonabortable } from '../types.js';
declare class SentenceDiff extends Diff<string, string> {
tokenize(value: string): string[];
}
export declare const sentenceDiff: SentenceDiff;
/**
* diffs two blocks of text, treating each sentence, and the whitespace between each pair of sentences, as a token.
* The characters `.`, `!`, and `?`, when followed by whitespace, are treated as marking the end of a sentence; nothing else besides the end of the string is considered to mark a sentence end.
*
* (For more sophisticated detection of sentence breaks, including support for non-English punctuation, consider instead tokenizing with an [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) with `granularity: 'sentence'` and passing the result to `diffArrays`.)
*
* @returns a list of change objects.
*/
export declare function diffSentences(oldStr: string, newStr: string, options: DiffCallbackNonabortable<string>): undefined;
export declare function diffSentences(oldStr: string, newStr: string, options: DiffSentencesOptionsAbortable & CallbackOptionAbortable<string>): undefined;
export declare function diffSentences(oldStr: string, newStr: string, options: DiffSentencesOptionsNonabortable & CallbackOptionNonabortable<string>): undefined;
export declare function diffSentences(oldStr: string, newStr: string, options: DiffSentencesOptionsAbortable): ChangeObject<string>[] | undefined;
export declare function diffSentences(oldStr: string, newStr: string, options?: DiffSentencesOptionsNonabortable): ChangeObject<string>[];
export {};
//# sourceMappingURL=sentence.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"sentence.d.ts","sourceRoot":"","sources":["../../src/diff/sentence.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAK,EACV,YAAY,EACZ,uBAAuB,EACvB,0BAA0B,EAC1B,wBAAwB,EACxB,6BAA6B,EAC7B,gCAAgC,EACjC,MAAM,aAAa,CAAC;AAMrB,cAAM,YAAa,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IAC7C,QAAQ,CAAC,KAAK,EAAE,MAAM;CAoCvB;AAED,eAAO,MAAM,YAAY,cAAqB,CAAC;AAE/C;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS,CAAC;AACb,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,6BAA6B,GAAG,uBAAuB,CAAC,MAAM,CAAC,GACvE,SAAS,CAAA;AACZ,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,gCAAgC,GAAG,0BAA0B,CAAC,MAAM,CAAC,GAC7E,SAAS,CAAA;AACZ,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,6BAA6B,GACrC,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAA;AACrC,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,gCAAgC,GACzC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAA"}

View File

@@ -0,0 +1,67 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.sentenceDiff = void 0;
exports.diffSentences = diffSentences;
var base_js_1 = require("./base.js");
function isSentenceEndPunct(char) {
return char == '.' || char == '!' || char == '?';
}
var SentenceDiff = /** @class */ (function (_super) {
__extends(SentenceDiff, _super);
function SentenceDiff() {
return _super !== null && _super.apply(this, arguments) || this;
}
SentenceDiff.prototype.tokenize = function (value) {
var _a;
// If in future we drop support for environments that don't support lookbehinds, we can replace
// this entire function with:
// return value.split(/(?<=[.!?])(\s+|$)/);
// but until then, for similar reasons to the trailingWs function in string.ts, we are forced
// to do this verbosely "by hand" instead of using a regex.
var result = [];
var tokenStartI = 0;
for (var i = 0; i < value.length; i++) {
if (i == value.length - 1) {
result.push(value.slice(tokenStartI));
break;
}
if (isSentenceEndPunct(value[i]) && value[i + 1].match(/\s/)) {
// We've hit a sentence break - i.e. a punctuation mark followed by whitespace.
// We now want to push TWO tokens to the result:
// 1. the sentence
result.push(value.slice(tokenStartI, i + 1));
// 2. the whitespace
i = tokenStartI = i + 1;
while ((_a = value[i + 1]) === null || _a === void 0 ? void 0 : _a.match(/\s/)) {
i++;
}
result.push(value.slice(tokenStartI, i + 1));
// Then the next token (a sentence) starts on the character after the whitespace.
// (It's okay if this is off the end of the string - then the outer loop will terminate
// here anyway.)
tokenStartI = i + 1;
}
}
return result;
};
return SentenceDiff;
}(base_js_1.default));
exports.sentenceDiff = new SentenceDiff();
function diffSentences(oldStr, newStr, options) {
return exports.sentenceDiff.diff(oldStr, newStr, options);
}

View File

@@ -0,0 +1,35 @@
import Diff from './base.js';
import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffWordsOptionsAbortable, DiffWordsOptionsNonabortable } from '../types.js';
declare class WordDiff extends Diff<string, string> {
equals(left: string, right: string, options: DiffWordsOptionsAbortable | DiffWordsOptionsNonabortable): boolean;
tokenize(value: string, options?: DiffWordsOptionsAbortable | DiffWordsOptionsNonabortable): string[];
join(tokens: string[]): string;
postProcess(changes: ChangeObject<string>[], options: any): ChangeObject<string>[];
}
export declare const wordDiff: WordDiff;
/**
* diffs two blocks of text, treating each word and each punctuation mark as a token.
* Whitespace is ignored when computing the diff (but preserved as far as possible in the final change objects).
*
* @returns a list of change objects.
*/
export declare function diffWords(oldStr: string, newStr: string, options: DiffCallbackNonabortable<string>): undefined;
export declare function diffWords(oldStr: string, newStr: string, options: DiffWordsOptionsAbortable & CallbackOptionAbortable<string>): undefined;
export declare function diffWords(oldStr: string, newStr: string, options: DiffWordsOptionsNonabortable & CallbackOptionNonabortable<string>): undefined;
export declare function diffWords(oldStr: string, newStr: string, options: DiffWordsOptionsAbortable): ChangeObject<string>[] | undefined;
export declare function diffWords(oldStr: string, newStr: string, options?: DiffWordsOptionsNonabortable): ChangeObject<string>[];
declare class WordsWithSpaceDiff extends Diff<string, string> {
tokenize(value: string): [] | RegExpMatchArray;
}
export declare const wordsWithSpaceDiff: WordsWithSpaceDiff;
/**
* diffs two blocks of text, treating each word, punctuation mark, newline, or run of (non-newline) whitespace as a token.
* @returns a list of change objects
*/
export declare function diffWordsWithSpace(oldStr: string, newStr: string, options: DiffCallbackNonabortable<string>): undefined;
export declare function diffWordsWithSpace(oldStr: string, newStr: string, options: DiffWordsOptionsAbortable & CallbackOptionAbortable<string>): undefined;
export declare function diffWordsWithSpace(oldStr: string, newStr: string, options: DiffWordsOptionsNonabortable & CallbackOptionNonabortable<string>): undefined;
export declare function diffWordsWithSpace(oldStr: string, newStr: string, options: DiffWordsOptionsAbortable): ChangeObject<string>[] | undefined;
export declare function diffWordsWithSpace(oldStr: string, newStr: string, options?: DiffWordsOptionsNonabortable): ChangeObject<string>[];
export {};
//# sourceMappingURL=word.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"word.d.ts","sourceRoot":"","sources":["../../src/diff/word.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,yBAAyB,EAAE,4BAA4B,EAAC,MAAM,aAAa,CAAC;AAqDvL,cAAM,QAAS,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IACzC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,yBAAyB,GAAG,4BAA4B;IASrG,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,yBAAyB,GAAG,4BAAiC;IAwC9F,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE;IAerB,WAAW,CAAC,OAAO,EAAE,YAAY,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,EAAE,GAAG;CA6B1D;AAED,eAAO,MAAM,QAAQ,UAAiB,CAAC;AAEvC;;;;;GAKG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS,CAAC;AACb,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GAAG,uBAAuB,CAAC,MAAM,CAAC,GACnE,SAAS,CAAA;AACZ,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,4BAA4B,GAAG,0BAA0B,CAAC,MAAM,CAAC,GACzE,SAAS,CAAA;AACZ,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GACjC,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAA;AACrC,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,4BAA4B,GACrC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAA;AA4IzB,cAAM,kBAAmB,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IACnD,QAAQ,CAAC,KAAK,EAAE,MAAM;CASvB;AAED,eAAO,MAAM,kBAAkB,oBAA2B,CAAC;AAE3D;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,wBAAwB,CAAC,MAAM,CAAC,GACxC,SAAS,CAAC;AACb,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GAAG,uBAAuB,CAAC,MAAM,CAAC,GACnE,SAAS,CAAA;AACZ,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,4BAA4B,GAAG,0BAA0B,CAAC,MAAM,CAAC,GACzE,SAAS,CAAA;AACZ,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,yBAAyB,GACjC,YAAY,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,CAAA;AACrC,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,4BAA4B,GACrC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAA"}

View File

@@ -0,0 +1,312 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.wordsWithSpaceDiff = exports.wordDiff = void 0;
exports.diffWords = diffWords;
exports.diffWordsWithSpace = diffWordsWithSpace;
var base_js_1 = require("./base.js");
var string_js_1 = require("../util/string.js");
// Based on https://en.wikipedia.org/wiki/Latin_script_in_Unicode
//
// Chars/ranges counted as "word" characters by this regex are as follows:
//
// + U+00AD Soft hyphen
// + 00C000FF (letters with diacritics from the Latin-1 Supplement), except:
// - U+00D7 × Multiplication sign
// - U+00F7 ÷ Division sign
// + Latin Extended-A, 0100017F
// + Latin Extended-B, 0180024F
// + IPA Extensions, 025002AF
// + Spacing Modifier Letters, 02B002FF, except:
// - U+02C7 ˇ &#711; Caron
// - U+02D8 ˘ &#728; Breve
// - U+02D9 ˙ &#729; Dot Above
// - U+02DA ˚ &#730; Ring Above
// - U+02DB ˛ &#731; Ogonek
// - U+02DC ˜ &#732; Small Tilde
// - U+02DD ˝ &#733; Double Acute Accent
// + Latin Extended Additional, 1E001EFF
var extendedWordChars = 'a-zA-Z0-9_\\u{AD}\\u{C0}-\\u{D6}\\u{D8}-\\u{F6}\\u{F8}-\\u{2C6}\\u{2C8}-\\u{2D7}\\u{2DE}-\\u{2FF}\\u{1E00}-\\u{1EFF}';
// Each token is one of the following:
// - A punctuation mark plus the surrounding whitespace
// - A word plus the surrounding whitespace
// - Pure whitespace (but only in the special case where the entire text
// is just whitespace)
//
// We have to include surrounding whitespace in the tokens because the two
// alternative approaches produce horribly broken results:
// * If we just discard the whitespace, we can't fully reproduce the original
// text from the sequence of tokens and any attempt to render the diff will
// get the whitespace wrong.
// * If we have separate tokens for whitespace, then in a typical text every
// second token will be a single space character. But this often results in
// the optimal diff between two texts being a perverse one that preserves
// the spaces between words but deletes and reinserts actual common words.
// See https://github.com/kpdecker/jsdiff/issues/160#issuecomment-1866099640
// for an example.
//
// Keeping the surrounding whitespace of course has implications for .equals
// and .join, not just .tokenize.
// This regex does NOT fully implement the tokenization rules described above.
// Instead, it gives runs of whitespace their own "token". The tokenize method
// then handles stitching whitespace tokens onto adjacent word or punctuation
// tokens.
var tokenizeIncludingWhitespace = new RegExp("[".concat(extendedWordChars, "]+|\\s+|[^").concat(extendedWordChars, "]"), 'ug');
var WordDiff = /** @class */ (function (_super) {
__extends(WordDiff, _super);
function WordDiff() {
return _super !== null && _super.apply(this, arguments) || this;
}
WordDiff.prototype.equals = function (left, right, options) {
if (options.ignoreCase) {
left = left.toLowerCase();
right = right.toLowerCase();
}
return left.trim() === right.trim();
};
WordDiff.prototype.tokenize = function (value, options) {
if (options === void 0) { options = {}; }
var parts;
if (options.intlSegmenter) {
var segmenter = options.intlSegmenter;
if (segmenter.resolvedOptions().granularity != 'word') {
throw new Error('The segmenter passed must have a granularity of "word"');
}
// We want `parts` to be an array whose elements alternate between being
// pure whitespace and being pure non-whitespace. This is ALMOST what the
// segments returned by a word-based Intl.Segmenter already look like,
// but not quite - see explanation in the docs of our custom segment()
// function.
parts = (0, string_js_1.segment)(value, segmenter);
}
else {
parts = value.match(tokenizeIncludingWhitespace) || [];
}
var tokens = [];
var prevPart = null;
parts.forEach(function (part) {
if ((/\s/).test(part)) {
if (prevPart == null) {
tokens.push(part);
}
else {
tokens.push(tokens.pop() + part);
}
}
else if (prevPart != null && (/\s/).test(prevPart)) {
if (tokens[tokens.length - 1] == prevPart) {
tokens.push(tokens.pop() + part);
}
else {
tokens.push(prevPart + part);
}
}
else {
tokens.push(part);
}
prevPart = part;
});
return tokens;
};
WordDiff.prototype.join = function (tokens) {
// Tokens being joined here will always have appeared consecutively in the
// same text, so we can simply strip off the leading whitespace from all the
// tokens except the first (and except any whitespace-only tokens - but such
// a token will always be the first and only token anyway) and then join them
// and the whitespace around words and punctuation will end up correct.
return tokens.map(function (token, i) {
if (i == 0) {
return token;
}
else {
return token.replace((/^\s+/), '');
}
}).join('');
};
WordDiff.prototype.postProcess = function (changes, options) {
if (!changes || options.oneChangePerToken) {
return changes;
}
var lastKeep = null;
// Change objects representing any insertion or deletion since the last
// "keep" change object. There can be at most one of each.
var insertion = null;
var deletion = null;
changes.forEach(function (change) {
if (change.added) {
insertion = change;
}
else if (change.removed) {
deletion = change;
}
else {
if (insertion || deletion) { // May be false at start of text
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, change, options.intlSegmenter);
}
lastKeep = change;
insertion = null;
deletion = null;
}
});
if (insertion || deletion) {
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, null, options.intlSegmenter);
}
return changes;
};
return WordDiff;
}(base_js_1.default));
exports.wordDiff = new WordDiff();
function diffWords(oldStr, newStr, options) {
// This option has never been documented and never will be (it's clearer to
// just call `diffWordsWithSpace` directly if you need that behavior), but
// has existed in jsdiff for a long time, so we retain support for it here
// for the sake of backwards compatibility.
if ((options === null || options === void 0 ? void 0 : options.ignoreWhitespace) != null && !options.ignoreWhitespace) {
return diffWordsWithSpace(oldStr, newStr, options);
}
return exports.wordDiff.diff(oldStr, newStr, options);
}
function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep, segmenter) {
// Before returning, we tidy up the leading and trailing whitespace of the
// change objects to eliminate cases where trailing whitespace in one object
// is repeated as leading whitespace in the next.
// Below are examples of the outcomes we want here to explain the code.
// I=insert, K=keep, D=delete
// 1. diffing 'foo bar baz' vs 'foo baz'
// Prior to cleanup, we have K:'foo ' D:' bar ' K:' baz'
// After cleanup, we want: K:'foo ' D:'bar ' K:'baz'
//
// 2. Diffing 'foo bar baz' vs 'foo qux baz'
// Prior to cleanup, we have K:'foo ' D:' bar ' I:' qux ' K:' baz'
// After cleanup, we want K:'foo ' D:'bar' I:'qux' K:' baz'
//
// 3. Diffing 'foo\nbar baz' vs 'foo baz'
// Prior to cleanup, we have K:'foo ' D:'\nbar ' K:' baz'
// After cleanup, we want K'foo' D:'\nbar' K:' baz'
//
// 4. Diffing 'foo baz' vs 'foo\nbar baz'
// Prior to cleanup, we have K:'foo\n' I:'\nbar ' K:' baz'
// After cleanup, we ideally want K'foo' I:'\nbar' K:' baz'
// but don't actually manage this currently (the pre-cleanup change
// objects don't contain enough information to make it possible).
//
// 5. Diffing 'foo bar baz' vs 'foo baz'
// Prior to cleanup, we have K:'foo ' D:' bar ' K:' baz'
// After cleanup, we want K:'foo ' D:' bar ' K:'baz'
//
// Our handling is unavoidably imperfect in the case where there's a single
// indel between keeps and the whitespace has changed. For instance, consider
// diffing 'foo\tbar\nbaz' vs 'foo baz'. Unless we create an extra change
// object to represent the insertion of the space character (which isn't even
// a token), we have no way to avoid losing information about the texts'
// original whitespace in the result we return. Still, we do our best to
// output something that will look sensible if we e.g. print it with
// insertions in green and deletions in red.
// Between two "keep" change objects (or before the first or after the last
// change object), we can have either:
// * A "delete" followed by an "insert"
// * Just an "insert"
// * Just a "delete"
// We handle the three cases separately.
if (deletion && insertion) {
var _a = (0, string_js_1.leadingAndTrailingWs)(deletion.value, segmenter), oldWsPrefix = _a[0], oldWsSuffix = _a[1];
var _b = (0, string_js_1.leadingAndTrailingWs)(insertion.value, segmenter), newWsPrefix = _b[0], newWsSuffix = _b[1];
if (startKeep) {
var commonWsPrefix = (0, string_js_1.longestCommonPrefix)(oldWsPrefix, newWsPrefix);
startKeep.value = (0, string_js_1.replaceSuffix)(startKeep.value, newWsPrefix, commonWsPrefix);
deletion.value = (0, string_js_1.removePrefix)(deletion.value, commonWsPrefix);
insertion.value = (0, string_js_1.removePrefix)(insertion.value, commonWsPrefix);
}
if (endKeep) {
var commonWsSuffix = (0, string_js_1.longestCommonSuffix)(oldWsSuffix, newWsSuffix);
endKeep.value = (0, string_js_1.replacePrefix)(endKeep.value, newWsSuffix, commonWsSuffix);
deletion.value = (0, string_js_1.removeSuffix)(deletion.value, commonWsSuffix);
insertion.value = (0, string_js_1.removeSuffix)(insertion.value, commonWsSuffix);
}
}
else if (insertion) {
// The whitespaces all reflect what was in the new text rather than
// the old, so we essentially have no information about whitespace
// insertion or deletion. We just want to dedupe the whitespace.
// We do that by having each change object keep its trailing
// whitespace and deleting duplicate leading whitespace where
// present.
if (startKeep) {
var ws = (0, string_js_1.leadingWs)(insertion.value, segmenter);
insertion.value = insertion.value.substring(ws.length);
}
if (endKeep) {
var ws = (0, string_js_1.leadingWs)(endKeep.value, segmenter);
endKeep.value = endKeep.value.substring(ws.length);
}
// otherwise we've got a deletion and no insertion
}
else if (startKeep && endKeep) {
var newWsFull = (0, string_js_1.leadingWs)(endKeep.value, segmenter), _c = (0, string_js_1.leadingAndTrailingWs)(deletion.value, segmenter), delWsStart = _c[0], delWsEnd = _c[1];
// Any whitespace that comes straight after startKeep in both the old and
// new texts, assign to startKeep and remove from the deletion.
var newWsStart = (0, string_js_1.longestCommonPrefix)(newWsFull, delWsStart);
deletion.value = (0, string_js_1.removePrefix)(deletion.value, newWsStart);
// Any whitespace that comes straight before endKeep in both the old and
// new texts, and hasn't already been assigned to startKeep, assign to
// endKeep and remove from the deletion.
var newWsEnd = (0, string_js_1.longestCommonSuffix)((0, string_js_1.removePrefix)(newWsFull, newWsStart), delWsEnd);
deletion.value = (0, string_js_1.removeSuffix)(deletion.value, newWsEnd);
endKeep.value = (0, string_js_1.replacePrefix)(endKeep.value, newWsFull, newWsEnd);
// If there's any whitespace from the new text that HASN'T already been
// assigned, assign it to the start:
startKeep.value = (0, string_js_1.replaceSuffix)(startKeep.value, newWsFull, newWsFull.slice(0, newWsFull.length - newWsEnd.length));
}
else if (endKeep) {
// We are at the start of the text. Preserve all the whitespace on
// endKeep, and just remove whitespace from the end of deletion to the
// extent that it overlaps with the start of endKeep.
var endKeepWsPrefix = (0, string_js_1.leadingWs)(endKeep.value, segmenter);
var deletionWsSuffix = (0, string_js_1.trailingWs)(deletion.value, segmenter);
var overlap = (0, string_js_1.maximumOverlap)(deletionWsSuffix, endKeepWsPrefix);
deletion.value = (0, string_js_1.removeSuffix)(deletion.value, overlap);
}
else if (startKeep) {
// We are at the END of the text. Preserve all the whitespace on
// startKeep, and just remove whitespace from the start of deletion to
// the extent that it overlaps with the end of startKeep.
var startKeepWsSuffix = (0, string_js_1.trailingWs)(startKeep.value, segmenter);
var deletionWsPrefix = (0, string_js_1.leadingWs)(deletion.value, segmenter);
var overlap = (0, string_js_1.maximumOverlap)(startKeepWsSuffix, deletionWsPrefix);
deletion.value = (0, string_js_1.removePrefix)(deletion.value, overlap);
}
}
var WordsWithSpaceDiff = /** @class */ (function (_super) {
__extends(WordsWithSpaceDiff, _super);
function WordsWithSpaceDiff() {
return _super !== null && _super.apply(this, arguments) || this;
}
WordsWithSpaceDiff.prototype.tokenize = function (value) {
// Slightly different to the tokenizeIncludingWhitespace regex used above in
// that this one treats each individual newline as a distinct token, rather
// than merging them into other surrounding whitespace. This was requested
// in https://github.com/kpdecker/jsdiff/issues/180 &
// https://github.com/kpdecker/jsdiff/issues/211
var regex = new RegExp("(\\r?\\n)|[".concat(extendedWordChars, "]+|[^\\S\\n\\r]+|[^").concat(extendedWordChars, "]"), 'ug');
return value.match(regex) || [];
};
return WordsWithSpaceDiff;
}(base_js_1.default));
exports.wordsWithSpaceDiff = new WordsWithSpaceDiff();
function diffWordsWithSpace(oldStr, newStr, options) {
return exports.wordsWithSpaceDiff.diff(oldStr, newStr, options);
}