UNPKG

datum-merge

Version:

Simplified diff and merging for deeply nested objects

88 lines (87 loc) 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getValueByPointer = exports.asLodashPath = exports.deepPatchLog = exports.applyPatchLog = exports.diffToPatchLog = void 0; const lodash_es_1 = require("lodash-es"); const diff_high_1 = require("./diff-high"); function diffToPatchLog(differences, storePrev = false) { var _a, _b, _c; const jsonPatch = []; for (const dif of differences) { let pointer = asJsonPointer((_a = dif.path) !== null && _a !== void 0 ? _a : []); switch (dif.kind) { case "N": jsonPatch.push({ op: "add", path: pointer, value: dif.rhs }); break; case "E": jsonPatch.push({ op: "replace", path: pointer, value: dif.rhs, prev: dif.lhs }); break; case "D": jsonPatch.push({ op: "remove", path: pointer, prev: dif.lhs }); break; case "A": pointer = `${pointer}/${dif.index}`; if (((_b = dif.item) === null || _b === void 0 ? void 0 : _b.kind) === 'N') { jsonPatch.push({ op: "add", path: pointer, value: dif.item.rhs }); } if (((_c = dif.item) === null || _c === void 0 ? void 0 : _c.kind) === 'D') { jsonPatch.push({ op: "remove", path: pointer, prev: dif.item.lhs }); } break; } } if (!storePrev) { jsonPatch.forEach((p) => delete p.prev); } return jsonPatch; } exports.diffToPatchLog = diffToPatchLog; function applyPatchLog(patchLog, target = {}) { if (!patchLog) return target; for (const patchItem of patchLog) { const difPath = asLodashPath(patchItem.path); if (patchItem.op === "remove") { (0, lodash_es_1.unset)(target, difPath); } else { (0, lodash_es_1.set)(target, difPath, patchItem.value); } } return target; } exports.applyPatchLog = applyPatchLog; function deepPatchLog(lhsObj, rhsObj, orderInd = false, storePrev = false) { const differences = (0, diff_high_1.deepDiffLow)(lhsObj, rhsObj, orderInd); return !differences ? [] : diffToPatchLog(differences, storePrev); } exports.deepPatchLog = deepPatchLog; ; function escapePathPart(path) { if (typeof path === 'number') return path.toString(); if (typeof path === 'symbol') return path.toString(); if (path.indexOf('/') === -1 && path.indexOf('~') === -1) return path; return path.replace(/~/g, '~0').replace(/\//g, '~1'); } function unescapePathPart(path) { return path.replace(/~1/g, '/').replace(/~0/g, '~'); } function asJsonPointer(path) { return !(path === null || path === void 0 ? void 0 : path.length) ? "" : "/" + path.map((s) => escapePathPart(s)).join("/"); } function asLodashPath(pointer) { if (!pointer || !pointer.startsWith("/")) return []; const parts = pointer.slice(1).split("/") .map((s) => unescapePathPart(s)); return !(parts === null || parts === void 0 ? void 0 : parts.length) ? [] : (0, lodash_es_1.toPath)(parts.join(".")); } exports.asLodashPath = asLodashPath; function getValueByPointer(document, pointer) { return pointer === "" ? document : (0, lodash_es_1.get)(document, asLodashPath(pointer)); } exports.getValueByPointer = getValueByPointer;