UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

88 lines 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cmpNode = void 0; const nodes_1 = require("../nodes"); const json_crdt_patch_1 = require("../../json-crdt-patch"); const util2_1 = require("sonic-forest/lib/util2"); const cmpRga = (a, b) => { const maxIdChunkA = (0, util2_1.last2)(a.ids); const maxIdChunkB = (0, util2_1.last2)(b.ids); if (maxIdChunkA && maxIdChunkB && !(0, json_crdt_patch_1.equal)(maxIdChunkA.id, maxIdChunkB.id)) return false; return a.length() === b.length() && a.size() === b.size(); }; /** * Performs type and metadata shallow check of two JSON CRDT nodes. Compares * node type and their metadata (like timestamps). Does not compare the content * of the nodes, however if the metadata matches the content is likely the same * as well. * * @param a The first JSON CRDT node. * @param b The second JSON CRDT node. * @returns True if they are equal, false otherwise. */ const cmpNode = (a, b) => { if (a === b) return true; if (a instanceof nodes_1.ConNode) return b instanceof nodes_1.ConNode && (0, json_crdt_patch_1.equal)(a.id, b.id); else if (a instanceof nodes_1.ValNode) return b instanceof nodes_1.ValNode && (0, json_crdt_patch_1.equal)(a.id, b.id) && (0, json_crdt_patch_1.equal)(a.val, b.val); else if (a instanceof nodes_1.StrNode) { if (!(b instanceof nodes_1.StrNode) || !(0, json_crdt_patch_1.equal)(a.id, b.id)) return false; return cmpRga(a, b); } else if (a instanceof nodes_1.ObjNode) { if (!(b instanceof nodes_1.ObjNode) || !(0, json_crdt_patch_1.equal)(a.id, b.id)) return false; const keys1 = a.keys; const keys2 = b.keys; const length1 = keys1.size; const length2 = keys2.size; if (length1 !== length2) return false; for (const key of keys1.keys()) { const ts1 = keys1.get(key); const ts2 = keys2.get(key); if (!ts1 || !ts2 || !(0, json_crdt_patch_1.equal)(ts1, ts2)) return false; } return true; } else if (a instanceof nodes_1.ArrNode) { if (!(b instanceof nodes_1.ArrNode) || !(0, json_crdt_patch_1.equal)(a.id, b.id)) return false; return cmpRga(a, b); } else if (a instanceof nodes_1.VecNode) { if (!(b instanceof nodes_1.VecNode) || !(0, json_crdt_patch_1.equal)(a.id, b.id)) return false; const elementsA = a.elements; const elementsB = b.elements; const length = elementsA.length; if (length !== elementsB.length) return false; for (let i = 0; i < length; i++) { const ts1 = elementsA[i]; const ts2 = elementsB[i]; if (!ts1) { if (ts2) return false; } else { if (!ts2 || !(0, json_crdt_patch_1.equal)(ts1, ts2)) return false; } } return true; } else if (a instanceof nodes_1.BinNode) { if (!(b instanceof nodes_1.BinNode) || !(0, json_crdt_patch_1.equal)(a.id, b.id)) return false; return cmpRga(a, b); } return false; }; exports.cmpNode = cmpNode; //# sourceMappingURL=cmpNode.js.map