') : '')); } }, { 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(''); } }, { 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 \n \n \n \n \n \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(''); } }, { 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 }); })));