UNPKG

git-documentdb

Version:

Offline-first database that syncs with Git

41 lines 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonDiff = void 0; const jsondiffpatch_1 = require("@sosuisen/jsondiffpatch"); const JSON_DIFF_MINIMUM_TEXT_LENGTH = Number.MAX_SAFE_INTEGER; class JsonDiff { constructor(options) { var _a; options !== null && options !== void 0 ? options : (options = { keyInArrayedObject: undefined, plainTextProperties: undefined, }); (_a = options.keyInArrayedObject) !== null && _a !== void 0 ? _a : (options.keyInArrayedObject = []); const objectHash = (obj, index) => { for (let i = 0; i < options.keyInArrayedObject.length; i++) { const id = obj[options.keyInArrayedObject[i]]; if (id !== undefined) { return id; } } return '$$index:' + index; }; this._jsonDiffPatch = (0, jsondiffpatch_1.create)({ objectHash, textDiff: { minLength: JSON_DIFF_MINIMUM_TEXT_LENGTH, plainTextProperties: options.plainTextProperties, }, }); } diff(oldDoc, newDoc) { if (oldDoc === undefined) oldDoc = {}; const oldDocClone = JSON.parse(JSON.stringify(oldDoc)); const newDocClone = JSON.parse(JSON.stringify(newDoc)); const diff = this._jsonDiffPatch.diff(oldDocClone, newDocClone); return diff; } } exports.JsonDiff = JsonDiff; //# sourceMappingURL=json_diff.js.map