UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

68 lines (67 loc) 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.equalSchema = void 0; const deepEqual_1 = require("@jsonjoy.com/util/lib/json-equal/deepEqual"); const nodes_1 = require("../nodes"); /** * Deeply checks if two JSON nodes have the same schema and values. Does not * verify that the CRDT metadata (like timestamps) are the same, only that * the structure and values are equal. * * @param a The first JSON CRDT node. * @param b The second JSON CRDT node. * @returns True if the schemas and values are equal, false otherwise. */ const equalSchema = (a, b, compareContent) => { if (a === b) return true; if (a instanceof nodes_1.ConNode) return b instanceof nodes_1.ConNode && (!compareContent || (0, deepEqual_1.deepEqual)(a.val, b.val)); else if (a instanceof nodes_1.ValNode) return b instanceof nodes_1.ValNode && (0, exports.equalSchema)(a.node(), b.node(), compareContent); else if (a instanceof nodes_1.StrNode) return b instanceof nodes_1.StrNode && (!compareContent || (a.length() === b.length() && a.view() === b.view())); else if (a instanceof nodes_1.ObjNode) { if (!(b instanceof nodes_1.ObjNode)) 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()) { if (!keys2.has(key)) return false; if (!(0, exports.equalSchema)(a.get(key), b.get(key), compareContent)) return false; } return true; } else if (a instanceof nodes_1.ArrNode) { if (!(b instanceof nodes_1.ArrNode)) return false; const length = a.length(); if (length !== b.length()) return false; for (let i = 0; i < length; i++) if (!(0, exports.equalSchema)(a.getNode(i), b.getNode(i), compareContent)) return false; return true; } else if (a instanceof nodes_1.VecNode) { if (!(b instanceof nodes_1.VecNode)) return false; const length = a.length(); if (length !== b.length()) return false; for (let i = 0; i < length; i++) if (!(0, exports.equalSchema)(a.get(i), b.get(i), compareContent)) return false; return true; } else if (a instanceof nodes_1.BinNode) return b instanceof nodes_1.BinNode && (!compareContent || (a.length() === b.length() && (0, deepEqual_1.deepEqual)(a.view(), b.view()))); return false; }; exports.equalSchema = equalSchema;