blob: 304e06d1533ac72c72e7248c3a2db69f2bf36654 [file] [log] [blame]
import dmp from 'diff-match-patch';
import chalk from 'chalk';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var get = function get(object, property, receiver) {
if (object === null) object = Function.prototype;
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
};
var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
var slicedToArray = function () {
function sliceIterator(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"]) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
return function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else if (Symbol.iterator in Object(arr)) {
return sliceIterator(arr, i);
} else {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
};
}();
var toConsumableArray = function (arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
} else {
return Array.from(arr);
}
};
var Processor = function () {
function Processor(options) {
classCallCheck(this, Processor);
this.selfOptions = options || {};
this.pipes = {};
}
createClass(Processor, [{
key: 'options',
value: function options(_options) {
if (_options) {
this.selfOptions = _options;
}
return this.selfOptions;
}
}, {
key: 'pipe',
value: function pipe(name, pipeArg) {
var pipe = pipeArg;
if (typeof name === 'string') {
if (typeof pipe === 'undefined') {
return this.pipes[name];
} else {
this.pipes[name] = pipe;
}
}
if (name && name.name) {
pipe = name;
if (pipe.processor === this) {
return pipe;
}
this.pipes[pipe.name] = pipe;
}
pipe.processor = this;
return pipe;
}
}, {
key: 'process',
value: function process(input, pipe) {
var context = input;
context.options = this.options();
var nextPipe = pipe || input.pipe || 'default';
var lastPipe = void 0;
var lastContext = void 0;
while (nextPipe) {
if (typeof context.nextAfterChildren !== 'undefined') {
// children processed and coming back to parent
context.next = context.nextAfterChildren;
context.nextAfterChildren = null;
}
if (typeof nextPipe === 'string') {
nextPipe = this.pipe(nextPipe);
}
nextPipe.process(context);
lastContext = context;
lastPipe = nextPipe;
nextPipe = null;
if (context) {
if (context.next) {
context = context.next;
nextPipe = lastContext.nextPipe || context.pipe || lastPipe;
}
}
}
return context.hasResult ? context.result : undefined;
}
}]);
return Processor;
}();
var Pipe = function () {
function Pipe(name) {
classCallCheck(this, Pipe);
this.name = name;
this.filters = [];
}
createClass(Pipe, [{
key: 'process',
value: function process(input) {
if (!this.processor) {
throw new Error('add this pipe to a processor before using it');
}
var debug = this.debug;
var length = this.filters.length;
var context = input;
for (var index = 0; index < length; index++) {
var filter = this.filters[index];
if (debug) {
this.log('filter: ' + filter.filterName);
}
filter(context);
if ((typeof context === 'undefined' ? 'undefined' : _typeof(context)) === 'object' && context.exiting) {
context.exiting = false;
break;
}
}
if (!context.next && this.resultCheck) {
this.resultCheck(context);
}
}
}, {
key: 'log',
value: function log(msg) {
console.log('[jsondiffpatch] ' + this.name + ' pipe, ' + msg);
}
}, {
key: 'append',
value: function append() {
var _filters;
(_filters = this.filters).push.apply(_filters, arguments);
return this;
}
}, {
key: 'prepend',
value: function prepend() {
var _filters2;
(_filters2 = this.filters).unshift.apply(_filters2, arguments);
return this;
}
}, {
key: 'indexOf',
value: function indexOf(filterName) {
if (!filterName) {
throw new Error('a filter name is required');
}
for (var index = 0; index < this.filters.length; index++) {
var filter = this.filters[index];
if (filter.filterName === filterName) {
return index;
}
}
throw new Error('filter not found: ' + filterName);
}
}, {
key: 'list',
value: function list() {
return this.filters.map(function (f) {
return f.filterName;
});
}
}, {
key: 'after',
value: function after(filterName) {
var index = this.indexOf(filterName);
var params = Array.prototype.slice.call(arguments, 1);
if (!params.length) {
throw new Error('a filter is required');
}
params.unshift(index + 1, 0);
Array.prototype.splice.apply(this.filters, params);
return this;
}
}, {
key: 'before',
value: function before(filterName) {
var index = this.indexOf(filterName);
var params = Array.prototype.slice.call(arguments, 1);
if (!params.length) {
throw new Error('a filter is required');
}
params.unshift(index, 0);
Array.prototype.splice.apply(this.filters, params);
return this;
}
}, {
key: 'replace',
value: function replace(filterName) {
var index = this.indexOf(filterName);
var params = Array.prototype.slice.call(arguments, 1);
if (!params.length) {
throw new Error('a filter is required');
}
params.unshift(index, 1);
Array.prototype.splice.apply(this.filters, params);
return this;
}
}, {
key: 'remove',
value: function remove(filterName) {
var index = this.indexOf(filterName);
this.filters.splice(index, 1);
return this;
}
}, {
key: 'clear',
value: function clear() {
this.filters.length = 0;
return this;
}
}, {
key: 'shouldHaveResult',
value: function shouldHaveResult(should) {
if (should === false) {
this.resultCheck = null;
return;
}
if (this.resultCheck) {
return;
}
var pipe = this;
this.resultCheck = function (context) {
if (!context.hasResult) {
console.log(context);
var error = new Error(pipe.name + ' failed');
error.noResult = true;
throw error;
}
};
return this;
}
}]);
return Pipe;
}();
var Context = function () {
function Context() {
classCallCheck(this, Context);
}
createClass(Context, [{
key: 'setResult',
value: function setResult(result) {
this.result = result;
this.hasResult = true;
return this;
}
}, {
key: 'exit',
value: function exit() {
this.exiting = true;
return this;
}
}, {
key: 'switchTo',
value: function switchTo(next, pipe) {
if (typeof next === 'string' || next instanceof Pipe) {
this.nextPipe = next;
} else {
this.next = next;
if (pipe) {
this.nextPipe = pipe;
}
}
return this;
}
}, {
key: 'push',
value: function push(child, name) {
child.parent = this;
if (typeof name !== 'undefined') {
child.childName = name;
}
child.root = this.root || this;
child.options = child.options || this.options;
if (!this.children) {
this.children = [child];
this.nextAfterChildren = this.next || null;
this.next = child;
} else {
this.children[this.children.length - 1].next = child;
this.children.push(child);
}
child.next = this;
return this;
}
}]);
return Context;
}();
var isArray = typeof Array.isArray === 'function' ? Array.isArray : function (a) {
return a instanceof Array;
};
function cloneRegExp(re) {
var regexMatch = /^\/(.*)\/([gimyu]*)$/.exec(re.toString());
return new RegExp(regexMatch[1], regexMatch[2]);
}
function clone(arg) {
if ((typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) !== 'object') {
return arg;
}
if (arg === null) {
return null;
}
if (isArray(arg)) {
return arg.map(clone);
}
if (arg instanceof Date) {
return new Date(arg.getTime());
}
if (arg instanceof RegExp) {
return cloneRegExp(arg);
}
var cloned = {};
for (var name in arg) {
if (Object.prototype.hasOwnProperty.call(arg, name)) {
cloned[name] = clone(arg[name]);
}
}
return cloned;
}
var DiffContext = function (_Context) {
inherits(DiffContext, _Context);
function DiffContext(left, right) {
classCallCheck(this, DiffContext);
var _this = possibleConstructorReturn(this, (DiffContext.__proto__ || Object.getPrototypeOf(DiffContext)).call(this));
_this.left = left;
_this.right = right;
_this.pipe = 'diff';
return _this;
}
createClass(DiffContext, [{
key: 'setResult',
value: function setResult(result) {
if (this.options.cloneDiffValues && (typeof result === 'undefined' ? 'undefined' : _typeof(result)) === 'object') {
var clone$$1 = typeof this.options.cloneDiffValues === 'function' ? this.options.cloneDiffValues : clone;
if (_typeof(result[0]) === 'object') {
result[0] = clone$$1(result[0]);
}
if (_typeof(result[1]) === 'object') {
result[1] = clone$$1(result[1]);
}
}
return Context.prototype.setResult.apply(this, arguments);
}
}]);
return DiffContext;
}(Context);
var PatchContext = function (_Context) {
inherits(PatchContext, _Context);
function PatchContext(left, delta) {
classCallCheck(this, PatchContext);
var _this = possibleConstructorReturn(this, (PatchContext.__proto__ || Object.getPrototypeOf(PatchContext)).call(this));
_this.left = left;
_this.delta = delta;
_this.pipe = 'patch';
return _this;
}
return PatchContext;
}(Context);
var ReverseContext = function (_Context) {
inherits(ReverseContext, _Context);
function ReverseContext(delta) {
classCallCheck(this, ReverseContext);
var _this = possibleConstructorReturn(this, (ReverseContext.__proto__ || Object.getPrototypeOf(ReverseContext)).call(this));
_this.delta = delta;
_this.pipe = 'reverse';
return _this;
}
return ReverseContext;
}(Context);
var isArray$1 = typeof Array.isArray === 'function' ? Array.isArray : function (a) {
return a instanceof Array;
};
var diffFilter = function trivialMatchesDiffFilter(context) {
if (context.left === context.right) {
context.setResult(undefined).exit();
return;
}
if (typeof context.left === 'undefined') {
if (typeof context.right === 'function') {
throw new Error('functions are not supported');
}
context.setResult([context.right]).exit();
return;
}
if (typeof context.right === 'undefined') {
context.setResult([context.left, 0, 0]).exit();
return;
}
if (typeof context.left === 'function' || typeof context.right === 'function') {
throw new Error('functions are not supported');
}
context.leftType = context.left === null ? 'null' : _typeof(context.left);
context.rightType = context.right === null ? 'null' : _typeof(context.right);
if (context.leftType !== context.rightType) {
context.setResult([context.left, context.right]).exit();
return;
}
if (context.leftType === 'boolean' || context.leftType === 'number') {
context.setResult([context.left, context.right]).exit();
return;
}
if (context.leftType === 'object') {
context.leftIsArray = isArray$1(context.left);
}
if (context.rightType === 'object') {
context.rightIsArray = isArray$1(context.right);
}
if (context.leftIsArray !== context.rightIsArray) {
context.setResult([context.left, context.right]).exit();
return;
}
if (context.left instanceof RegExp) {
if (context.right instanceof RegExp) {
context.setResult([context.left.toString(), context.right.toString()]).exit();
} else {
context.setResult([context.left, context.right]).exit();
}
}
};
diffFilter.filterName = 'trivial';
var patchFilter = function trivialMatchesPatchFilter(context) {
if (typeof context.delta === 'undefined') {
context.setResult(context.left).exit();
return;
}
context.nested = !isArray$1(context.delta);
if (context.nested) {
return;
}
if (context.delta.length === 1) {
context.setResult(context.delta[0]).exit();
return;
}
if (context.delta.length === 2) {
if (context.left instanceof RegExp) {
var regexArgs = /^\/(.*)\/([gimyu]+)$/.exec(context.delta[1]);
if (regexArgs) {
context.setResult(new RegExp(regexArgs[1], regexArgs[2])).exit();
return;
}
}
context.setResult(context.delta[1]).exit();
return;
}
if (context.delta.length === 3 && context.delta[2] === 0) {
context.setResult(undefined).exit();
}
};
patchFilter.filterName = 'trivial';
var reverseFilter = function trivialReferseFilter(context) {
if (typeof context.delta === 'undefined') {
context.setResult(context.delta).exit();
return;
}
context.nested = !isArray$1(context.delta);
if (context.nested) {
return;
}
if (context.delta.length === 1) {
context.setResult([context.delta[0], 0, 0]).exit();
return;
}
if (context.delta.length === 2) {
context.setResult([context.delta[1], context.delta[0]]).exit();
return;
}
if (context.delta.length === 3 && context.delta[2] === 0) {
context.setResult([context.delta[0]]).exit();
}
};
reverseFilter.filterName = 'trivial';
function collectChildrenDiffFilter(context) {
if (!context || !context.children) {
return;
}
var length = context.children.length;
var child = void 0;
var result = context.result;
for (var index = 0; index < length; index++) {
child = context.children[index];
if (typeof child.result === 'undefined') {
continue;
}
result = result || {};
result[child.childName] = child.result;
}
if (result && context.leftIsArray) {
result._t = 'a';
}
context.setResult(result).exit();
}
collectChildrenDiffFilter.filterName = 'collectChildren';
function objectsDiffFilter(context) {
if (context.leftIsArray || context.leftType !== 'object') {
return;
}
var name = void 0;
var child = void 0;
var propertyFilter = context.options.propertyFilter;
for (name in context.left) {
if (!Object.prototype.hasOwnProperty.call(context.left, name)) {
continue;
}
if (propertyFilter && !propertyFilter(name, context)) {
continue;
}
child = new DiffContext(context.left[name], context.right[name]);
context.push(child, name);
}
for (name in context.right) {
if (!Object.prototype.hasOwnProperty.call(context.right, name)) {
continue;
}
if (propertyFilter && !propertyFilter(name, context)) {
continue;
}
if (typeof context.left[name] === 'undefined') {
child = new DiffContext(undefined, context.right[name]);
context.push(child, name);
}
}
if (!context.children || context.children.length === 0) {
context.setResult(undefined).exit();
return;
}
context.exit();
}
objectsDiffFilter.filterName = 'objects';
var patchFilter$1 = function nestedPatchFilter(context) {
if (!context.nested) {
return;
}
if (context.delta._t) {
return;
}
var name = void 0;
var child = void 0;
for (name in context.delta) {
child = new PatchContext(context.left[name], context.delta[name]);
context.push(child, name);
}
context.exit();
};
patchFilter$1.filterName = 'objects';
var collectChildrenPatchFilter = function collectChildrenPatchFilter(context) {
if (!context || !context.children) {
return;
}
if (context.delta._t) {
return;
}
var length = context.children.length;
var child = void 0;
for (var index = 0; index < length; index++) {
child = context.children[index];
if (Object.prototype.hasOwnProperty.call(context.left, child.childName) && child.result === undefined) {
delete context.left[child.childName];
} else if (context.left[child.childName] !== child.result) {
context.left[child.childName] = child.result;
}
}
context.setResult(context.left).exit();
};
collectChildrenPatchFilter.filterName = 'collectChildren';
var reverseFilter$1 = function nestedReverseFilter(context) {
if (!context.nested) {
return;
}
if (context.delta._t) {
return;
}
var name = void 0;
var child = void 0;
for (name in context.delta) {
child = new ReverseContext(context.delta[name]);
context.push(child, name);
}
context.exit();
};
reverseFilter$1.filterName = 'objects';
function collectChildrenReverseFilter(context) {
if (!context || !context.children) {
return;
}
if (context.delta._t) {
return;
}
var length = context.children.length;
var child = void 0;
var delta = {};
for (var index = 0; index < length; index++) {
child = context.children[index];
if (delta[child.childName] !== child.result) {
delta[child.childName] = child.result;
}
}
context.setResult(delta).exit();
}
collectChildrenReverseFilter.filterName = 'collectChildren';
/*
LCS implementation that supports arrays or strings
reference: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
*/
var defaultMatch = function defaultMatch(array1, array2, index1, index2) {
return array1[index1] === array2[index2];
};
var lengthMatrix = function lengthMatrix(array1, array2, match, context) {
var len1 = array1.length;
var len2 = array2.length;
var x = void 0,
y = void 0;
// initialize empty matrix of len1+1 x len2+1
var matrix = [len1 + 1];
for (x = 0; x < len1 + 1; x++) {
matrix[x] = [len2 + 1];
for (y = 0; y < len2 + 1; y++) {
matrix[x][y] = 0;
}
}
matrix.match = match;
// save sequence lengths for each coordinate
for (x = 1; x < len1 + 1; x++) {
for (y = 1; y < len2 + 1; y++) {
if (match(array1, array2, x - 1, y - 1, context)) {
matrix[x][y] = matrix[x - 1][y - 1] + 1;
} else {
matrix[x][y] = Math.max(matrix[x - 1][y], matrix[x][y - 1]);
}
}
}
return matrix;
};
var backtrack = function backtrack(matrix, array1, array2, index1, index2, context) {
if (index1 === 0 || index2 === 0) {
return {
sequence: [],
indices1: [],
indices2: []
};
}
if (matrix.match(array1, array2, index1 - 1, index2 - 1, context)) {
var subsequence = backtrack(matrix, array1, array2, index1 - 1, index2 - 1, context);
subsequence.sequence.push(array1[index1 - 1]);
subsequence.indices1.push(index1 - 1);
subsequence.indices2.push(index2 - 1);
return subsequence;
}
if (matrix[index1][index2 - 1] > matrix[index1 - 1][index2]) {
return backtrack(matrix, array1, array2, index1, index2 - 1, context);
} else {
return backtrack(matrix, array1, array2, index1 - 1, index2, context);
}
};
var get$1 = function get(array1, array2, match, context) {
var innerContext = context || {};
var matrix = lengthMatrix(array1, array2, match || defaultMatch, innerContext);
var result = backtrack(matrix, array1, array2, array1.length, array2.length, innerContext);
if (typeof array1 === 'string' && typeof array2 === 'string') {
result.sequence = result.sequence.join('');
}
return result;
};
var lcs = {
get: get$1
};
var ARRAY_MOVE = 3;
var isArray$2 = typeof Array.isArray === 'function' ? Array.isArray : function (a) {
return a instanceof Array;
};
var arrayIndexOf = typeof Array.prototype.indexOf === 'function' ? function (array, item) {
return array.indexOf(item);
} : function (array, item) {
var length = array.length;
for (var i = 0; i < length; i++) {
if (array[i] === item) {
return i;
}
}
return -1;
};
function arraysHaveMatchByRef(array1, array2, len1, len2) {
for (var index1 = 0; index1 < len1; index1++) {
var val1 = array1[index1];
for (var index2 = 0; index2 < len2; index2++) {
var val2 = array2[index2];
if (index1 !== index2 && val1 === val2) {
return true;
}
}
}
}
function matchItems(array1, array2, index1, index2, context) {
var value1 = array1[index1];
var value2 = array2[index2];
if (value1 === value2) {
return true;
}
if ((typeof value1 === 'undefined' ? 'undefined' : _typeof(value1)) !== 'object' || (typeof value2 === 'undefined' ? 'undefined' : _typeof(value2)) !== 'object') {
return false;
}
var objectHash = context.objectHash;
if (!objectHash) {
// no way to match objects was provided, try match by position
return context.matchByPosition && index1 === index2;
}
var hash1 = void 0;
var hash2 = void 0;
if (typeof index1 === 'number') {
context.hashCache1 = context.hashCache1 || [];
hash1 = context.hashCache1[index1];
if (typeof hash1 === 'undefined') {
context.hashCache1[index1] = hash1 = objectHash(value1, index1);
}
} else {
hash1 = objectHash(value1);
}
if (typeof hash1 === 'undefined') {
return false;
}
if (typeof index2 === 'number') {
context.hashCache2 = context.hashCache2 || [];
hash2 = context.hashCache2[index2];
if (typeof hash2 === 'undefined') {
context.hashCache2[index2] = hash2 = objectHash(value2, index2);
}
} else {
hash2 = objectHash(value2);
}
if (typeof hash2 === 'undefined') {
return false;
}
return hash1 === hash2;
}
var diffFilter$1 = function arraysDiffFilter(context) {
if (!context.leftIsArray) {
return;
}
var matchContext = {
objectHash: context.options && context.options.objectHash,
matchByPosition: context.options && context.options.matchByPosition
};
var commonHead = 0;
var commonTail = 0;
var index = void 0;
var index1 = void 0;
var index2 = void 0;
var array1 = context.left;
var array2 = context.right;
var len1 = array1.length;
var len2 = array2.length;
var child = void 0;
if (len1 > 0 && len2 > 0 && !matchContext.objectHash && typeof matchContext.matchByPosition !== 'boolean') {
matchContext.matchByPosition = !arraysHaveMatchByRef(array1, array2, len1, len2);
}
// separate common head
while (commonHead < len1 && commonHead < len2 && matchItems(array1, array2, commonHead, commonHead, matchContext)) {
index = commonHead;
child = new DiffContext(context.left[index], context.right[index]);
context.push(child, index);
commonHead++;
}
// separate common tail
while (commonTail + commonHead < len1 && commonTail + commonHead < len2 && matchItems(array1, array2, len1 - 1 - commonTail, len2 - 1 - commonTail, matchContext)) {
index1 = len1 - 1 - commonTail;
index2 = len2 - 1 - commonTail;
child = new DiffContext(context.left[index1], context.right[index2]);
context.push(child, index2);
commonTail++;
}
var result = void 0;
if (commonHead + commonTail === len1) {
if (len1 === len2) {
// arrays are identical
context.setResult(undefined).exit();
return;
}
// trivial case, a block (1 or more consecutive items) was added
result = result || {
_t: 'a'
};
for (index = commonHead; index < len2 - commonTail; index++) {
result[index] = [array2[index]];
}
context.setResult(result).exit();
return;
}
if (commonHead + commonTail === len2) {
// trivial case, a block (1 or more consecutive items) was removed
result = result || {
_t: 'a'
};
for (index = commonHead; index < len1 - commonTail; index++) {
result['_' + index] = [array1[index], 0, 0];
}
context.setResult(result).exit();
return;
}
// reset hash cache
delete matchContext.hashCache1;
delete matchContext.hashCache2;
// diff is not trivial, find the LCS (Longest Common Subsequence)
var trimmed1 = array1.slice(commonHead, len1 - commonTail);
var trimmed2 = array2.slice(commonHead, len2 - commonTail);
var seq = lcs.get(trimmed1, trimmed2, matchItems, matchContext);
var removedItems = [];
result = result || {
_t: 'a'
};
for (index = commonHead; index < len1 - commonTail; index++) {
if (arrayIndexOf(seq.indices1, index - commonHead) < 0) {
// removed
result['_' + index] = [array1[index], 0, 0];
removedItems.push(index);
}
}
var detectMove = true;
if (context.options && context.options.arrays && context.options.arrays.detectMove === false) {
detectMove = false;
}
var includeValueOnMove = false;
if (context.options && context.options.arrays && context.options.arrays.includeValueOnMove) {
includeValueOnMove = true;
}
var removedItemsLength = removedItems.length;
for (index = commonHead; index < len2 - commonTail; index++) {
var indexOnArray2 = arrayIndexOf(seq.indices2, index - commonHead);
if (indexOnArray2 < 0) {
// added, try to match with a removed item and register as position move
var isMove = false;
if (detectMove && removedItemsLength > 0) {
for (var removeItemIndex1 = 0; removeItemIndex1 < removedItemsLength; removeItemIndex1++) {
index1 = removedItems[removeItemIndex1];
if (matchItems(trimmed1, trimmed2, index1 - commonHead, index - commonHead, matchContext)) {
// store position move as: [originalValue, newPosition, ARRAY_MOVE]
result['_' + index1].splice(1, 2, index, ARRAY_MOVE);
if (!includeValueOnMove) {
// don't include moved value on diff, to save bytes
result['_' + index1][0] = '';
}
index2 = index;
child = new DiffContext(context.left[index1], context.right[index2]);
context.push(child, index2);
removedItems.splice(removeItemIndex1, 1);
isMove = true;
break;
}
}
}
if (!isMove) {
// added
result[index] = [array2[index]];
}
} else {
// match, do inner diff
index1 = seq.indices1[indexOnArray2] + commonHead;
index2 = seq.indices2[indexOnArray2] + commonHead;
child = new DiffContext(context.left[index1], context.right[index2]);
context.push(child, index2);
}
}
context.setResult(result).exit();
};
diffFilter$1.filterName = 'arrays';
var compare = {
numerically: function numerically(a, b) {
return a - b;
},
numericallyBy: function numericallyBy(name) {
return function (a, b) {
return a[name] - b[name];
};
}
};
var patchFilter$2 = function nestedPatchFilter(context) {
if (!context.nested) {
return;
}
if (context.delta._t !== 'a') {
return;
}
var index = void 0;
var index1 = void 0;
var delta = context.delta;
var array = context.left;
// first, separate removals, insertions and modifications
var toRemove = [];
var toInsert = [];
var toModify = [];
for (index in delta) {
if (index !== '_t') {
if (index[0] === '_') {
// removed item from original array
if (delta[index][2] === 0 || delta[index][2] === ARRAY_MOVE) {
toRemove.push(parseInt(index.slice(1), 10));
} else {
throw new Error('only removal or move can be applied at original array indices,' + (' invalid diff type: ' + delta[index][2]));
}
} else {
if (delta[index].length === 1) {
// added item at new array
toInsert.push({
index: parseInt(index, 10),
value: delta[index][0]
});
} else {
// modified item at new array
toModify.push({
index: parseInt(index, 10),
delta: delta[index]
});
}
}
}
}
// remove items, in reverse order to avoid sawing our own floor
toRemove = toRemove.sort(compare.numerically);
for (index = toRemove.length - 1; index >= 0; index--) {
index1 = toRemove[index];
var indexDiff = delta['_' + index1];
var removedValue = array.splice(index1, 1)[0];
if (indexDiff[2] === ARRAY_MOVE) {
// reinsert later
toInsert.push({
index: indexDiff[1],
value: removedValue
});
}
}
// insert items, in reverse order to avoid moving our own floor
toInsert = toInsert.sort(compare.numericallyBy('index'));
var toInsertLength = toInsert.length;
for (index = 0; index < toInsertLength; index++) {
var insertion = toInsert[index];
array.splice(insertion.index, 0, insertion.value);
}
// apply modifications
var toModifyLength = toModify.length;
var child = void 0;
if (toModifyLength > 0) {
for (index = 0; index < toModifyLength; index++) {
var modification = toModify[index];
child = new PatchContext(context.left[modification.index], modification.delta);
context.push(child, modification.index);
}
}
if (!context.children) {
context.setResult(context.left).exit();
return;
}
context.exit();
};
patchFilter$2.filterName = 'arrays';
var collectChildrenPatchFilter$1 = function collectChildrenPatchFilter(context) {
if (!context || !context.children) {
return;
}
if (context.delta._t !== 'a') {
return;
}
var length = context.children.length;
var child = void 0;
for (var index = 0; index < length; index++) {
child = context.children[index];
context.left[child.childName] = child.result;
}
context.setResult(context.left).exit();
};
collectChildrenPatchFilter$1.filterName = 'arraysCollectChildren';
var reverseFilter$2 = function arraysReverseFilter(context) {
if (!context.nested) {
if (context.delta[2] === ARRAY_MOVE) {
context.newName = '_' + context.delta[1];
context.setResult([context.delta[0], parseInt(context.childName.substr(1), 10), ARRAY_MOVE]).exit();
}
return;
}
if (context.delta._t !== 'a') {
return;
}
var name = void 0;
var child = void 0;
for (name in context.delta) {
if (name === '_t') {
continue;
}
child = new ReverseContext(context.delta[name]);
context.push(child, name);
}
context.exit();
};
reverseFilter$2.filterName = 'arrays';
var reverseArrayDeltaIndex = function reverseArrayDeltaIndex(delta, index, itemDelta) {
if (typeof index === 'string' && index[0] === '_') {
return parseInt(index.substr(1), 10);
} else if (isArray$2(itemDelta) && itemDelta[2] === 0) {
return '_' + index;
}
var reverseIndex = +index;
for (var deltaIndex in delta) {
var deltaItem = delta[deltaIndex];
if (isArray$2(deltaItem)) {
if (deltaItem[2] === ARRAY_MOVE) {
var moveFromIndex = parseInt(deltaIndex.substr(1), 10);
var moveToIndex = deltaItem[1];
if (moveToIndex === +index) {
return moveFromIndex;
}
if (moveFromIndex <= reverseIndex && moveToIndex > reverseIndex) {
reverseIndex++;
} else if (moveFromIndex >= reverseIndex && moveToIndex < reverseIndex) {
reverseIndex--;
}
} else if (deltaItem[2] === 0) {
var deleteIndex = parseInt(deltaIndex.substr(1), 10);
if (deleteIndex <= reverseIndex) {
reverseIndex++;
}
} else if (deltaItem.length === 1 && deltaIndex <= reverseIndex) {
reverseIndex--;
}
}
}
return reverseIndex;
};
function collectChildrenReverseFilter$1(context) {
if (!context || !context.children) {
return;
}
if (context.delta._t !== 'a') {
return;
}
var length = context.children.length;
var child = void 0;
var delta = {
_t: 'a'
};
for (var index = 0; index < length; index++) {
child = context.children[index];
var name = child.newName;
if (typeof name === 'undefined') {
name = reverseArrayDeltaIndex(context.delta, child.childName, child.result);
}
if (delta[name] !== child.result) {
delta[name] = child.result;
}
}
context.setResult(delta).exit();
}
collectChildrenReverseFilter$1.filterName = 'arraysCollectChildren';
var diffFilter$2 = function datesDiffFilter(context) {
if (context.left instanceof Date) {
if (context.right instanceof Date) {
if (context.left.getTime() !== context.right.getTime()) {
context.setResult([context.left, context.right]);
} else {
context.setResult(undefined);
}
} else {
context.setResult([context.left, context.right]);
}
context.exit();
} else if (context.right instanceof Date) {
context.setResult([context.left, context.right]).exit();
}
};
diffFilter$2.filterName = 'dates';
/* global diff_match_patch */
var TEXT_DIFF = 2;
var DEFAULT_MIN_LENGTH = 60;
var cachedDiffPatch = null;
var getDiffMatchPatch = function getDiffMatchPatch(required) {
/* jshint camelcase: false */
if (!cachedDiffPatch) {
var instance = void 0;
/* eslint-disable camelcase, new-cap */
if (typeof diff_match_patch !== 'undefined') {
// already loaded, probably a browser
instance = typeof diff_match_patch === 'function' ? new diff_match_patch() : new diff_match_patch.diff_match_patch();
} else if (dmp) {
try {
instance = dmp && new dmp();
} catch (err) {
instance = null;
}
}
/* eslint-enable camelcase, new-cap */
if (!instance) {
if (!required) {
return null;
}
var error = new Error('text diff_match_patch library not found');
// eslint-disable-next-line camelcase
error.diff_match_patch_not_found = true;
throw error;
}
cachedDiffPatch = {
diff: function diff(txt1, txt2) {
return instance.patch_toText(instance.patch_make(txt1, txt2));
},
patch: function patch(txt1, _patch) {
var results = instance.patch_apply(instance.patch_fromText(_patch), txt1);
for (var i = 0; i < results[1].length; i++) {
if (!results[1][i]) {
var _error = new Error('text patch failed');
_error.textPatchFailed = true;
}
}
return results[0];
}
};
}
return cachedDiffPatch;
};
var diffFilter$3 = function textsDiffFilter(context) {
if (context.leftType !== 'string') {
return;
}
var minLength = context.options && context.options.textDiff && context.options.textDiff.minLength || DEFAULT_MIN_LENGTH;
if (context.left.length < minLength || context.right.length < minLength) {
context.setResult([context.left, context.right]).exit();
return;
}
// large text, try to use a text-diff algorithm
var diffMatchPatch = getDiffMatchPatch();
if (!diffMatchPatch) {
// diff-match-patch library not available,
// fallback to regular string replace
context.setResult([context.left, context.right]).exit();
return;
}
var diff = diffMatchPatch.diff;
context.setResult([diff(context.left, context.right), 0, TEXT_DIFF]).exit();
};
diffFilter$3.filterName = 'texts';
var patchFilter$3 = function textsPatchFilter(context) {
if (context.nested) {
return;
}
if (context.delta[2] !== TEXT_DIFF) {
return;
}
// text-diff, use a text-patch algorithm
var patch = getDiffMatchPatch(true).patch;
context.setResult(patch(context.left, context.delta[0])).exit();
};
patchFilter$3.filterName = 'texts';
var textDeltaReverse = function textDeltaReverse(delta) {
var i = void 0;
var l = void 0;
var lines = void 0;
var line = void 0;
var lineTmp = void 0;
var header = null;
var headerRegex = /^@@ +-(\d+),(\d+) +\+(\d+),(\d+) +@@$/;
var lineHeader = void 0;
lines = delta.split('\n');
for (i = 0, l = lines.length; i < l; i++) {
line = lines[i];
var lineStart = line.slice(0, 1);
if (lineStart === '@') {
header = headerRegex.exec(line);
lineHeader = i;
// fix header
lines[lineHeader] = '@@ -' + header[3] + ',' + header[4] + ' +' + header[1] + ',' + header[2] + ' @@';
} else if (lineStart === '+') {
lines[i] = '-' + lines[i].slice(1);
if (lines[i - 1].slice(0, 1) === '+') {
// swap lines to keep default order (-+)
lineTmp = lines[i];
lines[i] = lines[i - 1];
lines[i - 1] = lineTmp;
}
} else if (lineStart === '-') {
lines[i] = '+' + lines[i].slice(1);
}
}
return lines.join('\n');
};
var reverseFilter$3 = function textsReverseFilter(context) {
if (context.nested) {
return;
}
if (context.delta[2] !== TEXT_DIFF) {
return;
}
// text-diff, use a text-diff algorithm
context.setResult([textDeltaReverse(context.delta[0]), 0, TEXT_DIFF]).exit();
};
reverseFilter$3.filterName = 'texts';
var DiffPatcher = function () {
function DiffPatcher(options) {
classCallCheck(this, DiffPatcher);
this.processor = new Processor(options);
this.processor.pipe(new Pipe('diff').append(collectChildrenDiffFilter, diffFilter, diffFilter$2, diffFilter$3, objectsDiffFilter, diffFilter$1).shouldHaveResult());
this.processor.pipe(new Pipe('patch').append(collectChildrenPatchFilter, collectChildrenPatchFilter$1, patchFilter, patchFilter$3, patchFilter$1, patchFilter$2).shouldHaveResult());
this.processor.pipe(new Pipe('reverse').append(collectChildrenReverseFilter, collectChildrenReverseFilter$1, reverseFilter, reverseFilter$3, reverseFilter$1, reverseFilter$2).shouldHaveResult());
}
createClass(DiffPatcher, [{
key: 'options',
value: function options() {
var _processor;
return (_processor = this.processor).options.apply(_processor, arguments);
}
}, {
key: 'diff',
value: function diff(left, right) {
return this.processor.process(new DiffContext(left, right));
}
}, {
key: 'patch',
value: function patch(left, delta) {
return this.processor.process(new PatchContext(left, delta));
}
}, {
key: 'reverse',
value: function reverse(delta) {
return this.processor.process(new ReverseContext(delta));
}
}, {
key: 'unpatch',
value: function unpatch(right, delta) {
return this.patch(right, this.reverse(delta));
}
}, {
key: 'clone',
value: function clone$$1(value) {
return clone(value);
}
}]);
return DiffPatcher;
}();
var isArray$3 = typeof Array.isArray === 'function' ? Array.isArray : function (a) {
return a instanceof Array;
};
var getObjectKeys = typeof Object.keys === 'function' ? function (obj) {
return Object.keys(obj);
} : function (obj) {
var names = [];
for (var property in obj) {
if (Object.prototype.hasOwnProperty.call(obj, property)) {
names.push(property);
}
}
return names;
};
var trimUnderscore = function trimUnderscore(str) {
if (str.substr(0, 1) === '_') {
return str.slice(1);
}
return str;
};
var arrayKeyToSortNumber = function arrayKeyToSortNumber(key) {
if (key === '_t') {
return -1;
} else {
if (key.substr(0, 1) === '_') {
return parseInt(key.slice(1), 10);
} else {
return parseInt(key, 10) + 0.1;
}
}
};
var arrayKeyComparer = function arrayKeyComparer(key1, key2) {
return arrayKeyToSortNumber(key1) - arrayKeyToSortNumber(key2);
};
var BaseFormatter = function () {
function BaseFormatter() {
classCallCheck(this, BaseFormatter);
}
createClass(BaseFormatter, [{
key: 'format',
value: function format(delta, left) {
var context = {};
this.prepareContext(context);
this.recurse(context, delta, left);
return this.finalize(context);
}
}, {
key: 'prepareContext',
value: function prepareContext(context) {
context.buffer = [];
context.out = function () {
var _buffer;
(_buffer = this.buffer).push.apply(_buffer, arguments);
};
}
}, {
key: 'typeFormattterNotFound',
value: function typeFormattterNotFound(context, deltaType) {
throw new Error('cannot format delta type: ' + deltaType);
}
}, {
key: 'typeFormattterErrorFormatter',
value: function typeFormattterErrorFormatter(context, err) {
return err.toString();
}
}, {
key: 'finalize',
value: function finalize(_ref) {
var buffer = _ref.buffer;
if (isArray$3(buffer)) {
return buffer.join('');
}
}
}, {
key: 'recurse',
value: function recurse(context, delta, left, key, leftKey, movedFrom, isLast) {
var useMoveOriginHere = delta && movedFrom;
var leftValue = useMoveOriginHere ? movedFrom.value : left;
if (typeof delta === 'undefined' && typeof key === 'undefined') {
return undefined;
}
var type = this.getDeltaType(delta, movedFrom);
var nodeType = type === 'node' ? delta._t === 'a' ? 'array' : 'object' : '';
if (typeof key !== 'undefined') {
this.nodeBegin(context, key, leftKey, type, nodeType, isLast);
} else {
this.rootBegin(context, type, nodeType);
}
var typeFormattter = void 0;
try {
typeFormattter = this['format_' + type] || this.typeFormattterNotFound(context, type);
typeFormattter.call(this, context, delta, leftValue, key, leftKey, movedFrom);
} catch (err) {
this.typeFormattterErrorFormatter(context, err, delta, leftValue, key, leftKey, movedFrom);
if (typeof console !== 'undefined' && console.error) {
console.error(err.stack);
}
}
if (typeof key !== 'undefined') {
this.nodeEnd(context, key, leftKey, type, nodeType, isLast);
} else {
this.rootEnd(context, type, nodeType);
}
}
}, {
key: 'formatDeltaChildren',
value: function formatDeltaChildren(context, delta, left) {
var self = this;
this.forEachDeltaKey(delta, left, function (key, leftKey, movedFrom, isLast) {
self.recurse(context, delta[key], left ? left[leftKey] : undefined, key, leftKey, movedFrom, isLast);
});
}
}, {
key: 'forEachDeltaKey',
value: function forEachDeltaKey(delta, left, fn) {
var keys = getObjectKeys(delta);
var arrayKeys = delta._t === 'a';
var moveDestinations = {};
var name = void 0;
if (typeof left !== 'undefined') {
for (name in left) {
if (Object.prototype.hasOwnProperty.call(left, name)) {
if (typeof delta[name] === 'undefined' && (!arrayKeys || typeof delta['_' + name] === 'undefined')) {
keys.push(name);
}
}
}
}
// look for move destinations
for (name in delta) {
if (Object.prototype.hasOwnProperty.call(delta, name)) {
var value = delta[name];
if (isArray$3(value) && value[2] === 3) {
moveDestinations[value[1].toString()] = {
key: name,
value: left && left[parseInt(name.substr(1))]
};
if (this.includeMoveDestinations !== false) {
if (typeof left === 'undefined' && typeof delta[value[1]] === 'undefined') {
keys.push(value[1].toString());
}
}
}
}
}
if (arrayKeys) {
keys.sort(arrayKeyComparer);
} else {
keys.sort();
}
for (var index = 0, length = keys.length; index < length; index++) {
var key = keys[index];
if (arrayKeys && key === '_t') {
continue;
}
var leftKey = arrayKeys ? typeof key === 'number' ? key : parseInt(trimUnderscore(key), 10) : key;
var isLast = index === length - 1;
fn(key, leftKey, moveDestinations[leftKey], isLast);
}
}
}, {
key: 'getDeltaType',
value: function getDeltaType(delta, movedFrom) {
if (typeof delta === 'undefined') {
if (typeof movedFrom !== 'undefined') {
return 'movedestination';
}
return 'unchanged';
}
if (isArray$3(delta)) {
if (delta.length === 1) {
return 'added';
}
if (delta.length === 2) {
return 'modified';
}
if (delta.length === 3 && delta[2] === 0) {
return 'deleted';
}
if (delta.length === 3 && delta[2] === 2) {
return 'textdiff';
}
if (delta.length === 3 && delta[2] === 3) {
return 'moved';
}
} else if ((typeof delta === 'undefined' ? 'undefined' : _typeof(delta)) === 'object') {
return 'node';
}
return 'unknown';
}
}, {
key: 'parseTextDiff',
value: function parseTextDiff(value) {
var output = [];
var lines = value.split('\n@@ ');
for (var i = 0, l = lines.length; i < l; i++) {
var line = lines[i];
var lineOutput = {
pieces: []
};
var location = /^(?:@@ )?[-+]?(\d+),(\d+)/.exec(line).slice(1);
lineOutput.location = {
line: location[0],
chr: location[1]
};
var pieces = line.split('\n').slice(1);
for (var pieceIndex = 0, piecesLength = pieces.length; pieceIndex < piecesLength; pieceIndex++) {
var piece = pieces[pieceIndex];
if (!piece.length) {
continue;
}
var pieceOutput = {
type: 'context'
};
if (piece.substr(0, 1) === '+') {
pieceOutput.type = 'added';
} else if (piece.substr(0, 1) === '-') {
pieceOutput.type = 'deleted';
}
pieceOutput.text = piece.slice(1);
lineOutput.pieces.push(pieceOutput);
}
output.push(lineOutput);
}
return output;
}
}]);
return BaseFormatter;
}();
var base = Object.freeze({
default: BaseFormatter
});
var HtmlFormatter = function (_BaseFormatter) {
inherits(HtmlFormatter, _BaseFormatter);
function HtmlFormatter() {
classCallCheck(this, HtmlFormatter);
return possibleConstructorReturn(this, (HtmlFormatter.__proto__ || Object.getPrototypeOf(HtmlFormatter)).apply(this, arguments));
}
createClass(HtmlFormatter, [{
key: 'typeFormattterErrorFormatter',
value: function typeFormattterErrorFormatter(context, err) {
context.out('<pre class="jsondiffpatch-error">' + err + '</pre>');
}
}, {
key: 'formatValue',
value: function formatValue(context, value) {
context.out('<pre>' + htmlEscape(JSON.stringify(value, null, 2)) + '</pre>');
}
}, {
key: 'formatTextDiffString',
value: function formatTextDiffString(context, value) {
var lines = this.parseTextDiff(value);
context.out('<ul class="jsondiffpatch-textdiff">');
for (var i = 0, l = lines.length; i < l; i++) {
var line = lines[i];
context.out('<li><div class="jsondiffpatch-textdiff-location">' + ('<span class="jsondiffpatch-textdiff-line-number">' + line.location.line + '</span><span class="jsondiffpatch-textdiff-char">' + line.location.chr + '</span></div><div class="jsondiffpatch-textdiff-line">'));
var pieces = line.pieces;
for (var pieceIndex = 0, piecesLength = pieces.length; pieceIndex < piecesLength; pieceIndex++) {
/* global decodeURI */
var piece = pieces[pieceIndex];
context.out('<span class="jsondiffpatch-textdiff-' + piece.type + '">' + htmlEscape(decodeURI(piece.text)) + '</span>');
}
context.out('</div></li>');
}
context.out('</ul>');
}
}, {
key: 'rootBegin',
value: function rootBegin(context, type, nodeType) {
var nodeClass = 'jsondiffpatch-' + type + (nodeType ? ' jsondiffpatch-child-node-type-' + nodeType : '');
context.out('<div class="jsondiffpatch-delta ' + nodeClass + '">');
}
}, {
key: 'rootEnd',
value: function rootEnd(context) {
context.out('</div>' + (context.hasArrows ? '<script type="text/javascript">setTimeout(' + (adjustArrows.toString() + ',10);</script>') : ''));
}
}, {
key: 'nodeBegin',
value: function nodeBegin(context, key, leftKey, type, nodeType) {
var nodeClass = 'jsondiffpatch-' + type + (nodeType ? ' jsondiffpatch-child-node-type-' + nodeType : '');
context.out('<li class="' + nodeClass + '" data-key="' + leftKey + '">' + ('<div class="jsondiffpatch-property-name">' + leftKey + '</div>'));
}
}, {
key: 'nodeEnd',
value: function nodeEnd(context) {
context.out('</li>');
}
/* jshint camelcase: false */
/* eslint-disable camelcase */
}, {
key: 'format_unchanged',
value: function format_unchanged(context, delta, left) {
if (typeof left === 'undefined') {
return;
}
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, left);
context.out('</div>');
}
}, {
key: 'format_movedestination',
value: function format_movedestination(context, delta, left) {
if (typeof left === 'undefined') {
return;
}
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, left);
context.out('</div>');
}
}, {
key: 'format_node',
value: function format_node(context, delta, left) {
// recurse
var nodeType = delta._t === 'a' ? 'array' : 'object';
context.out('<ul class="jsondiffpatch-node jsondiffpatch-node-type-' + nodeType + '">');
this.formatDeltaChildren(context, delta, left);
context.out('</ul>');
}
}, {
key: 'format_added',
value: function format_added(context, delta) {
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, delta[0]);
context.out('</div>');
}
}, {
key: 'format_modified',
value: function format_modified(context, delta) {
context.out('<div class="jsondiffpatch-value jsondiffpatch-left-value">');
this.formatValue(context, delta[0]);
context.out('</div>' + '<div class="jsondiffpatch-value jsondiffpatch-right-value">');
this.formatValue(context, delta[1]);
context.out('</div>');
}
}, {
key: 'format_deleted',
value: function format_deleted(context, delta) {
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, delta[0]);
context.out('</div>');
}
}, {
key: 'format_moved',
value: function format_moved(context, delta) {
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, delta[0]);
context.out('</div><div class="jsondiffpatch-moved-destination">' + delta[1] + '</div>');
// draw an SVG arrow from here to move destination
context.out(
/* jshint multistr: true */
'<div class="jsondiffpatch-arrow" ' + 'style="position: relative; left: -34px;">\n <svg width="30" height="60" ' + 'style="position: absolute; display: none;">\n <defs>\n <marker id="markerArrow" markerWidth="8" markerHeight="8"\n refx="2" refy="4"\n orient="auto" markerUnits="userSpaceOnUse">\n <path d="M1,1 L1,7 L7,4 L1,1" style="fill: #339;" />\n </marker>\n </defs>\n <path d="M30,0 Q-10,25 26,50"\n style="stroke: #88f; stroke-width: 2px; fill: none; ' + 'stroke-opacity: 0.5; marker-end: url(#markerArrow);"\n ></path>\n </svg>\n </div>');
context.hasArrows = true;
}
}, {
key: 'format_textdiff',
value: function format_textdiff(context, delta) {
context.out('<div class="jsondiffpatch-value">');
this.formatTextDiffString(context, delta[0]);
context.out('</div>');
}
}]);
return HtmlFormatter;
}(BaseFormatter);
function htmlEscape(text) {
var html = text;
var replacements = [[/&/g, '&amp;'], [/</g, '&lt;'], [/>/g, '&gt;'], [/'/g, '&apos;'], [/"/g, '&quot;']];
for (var i = 0; i < replacements.length; i++) {
html = html.replace(replacements[i][0], replacements[i][1]);
}
return html;
}
var adjustArrows = function jsondiffpatchHtmlFormatterAdjustArrows(nodeArg) {
var node = nodeArg || document;
var getElementText = function getElementText(_ref) {
var textContent = _ref.textContent,
innerText = _ref.innerText;
return textContent || innerText;
};
var eachByQuery = function eachByQuery(el, query, fn) {
var elems = el.querySelectorAll(query);
for (var i = 0, l = elems.length; i < l; i++) {
fn(elems[i]);
}
};
var eachChildren = function eachChildren(_ref2, fn) {
var children = _ref2.children;
for (var i = 0, l = children.length; i < l; i++) {
fn(children[i], i);
}
};
eachByQuery(node, '.jsondiffpatch-arrow', function (_ref3) {
var parentNode = _ref3.parentNode,
children = _ref3.children,
style = _ref3.style;
var arrowParent = parentNode;
var svg = children[0];
var path = svg.children[1];
svg.style.display = 'none';
var destination = getElementText(arrowParent.querySelector('.jsondiffpatch-moved-destination'));
var container = arrowParent.parentNode;
var destinationElem = void 0;
eachChildren(container, function (child) {
if (child.getAttribute('data-key') === destination) {
destinationElem = child;
}
});
if (!destinationElem) {
return;
}
try {
var distance = destinationElem.offsetTop - arrowParent.offsetTop;
svg.setAttribute('height', Math.abs(distance) + 6);
style.top = -8 + (distance > 0 ? 0 : distance) + 'px';
var curve = distance > 0 ? 'M30,0 Q-10,' + Math.round(distance / 2) + ' 26,' + (distance - 4) : 'M30,' + -distance + ' Q-10,' + Math.round(-distance / 2) + ' 26,4';
path.setAttribute('d', curve);
svg.style.display = '';
} catch (err) {}
});
};
/* jshint camelcase: true */
/* eslint-enable camelcase */
var showUnchanged = function showUnchanged(show, node, delay) {
var el = node || document.body;
var prefix = 'jsondiffpatch-unchanged-';
var classes = {
showing: prefix + 'showing',
hiding: prefix + 'hiding',
visible: prefix + 'visible',
hidden: prefix + 'hidden'
};
var list = el.classList;
if (!list) {
return;
}
if (!delay) {
list.remove(classes.showing);
list.remove(classes.hiding);
list.remove(classes.visible);
list.remove(classes.hidden);
if (show === false) {
list.add(classes.hidden);
}
return;
}
if (show === false) {
list.remove(classes.showing);
list.add(classes.visible);
setTimeout(function () {
list.add(classes.hiding);
}, 10);
} else {
list.remove(classes.hiding);
list.add(classes.showing);
list.remove(classes.hidden);
}
var intervalId = setInterval(function () {
adjustArrows(el);
}, 100);
setTimeout(function () {
list.remove(classes.showing);
list.remove(classes.hiding);
if (show === false) {
list.add(classes.hidden);
list.remove(classes.visible);
} else {
list.add(classes.visible);
list.remove(classes.hidden);
}
setTimeout(function () {
list.remove(classes.visible);
clearInterval(intervalId);
}, delay + 400);
}, delay);
};
var hideUnchanged = function hideUnchanged(node, delay) {
return showUnchanged(false, node, delay);
};
var defaultInstance = void 0;
function format(delta, left) {
if (!defaultInstance) {
defaultInstance = new HtmlFormatter();
}
return defaultInstance.format(delta, left);
}
var html = Object.freeze({
showUnchanged: showUnchanged,
hideUnchanged: hideUnchanged,
default: HtmlFormatter,
format: format
});
var AnnotatedFormatter = function (_BaseFormatter) {
inherits(AnnotatedFormatter, _BaseFormatter);
function AnnotatedFormatter() {
classCallCheck(this, AnnotatedFormatter);
var _this = possibleConstructorReturn(this, (AnnotatedFormatter.__proto__ || Object.getPrototypeOf(AnnotatedFormatter)).call(this));
_this.includeMoveDestinations = false;
return _this;
}
createClass(AnnotatedFormatter, [{
key: 'prepareContext',
value: function prepareContext(context) {
get(AnnotatedFormatter.prototype.__proto__ || Object.getPrototypeOf(AnnotatedFormatter.prototype), 'prepareContext', this).call(this, context);
context.indent = function (levels) {
this.indentLevel = (this.indentLevel || 0) + (typeof levels === 'undefined' ? 1 : levels);
this.indentPad = new Array(this.indentLevel + 1).join('&nbsp;&nbsp;');
};
context.row = function (json, htmlNote) {
context.out('<tr><td style="white-space: nowrap;">' + '<pre class="jsondiffpatch-annotated-indent"' + ' style="display: inline-block">');
context.out(context.indentPad);
context.out('</pre><pre style="display: inline-block">');
context.out(json);
context.out('</pre></td><td class="jsondiffpatch-delta-note"><div>');
context.out(htmlNote);
context.out('</div></td></tr>');
};
}
}, {
key: 'typeFormattterErrorFormatter',
value: function typeFormattterErrorFormatter(context, err) {
context.row('', '<pre class="jsondiffpatch-error">' + err + '</pre>');
}
}, {
key: 'formatTextDiffString',
value: function formatTextDiffString(context, value) {
var lines = this.parseTextDiff(value);
context.out('<ul class="jsondiffpatch-textdiff">');
for (var i = 0, l = lines.length; i < l; i++) {
var line = lines[i];
context.out('<li><div class="jsondiffpatch-textdiff-location">' + ('<span class="jsondiffpatch-textdiff-line-number">' + line.location.line + '</span><span class="jsondiffpatch-textdiff-char">' + line.location.chr + '</span></div><div class="jsondiffpatch-textdiff-line">'));
var pieces = line.pieces;
for (var pieceIndex = 0, piecesLength = pieces.length; pieceIndex < piecesLength; pieceIndex++) {
var piece = pieces[pieceIndex];
context.out('<span class="jsondiffpatch-textdiff-' + piece.type + '">' + piece.text + '</span>');
}
context.out('</div></li>');
}
context.out('</ul>');
}
}, {
key: 'rootBegin',
value: function rootBegin(context, type, nodeType) {
context.out('<table class="jsondiffpatch-annotated-delta">');
if (type === 'node') {
context.row('{');
context.indent();
}
if (nodeType === 'array') {
context.row('"_t": "a",', 'Array delta (member names indicate array indices)');
}
}
}, {
key: 'rootEnd',
value: function rootEnd(context, type) {
if (type === 'node') {
context.indent(-1);
context.row('}');
}
context.out('</table>');
}
}, {
key: 'nodeBegin',
value: function nodeBegin(context, key, leftKey, type, nodeType) {
context.row('&quot;' + key + '&quot;: {');
if (type === 'node') {
context.indent();
}
if (nodeType === 'array') {
context.row('"_t": "a",', 'Array delta (member names indicate array indices)');
}
}
}, {
key: 'nodeEnd',
value: function nodeEnd(context, key, leftKey, type, nodeType, isLast) {
if (type === 'node') {
context.indent(-1);
}
context.row('}' + (isLast ? '' : ','));
}
/* jshint camelcase: false */
/* eslint-disable camelcase */
}, {
key: 'format_unchanged',
value: function format_unchanged() {}
}, {
key: 'format_movedestination',
value: function format_movedestination() {}
}, {
key: 'format_node',
value: function format_node(context, delta, left) {
// recurse
this.formatDeltaChildren(context, delta, left);
}
}]);
return AnnotatedFormatter;
}(BaseFormatter);
/* eslint-enable camelcase */
var wrapPropertyName = function wrapPropertyName(name) {
return '<pre style="display:inline-block">&quot;' + name + '&quot;</pre>';
};
var deltaAnnotations = {
added: function added(delta, left, key, leftKey) {
var formatLegend = ' <pre>([newValue])</pre>';
if (typeof leftKey === 'undefined') {
return 'new value' + formatLegend;
}
if (typeof leftKey === 'number') {
return 'insert at index ' + leftKey + formatLegend;
}
return 'add property ' + wrapPropertyName(leftKey) + formatLegend;
},
modified: function modified(delta, left, key, leftKey) {
var formatLegend = ' <pre>([previousValue, newValue])</pre>';
if (typeof leftKey === 'undefined') {
return 'modify value' + formatLegend;
}
if (typeof leftKey === 'number') {
return 'modify at index ' + leftKey + formatLegend;
}
return 'modify property ' + wrapPropertyName(leftKey) + formatLegend;
},
deleted: function deleted(delta, left, key, leftKey) {
var formatLegend = ' <pre>([previousValue, 0, 0])</pre>';
if (typeof leftKey === 'undefined') {
return 'delete value' + formatLegend;
}
if (typeof leftKey === 'number') {
return 'remove index ' + leftKey + formatLegend;
}
return 'delete property ' + wrapPropertyName(leftKey) + formatLegend;
},
moved: function moved(delta, left, key, leftKey) {
return 'move from <span title="(position to remove at original state)">' + ('index ' + leftKey + '</span> to <span title="(position to insert at final') + (' state)">index ' + delta[1] + '</span>');
},
textdiff: function textdiff(delta, left, key, leftKey) {
var location = typeof leftKey === 'undefined' ? '' : typeof leftKey === 'number' ? ' at index ' + leftKey : ' at property ' + wrapPropertyName(leftKey);
return 'text diff' + location + ', format is <a href="https://code.google.com/' + 'p/google-diff-match-patch/wiki/Unidiff">a variation of Unidiff</a>';
}
};
var formatAnyChange = function formatAnyChange(context, delta) {
var deltaType = this.getDeltaType(delta);
var annotator = deltaAnnotations[deltaType];
var htmlNote = annotator && annotator.apply(annotator, Array.prototype.slice.call(arguments, 1));
var json = JSON.stringify(delta, null, 2);
if (deltaType === 'textdiff') {
// split text diffs lines
json = json.split('\\n').join('\\n"+\n "');
}
context.indent();
context.row(json, htmlNote);
context.indent(-1);
};
/* eslint-disable camelcase */
AnnotatedFormatter.prototype.format_added = formatAnyChange;
AnnotatedFormatter.prototype.format_modified = formatAnyChange;
AnnotatedFormatter.prototype.format_deleted = formatAnyChange;
AnnotatedFormatter.prototype.format_moved = formatAnyChange;
AnnotatedFormatter.prototype.format_textdiff = formatAnyChange;
var defaultInstance$1 = void 0;
function format$1(delta, left) {
if (!defaultInstance$1) {
defaultInstance$1 = new AnnotatedFormatter();
}
return defaultInstance$1.format(delta, left);
}
var annotated = Object.freeze({
default: AnnotatedFormatter,
format: format$1
});
var OPERATIONS = {
add: 'add',
remove: 'remove',
replace: 'replace',
move: 'move'
};
var JSONFormatter = function (_BaseFormatter) {
inherits(JSONFormatter, _BaseFormatter);
function JSONFormatter() {
classCallCheck(this, JSONFormatter);
var _this = possibleConstructorReturn(this, (JSONFormatter.__proto__ || Object.getPrototypeOf(JSONFormatter)).call(this));
_this.includeMoveDestinations = true;
return _this;
}
createClass(JSONFormatter, [{
key: 'prepareContext',
value: function prepareContext(context) {
get(JSONFormatter.prototype.__proto__ || Object.getPrototypeOf(JSONFormatter.prototype), 'prepareContext', this).call(this, context);
context.result = [];
context.path = [];
context.pushCurrentOp = function (obj) {
var op = obj.op,
value = obj.value;
var val = {
op: op,
path: this.currentPath()
};
if (typeof value !== 'undefined') {
val.value = value;
}
this.result.push(val);
};
context.pushMoveOp = function (to) {
var from = this.currentPath();
this.result.push({
op: OPERATIONS.move,
from: from,
path: this.toPath(to)
});
};
context.currentPath = function () {
return '/' + this.path.join('/');
};
context.toPath = function (toPath) {
var to = this.path.slice();
to[to.length - 1] = toPath;
return '/' + to.join('/');
};
}
}, {
key: 'typeFormattterErrorFormatter',
value: function typeFormattterErrorFormatter(context, err) {
context.out('[ERROR] ' + err);
}
}, {
key: 'rootBegin',
value: function rootBegin() {}
}, {
key: 'rootEnd',
value: function rootEnd() {}
}, {
key: 'nodeBegin',
value: function nodeBegin(_ref, key, leftKey) {
var path = _ref.path;
path.push(leftKey);
}
}, {
key: 'nodeEnd',
value: function nodeEnd(_ref2) {
var path = _ref2.path;
path.pop();
}
/* jshint camelcase: false */
/* eslint-disable camelcase */
}, {
key: 'format_unchanged',
value: function format_unchanged() {}
}, {
key: 'format_movedestination',
value: function format_movedestination() {}
}, {
key: 'format_node',
value: function format_node(context, delta, left) {
this.formatDeltaChildren(context, delta, left);
}
}, {
key: 'format_added',
value: function format_added(context, delta) {
context.pushCurrentOp({ op: OPERATIONS.add, value: delta[0] });
}
}, {
key: 'format_modified',
value: function format_modified(context, delta) {
context.pushCurrentOp({ op: OPERATIONS.replace, value: delta[1] });
}
}, {
key: 'format_deleted',
value: function format_deleted(context) {
context.pushCurrentOp({ op: OPERATIONS.remove });
}
}, {
key: 'format_moved',
value: function format_moved(context, delta) {
var to = delta[1];
context.pushMoveOp(to);
}
}, {
key: 'format_textdiff',
value: function format_textdiff() {
throw new Error('Not implemented');
}
}, {
key: 'format',
value: function format(delta, left) {
var context = {};
this.prepareContext(context);
this.recurse(context, delta, left);
return context.result;
}
}]);
return JSONFormatter;
}(BaseFormatter);
var last = function last(arr) {
return arr[arr.length - 1];
};
var sortBy = function sortBy(arr, pred) {
arr.sort(pred);
return arr;
};
var compareByIndexDesc = function compareByIndexDesc(indexA, indexB) {
var lastA = parseInt(indexA, 10);
var lastB = parseInt(indexB, 10);
if (!(isNaN(lastA) || isNaN(lastB))) {
return lastB - lastA;
} else {
return 0;
}
};
var opsByDescendingOrder = function opsByDescendingOrder(removeOps) {
return sortBy(removeOps, function (a, b) {
var splitA = a.path.split('/');
var splitB = b.path.split('/');
if (splitA.length !== splitB.length) {
return splitA.length - splitB.length;
} else {
return compareByIndexDesc(last(splitA), last(splitB));
}
});
};
var partitionOps = function partitionOps(arr, fns) {
var initArr = Array(fns.length + 1).fill().map(function () {
return [];
});
return arr.map(function (item) {
var position = fns.map(function (fn) {
return fn(item);
}).indexOf(true);
if (position < 0) {
position = fns.length;
}
return { item: item, position: position };
}).reduce(function (acc, item) {
acc[item.position].push(item.item);
return acc;
}, initArr);
};
var isMoveOp = function isMoveOp(_ref3) {
var op = _ref3.op;
return op === 'move';
};
var isRemoveOp = function isRemoveOp(_ref4) {
var op = _ref4.op;
return op === 'remove';
};
var reorderOps = function reorderOps(diff) {
var _partitionOps = partitionOps(diff, [isMoveOp, isRemoveOp]),
_partitionOps2 = slicedToArray(_partitionOps, 3),
moveOps = _partitionOps2[0],
removedOps = _partitionOps2[1],
restOps = _partitionOps2[2];
var removeOpsReverse = opsByDescendingOrder(removedOps);
return [].concat(toConsumableArray(removeOpsReverse), toConsumableArray(moveOps), toConsumableArray(restOps));
};
var defaultInstance$2 = void 0;
var format$2 = function format(delta, left) {
if (!defaultInstance$2) {
defaultInstance$2 = new JSONFormatter();
}
return reorderOps(defaultInstance$2.format(delta, left));
};
var log = function log(delta, left) {
console.log(format$2(delta, left));
};
var jsonpatch = Object.freeze({
default: JSONFormatter,
partitionOps: partitionOps,
format: format$2,
log: log
});
function chalkColor(name) {
return chalk && chalk[name] || function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return args;
};
}
var colors = {
added: chalkColor('green'),
deleted: chalkColor('red'),
movedestination: chalkColor('gray'),
moved: chalkColor('yellow'),
unchanged: chalkColor('gray'),
error: chalkColor('white.bgRed'),
textDiffLine: chalkColor('gray')
};
var ConsoleFormatter = function (_BaseFormatter) {
inherits(ConsoleFormatter, _BaseFormatter);
function ConsoleFormatter() {
classCallCheck(this, ConsoleFormatter);
var _this = possibleConstructorReturn(this, (ConsoleFormatter.__proto__ || Object.getPrototypeOf(ConsoleFormatter)).call(this));
_this.includeMoveDestinations = false;
return _this;
}
createClass(ConsoleFormatter, [{
key: 'prepareContext',
value: function prepareContext(context) {
get(ConsoleFormatter.prototype.__proto__ || Object.getPrototypeOf(ConsoleFormatter.prototype), 'prepareContext', this).call(this, context);
context.indent = function (levels) {
this.indentLevel = (this.indentLevel || 0) + (typeof levels === 'undefined' ? 1 : levels);
this.indentPad = new Array(this.indentLevel + 1).join(' ');
this.outLine();
};
context.outLine = function () {
this.buffer.push('\n' + (this.indentPad || ''));
};
context.out = function () {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
for (var i = 0, l = args.length; i < l; i++) {
var lines = args[i].split('\n');
var text = lines.join('\n' + (this.indentPad || ''));
if (this.color && this.color[0]) {
text = this.color[0](text);
}
this.buffer.push(text);
}
};
context.pushColor = function (color) {
this.color = this.color || [];
this.color.unshift(color);
};
context.popColor = function () {
this.color = this.color || [];
this.color.shift();
};
}
}, {
key: 'typeFormattterErrorFormatter',
value: function typeFormattterErrorFormatter(context, err) {
context.pushColor(colors.error);
context.out('[ERROR]' + err);
context.popColor();
}
}, {
key: 'formatValue',
value: function formatValue(context, value) {
context.out(JSON.stringify(value, null, 2));
}
}, {
key: 'formatTextDiffString',
value: function formatTextDiffString(context, value) {
var lines = this.parseTextDiff(value);
context.indent();
for (var i = 0, l = lines.length; i < l; i++) {
var line = lines[i];
context.pushColor(colors.textDiffLine);
context.out(line.location.line + ',' + line.location.chr + ' ');
context.popColor();
var pieces = line.pieces;
for (var pieceIndex = 0, piecesLength = pieces.length; pieceIndex < piecesLength; pieceIndex++) {
var piece = pieces[pieceIndex];
context.pushColor(colors[piece.type]);
context.out(piece.text);
context.popColor();
}
if (i < l - 1) {
context.outLine();
}
}
context.indent(-1);
}
}, {
key: 'rootBegin',
value: function rootBegin(context, type, nodeType) {
context.pushColor(colors[type]);
if (type === 'node') {
context.out(nodeType === 'array' ? '[' : '{');
context.indent();
}
}
}, {
key: 'rootEnd',
value: function rootEnd(context, type, nodeType) {
if (type === 'node') {
context.indent(-1);
context.out(nodeType === 'array' ? ']' : '}');
}
context.popColor();
}
}, {
key: 'nodeBegin',
value: function nodeBegin(context, key, leftKey, type, nodeType) {
context.pushColor(colors[type]);
context.out(leftKey + ': ');
if (type === 'node') {
context.out(nodeType === 'array' ? '[' : '{');
context.indent();
}
}
}, {
key: 'nodeEnd',
value: function nodeEnd(context, key, leftKey, type, nodeType, isLast) {
if (type === 'node') {
context.indent(-1);
context.out(nodeType === 'array' ? ']' : '}' + (isLast ? '' : ','));
}
if (!isLast) {
context.outLine();
}
context.popColor();
}
/* jshint camelcase: false */
/* eslint-disable camelcase */
}, {
key: 'format_unchanged',
value: function format_unchanged(context, delta, left) {
if (typeof left === 'undefined') {
return;
}
this.formatValue(context, left);
}
}, {
key: 'format_movedestination',
value: function format_movedestination(context, delta, left) {
if (typeof left === 'undefined') {
return;
}
this.formatValue(context, left);
}
}, {
key: 'format_node',
value: function format_node(context, delta, left) {
// recurse
this.formatDeltaChildren(context, delta, left);
}
}, {
key: 'format_added',
value: function format_added(context, delta) {
this.formatValue(context, delta[0]);
}
}, {
key: 'format_modified',
value: function format_modified(context, delta) {
context.pushColor(colors.deleted);
this.formatValue(context, delta[0]);
context.popColor();
context.out(' => ');
context.pushColor(colors.added);
this.formatValue(context, delta[1]);
context.popColor();
}
}, {
key: 'format_deleted',
value: function format_deleted(context, delta) {
this.formatValue(context, delta[0]);
}
}, {
key: 'format_moved',
value: function format_moved(context, delta) {
context.out('==> ' + delta[1]);
}
}, {
key: 'format_textdiff',
value: function format_textdiff(context, delta) {
this.formatTextDiffString(context, delta[0]);
}
}]);
return ConsoleFormatter;
}(BaseFormatter);
var defaultInstance$3 = void 0;
var format$3 = function format(delta, left) {
if (!defaultInstance$3) {
defaultInstance$3 = new ConsoleFormatter();
}
return defaultInstance$3.format(delta, left);
};
function log$1(delta, left) {
console.log(format$3(delta, left));
}
var console$1 = Object.freeze({
default: ConsoleFormatter,
format: format$3,
log: log$1
});
var index = Object.freeze({
base: base,
html: html,
annotated: annotated,
jsonpatch: jsonpatch,
console: console$1
});
// use as 2nd parameter for JSON.parse to revive Date instances
function dateReviver(key, value) {
var parts = void 0;
if (typeof value === 'string') {
// eslint-disable-next-line max-len
parts = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d*))?(Z|([+-])(\d{2}):(\d{2}))$/.exec(value);
if (parts) {
return new Date(Date.UTC(+parts[1], +parts[2] - 1, +parts[3], +parts[4], +parts[5], +parts[6], +(parts[7] || 0)));
}
}
return value;
}
function create(options) {
return new DiffPatcher(options);
}
var defaultInstance$4 = void 0;
function diff() {
if (!defaultInstance$4) {
defaultInstance$4 = new DiffPatcher();
}
return defaultInstance$4.diff.apply(defaultInstance$4, arguments);
}
function patch() {
if (!defaultInstance$4) {
defaultInstance$4 = new DiffPatcher();
}
return defaultInstance$4.patch.apply(defaultInstance$4, arguments);
}
function unpatch() {
if (!defaultInstance$4) {
defaultInstance$4 = new DiffPatcher();
}
return defaultInstance$4.unpatch.apply(defaultInstance$4, arguments);
}
function reverse() {
if (!defaultInstance$4) {
defaultInstance$4 = new DiffPatcher();
}
return defaultInstance$4.reverse.apply(defaultInstance$4, arguments);
}
function clone$1() {
if (!defaultInstance$4) {
defaultInstance$4 = new DiffPatcher();
}
return defaultInstance$4.clone.apply(defaultInstance$4, arguments);
}
export { DiffPatcher, index as formatters, console$1 as console, create, dateReviver, diff, patch, unpatch, reverse, clone$1 as clone };
//# sourceMappingURL=jsondiffpatch.esm.js.map