reptree 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -348,7 +348,7 @@ var TreeState = class {
348
348
  }
349
349
  };
350
350
 
351
- // src/uuid.ts
351
+ // src/utils/uuid.ts
352
352
  var removeDashes = (guid) => guid.replace(/-/g, "");
353
353
  function uuid() {
354
354
  return removeDashes(crypto.randomUUID());
@@ -518,7 +518,7 @@ var Vertex = class _Vertex {
518
518
  if (props && typeof props === "object" && "children" in props) {
519
519
  throw new Error("Passing children inside props is not supported at the moment");
520
520
  }
521
- const normalized = _Vertex.normalizePropsForCreation(props, name);
521
+ const normalized = _Vertex.normalizePropsForCreation(props);
522
522
  return this.tree.newNamedVertex(this.id, name, normalized);
523
523
  }
524
524
  setProperty(key, value) {
@@ -597,21 +597,32 @@ var Vertex = class _Vertex {
597
597
  * - Filters unsupported field types with a console warning
598
598
  * - When a name param is provided to newNamedChild, ignores conflicting name in props
599
599
  */
600
- static normalizePropsForCreation(props, explicitName) {
600
+ static normalizePropsForCreation(props) {
601
601
  if (!props) return null;
602
602
  const input = props;
603
603
  const out = {};
604
604
  const skipped = [];
605
+ const isJsonValue2 = (v) => {
606
+ if (v === null) return true;
607
+ const t = typeof v;
608
+ if (t === "string" || t === "number" || t === "boolean") return true;
609
+ if (Array.isArray(v)) return v.every(isJsonValue2);
610
+ if (t === "object") {
611
+ const proto = Object.getPrototypeOf(v);
612
+ if (proto !== Object.prototype && proto !== null) return false;
613
+ for (const val of Object.values(v)) {
614
+ if (!isJsonValue2(val)) return false;
615
+ }
616
+ return true;
617
+ }
618
+ return false;
619
+ };
605
620
  for (const [rawKey, rawValue] of Object.entries(input)) {
606
621
  if (rawValue === void 0) {
607
622
  continue;
608
623
  }
609
624
  if (rawKey === "children") continue;
610
625
  let key = rawKey;
611
- if (rawKey === "name" && explicitName !== void 0) {
612
- console.warn('newNamedChild: "name" in props is ignored because a name argument was provided');
613
- continue;
614
- }
615
626
  let value = rawValue;
616
627
  if (key === "_c") {
617
628
  if (value instanceof Date) {
@@ -622,23 +633,14 @@ var Vertex = class _Vertex {
622
633
  continue;
623
634
  }
624
635
  }
625
- const isPrimitive = (v) => typeof v === "string" || typeof v === "number" || typeof v === "boolean";
626
- if (Array.isArray(value)) {
627
- if (!value.every(isPrimitive)) {
628
- skipped.push(rawKey);
629
- continue;
630
- }
631
- } else if (typeof value === "object" && value !== null) {
632
- skipped.push(rawKey);
633
- continue;
634
- } else if (!isPrimitive(value)) {
636
+ if (!isJsonValue2(value)) {
635
637
  skipped.push(rawKey);
636
638
  continue;
637
639
  }
638
640
  out[key] = value;
639
641
  }
640
642
  if (skipped.length > 0) {
641
- console.warn(`Some fields were skipped due to unsupported types: ${skipped.join(", ")}`);
643
+ throw new Error(`Unsupported property types for keys: ${skipped.join(", ")}`);
642
644
  }
643
645
  return Object.keys(out).length > 0 ? out : null;
644
646
  }
@@ -829,6 +831,61 @@ var StateVector = class _StateVector {
829
831
  }
830
832
  };
831
833
 
834
+ // src/utils/deepEqual.ts
835
+ function deepEqual(a, b) {
836
+ if (a === b) return true;
837
+ if (a === null || b === null) return a === b;
838
+ const ta = typeof a;
839
+ const tb = typeof b;
840
+ if (ta !== tb) return false;
841
+ if (ta !== "object") return false;
842
+ const aIsArr = Array.isArray(a);
843
+ const bIsArr = Array.isArray(b);
844
+ if (aIsArr || bIsArr) {
845
+ if (!(aIsArr && bIsArr)) return false;
846
+ if (a.length !== b.length) return false;
847
+ for (let i = 0; i < a.length; i++) {
848
+ if (!deepEqual(a[i], b[i])) return false;
849
+ }
850
+ return true;
851
+ }
852
+ const aProto = Object.getPrototypeOf(a);
853
+ const bProto = Object.getPrototypeOf(b);
854
+ if (aProto !== Object.prototype && aProto !== null || bProto !== Object.prototype && bProto !== null) {
855
+ return false;
856
+ }
857
+ const aKeys = Object.keys(a);
858
+ const bKeys = Object.keys(b);
859
+ if (aKeys.length !== bKeys.length) return false;
860
+ for (const key of aKeys) {
861
+ if (!Object.prototype.hasOwnProperty.call(b, key)) return false;
862
+ if (!deepEqual(a[key], b[key])) return false;
863
+ }
864
+ return true;
865
+ }
866
+
867
+ // src/utils/isJsonValue.ts
868
+ function isJsonValue(v) {
869
+ if (v === void 0) return true;
870
+ if (v === null) return true;
871
+ const t = typeof v;
872
+ if (t === "string" || t === "number" || t === "boolean") return true;
873
+ if (t === "bigint" || t === "function" || t === "symbol") return false;
874
+ if (Array.isArray(v)) return v.every(isJsonValue);
875
+ if (t === "object") {
876
+ if (v instanceof Date) return false;
877
+ if (v instanceof Map || v instanceof Set || v instanceof RegExp) return false;
878
+ if (ArrayBuffer.isView(v)) return false;
879
+ const proto = Object.getPrototypeOf(v);
880
+ if (proto !== Object.prototype && proto !== null) return false;
881
+ for (const val of Object.values(v)) {
882
+ if (!isJsonValue(val)) return false;
883
+ }
884
+ return true;
885
+ }
886
+ return false;
887
+ }
888
+
832
889
  // src/RepTree.ts
833
890
  var _RepTree = class _RepTree {
834
891
  /**
@@ -995,6 +1052,9 @@ var _RepTree = class _RepTree {
995
1052
  this.moveVertex(vertexId, _RepTree.NULL_VERTEX_ID);
996
1053
  }
997
1054
  setTransientVertexProperty(vertexId, key, value) {
1055
+ if (!isJsonValue(value)) {
1056
+ throw new Error(`Unsupported transient property value for key "${key}"`);
1057
+ }
998
1058
  this.lamportClock++;
999
1059
  const op = newSetTransientVertexPropertyOp(this.lamportClock, this.peerId, vertexId, key, value);
1000
1060
  this.localOps.push(op);
@@ -1020,6 +1080,29 @@ var _RepTree = class _RepTree {
1020
1080
  vertex.clearAllTransientProperties();
1021
1081
  }
1022
1082
  setVertexProperty(vertexId, key, value) {
1083
+ const isJsonValue2 = (v) => {
1084
+ if (v === void 0) return true;
1085
+ if (v === null) return true;
1086
+ const t = typeof v;
1087
+ if (t === "string" || t === "number" || t === "boolean") return true;
1088
+ if (t === "bigint" || t === "function" || t === "symbol") return false;
1089
+ if (Array.isArray(v)) return v.every(isJsonValue2);
1090
+ if (t === "object") {
1091
+ if (v instanceof Date) return false;
1092
+ if (v instanceof Map || v instanceof Set || v instanceof RegExp) return false;
1093
+ if (ArrayBuffer.isView(v)) return false;
1094
+ const proto = Object.getPrototypeOf(v);
1095
+ if (proto !== Object.prototype && proto !== null) return false;
1096
+ for (const val of Object.values(v)) {
1097
+ if (!isJsonValue2(val)) return false;
1098
+ }
1099
+ return true;
1100
+ }
1101
+ return false;
1102
+ };
1103
+ if (!isJsonValue2(value)) {
1104
+ throw new Error(`Unsupported property value for key "${key}"`);
1105
+ }
1023
1106
  this.lamportClock++;
1024
1107
  const op = newSetVertexPropertyOp(this.lamportClock, this.peerId, vertexId, key, value);
1025
1108
  this.localOps.push(op);
@@ -1181,12 +1264,8 @@ var _RepTree = class _RepTree {
1181
1264
  }
1182
1265
  for (const propA of propertiesA) {
1183
1266
  const propB = propertiesB.find((p) => p.key === propA.key);
1184
- if (!propB) {
1185
- return false;
1186
- }
1187
- if (propA.value !== propB.value) {
1188
- return false;
1189
- }
1267
+ if (!propB) return false;
1268
+ if (!deepEqual(propA.value, propB.value)) return false;
1190
1269
  }
1191
1270
  }
1192
1271
  for (const childId of childrenA) {
@@ -1316,7 +1395,6 @@ var _RepTree = class _RepTree {
1316
1395
  }
1317
1396
  }
1318
1397
  }
1319
- // Non-LWW modify-property flow removed
1320
1398
  applyOperation(op) {
1321
1399
  if (isMoveVertexOp(op)) {
1322
1400
  this.applyMove(op);