>} Two element Array, containing the
+ * new text and an array of boolean values.
+ */
+diff_match_patch.prototype.patch_apply = function(patches, text) {
+ if (patches.length == 0) {
+ return [text, []];
+ }
+
+ // Deep copy the patches so that no changes are made to originals.
+ patches = this.patch_deepCopy(patches);
+
+ var nullPadding = this.patch_addPadding(patches);
+ text = nullPadding + text + nullPadding;
+
+ this.patch_splitMax(patches);
+ // delta keeps track of the offset between the expected and actual location
+ // of the previous patch. If there are patches expected at positions 10 and
+ // 20, but the first patch was found at 12, delta is 2 and the second patch
+ // has an effective expected position of 22.
+ var delta = 0;
+ var results = [];
+ for (var x = 0; x < patches.length; x++) {
+ var expected_loc = patches[x].start2 + delta;
+ var text1 = this.diff_text1(patches[x].diffs);
+ var start_loc;
+ var end_loc = -1;
+ if (text1.length > this.Match_MaxBits) {
+ // patch_splitMax will only provide an oversized pattern in the case of
+ // a monster delete.
+ start_loc = this.match_main(text, text1.substring(0, this.Match_MaxBits),
+ expected_loc);
+ if (start_loc != -1) {
+ end_loc = this.match_main(text,
+ text1.substring(text1.length - this.Match_MaxBits),
+ expected_loc + text1.length - this.Match_MaxBits);
+ if (end_loc == -1 || start_loc >= end_loc) {
+ // Can't find valid trailing context. Drop this patch.
+ start_loc = -1;
+ }
+ }
+ } else {
+ start_loc = this.match_main(text, text1, expected_loc);
+ }
+ if (start_loc == -1) {
+ // No match found. :(
+ results[x] = false;
+ // Subtract the delta for this failed patch from subsequent patches.
+ delta -= patches[x].length2 - patches[x].length1;
+ } else {
+ // Found a match. :)
+ results[x] = true;
+ delta = start_loc - expected_loc;
+ var text2;
+ if (end_loc == -1) {
+ text2 = text.substring(start_loc, start_loc + text1.length);
+ } else {
+ text2 = text.substring(start_loc, end_loc + this.Match_MaxBits);
+ }
+ if (text1 == text2) {
+ // Perfect match, just shove the replacement text in.
+ text = text.substring(0, start_loc) +
+ this.diff_text2(patches[x].diffs) +
+ text.substring(start_loc + text1.length);
+ } else {
+ // Imperfect match. Run a diff to get a framework of equivalent
+ // indices.
+ var diffs = this.diff_main(text1, text2, false);
+ if (text1.length > this.Match_MaxBits &&
+ this.diff_levenshtein(diffs) / text1.length >
+ this.Patch_DeleteThreshold) {
+ // The end points match, but the content is unacceptably bad.
+ results[x] = false;
+ } else {
+ this.diff_cleanupSemanticLossless(diffs);
+ var index1 = 0;
+ var index2;
+ for (var y = 0; y < patches[x].diffs.length; y++) {
+ var mod = patches[x].diffs[y];
+ if (mod[0] !== DIFF_EQUAL) {
+ index2 = this.diff_xIndex(diffs, index1);
+ }
+ if (mod[0] === DIFF_INSERT) { // Insertion
+ text = text.substring(0, start_loc + index2) + mod[1] +
+ text.substring(start_loc + index2);
+ } else if (mod[0] === DIFF_DELETE) { // Deletion
+ text = text.substring(0, start_loc + index2) +
+ text.substring(start_loc + this.diff_xIndex(diffs,
+ index1 + mod[1].length));
+ }
+ if (mod[0] !== DIFF_DELETE) {
+ index1 += mod[1].length;
+ }
+ }
+ }
+ }
+ }
+ }
+ // Strip the padding off.
+ text = text.substring(nullPadding.length, text.length - nullPadding.length);
+ return [text, results];
+};
+
+
+/**
+ * Add some padding on text start and end so that edges can match something.
+ * Intended to be called only from within patch_apply.
+ * @param {!Array.} patches Array of Patch objects.
+ * @return {string} The padding string added to each side.
+ */
+diff_match_patch.prototype.patch_addPadding = function(patches) {
+ var paddingLength = this.Patch_Margin;
+ var nullPadding = '';
+ for (var x = 1; x <= paddingLength; x++) {
+ nullPadding += String.fromCharCode(x);
+ }
+
+ // Bump all the patches forward.
+ for (var x = 0; x < patches.length; x++) {
+ patches[x].start1 += paddingLength;
+ patches[x].start2 += paddingLength;
+ }
+
+ // Add some padding on start of first diff.
+ var patch = patches[0];
+ var diffs = patch.diffs;
+ if (diffs.length == 0 || diffs[0][0] != DIFF_EQUAL) {
+ // Add nullPadding equality.
+ diffs.unshift([DIFF_EQUAL, nullPadding]);
+ patch.start1 -= paddingLength; // Should be 0.
+ patch.start2 -= paddingLength; // Should be 0.
+ patch.length1 += paddingLength;
+ patch.length2 += paddingLength;
+ } else if (paddingLength > diffs[0][1].length) {
+ // Grow first equality.
+ var extraLength = paddingLength - diffs[0][1].length;
+ diffs[0][1] = nullPadding.substring(diffs[0][1].length) + diffs[0][1];
+ patch.start1 -= extraLength;
+ patch.start2 -= extraLength;
+ patch.length1 += extraLength;
+ patch.length2 += extraLength;
+ }
+
+ // Add some padding on end of last diff.
+ patch = patches[patches.length - 1];
+ diffs = patch.diffs;
+ if (diffs.length == 0 || diffs[diffs.length - 1][0] != DIFF_EQUAL) {
+ // Add nullPadding equality.
+ diffs.push([DIFF_EQUAL, nullPadding]);
+ patch.length1 += paddingLength;
+ patch.length2 += paddingLength;
+ } else if (paddingLength > diffs[diffs.length - 1][1].length) {
+ // Grow last equality.
+ var extraLength = paddingLength - diffs[diffs.length - 1][1].length;
+ diffs[diffs.length - 1][1] += nullPadding.substring(0, extraLength);
+ patch.length1 += extraLength;
+ patch.length2 += extraLength;
+ }
+
+ return nullPadding;
+};
+
+
+/**
+ * Look through the patches and break up any which are longer than the maximum
+ * limit of the match algorithm.
+ * Intended to be called only from within patch_apply.
+ * @param {!Array.} patches Array of Patch objects.
+ */
+diff_match_patch.prototype.patch_splitMax = function(patches) {
+ var patch_size = this.Match_MaxBits;
+ for (var x = 0; x < patches.length; x++) {
+ if (patches[x].length1 <= patch_size) {
+ continue;
+ }
+ var bigpatch = patches[x];
+ // Remove the big old patch.
+ patches.splice(x--, 1);
+ var start1 = bigpatch.start1;
+ var start2 = bigpatch.start2;
+ var precontext = '';
+ while (bigpatch.diffs.length !== 0) {
+ // Create one of several smaller patches.
+ var patch = new diff_match_patch.patch_obj();
+ var empty = true;
+ patch.start1 = start1 - precontext.length;
+ patch.start2 = start2 - precontext.length;
+ if (precontext !== '') {
+ patch.length1 = patch.length2 = precontext.length;
+ patch.diffs.push([DIFF_EQUAL, precontext]);
+ }
+ while (bigpatch.diffs.length !== 0 &&
+ patch.length1 < patch_size - this.Patch_Margin) {
+ var diff_type = bigpatch.diffs[0][0];
+ var diff_text = bigpatch.diffs[0][1];
+ if (diff_type === DIFF_INSERT) {
+ // Insertions are harmless.
+ patch.length2 += diff_text.length;
+ start2 += diff_text.length;
+ patch.diffs.push(bigpatch.diffs.shift());
+ empty = false;
+ } else if (diff_type === DIFF_DELETE && patch.diffs.length == 1 &&
+ patch.diffs[0][0] == DIFF_EQUAL &&
+ diff_text.length > 2 * patch_size) {
+ // This is a large deletion. Let it pass in one chunk.
+ patch.length1 += diff_text.length;
+ start1 += diff_text.length;
+ empty = false;
+ patch.diffs.push([diff_type, diff_text]);
+ bigpatch.diffs.shift();
+ } else {
+ // Deletion or equality. Only take as much as we can stomach.
+ diff_text = diff_text.substring(0,
+ patch_size - patch.length1 - this.Patch_Margin);
+ patch.length1 += diff_text.length;
+ start1 += diff_text.length;
+ if (diff_type === DIFF_EQUAL) {
+ patch.length2 += diff_text.length;
+ start2 += diff_text.length;
+ } else {
+ empty = false;
+ }
+ patch.diffs.push([diff_type, diff_text]);
+ if (diff_text == bigpatch.diffs[0][1]) {
+ bigpatch.diffs.shift();
+ } else {
+ bigpatch.diffs[0][1] =
+ bigpatch.diffs[0][1].substring(diff_text.length);
+ }
+ }
+ }
+ // Compute the head context for the next patch.
+ precontext = this.diff_text2(patch.diffs);
+ precontext =
+ precontext.substring(precontext.length - this.Patch_Margin);
+ // Append the end context for this patch.
+ var postcontext = this.diff_text1(bigpatch.diffs)
+ .substring(0, this.Patch_Margin);
+ if (postcontext !== '') {
+ patch.length1 += postcontext.length;
+ patch.length2 += postcontext.length;
+ if (patch.diffs.length !== 0 &&
+ patch.diffs[patch.diffs.length - 1][0] === DIFF_EQUAL) {
+ patch.diffs[patch.diffs.length - 1][1] += postcontext;
+ } else {
+ patch.diffs.push([DIFF_EQUAL, postcontext]);
+ }
+ }
+ if (!empty) {
+ patches.splice(++x, 0, patch);
+ }
+ }
+ }
+};
+
+
+/**
+ * Take a list of patches and return a textual representation.
+ * @param {!Array.} patches Array of Patch objects.
+ * @return {string} Text representation of patches.
+ */
+diff_match_patch.prototype.patch_toText = function(patches) {
+ var text = [];
+ for (var x = 0; x < patches.length; x++) {
+ text[x] = patches[x];
+ }
+ return text.join('');
+};
+
+
+/**
+ * Parse a textual representation of patches and return a list of Patch objects.
+ * @param {string} textline Text representation of patches.
+ * @return {!Array.} Array of Patch objects.
+ * @throws {!Error} If invalid input.
+ */
+diff_match_patch.prototype.patch_fromText = function(textline) {
+ var patches = [];
+ if (!textline) {
+ return patches;
+ }
+ var text = textline.split('\n');
+ var textPointer = 0;
+ var patchHeader = /^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$/;
+ while (textPointer < text.length) {
+ var m = text[textPointer].match(patchHeader);
+ if (!m) {
+ throw new Error('Invalid patch string: ' + text[textPointer]);
+ }
+ var patch = new diff_match_patch.patch_obj();
+ patches.push(patch);
+ patch.start1 = parseInt(m[1], 10);
+ if (m[2] === '') {
+ patch.start1--;
+ patch.length1 = 1;
+ } else if (m[2] == '0') {
+ patch.length1 = 0;
+ } else {
+ patch.start1--;
+ patch.length1 = parseInt(m[2], 10);
+ }
+
+ patch.start2 = parseInt(m[3], 10);
+ if (m[4] === '') {
+ patch.start2--;
+ patch.length2 = 1;
+ } else if (m[4] == '0') {
+ patch.length2 = 0;
+ } else {
+ patch.start2--;
+ patch.length2 = parseInt(m[4], 10);
+ }
+ textPointer++;
+
+ while (textPointer < text.length) {
+ var sign = text[textPointer].charAt(0);
+ try {
+ var line = decodeURI(text[textPointer].substring(1));
+ } catch (ex) {
+ // Malformed URI sequence.
+ throw new Error('Illegal escape in patch_fromText: ' + line);
+ }
+ if (sign == '-') {
+ // Deletion.
+ patch.diffs.push([DIFF_DELETE, line]);
+ } else if (sign == '+') {
+ // Insertion.
+ patch.diffs.push([DIFF_INSERT, line]);
+ } else if (sign == ' ') {
+ // Minor equality.
+ patch.diffs.push([DIFF_EQUAL, line]);
+ } else if (sign == '@') {
+ // Start of next patch.
+ break;
+ } else if (sign === '') {
+ // Blank line? Whatever.
+ } else {
+ // WTF?
+ throw new Error('Invalid patch mode "' + sign + '" in: ' + line);
+ }
+ textPointer++;
+ }
+ }
+ return patches;
+};
+
+
+/**
+ * Class representing one patch operation.
+ * @constructor
+ */
+diff_match_patch.patch_obj = function() {
+ /** @type {!Array.} */
+ this.diffs = [];
+ /** @type {?number} */
+ this.start1 = null;
+ /** @type {?number} */
+ this.start2 = null;
+ /** @type {number} */
+ this.length1 = 0;
+ /** @type {number} */
+ this.length2 = 0;
+};
+
+
+/**
+ * Emmulate GNU diff's format.
+ * Header: @@ -382,8 +481,9 @@
+ * Indicies are printed as 1-based, not 0-based.
+ * @return {string} The GNU diff string.
+ */
+diff_match_patch.patch_obj.prototype.toString = function() {
+ var coords1, coords2;
+ if (this.length1 === 0) {
+ coords1 = this.start1 + ',0';
+ } else if (this.length1 == 1) {
+ coords1 = this.start1 + 1;
+ } else {
+ coords1 = (this.start1 + 1) + ',' + this.length1;
+ }
+ if (this.length2 === 0) {
+ coords2 = this.start2 + ',0';
+ } else if (this.length2 == 1) {
+ coords2 = this.start2 + 1;
+ } else {
+ coords2 = (this.start2 + 1) + ',' + this.length2;
+ }
+ var text = ['@@ -' + coords1 + ' +' + coords2 + ' @@\n'];
+ var op;
+ // Escape the body of the patch with %xx notation.
+ for (var x = 0; x < this.diffs.length; x++) {
+ switch (this.diffs[x][0]) {
+ case DIFF_INSERT:
+ op = '+';
+ break;
+ case DIFF_DELETE:
+ op = '-';
+ break;
+ case DIFF_EQUAL:
+ op = ' ';
+ break;
+ }
+ text[x + 1] = op + encodeURI(this.diffs[x][1]) + '\n';
+ }
+ return text.join('').replace(/%20/g, ' ');
+};
+
+
+// The following export code was added by @ForbesLindesay
+module.exports = diff_match_patch;
+module.exports['diff_match_patch'] = diff_match_patch;
+module.exports['DIFF_DELETE'] = DIFF_DELETE;
+module.exports['DIFF_INSERT'] = DIFF_INSERT;
+module.exports['DIFF_EQUAL'] = DIFF_EQUAL;
+});
+
+/* 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 (diffMatchPatch) {
+ try {
+ instance = diffMatchPatch && new diffMatchPatch();
+ } 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$$1 = getDiffMatchPatch();
+ if (!diffMatchPatch$$1) {
+ // diff-match-patch library not available,
+ // fallback to regular string replace
+ context.setResult([context.left, context.right]).exit();
+ return;
+ }
+ var diff = diffMatchPatch$$1.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('' + err + '
');
+ }
+ }, {
+ key: 'formatValue',
+ value: function formatValue(context, value) {
+ context.out('' + htmlEscape(JSON.stringify(value, null, 2)) + '
');
+ }
+ }, {
+ key: 'formatTextDiffString',
+ value: function formatTextDiffString(context, value) {
+ var lines = this.parseTextDiff(value);
+ context.out('');
+ for (var i = 0, l = lines.length; i < l; i++) {
+ var line = lines[i];
+ context.out('' + ('' + line.location.line + '' + line.location.chr + '
'));
+ var pieces = line.pieces;
+ for (var pieceIndex = 0, piecesLength = pieces.length; pieceIndex < piecesLength; pieceIndex++) {
+ /* global decodeURI */
+ var piece = pieces[pieceIndex];
+ context.out('' + htmlEscape(decodeURI(piece.text)) + '');
+ }
+ context.out('
');
+ }
+ context.out('
');
+ }
+ }, {
+ key: 'rootBegin',
+ value: function rootBegin(context, type, nodeType) {
+ var nodeClass = 'jsondiffpatch-' + type + (nodeType ? ' jsondiffpatch-child-node-type-' + nodeType : '');
+ context.out('');
+ }
+ }, {
+ key: 'rootEnd',
+ value: function rootEnd(context) {
+ context.out('
' + (context.hasArrows ? '') : ''));
+ }
+ }, {
+ key: 'nodeBegin',
+ value: function nodeBegin(context, key, leftKey, type, nodeType) {
+ var nodeClass = 'jsondiffpatch-' + type + (nodeType ? ' jsondiffpatch-child-node-type-' + nodeType : '');
+ context.out('' + ('' + leftKey + '
'));
+ }
+ }, {
+ key: 'nodeEnd',
+ value: function nodeEnd(context) {
+ context.out('');
+ }
+
+ /* jshint camelcase: false */
+ /* eslint-disable camelcase */
+
+ }, {
+ key: 'format_unchanged',
+ value: function format_unchanged(context, delta, left) {
+ if (typeof left === 'undefined') {
+ return;
+ }
+ context.out('');
+ this.formatValue(context, left);
+ context.out('
');
+ }
+ }, {
+ key: 'format_movedestination',
+ value: function format_movedestination(context, delta, left) {
+ if (typeof left === 'undefined') {
+ return;
+ }
+ context.out('');
+ this.formatValue(context, left);
+ context.out('
');
+ }
+ }, {
+ key: 'format_node',
+ value: function format_node(context, delta, left) {
+ // recurse
+ var nodeType = delta._t === 'a' ? 'array' : 'object';
+ context.out('');
+ this.formatDeltaChildren(context, delta, left);
+ context.out('
');
+ }
+ }, {
+ key: 'format_added',
+ value: function format_added(context, delta) {
+ context.out('');
+ this.formatValue(context, delta[0]);
+ context.out('
');
+ }
+ }, {
+ key: 'format_modified',
+ value: function format_modified(context, delta) {
+ context.out('');
+ this.formatValue(context, delta[0]);
+ context.out('
' + '');
+ this.formatValue(context, delta[1]);
+ context.out('
');
+ }
+ }, {
+ key: 'format_deleted',
+ value: function format_deleted(context, delta) {
+ context.out('');
+ this.formatValue(context, delta[0]);
+ context.out('
');
+ }
+ }, {
+ key: 'format_moved',
+ value: function format_moved(context, delta) {
+ context.out('');
+ this.formatValue(context, delta[0]);
+ context.out('
' + delta[1] + '
');
+
+ // draw an SVG arrow from here to move destination
+ context.out(
+ /* jshint multistr: true */
+ '\n
\n
');
+ context.hasArrows = true;
+ }
+ }, {
+ key: 'format_textdiff',
+ value: function format_textdiff(context, delta) {
+ context.out('');
+ this.formatTextDiffString(context, delta[0]);
+ context.out('
');
+ }
+ }]);
+ return HtmlFormatter;
+}(BaseFormatter);
+
+function htmlEscape(text) {
+ var html = text;
+ var replacements = [[/&/g, '&'], [//g, '>'], [/'/g, '''], [/"/g, '"']];
+ 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(' ');
+ };
+ context.row = function (json, htmlNote) {
+ context.out('' + '');
+ context.out(context.indentPad);
+ context.out(' ');
+ context.out(json);
+ context.out(' | ');
+ context.out(htmlNote);
+ context.out(' |
');
+ };
+ }
+ }, {
+ key: 'typeFormattterErrorFormatter',
+ value: function typeFormattterErrorFormatter(context, err) {
+ context.row('', '' + err + '
');
+ }
+ }, {
+ key: 'formatTextDiffString',
+ value: function formatTextDiffString(context, value) {
+ var lines = this.parseTextDiff(value);
+ context.out('');
+ for (var i = 0, l = lines.length; i < l; i++) {
+ var line = lines[i];
+ context.out('' + ('' + line.location.line + '' + line.location.chr + '
'));
+ var pieces = line.pieces;
+ for (var pieceIndex = 0, piecesLength = pieces.length; pieceIndex < piecesLength; pieceIndex++) {
+ var piece = pieces[pieceIndex];
+ context.out('' + piece.text + '');
+ }
+ context.out('
');
+ }
+ context.out('
');
+ }
+ }, {
+ key: 'rootBegin',
+ value: function rootBegin(context, type, nodeType) {
+ context.out('');
+ 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('
');
+ }
+ }, {
+ key: 'nodeBegin',
+ value: function nodeBegin(context, key, leftKey, type, nodeType) {
+ context.row('"' + key + '": {');
+ 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 '"' + name + '"
';
+};
+
+var deltaAnnotations = {
+ added: function added(delta, left, key, leftKey) {
+ var formatLegend = ' ([newValue])
';
+ 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 = ' ([previousValue, newValue])
';
+ 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 = ' ([previousValue, 0, 0])
';
+ 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 ' + ('index ' + leftKey + ' to index ' + delta[1] + '');
+ },
+ 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 variation of Unidiff';
+ }
+};
+
+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);
+}
+
+exports.DiffPatcher = DiffPatcher;
+exports.formatters = index;
+exports.console = console$1;
+exports.create = create;
+exports.dateReviver = dateReviver;
+exports.diff = diff;
+exports.patch = patch;
+exports.unpatch = unpatch;
+exports.reverse = reverse;
+exports.clone = clone$1;
+
+Object.defineProperty(exports, '__esModule', { value: true });
+
+})));