@loro-extended/change 0.3.0 → 0.4.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
@@ -62,7 +62,8 @@ function isValueShape(schema) {
62
62
  "uint8array",
63
63
  "object",
64
64
  "record",
65
- "array"
65
+ "array",
66
+ "union"
66
67
  ].includes(schema.valueType);
67
68
  }
68
69
  function isObjectValue(value) {
@@ -72,9 +73,7 @@ function isObjectValue(value) {
72
73
  // src/conversion.ts
73
74
  function convertTextInput(value) {
74
75
  const text = new LoroText();
75
- if (value.length > 0) {
76
- text.insert(0, value);
77
- }
76
+ text.insert(0, value);
78
77
  return text;
79
78
  }
80
79
  function convertCounterInput(value) {
@@ -702,7 +701,6 @@ var TextDraftNode = class extends DraftNode {
702
701
  }
703
702
  // Text methods
704
703
  insert(index, content) {
705
- if (content.length === 0) return;
706
704
  this.container.insert(index, content);
707
705
  }
708
706
  delete(index, len) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/change.ts","../src/draft-nodes/base.ts","../src/draft-nodes/counter.ts","../src/conversion.ts","../src/utils/type-guards.ts","../src/draft-nodes/list-base.ts","../src/draft-nodes/list.ts","../src/draft-nodes/map.ts","../src/draft-nodes/movable-list.ts","../src/draft-nodes/record.ts","../src/draft-nodes/text.ts","../src/draft-nodes/tree.ts","../src/draft-nodes/utils.ts","../src/draft-nodes/doc.ts","../src/json-patch.ts","../src/overlay.ts","../src/validation.ts","../src/shape.ts"],"sourcesContent":["/** biome-ignore-all lint/suspicious/noExplicitAny: fix later */\n\nimport { LoroDoc } from \"loro-crdt\"\nimport { DraftDoc } from \"./draft-nodes/doc.js\"\nimport {\n type JsonPatch,\n JsonPatchApplicator,\n type JsonPatchOperation,\n normalizePath,\n} from \"./json-patch.js\"\nimport { overlayEmptyState } from \"./overlay.js\"\nimport type { DocShape } from \"./shape.js\"\nimport type { Draft, InferPlainType } from \"./types.js\"\nimport { validateEmptyState } from \"./validation.js\"\n\n// Core TypedDoc abstraction around LoroDoc\nexport class TypedDoc<Shape extends DocShape> {\n constructor(\n private shape: Shape,\n private emptyState: InferPlainType<Shape>,\n private doc: LoroDoc = new LoroDoc(),\n ) {\n validateEmptyState(emptyState, shape)\n }\n\n get value(): InferPlainType<Shape> {\n const crdtValue = this.doc.toJSON()\n return overlayEmptyState(\n this.shape,\n crdtValue,\n this.emptyState,\n ) as InferPlainType<Shape>\n }\n\n change(fn: (draft: Draft<Shape>) => void): InferPlainType<Shape> {\n // Reuse existing DocumentDraft system with empty state integration\n const draft = new DraftDoc({\n shape: this.shape,\n emptyState: this.emptyState,\n doc: this.doc,\n })\n fn(draft as unknown as Draft<Shape>)\n draft.absorbPlainValues()\n this.doc.commit()\n return this.value\n }\n\n /**\n * Apply JSON Patch operations to the document\n *\n * @param patch - Array of JSON Patch operations (RFC 6902)\n * @param pathPrefix - Optional path prefix for scoped operations\n * @returns Updated document value\n *\n * @example\n * ```typescript\n * const result = typedDoc.applyPatch([\n * { op: 'add', path: '/users/0/name', value: 'Alice' },\n * { op: 'replace', path: '/settings/theme', value: 'dark' }\n * ])\n * ```\n */\n applyPatch(\n patch: JsonPatch,\n pathPrefix?: (string | number)[],\n ): InferPlainType<Shape> {\n return this.change(draft => {\n const applicator = new JsonPatchApplicator(draft)\n\n // Apply path prefix if provided\n const prefixedPatch = pathPrefix\n ? patch.map((op: JsonPatchOperation) => ({\n ...op,\n path: [...pathPrefix, ...normalizePath(op.path)],\n }))\n : patch\n\n applicator.applyPatch(prefixedPatch)\n })\n }\n\n // Expose underlying doc for advanced use cases\n get loroDoc(): LoroDoc {\n return this.doc\n }\n\n // Expose shape for internal use\n get docShape(): Shape {\n return this.shape\n }\n\n // Get raw CRDT value without overlay\n get rawValue(): any {\n return this.doc.toJSON()\n }\n}\n\n// Factory function for TypedLoroDoc\nexport function createTypedDoc<Shape extends DocShape>(\n shape: Shape,\n emptyState: InferPlainType<Shape>,\n existingDoc?: LoroDoc,\n): TypedDoc<Shape> {\n return new TypedDoc<Shape>(shape, emptyState, existingDoc || new LoroDoc())\n}\n","import type { ContainerShape, DocShape, ShapeToContainer } from \"../shape.js\"\nimport type { InferPlainType } from \"../types.js\"\n\nexport type DraftNodeParams<Shape extends DocShape | ContainerShape> = {\n shape: Shape\n emptyState?: InferPlainType<Shape>\n getContainer: () => ShapeToContainer<Shape>\n}\n\n// Base class for all draft nodes\nexport abstract class DraftNode<Shape extends DocShape | ContainerShape> {\n protected _cachedContainer?: ShapeToContainer<Shape>\n\n constructor(protected _params: DraftNodeParams<Shape>) {}\n\n abstract absorbPlainValues(): void\n\n protected get shape(): Shape {\n return this._params.shape\n }\n\n protected get emptyState(): InferPlainType<Shape> | undefined {\n return this._params.emptyState\n }\n\n protected get container(): ShapeToContainer<Shape> {\n if (!this._cachedContainer) {\n const container = this._params.getContainer()\n this._cachedContainer = container\n return container\n }\n return this._cachedContainer\n }\n}\n","import type { CounterContainerShape } from \"../shape.js\"\nimport { DraftNode } from \"./base.js\"\n\n// Counter draft node\nexport class CounterDraftNode extends DraftNode<CounterContainerShape> {\n absorbPlainValues() {\n // no plain values contained within\n }\n\n increment(value: number): void {\n this.container.increment(value)\n }\n\n decrement(value: number): void {\n this.container.decrement(value)\n }\n\n get value(): number {\n return this.container.value\n }\n}\n","import {\n type Container,\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n type Value,\n} from \"loro-crdt\"\nimport type {\n ArrayValueShape,\n ContainerOrValueShape,\n ListContainerShape,\n MapContainerShape,\n MovableListContainerShape,\n ObjectValueShape,\n RecordContainerShape,\n RecordValueShape,\n} from \"./shape.js\"\nimport {\n isContainer,\n isContainerShape,\n isObjectValue,\n isValueShape,\n} from \"./utils/type-guards.js\"\n\n/**\n * Converts string input to LoroText container\n */\nfunction convertTextInput(value: string): LoroText {\n const text = new LoroText()\n\n // TODO(duane): condition can be removed when https://github.com/loro-dev/loro/issues/872 is addressed\n if (value.length > 0) {\n text.insert(0, value)\n }\n\n return text\n}\n\n/**\n * Converts number input to LoroCounter container\n */\nfunction convertCounterInput(value: number): LoroCounter {\n const counter = new LoroCounter()\n counter.increment(value)\n return counter\n}\n\n/**\n * Converts array input to LoroList container\n */\nfunction convertListInput(\n value: Value[],\n shape: ListContainerShape | ArrayValueShape,\n // parentPath: string[],\n): LoroList | Value[] {\n if (!isContainerShape(shape)) {\n return value\n }\n\n const list = new LoroList()\n\n for (const item of value) {\n const convertedItem = convertInputToNode(item, shape.shape)\n if (isContainer(convertedItem)) {\n list.pushContainer(convertedItem)\n } else {\n list.push(convertedItem)\n }\n }\n\n return list\n}\n\n/**\n * Converts array input to LoroMovableList container\n */\nfunction convertMovableListInput(\n value: Value[],\n shape: MovableListContainerShape | ArrayValueShape,\n // parentPath: string[],\n): LoroMovableList | Value[] {\n if (!isContainerShape(shape)) {\n return value\n }\n\n const list = new LoroMovableList()\n\n for (const item of value) {\n const convertedItem = convertInputToNode(item, shape.shape)\n if (isContainer(convertedItem)) {\n list.pushContainer(convertedItem)\n } else {\n list.push(convertedItem)\n }\n }\n\n return list\n}\n\n/**\n * Converts object input to LoroMap container\n */\nfunction convertMapInput(\n value: { [key: string]: Value },\n shape: MapContainerShape | ObjectValueShape,\n): LoroMap | { [key: string]: Value } {\n if (!isContainerShape(shape)) {\n return value\n }\n\n const map = new LoroMap()\n for (const [k, v] of Object.entries(value)) {\n const nestedSchema = shape.shapes[k]\n if (nestedSchema) {\n const convertedValue = convertInputToNode(v, nestedSchema)\n if (isContainer(convertedValue)) {\n map.setContainer(k, convertedValue)\n } else {\n map.set(k, convertedValue)\n }\n } else {\n map.set(k, value)\n }\n }\n\n return map\n}\n\n/**\n * Converts object input to LoroMap container (Record)\n */\nfunction convertRecordInput(\n value: { [key: string]: Value },\n shape: RecordContainerShape | RecordValueShape,\n): LoroMap | { [key: string]: Value } {\n if (!isContainerShape(shape)) {\n return value\n }\n\n const map = new LoroMap()\n for (const [k, v] of Object.entries(value)) {\n const convertedValue = convertInputToNode(v, shape.shape)\n if (isContainer(convertedValue)) {\n map.setContainer(k, convertedValue)\n } else {\n map.set(k, convertedValue)\n }\n }\n\n return map\n}\n\n/**\n * Main conversion function that transforms input values to appropriate CRDT containers\n * based on schema definitions\n */\nexport function convertInputToNode<Shape extends ContainerOrValueShape>(\n value: Value,\n shape: Shape,\n): Container | Value {\n switch (shape._type) {\n case \"text\": {\n if (typeof value !== \"string\") {\n throw new Error(\"string expected\")\n }\n\n return convertTextInput(value)\n }\n case \"counter\": {\n if (typeof value !== \"number\") {\n throw new Error(\"number expected\")\n }\n\n return convertCounterInput(value)\n }\n case \"list\": {\n if (!Array.isArray(value)) {\n throw new Error(\"array expected\")\n }\n\n return convertListInput(value, shape)\n }\n case \"movableList\": {\n if (!Array.isArray(value)) {\n throw new Error(\"array expected\")\n }\n\n return convertMovableListInput(value, shape)\n }\n case \"map\": {\n if (!isObjectValue(value)) {\n throw new Error(\"object expected\")\n }\n\n return convertMapInput(value, shape)\n }\n case \"record\": {\n if (!isObjectValue(value)) {\n throw new Error(\"object expected\")\n }\n\n return convertRecordInput(value, shape)\n }\n case \"value\": {\n if (!isValueShape(shape)) {\n throw new Error(\"value expected\")\n }\n\n return value\n }\n\n case \"tree\":\n throw new Error(\"tree type unimplemented\")\n\n default:\n throw new Error(`unexpected type: ${(shape as Shape)._type}`)\n }\n}\n","import type {\n Container,\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n LoroTree,\n LoroTreeNode,\n Value,\n} from \"loro-crdt\"\nimport type {\n ContainerOrValueShape,\n ContainerShape,\n CounterContainerShape,\n ListContainerShape,\n MapContainerShape,\n MovableListContainerShape,\n RecordContainerShape,\n TextContainerShape,\n TreeContainerShape,\n ValueShape,\n} from \"../shape.js\"\n\nexport { isContainer, isContainerId } from \"loro-crdt\"\n\n/**\n * Type guard to check if a container is a LoroCounter\n */\nexport function isLoroCounter(container: Container): container is LoroCounter {\n return container.kind() === \"Counter\"\n}\n\n/**\n * Type guard to check if a container is a LoroList\n */\nexport function isLoroList(container: Container): container is LoroList {\n return container.kind() === \"List\"\n}\n\n/**\n * Type guard to check if a container is a LoroMap\n */\nexport function isLoroMap(container: Container): container is LoroMap {\n return container.kind() === \"Map\"\n}\n\n/**\n * Type guard to check if a container is a LoroMovableList\n */\nexport function isLoroMovableList(\n container: Container,\n): container is LoroMovableList {\n return container.kind() === \"MovableList\"\n}\n\n/**\n * Type guard to check if a container is a LoroText\n */\nexport function isLoroText(container: Container): container is LoroText {\n return container.kind() === \"Text\"\n}\n\n/**\n * Type guard to check if a container is a LoroTree\n */\nexport function isLoroTree(container: Container): container is LoroTree {\n return container.kind() === \"Tree\"\n}\n\n/**\n * Type guard to check if an object is a LoroTreeNode\n * Note: LoroTreeNode is not a Container, so we check for its specific properties\n */\nexport function isLoroTreeNode(obj: any): obj is LoroTreeNode {\n return (\n obj &&\n typeof obj === \"object\" &&\n typeof obj.id === \"string\" &&\n typeof obj.data === \"object\" &&\n typeof obj.parent === \"function\" &&\n typeof obj.children === \"function\" &&\n typeof obj.createNode === \"function\"\n )\n}\n\n/**\n * Type guard to ensure cached container matches expected type using kind() method\n */\nexport function assertContainerType<T extends Container>(\n cached: Container,\n expected: T,\n context: string = \"container operation\",\n): asserts cached is T {\n if (cached.kind() !== expected.kind()) {\n throw new Error(\n `Type safety violation in ${context}: ` +\n `cached container kind '${cached.kind()}' does not match ` +\n `expected kind '${expected.kind()}'`,\n )\n }\n\n // Additional safety check: ensure IDs match\n if (cached.id !== expected.id) {\n throw new Error(\n `Container ID mismatch in ${context}: ` +\n `cached ID '${cached.id}' does not match expected ID '${expected.id}'`,\n )\n }\n}\n\n/**\n * Type guard to check if a schema is for TextDraftNode\n */\nexport function isTextShape(\n schema: ContainerOrValueShape,\n): schema is TextContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"text\"\n}\n\n/**\n * Type guard to check if a schema is for CounterDraftNode\n */\nexport function isCounterShape(\n schema: ContainerOrValueShape,\n): schema is CounterContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"counter\"\n}\n\n/**\n * Type guard to check if a schema is for ListDraftNode\n */\nexport function isListShape(\n schema: ContainerOrValueShape,\n): schema is ListContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"list\"\n}\n\n/**\n * Type guard to check if a schema is for MovableListDraftNode\n */\nexport function isMovableListShape(\n schema: ContainerOrValueShape,\n): schema is MovableListContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"movableList\"\n}\n\n/**\n * Type guard to check if a schema is for MapDraftNode\n */\nexport function isMapShape(\n schema: ContainerOrValueShape,\n): schema is MapContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"map\"\n}\n\n/**\n * Type guard to check if a schema is for RecordDraftNode\n */\nexport function isRecordShape(\n schema: ContainerOrValueShape,\n): schema is RecordContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"record\"\n}\n\n/**\n * Type guard to check if a schema is for TreeDraftNode\n */\nexport function isTreeShape(\n schema: ContainerOrValueShape,\n): schema is TreeContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"tree\"\n}\n\nexport function isContainerShape(\n schema: ContainerOrValueShape,\n): schema is ContainerShape {\n return schema._type && schema._type !== \"value\"\n}\n\n/**\n * Type guard to check if a schema is any of the Value shapes\n */\nexport function isValueShape(\n schema: ContainerOrValueShape,\n): schema is ValueShape {\n return (\n schema._type === \"value\" &&\n [\n \"string\",\n \"number\",\n \"boolean\",\n \"null\",\n \"undefined\",\n \"uint8array\",\n \"object\",\n \"record\",\n \"array\",\n ].includes(schema.valueType)\n )\n}\n\nexport function isObjectValue(value: Value): value is { [key: string]: Value } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n !(value instanceof Uint8Array)\n )\n}\n","import type { Container, LoroList, LoroMovableList } from \"loro-crdt\"\nimport { convertInputToNode } from \"../conversion.js\"\nimport type {\n ContainerOrValueShape,\n ContainerShape,\n ListContainerShape,\n MovableListContainerShape,\n} from \"../shape.js\"\nimport { isContainer, isValueShape } from \"../utils/type-guards.js\"\nimport { DraftNode, type DraftNodeParams } from \"./base.js\"\nimport { createContainerDraftNode } from \"./utils.js\"\n\n// Shared logic for list operations\nexport abstract class ListDraftNodeBase<\n NestedShape extends ContainerOrValueShape,\n Item = NestedShape[\"_plain\"],\n DraftItem = NestedShape[\"_draft\"],\n> extends DraftNode<any> {\n // Cache for items returned by array methods to track mutations\n private itemCache = new Map<number, any>()\n\n protected get container(): LoroList | LoroMovableList {\n return super.container as LoroList | LoroMovableList\n }\n\n protected get shape():\n | ListContainerShape<NestedShape>\n | MovableListContainerShape<NestedShape> {\n return super.shape as\n | ListContainerShape<NestedShape>\n | MovableListContainerShape<NestedShape>\n }\n\n absorbPlainValues() {\n // Critical function: absorb mutated plain values back into Loro containers\n // This is called at the end of change() to persist mutations made to plain objects\n for (const [index, cachedItem] of this.itemCache.entries()) {\n if (cachedItem) {\n if (isValueShape(this.shape.shape)) {\n // For value shapes, delegate to subclass-specific absorption logic\n this.absorbValueAtIndex(index, cachedItem)\n } else {\n // For container shapes, the item should be a draft node that handles its own absorption\n if (\n cachedItem &&\n typeof cachedItem === \"object\" &&\n \"absorbPlainValues\" in cachedItem\n ) {\n ;(cachedItem as any).absorbPlainValues()\n }\n }\n }\n }\n\n // Clear the cache after absorbing values\n this.itemCache.clear()\n }\n\n // Abstract method to be implemented by subclasses\n // Each subclass knows how to handle its specific container type\n protected abstract absorbValueAtIndex(index: number, value: any): void\n\n protected insertWithConversion(index: number, item: Item): void {\n const convertedItem = convertInputToNode(item as any, this.shape.shape)\n if (isContainer(convertedItem)) {\n this.container.insertContainer(index, convertedItem)\n } else {\n this.container.insert(index, convertedItem)\n }\n }\n\n protected pushWithConversion(item: Item): void {\n const convertedItem = convertInputToNode(item as any, this.shape.shape)\n if (isContainer(convertedItem)) {\n this.container.pushContainer(convertedItem)\n } else {\n this.container.push(convertedItem)\n }\n }\n\n getDraftNodeParams(\n index: number,\n shape: ContainerShape,\n ): DraftNodeParams<ContainerShape> {\n return {\n shape,\n emptyState: undefined, // List items don't have empty state\n getContainer: () => {\n const containerItem = this.container.get(index)\n if (!containerItem || !isContainer(containerItem)) {\n throw new Error(`No container found at index ${index}`)\n }\n return containerItem\n },\n }\n }\n\n // Get item for predicate functions - always returns plain Item for filtering logic\n protected getPredicateItem(index: number): Item {\n // CRITICAL FIX: For predicates to work correctly with mutations,\n // we need to check if there's a cached (mutated) version first\n const cachedItem = this.itemCache.get(index)\n if (cachedItem && isValueShape(this.shape.shape)) {\n // For value shapes, if we have a cached item, use it so predicates see mutations\n return cachedItem as Item\n }\n\n const containerItem = this.container.get(index)\n if (containerItem === undefined) {\n return undefined as Item\n }\n\n if (isValueShape(this.shape.shape)) {\n // For value shapes, return the plain value directly\n return containerItem as Item\n } else {\n // For container shapes, we need to return the plain object representation\n // This allows predicates to access nested properties like article.metadata.author\n if (isContainer(containerItem)) {\n // Convert container to plain object for predicate logic\n // Handle different container types that may not have toJSON method\n if (\n typeof containerItem === \"object\" &&\n containerItem !== null &&\n \"toJSON\" in containerItem\n ) {\n return (containerItem as any).toJSON() as Item\n } else if (\n typeof containerItem === \"object\" &&\n containerItem !== null &&\n \"getShallowValue\" in containerItem\n ) {\n // For containers like LoroCounter that don't have toJSON but have getShallowValue\n return (containerItem as any).getShallowValue() as Item\n } else {\n // Fallback for other container types\n return containerItem as Item\n }\n }\n return containerItem as Item\n }\n }\n\n // Get item for return values - returns DraftItem that can be mutated\n protected getDraftItem(index: number): DraftItem {\n // Check if we already have a cached item for this index\n let cachedItem = this.itemCache.get(index)\n if (cachedItem) {\n return cachedItem\n }\n\n // Get the raw container item\n const containerItem = this.container.get(index)\n if (containerItem === undefined) {\n return undefined as DraftItem\n }\n\n if (isValueShape(this.shape.shape)) {\n // For value shapes, we need to ensure mutations persist\n // The key insight: we must return the SAME object for the same index\n // so that mutations to filtered/found items persist back to the cache\n if (typeof containerItem === \"object\" && containerItem !== null) {\n // Create a deep copy for objects so mutations can be tracked\n // IMPORTANT: Only create the copy once, then always return the same cached object\n cachedItem = JSON.parse(JSON.stringify(containerItem))\n } else {\n // For primitives, just use the value directly\n cachedItem = containerItem\n }\n this.itemCache.set(index, cachedItem)\n return cachedItem as DraftItem\n } else {\n // For container shapes, create a proper draft node using the new pattern\n cachedItem = createContainerDraftNode(\n this.getDraftNodeParams(index, this.shape.shape as ContainerShape),\n )\n this.itemCache.set(index, cachedItem)\n return cachedItem as DraftItem\n }\n }\n\n // Array-like methods for better developer experience\n // DUAL INTERFACE: Predicates get Item (plain data), return values are DraftItem (mutable)\n\n find(\n predicate: (item: Item, index: number) => boolean,\n ): DraftItem | undefined {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (predicate(predicateItem, i)) {\n return this.getDraftItem(i) // Return mutable draft item\n }\n }\n return undefined\n }\n\n findIndex(predicate: (item: Item, index: number) => boolean): number {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (predicate(predicateItem, i)) {\n return i\n }\n }\n return -1\n }\n\n map<ReturnType>(\n callback: (item: Item, index: number) => ReturnType,\n ): ReturnType[] {\n const result: ReturnType[] = []\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n result.push(callback(predicateItem, i))\n }\n return result\n }\n\n filter(predicate: (item: Item, index: number) => boolean): DraftItem[] {\n const result: DraftItem[] = []\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (predicate(predicateItem, i)) {\n result.push(this.getDraftItem(i)) // Return mutable draft items\n }\n }\n return result\n }\n\n forEach(callback: (item: Item, index: number) => void): void {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n callback(predicateItem, i)\n }\n }\n\n some(predicate: (item: Item, index: number) => boolean): boolean {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (predicate(predicateItem, i)) {\n return true\n }\n }\n return false\n }\n\n every(predicate: (item: Item, index: number) => boolean): boolean {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (!predicate(predicateItem, i)) {\n return false\n }\n }\n return true\n }\n\n insert(index: number, item: Item): void {\n // Update cache indices before performing the insert operation\n this.updateCacheForInsert(index)\n this.insertWithConversion(index, item)\n }\n\n delete(index: number, len: number): void {\n // Update cache indices before performing the delete operation\n this.updateCacheForDelete(index, len)\n this.container.delete(index, len)\n }\n\n push(item: Item): void {\n this.pushWithConversion(item)\n }\n\n pushContainer(container: Container): Container {\n return this.container.pushContainer(container)\n }\n\n insertContainer(index: number, container: Container): Container {\n return this.container.insertContainer(index, container)\n }\n\n get(index: number): DraftItem {\n return this.getDraftItem(index)\n }\n\n toArray(): Item[] {\n return this.container.toArray() as Item[]\n }\n\n get length(): number {\n return this.container.length\n }\n\n // Update cache indices when items are deleted\n private updateCacheForDelete(deleteIndex: number, deleteLen: number): void {\n const newCache = new Map<number, any>()\n\n for (const [cachedIndex, cachedItem] of this.itemCache.entries()) {\n if (cachedIndex < deleteIndex) {\n // Items before the deletion point keep their indices\n newCache.set(cachedIndex, cachedItem)\n } else if (cachedIndex >= deleteIndex + deleteLen) {\n // Items after the deletion range shift down by deleteLen\n newCache.set(cachedIndex - deleteLen, cachedItem)\n }\n // Items within the deletion range are removed from cache\n }\n\n this.itemCache = newCache\n }\n\n // Update cache indices when items are inserted\n private updateCacheForInsert(insertIndex: number): void {\n const newCache = new Map<number, any>()\n\n for (const [cachedIndex, cachedItem] of this.itemCache.entries()) {\n if (cachedIndex < insertIndex) {\n // Items before the insertion point keep their indices\n newCache.set(cachedIndex, cachedItem)\n } else {\n // Items at or after the insertion point shift up by 1\n newCache.set(cachedIndex + 1, cachedItem)\n }\n }\n\n this.itemCache = newCache\n }\n}\n","import type { LoroList } from \"loro-crdt\"\nimport type { ContainerOrValueShape } from \"../shape.js\"\nimport { ListDraftNodeBase } from \"./list-base.js\"\n\n// List draft node\nexport class ListDraftNode<\n NestedShape extends ContainerOrValueShape,\n> extends ListDraftNodeBase<NestedShape> {\n protected get container(): LoroList {\n return super.container as LoroList\n }\n\n protected absorbValueAtIndex(index: number, value: any): void {\n // LoroList doesn't have set method, need to delete and insert\n this.container.delete(index, 1)\n this.container.insert(index, value)\n }\n}\n","import {\n type Container,\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n LoroTree,\n type Value,\n} from \"loro-crdt\"\nimport type {\n ContainerOrValueShape,\n ContainerShape,\n MapContainerShape,\n ValueShape,\n} from \"../shape.js\"\nimport { isContainerShape, isValueShape } from \"../utils/type-guards.js\"\nimport { DraftNode, type DraftNodeParams } from \"./base.js\"\nimport { createContainerDraftNode } from \"./utils.js\"\n\nconst containerConstructor = {\n counter: LoroCounter,\n list: LoroList,\n map: LoroMap,\n movableList: LoroMovableList,\n record: LoroMap,\n text: LoroText,\n tree: LoroTree,\n} as const\n\n// Map draft node\nexport class MapDraftNode<\n NestedShapes extends Record<string, ContainerOrValueShape>,\n> extends DraftNode<any> {\n private propertyCache = new Map<string, DraftNode<ContainerShape> | Value>()\n\n constructor(params: DraftNodeParams<MapContainerShape<NestedShapes>>) {\n super(params)\n this.createLazyProperties()\n }\n\n protected get shape(): MapContainerShape<NestedShapes> {\n return super.shape as MapContainerShape<NestedShapes>\n }\n\n protected get container(): LoroMap {\n return super.container as LoroMap\n }\n\n absorbPlainValues() {\n for (const [key, node] of this.propertyCache.entries()) {\n if (node instanceof DraftNode) {\n // Contains a DraftNode, not a plain Value: keep recursing\n node.absorbPlainValues()\n continue\n }\n\n // Plain value!\n this.container.set(key, node)\n }\n }\n\n getDraftNodeParams<S extends ContainerShape>(\n key: string,\n shape: S,\n ): DraftNodeParams<ContainerShape> {\n const emptyState = (this.emptyState as any)?.[key]\n\n const LoroContainer = containerConstructor[shape._type]\n\n return {\n shape,\n emptyState,\n getContainer: () =>\n this.container.getOrCreateContainer(key, new (LoroContainer as any)()),\n }\n }\n\n getOrCreateNode<Shape extends ContainerShape | ValueShape>(\n key: string,\n shape: Shape,\n ): Shape extends ContainerShape ? DraftNode<Shape> : Value {\n let node = this.propertyCache.get(key)\n if (!node) {\n if (isContainerShape(shape)) {\n node = createContainerDraftNode(this.getDraftNodeParams(key, shape))\n } else {\n // For value shapes, first try to get the value from the container\n const containerValue = this.container.get(key)\n if (containerValue !== undefined) {\n node = containerValue as Value\n } else {\n // Only fall back to empty state if the container doesn't have the value\n const emptyState = (this.emptyState as any)?.[key]\n if (!emptyState) {\n throw new Error(\"empty state required\")\n }\n node = emptyState as Value\n }\n }\n if (!node) throw new Error(\"no container made\")\n this.propertyCache.set(key, node)\n }\n\n return node as Shape extends ContainerShape ? DraftNode<Shape> : Value\n }\n\n private createLazyProperties(): void {\n for (const key in this.shape.shapes) {\n const shape = this.shape.shapes[key]\n Object.defineProperty(this, key, {\n get: () => this.getOrCreateNode(key, shape),\n set: isValueShape(shape)\n ? value => {\n // console.log(\"set value\", value)\n this.container.set(key, value)\n }\n : undefined,\n })\n }\n }\n\n // TOOD(duane): return correct type here\n get(key: string): any {\n return this.container.get(key)\n }\n\n set(key: string, value: Value): void {\n this.container.set(key, value)\n }\n\n setContainer<C extends Container>(key: string, container: C): C {\n return this.container.setContainer(key, container)\n }\n\n delete(key: string): void {\n this.container.delete(key)\n }\n\n has(key: string): boolean {\n // LoroMap doesn't have a has method, so we check if get returns undefined\n return this.container.get(key) !== undefined\n }\n\n keys(): string[] {\n return this.container.keys()\n }\n\n values(): any[] {\n return this.container.values()\n }\n\n get size(): number {\n return this.container.size\n }\n}\n","import type { Container, LoroMovableList } from \"loro-crdt\"\nimport type { ContainerOrValueShape } from \"../shape.js\"\nimport { ListDraftNodeBase } from \"./list-base.js\"\n\n// Movable list draft node\nexport class MovableListDraftNode<\n NestedShape extends ContainerOrValueShape,\n Item = NestedShape[\"_plain\"],\n> extends ListDraftNodeBase<NestedShape> {\n protected get container(): LoroMovableList {\n return super.container as LoroMovableList\n }\n\n protected absorbValueAtIndex(index: number, value: any): void {\n // LoroMovableList has set method\n this.container.set(index, value)\n }\n\n move(from: number, to: number): void {\n this.container.move(from, to)\n }\n\n set(index: number, item: Exclude<Item, Container>) {\n return this.container.set(index, item)\n }\n}\n","import {\n type Container,\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n LoroTree,\n type Value,\n} from \"loro-crdt\"\nimport type {\n ContainerOrValueShape,\n ContainerShape,\n RecordContainerShape,\n} from \"../shape.js\"\nimport type { InferDraftType } from \"../types.js\"\nimport { isContainerShape, isValueShape } from \"../utils/type-guards.js\"\nimport { DraftNode, type DraftNodeParams } from \"./base.js\"\nimport { createContainerDraftNode } from \"./utils.js\"\n\nconst containerConstructor = {\n counter: LoroCounter,\n list: LoroList,\n map: LoroMap,\n movableList: LoroMovableList,\n record: LoroMap,\n text: LoroText,\n tree: LoroTree,\n} as const\n\n// Record draft node\nexport class RecordDraftNode<\n NestedShape extends ContainerOrValueShape,\n> extends DraftNode<any> {\n private nodeCache = new Map<string, DraftNode<ContainerShape> | Value>()\n\n constructor(params: DraftNodeParams<RecordContainerShape<NestedShape>>) {\n super(params)\n // We don't need to create lazy properties because keys are dynamic\n // But we could use a Proxy if we wanted property access syntax like record.key\n // However, for now let's stick to get/set methods or maybe Proxy for better DX?\n // The requirement says \"records with uniform specific key type and value\".\n // Usually records are accessed via keys.\n // If we want `draft.record.key`, we need a Proxy.\n // biome-ignore lint/correctness/noConstructorReturn: Proxy return is intentional\n return new Proxy(this, {\n get: (target, prop) => {\n if (typeof prop === \"string\" && !(prop in target)) {\n return target.get(prop)\n }\n return Reflect.get(target, prop)\n },\n set: (target, prop, value) => {\n if (typeof prop === \"string\" && !(prop in target)) {\n target.set(prop, value)\n return true\n }\n return Reflect.set(target, prop, value)\n },\n deleteProperty: (target, prop) => {\n if (typeof prop === \"string\" && !(prop in target)) {\n target.delete(prop)\n return true\n }\n return Reflect.deleteProperty(target, prop)\n },\n ownKeys: target => {\n return target.keys()\n },\n getOwnPropertyDescriptor: (target, prop) => {\n if (typeof prop === \"string\" && target.has(prop)) {\n return {\n configurable: true,\n enumerable: true,\n value: target.get(prop),\n }\n }\n return Reflect.getOwnPropertyDescriptor(target, prop)\n },\n })\n }\n\n protected get shape(): RecordContainerShape<NestedShape> {\n return super.shape as RecordContainerShape<NestedShape>\n }\n\n protected get container(): LoroMap {\n return super.container as LoroMap\n }\n\n absorbPlainValues() {\n for (const [key, node] of this.nodeCache.entries()) {\n if (node instanceof DraftNode) {\n // Contains a DraftNode, not a plain Value: keep recursing\n node.absorbPlainValues()\n continue\n }\n\n // Plain value!\n this.container.set(key, node)\n }\n }\n\n getDraftNodeParams<S extends ContainerShape>(\n key: string,\n shape: S,\n ): DraftNodeParams<ContainerShape> {\n const emptyState = (this.emptyState as any)?.[key]\n\n const LoroContainer = containerConstructor[shape._type]\n\n return {\n shape,\n emptyState,\n getContainer: () =>\n this.container.getOrCreateContainer(key, new (LoroContainer as any)()),\n }\n }\n\n getOrCreateNode(key: string): InferDraftType<NestedShape> {\n let node = this.nodeCache.get(key)\n if (!node) {\n const shape = this.shape.shape\n if (isContainerShape(shape)) {\n node = createContainerDraftNode(\n this.getDraftNodeParams(key, shape as ContainerShape),\n )\n } else {\n // For value shapes, first try to get the value from the container\n const containerValue = this.container.get(key)\n if (containerValue !== undefined) {\n node = containerValue as Value\n } else {\n // Only fall back to empty state if the container doesn't have the value\n const emptyState = (this.emptyState as any)?.[key]\n // For records, empty state might not have the key, which is fine?\n // But if we are accessing it, maybe we expect it to exist or be created?\n // If it's a value type, we can't really \"create\" it without a value.\n // So if it's undefined in container and empty state, we return undefined?\n // But the return type expects Value.\n // Let's check MapDraftNode.\n // MapDraftNode throws \"empty state required\" if not found.\n // But for Record, keys are dynamic.\n if (emptyState === undefined) {\n // If it's a value type and not in container or empty state,\n // we should probably return undefined if the type allows it,\n // or maybe the default value for that type?\n // But we don't have a default value generator for shapes.\n // Actually Shape.plain.* factories have _plain and _draft which are defaults.\n node = (shape as any)._plain\n } else {\n node = emptyState as Value\n }\n }\n }\n if (node !== undefined) {\n this.nodeCache.set(key, node)\n }\n }\n\n return node as any\n }\n\n get(key: string): InferDraftType<NestedShape> {\n return this.getOrCreateNode(key)\n }\n\n set(key: string, value: any): void {\n if (isValueShape(this.shape.shape)) {\n this.container.set(key, value)\n // Update cache if needed?\n // MapDraftNode updates container directly for values.\n // But we also cache values in nodeCache for consistency?\n // MapDraftNode doesn't cache values in propertyCache if they are set via setter?\n // Actually MapDraftNode setter:\n // set: isValueShape(shape) ? value => this.container.set(key, value) : undefined\n // It doesn't update propertyCache.\n // But getOrCreateNode checks propertyCache first.\n // So if we set it, we should probably update propertyCache or clear it for that key.\n this.nodeCache.set(key, value)\n } else {\n // For containers, we can't set them directly usually.\n // But if the user passes a plain object that matches the shape, maybe we should convert it?\n // But typically we modify the draft node.\n throw new Error(\n \"Cannot set container directly, modify the draft node instead\",\n )\n }\n }\n\n setContainer<C extends Container>(key: string, container: C): C {\n return this.container.setContainer(key, container)\n }\n\n delete(key: string): void {\n this.container.delete(key)\n this.nodeCache.delete(key)\n }\n\n has(key: string): boolean {\n return this.container.get(key) !== undefined\n }\n\n keys(): string[] {\n return this.container.keys()\n }\n\n values(): any[] {\n return this.container.values()\n }\n\n get size(): number {\n return this.container.size\n }\n}\n","import type { TextContainerShape } from \"../shape.js\"\nimport { DraftNode } from \"./base.js\"\n\n// Text draft node\nexport class TextDraftNode extends DraftNode<TextContainerShape> {\n absorbPlainValues() {\n // no plain values contained within\n }\n\n // Text methods\n insert(index: number, content: string): void {\n // TODO(duane): condition can be removed when https://github.com/loro-dev/loro/issues/872 is addressed\n if (content.length === 0) return\n this.container.insert(index, content)\n }\n\n delete(index: number, len: number): void {\n this.container.delete(index, len)\n }\n\n toString(): string {\n return this.container.toString()\n }\n\n update(text: string): void {\n this.container.update(text)\n }\n\n mark(range: { start: number; end: number }, key: string, value: any): void {\n this.container.mark(range, key, value)\n }\n\n unmark(range: { start: number; end: number }, key: string): void {\n this.container.unmark(range, key)\n }\n\n toDelta(): any[] {\n return this.container.toDelta()\n }\n\n applyDelta(delta: any[]): void {\n this.container.applyDelta(delta)\n }\n\n get length(): number {\n return this.container.length\n }\n}\n","import type { TreeContainerShape } from \"../shape.js\"\nimport { DraftNode } from \"./base.js\"\n\n// Tree draft node\nexport class TreeDraftNode<T extends TreeContainerShape> extends DraftNode<T> {\n absorbPlainValues() {\n // TODO(duane): implement for trees\n }\n\n createNode(parent?: any, index?: number): any {\n return this.container.createNode(parent, index)\n }\n\n move(target: any, parent?: any, index?: number): void {\n this.container.move(target, parent, index)\n }\n\n delete(target: any): void {\n this.container.delete(target)\n }\n\n has(target: any): boolean {\n return this.container.has(target)\n }\n\n getNodeByID(id: any): any {\n return this.container.getNodeByID\n ? this.container.getNodeByID(id)\n : undefined\n }\n}\n","import type {\n ContainerShape,\n CounterContainerShape,\n ListContainerShape,\n MapContainerShape,\n MovableListContainerShape,\n RecordContainerShape,\n TextContainerShape,\n TreeContainerShape,\n} from \"../shape.js\"\nimport type { DraftNode, DraftNodeParams } from \"./base.js\"\nimport { CounterDraftNode } from \"./counter.js\"\nimport { ListDraftNode } from \"./list.js\"\nimport { MapDraftNode } from \"./map.js\"\nimport { MovableListDraftNode } from \"./movable-list.js\"\nimport { RecordDraftNode } from \"./record.js\"\nimport { TextDraftNode } from \"./text.js\"\nimport { TreeDraftNode } from \"./tree.js\"\n\n// Generic catch-all overload\nexport function createContainerDraftNode<T extends ContainerShape>(\n params: DraftNodeParams<T>,\n): DraftNode<T>\n\n// Implementation\nexport function createContainerDraftNode(\n params: DraftNodeParams<ContainerShape>,\n): DraftNode<ContainerShape> {\n switch (params.shape._type) {\n case \"counter\":\n return new CounterDraftNode(\n params as DraftNodeParams<CounterContainerShape>,\n )\n case \"list\":\n return new ListDraftNode(params as DraftNodeParams<ListContainerShape>)\n case \"map\":\n return new MapDraftNode(params as DraftNodeParams<MapContainerShape>)\n case \"movableList\":\n return new MovableListDraftNode(\n params as DraftNodeParams<MovableListContainerShape>,\n )\n case \"record\":\n return new RecordDraftNode(\n params as DraftNodeParams<RecordContainerShape>,\n )\n case \"text\":\n return new TextDraftNode(params as DraftNodeParams<TextContainerShape>)\n case \"tree\":\n return new TreeDraftNode(params as DraftNodeParams<TreeContainerShape>)\n default:\n throw new Error(\n `Unknown container type: ${(params.shape as ContainerShape)._type}`,\n )\n }\n}\n","import type { LoroDoc } from \"loro-crdt\"\nimport type { InferPlainType } from \"../index.js\"\nimport type { ContainerShape, DocShape } from \"../shape.js\"\nimport { DraftNode, type DraftNodeParams } from \"./base.js\"\nimport { createContainerDraftNode } from \"./utils.js\"\n\nconst containerGetter = {\n counter: \"getCounter\",\n list: \"getList\",\n map: \"getMap\",\n movableList: \"getMovableList\",\n record: \"getMap\",\n text: \"getText\",\n tree: \"getTree\",\n} as const\n\n// Draft Document class -- the actual object passed to the change `mutation` function\nexport class DraftDoc<Shape extends DocShape> extends DraftNode<Shape> {\n private doc: LoroDoc\n private propertyCache = new Map<string, DraftNode<ContainerShape>>()\n private requiredEmptyState!: InferPlainType<Shape>\n\n constructor(\n _params: Omit<DraftNodeParams<Shape>, \"getContainer\"> & { doc: LoroDoc },\n ) {\n super({\n ..._params,\n getContainer: () => {\n throw new Error(\"can't get container on DraftDoc\")\n },\n })\n if (!_params.emptyState) throw new Error(\"emptyState required\")\n this.doc = _params.doc\n this.requiredEmptyState = _params.emptyState\n this.createLazyProperties()\n }\n\n getDraftNodeParams<S extends ContainerShape>(\n key: string,\n shape: S,\n ): DraftNodeParams<ContainerShape> {\n const getter = this.doc[containerGetter[shape._type]].bind(this.doc)\n\n return {\n shape,\n emptyState: this.requiredEmptyState[key],\n getContainer: () => getter(key),\n }\n }\n\n getOrCreateDraftNode(\n key: string,\n shape: ContainerShape,\n ): DraftNode<ContainerShape> {\n let node = this.propertyCache.get(key)\n\n if (!node) {\n node = createContainerDraftNode(this.getDraftNodeParams(key, shape))\n this.propertyCache.set(key, node)\n }\n\n return node\n }\n\n private createLazyProperties(): void {\n for (const key in this.shape.shapes) {\n const shape = this.shape.shapes[key]\n Object.defineProperty(this, key, {\n get: () => this.getOrCreateDraftNode(key, shape),\n })\n }\n }\n\n absorbPlainValues(): void {\n // By iterating over the propertyCache, we achieve a small optimization\n // by only absorbing values that have been 'touched' in some way\n for (const [, node] of this.propertyCache.entries()) {\n node.absorbPlainValues()\n }\n }\n}\n","/** biome-ignore-all lint/suspicious/noExplicitAny: JSON Patch values can be any type */\n\nimport type { DocShape } from \"./shape.js\"\nimport type { Draft } from \"./types.js\"\n\n// =============================================================================\n// JSON PATCH TYPES - Discriminated Union for Type Safety\n// =============================================================================\n\nexport type JsonPatchAddOperation = {\n op: \"add\"\n path: string | (string | number)[]\n value: any\n}\n\nexport type JsonPatchRemoveOperation = {\n op: \"remove\"\n path: string | (string | number)[]\n}\n\nexport type JsonPatchReplaceOperation = {\n op: \"replace\"\n path: string | (string | number)[]\n value: any\n}\n\nexport type JsonPatchMoveOperation = {\n op: \"move\"\n path: string | (string | number)[]\n from: string | (string | number)[]\n}\n\nexport type JsonPatchCopyOperation = {\n op: \"copy\"\n path: string | (string | number)[]\n from: string | (string | number)[]\n}\n\nexport type JsonPatchTestOperation = {\n op: \"test\"\n path: string | (string | number)[]\n value: any\n}\n\nexport type JsonPatchOperation =\n | JsonPatchAddOperation\n | JsonPatchRemoveOperation\n | JsonPatchReplaceOperation\n | JsonPatchMoveOperation\n | JsonPatchCopyOperation\n | JsonPatchTestOperation\n\nexport type JsonPatch = JsonPatchOperation[]\n\n// =============================================================================\n// PATH NAVIGATION UTILITIES\n// =============================================================================\n\n/**\n * Normalize JSON Pointer string to path array\n * Handles RFC 6901 escaping: ~1 -> /, ~0 -> ~\n */\nexport function normalizePath(\n path: string | (string | number)[],\n): (string | number)[] {\n if (Array.isArray(path)) {\n return path\n }\n\n // Handle JSON Pointer format (RFC 6901)\n if (path.startsWith(\"/\")) {\n return path\n .slice(1) // Remove leading slash\n .split(\"/\")\n .map(segment => {\n // Handle JSON Pointer escaping\n const unescaped = segment.replace(/~1/g, \"/\").replace(/~0/g, \"~\")\n // Try to parse as number for array indices\n const asNumber = Number(unescaped)\n return Number.isInteger(asNumber) && asNumber >= 0\n ? asNumber\n : unescaped\n })\n }\n\n // Handle simple dot notation or single segment\n return path.split(\".\").map(segment => {\n const asNumber = Number(segment)\n return Number.isInteger(asNumber) && asNumber >= 0 ? asNumber : segment\n })\n}\n\n/**\n * Navigate to a target path using natural DraftNode property access\n * This follows the existing patterns from the test suite\n */\nfunction navigateToPath<T extends DocShape>(\n draft: Draft<T>,\n path: (string | number)[],\n): { parent: any; key: string | number } {\n if (path.length === 0) {\n throw new Error(\"Cannot navigate to empty path\")\n }\n\n let current = draft as any\n\n // Navigate to parent of target\n for (let i = 0; i < path.length - 1; i++) {\n const segment = path[i]\n\n if (typeof segment === \"string\") {\n // Use natural property access - this leverages existing DraftNode lazy creation\n current = current[segment]\n if (current === undefined) {\n throw new Error(`Cannot navigate to path segment: ${segment}`)\n }\n } else if (typeof segment === \"number\") {\n // List/array access using get() method (following existing patterns)\n if (current.get && typeof current.get === \"function\") {\n current = current.get(segment)\n if (current === undefined) {\n throw new Error(`List index ${segment} does not exist`)\n }\n } else {\n throw new Error(`Cannot use numeric index ${segment} on non-list`)\n }\n } else {\n throw new Error(`Invalid path segment type: ${typeof segment}`)\n }\n }\n\n const targetKey = path[path.length - 1]\n return { parent: current, key: targetKey }\n}\n\n/**\n * Get value at path using natural DraftNode access patterns\n */\nfunction getValueAtPath<T extends DocShape>(\n draft: Draft<T>,\n path: (string | number)[],\n): any {\n if (path.length === 0) {\n return draft\n }\n\n const { parent, key } = navigateToPath(draft, path)\n\n if (typeof key === \"string\") {\n // Use natural property access or get() method\n if (parent.get && typeof parent.get === \"function\") {\n return parent.get(key)\n }\n return parent[key]\n } else if (typeof key === \"number\") {\n // List access using get() method\n if (parent.get && typeof parent.get === \"function\") {\n return parent.get(key)\n }\n throw new Error(`Cannot use numeric index ${key} on non-list`)\n }\n\n throw new Error(`Invalid key type: ${typeof key}`)\n}\n\n// =============================================================================\n// OPERATION HANDLERS - Following existing DraftNode patterns\n// =============================================================================\n\n/**\n * Handle 'add' operation using existing DraftNode methods\n */\nfunction handleAdd<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchAddOperation,\n): void {\n const path = normalizePath(operation.path)\n const { parent, key } = navigateToPath(draft, path)\n\n if (typeof key === \"string\") {\n // Map-like operations - use natural assignment or set() method\n if (parent.set && typeof parent.set === \"function\") {\n parent.set(key, operation.value)\n } else {\n // Natural property assignment (follows existing test patterns)\n parent[key] = operation.value\n }\n } else if (typeof key === \"number\") {\n // List operations - use insert() method (follows existing patterns)\n if (parent.insert && typeof parent.insert === \"function\") {\n parent.insert(key, operation.value)\n } else {\n throw new Error(`Cannot insert at numeric index ${key} on non-list`)\n }\n } else {\n throw new Error(`Invalid key type: ${typeof key}`)\n }\n}\n\n/**\n * Handle 'remove' operation using existing DraftNode methods\n */\nfunction handleRemove<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchRemoveOperation,\n): void {\n const path = normalizePath(operation.path)\n const { parent, key } = navigateToPath(draft, path)\n\n if (typeof key === \"string\") {\n // Map-like operations - use delete() method (follows existing patterns)\n if (parent.delete && typeof parent.delete === \"function\") {\n parent.delete(key)\n } else {\n delete parent[key]\n }\n } else if (typeof key === \"number\") {\n // List operations - use delete() method with count (follows existing patterns)\n if (parent.delete && typeof parent.delete === \"function\") {\n parent.delete(key, 1)\n } else {\n throw new Error(`Cannot remove at numeric index ${key} on non-list`)\n }\n } else {\n throw new Error(`Invalid key type: ${typeof key}`)\n }\n}\n\n/**\n * Handle 'replace' operation using existing DraftNode methods\n */\nfunction handleReplace<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchReplaceOperation,\n): void {\n const path = normalizePath(operation.path)\n const { parent, key } = navigateToPath(draft, path)\n\n if (typeof key === \"string\") {\n // Map-like operations - use set() method or natural assignment\n if (parent.set && typeof parent.set === \"function\") {\n parent.set(key, operation.value)\n } else {\n parent[key] = operation.value\n }\n } else if (typeof key === \"number\") {\n // List operations - delete then insert (follows existing patterns)\n if (\n parent.delete &&\n parent.insert &&\n typeof parent.delete === \"function\" &&\n typeof parent.insert === \"function\"\n ) {\n parent.delete(key, 1)\n parent.insert(key, operation.value)\n } else {\n throw new Error(`Cannot replace at numeric index ${key} on non-list`)\n }\n } else {\n throw new Error(`Invalid key type: ${typeof key}`)\n }\n}\n\n/**\n * Handle 'move' operation using existing DraftNode methods\n */\nfunction handleMove<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchMoveOperation,\n): void {\n const fromPath = normalizePath(operation.from)\n const toPath = normalizePath(operation.path)\n\n // For list moves within the same parent, we need special handling\n if (\n fromPath.length === toPath.length &&\n fromPath.slice(0, -1).every((segment, i) => segment === toPath[i])\n ) {\n // Same parent container - use list move operation if available\n const fromIndex = fromPath[fromPath.length - 1]\n const toIndex = toPath[toPath.length - 1]\n\n if (typeof fromIndex === \"number\" && typeof toIndex === \"number\") {\n const { parent } = navigateToPath(draft, fromPath.slice(0, -1))\n\n // Check if the parent has a move method (like LoroMovableList)\n if (parent.move && typeof parent.move === \"function\") {\n parent.move(fromIndex, toIndex)\n return\n }\n\n // Otherwise, get value, remove, then add at target index\n const value = getValueAtPath(draft, fromPath)\n handleRemove(draft, { op: \"remove\", path: operation.from })\n\n // For JSON Patch move semantics, the target index refers to the position\n // in the final array, not the intermediate array after removal.\n // No index adjustment needed - use the original target index.\n handleAdd(draft, { op: \"add\", path: operation.path, value })\n return\n }\n }\n\n // Different parents or non-numeric indices - standard move\n const value = getValueAtPath(draft, fromPath)\n handleRemove(draft, { op: \"remove\", path: operation.from })\n handleAdd(draft, { op: \"add\", path: operation.path, value })\n}\n\n/**\n * Handle 'copy' operation using existing DraftNode methods\n */\nfunction handleCopy<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchCopyOperation,\n): void {\n const fromPath = normalizePath(operation.from)\n\n // Get the value to copy\n const value = getValueAtPath(draft, fromPath)\n\n // Add to destination (no removal)\n handleAdd(draft, { op: \"add\", path: operation.path, value })\n}\n\n/**\n * Handle 'test' operation using existing DraftNode value access\n */\nfunction handleTest<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchTestOperation,\n): boolean {\n const path = normalizePath(operation.path)\n const actualValue = getValueAtPath(draft, path)\n\n // Deep equality check for test operation\n return JSON.stringify(actualValue) === JSON.stringify(operation.value)\n}\n\n// =============================================================================\n// MAIN APPLICATOR - Simple orchestration following existing patterns\n// =============================================================================\n\n/**\n * Main JSON Patch applicator - follows existing change() patterns\n */\nexport class JsonPatchApplicator<T extends DocShape> {\n constructor(private rootDraft: Draft<T>) {}\n\n /**\n * Apply a single JSON Patch operation\n */\n applyOperation(operation: JsonPatchOperation): void {\n switch (operation.op) {\n case \"add\":\n handleAdd(this.rootDraft, operation)\n break\n case \"remove\":\n handleRemove(this.rootDraft, operation)\n break\n case \"replace\":\n handleReplace(this.rootDraft, operation)\n break\n case \"move\":\n handleMove(this.rootDraft, operation)\n break\n case \"copy\":\n handleCopy(this.rootDraft, operation)\n break\n case \"test\":\n if (!handleTest(this.rootDraft, operation)) {\n throw new Error(`JSON Patch test failed at path: ${operation.path}`)\n }\n break\n default:\n // TypeScript will catch this at compile time with proper discriminated union\n throw new Error(\n `Unsupported JSON Patch operation: ${(operation as any).op}`,\n )\n }\n }\n\n /**\n * Apply multiple JSON Patch operations in sequence\n */\n applyPatch(patch: JsonPatch): void {\n for (const operation of patch) {\n this.applyOperation(operation)\n }\n }\n}\n","import type { Value } from \"loro-crdt\"\nimport type { ContainerShape, DocShape, ValueShape } from \"./shape.js\"\nimport { isObjectValue } from \"./utils/type-guards.js\"\n\n/**\n * Overlays CRDT state with empty state defaults\n */\nexport function overlayEmptyState<Shape extends DocShape>(\n shape: Shape,\n crdtValue: { [key: string]: Value },\n emptyValue: { [key: string]: Value },\n): { [key: string]: Value } {\n if (typeof crdtValue !== \"object\") {\n throw new Error(\"crdt object is required\")\n }\n\n if (typeof emptyValue !== \"object\") {\n throw new Error(\"empty object is required\")\n }\n\n const result = { ...emptyValue }\n\n for (const [key, propShape] of Object.entries(shape.shapes)) {\n const propCrdtValue = crdtValue[key]\n\n const propEmptyValue = emptyValue[key as keyof typeof emptyValue]\n\n result[key as keyof typeof result] = mergeValue(\n propShape,\n propCrdtValue,\n propEmptyValue,\n )\n }\n\n return result\n}\n\n/**\n * Merges individual CRDT values with empty state defaults\n */\nexport function mergeValue<Shape extends ContainerShape | ValueShape>(\n shape: Shape,\n crdtValue: Value,\n emptyValue: Value,\n): Value {\n if (crdtValue === undefined && emptyValue === undefined) {\n throw new Error(\"either crdt or empty value must be defined\")\n }\n\n switch (shape._type) {\n case \"text\":\n return crdtValue ?? emptyValue ?? \"\"\n case \"counter\":\n return crdtValue ?? emptyValue ?? 0\n case \"list\":\n case \"movableList\":\n return crdtValue ?? emptyValue ?? []\n case \"map\": {\n if (!isObjectValue(crdtValue) && crdtValue !== undefined) {\n throw new Error(\"map crdt must be object\")\n }\n\n const crdtMapValue = crdtValue ?? {}\n\n if (!isObjectValue(emptyValue) && emptyValue !== undefined) {\n throw new Error(\"map empty state must be object\")\n }\n\n const emptyMapValue = emptyValue ?? {}\n\n const result = { ...emptyMapValue }\n for (const [key, nestedShape] of Object.entries(shape.shapes)) {\n const nestedCrdtValue = crdtMapValue[key]\n const nestedEmptyValue = emptyMapValue[key]\n\n result[key as keyof typeof result] = mergeValue(\n nestedShape,\n nestedCrdtValue,\n nestedEmptyValue,\n )\n }\n\n return result\n }\n case \"tree\":\n return crdtValue ?? emptyValue ?? []\n default:\n return crdtValue ?? emptyValue\n }\n}\n","import type {\n ArrayValueShape,\n ContainerOrValueShape,\n DocShape,\n ListContainerShape,\n MapContainerShape,\n MovableListContainerShape,\n ObjectValueShape,\n RecordContainerShape,\n RecordValueShape,\n UnionValueShape,\n ValueShape,\n} from \"./shape.js\"\nimport type { InferPlainType } from \"./types.js\"\n\n/**\n * Validates a value against a ContainerShape or ValueShape schema\n */\nexport function validateValue(\n value: unknown,\n schema: ContainerOrValueShape,\n path: string = \"\",\n): unknown {\n if (!schema || typeof schema !== \"object\" || !(\"_type\" in schema)) {\n throw new Error(`Invalid schema at path ${path}: missing _type`)\n }\n\n const currentPath = path || \"root\"\n\n // Handle ContainerShape types\n if (schema._type === \"text\") {\n if (typeof value !== \"string\") {\n throw new Error(\n `Expected string at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n }\n\n if (schema._type === \"counter\") {\n if (typeof value !== \"number\") {\n throw new Error(\n `Expected number at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n }\n\n if (schema._type === \"list\" || schema._type === \"movableList\") {\n if (!Array.isArray(value)) {\n throw new Error(\n `Expected array at path ${currentPath}, got ${typeof value}`,\n )\n }\n const listSchema = schema as ListContainerShape | MovableListContainerShape\n return value.map((item, index) =>\n validateValue(item, listSchema.shape, `${currentPath}[${index}]`),\n )\n }\n\n if (schema._type === \"map\") {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `Expected object at path ${currentPath}, got ${typeof value}`,\n )\n }\n const mapSchema = schema as MapContainerShape\n const result: Record<string, unknown> = {}\n\n // Validate each property in the map shape\n for (const [key, nestedSchema] of Object.entries(mapSchema.shapes)) {\n const nestedPath = `${currentPath}.${key}`\n const nestedValue = (value as Record<string, unknown>)[key]\n result[key] = validateValue(nestedValue, nestedSchema, nestedPath)\n }\n return result\n }\n\n if (schema._type === \"record\") {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `Expected object at path ${currentPath}, got ${typeof value}`,\n )\n }\n const recordSchema = schema as RecordContainerShape\n const result: Record<string, unknown> = {}\n\n // Validate each property in the record\n for (const [key, nestedValue] of Object.entries(value)) {\n const nestedPath = `${currentPath}.${key}`\n result[key] = validateValue(nestedValue, recordSchema.shape, nestedPath)\n }\n return result\n }\n\n if (schema._type === \"tree\") {\n if (!Array.isArray(value)) {\n throw new Error(\n `Expected array for tree at path ${currentPath}, got ${typeof value}`,\n )\n }\n // Trees can contain any structure, so we just validate it's an array\n return value\n }\n\n // Handle ValueShape types\n if (schema._type === \"value\") {\n const valueSchema = schema as ValueShape\n\n switch (valueSchema.valueType) {\n case \"string\":\n if (typeof value !== \"string\") {\n throw new Error(\n `Expected string at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"number\":\n if (typeof value !== \"number\") {\n throw new Error(\n `Expected number at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"boolean\":\n if (typeof value !== \"boolean\") {\n throw new Error(\n `Expected boolean at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"null\":\n if (value !== null) {\n throw new Error(\n `Expected null at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"undefined\":\n if (value !== undefined) {\n throw new Error(\n `Expected undefined at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"uint8array\":\n if (!(value instanceof Uint8Array)) {\n throw new Error(\n `Expected Uint8Array at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"object\": {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `Expected object at path ${currentPath}, got ${typeof value}`,\n )\n }\n const objectSchema = valueSchema as ObjectValueShape\n const result: Record<string, unknown> = {}\n\n // Validate each property in the object shape\n for (const [key, nestedSchema] of Object.entries(objectSchema.shape)) {\n const nestedPath = `${currentPath}.${key}`\n const nestedValue = (value as Record<string, unknown>)[key]\n result[key] = validateValue(nestedValue, nestedSchema, nestedPath)\n }\n return result\n }\n\n case \"record\": {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `Expected object at path ${currentPath}, got ${typeof value}`,\n )\n }\n const recordSchema = valueSchema as RecordValueShape\n const result: Record<string, unknown> = {}\n\n // Validate each property in the record\n for (const [key, nestedValue] of Object.entries(value)) {\n const nestedPath = `${currentPath}.${key}`\n result[key] = validateValue(\n nestedValue,\n recordSchema.shape,\n nestedPath,\n )\n }\n return result\n }\n\n case \"array\": {\n if (!Array.isArray(value)) {\n throw new Error(\n `Expected array at path ${currentPath}, got ${typeof value}`,\n )\n }\n const arraySchema = valueSchema as ArrayValueShape\n return value.map((item, index) =>\n validateValue(item, arraySchema.shape, `${currentPath}[${index}]`),\n )\n }\n\n case \"union\": {\n const unionSchema = valueSchema as UnionValueShape\n let lastError: Error | null = null\n\n // Try to validate against each shape in the union\n for (const shape of unionSchema.shapes) {\n try {\n return validateValue(value, shape, currentPath)\n } catch (error) {\n lastError = error as Error\n }\n }\n\n throw new Error(\n `Value at path ${currentPath} does not match any union type: ${lastError?.message}`,\n )\n }\n\n default:\n throw new Error(`Unknown value type: ${(valueSchema as any).valueType}`)\n }\n }\n\n throw new Error(`Unknown schema type: ${(schema as any)._type}`)\n}\n\n/**\n * Validates empty state against schema structure without using Zod\n * Combines the functionality of createEmptyStateValidator and createValueValidator\n */\nexport function validateEmptyState<T extends DocShape>(\n emptyState: unknown,\n schema: T,\n): InferPlainType<T> {\n if (\n !emptyState ||\n typeof emptyState !== \"object\" ||\n Array.isArray(emptyState)\n ) {\n throw new Error(\"Empty state must be an object\")\n }\n\n const result: Record<string, unknown> = {}\n\n // Validate each property in the document schema\n for (const [key, schemaValue] of Object.entries(schema.shapes)) {\n const value = (emptyState as Record<string, unknown>)[key]\n result[key] = validateValue(value, schemaValue, key)\n }\n\n return result as InferPlainType<T>\n}\n","// biome-ignore-all lint/suspicious/noExplicitAny: required\n\nimport type {\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n LoroTree,\n} from \"loro-crdt\"\n\nimport type { CounterDraftNode } from \"./draft-nodes/counter.js\"\nimport type { ListDraftNode } from \"./draft-nodes/list.js\"\nimport type { MapDraftNode } from \"./draft-nodes/map.js\"\nimport type { MovableListDraftNode } from \"./draft-nodes/movable-list.js\"\nimport type { RecordDraftNode } from \"./draft-nodes/record.js\"\nimport type { TextDraftNode } from \"./draft-nodes/text.js\"\n\nexport interface Shape<Plain, Draft> {\n readonly _type: string\n readonly _plain: Plain\n readonly _draft: Draft\n}\n\nexport interface DocShape<\n NestedShapes extends Record<string, ContainerShape> = Record<\n string,\n ContainerShape\n >,\n> extends Shape<\n { [K in keyof NestedShapes]: NestedShapes[K][\"_plain\"] },\n { [K in keyof NestedShapes]: NestedShapes[K][\"_draft\"] }\n > {\n readonly _type: \"doc\"\n // A doc's root containers each separately has its own shape, hence 'shapes'\n readonly shapes: NestedShapes\n}\n\nexport interface TextContainerShape extends Shape<string, TextDraftNode> {\n readonly _type: \"text\"\n}\nexport interface CounterContainerShape extends Shape<number, CounterDraftNode> {\n readonly _type: \"counter\"\n}\nexport interface TreeContainerShape<NestedShape = ContainerOrValueShape>\n extends Shape<any, any> {\n readonly _type: \"tree\"\n // TODO(duane): What does a tree contain? One type, or many?\n readonly shape: NestedShape\n}\n\n// Container schemas using interfaces for recursive references\nexport interface ListContainerShape<\n NestedShape extends ContainerOrValueShape = ContainerOrValueShape,\n> extends Shape<NestedShape[\"_plain\"][], ListDraftNode<NestedShape>> {\n readonly _type: \"list\"\n // A list contains many elements, all of the same 'shape'\n readonly shape: NestedShape\n}\n\nexport interface MovableListContainerShape<\n NestedShape extends ContainerOrValueShape = ContainerOrValueShape,\n> extends Shape<NestedShape[\"_plain\"][], MovableListDraftNode<NestedShape>> {\n readonly _type: \"movableList\"\n // A list contains many elements, all of the same 'shape'\n readonly shape: NestedShape\n}\n\nexport interface MapContainerShape<\n NestedShapes extends Record<string, ContainerOrValueShape> = Record<\n string,\n ContainerOrValueShape\n >,\n> extends Shape<\n { [K in keyof NestedShapes]: NestedShapes[K][\"_plain\"] },\n MapDraftNode<NestedShapes> & {\n [K in keyof NestedShapes]: NestedShapes[K][\"_draft\"]\n }\n > {\n readonly _type: \"map\"\n // Each map property has its own shape, hence 'shapes'\n readonly shapes: NestedShapes\n}\n\nexport interface RecordContainerShape<\n NestedShape extends ContainerOrValueShape = ContainerOrValueShape,\n> extends Shape<\n Record<string, NestedShape[\"_plain\"]>,\n RecordDraftNode<NestedShape>\n > {\n readonly _type: \"record\"\n readonly shape: NestedShape\n}\n\nexport type ContainerShape =\n | CounterContainerShape\n | ListContainerShape\n | MapContainerShape\n | MovableListContainerShape\n | RecordContainerShape\n | TextContainerShape\n | TreeContainerShape\n\nexport type ContainerType = ContainerShape[\"_type\"]\n\n// LoroValue shape types - a shape for each of Loro's Value types\nexport interface StringValueShape extends Shape<string, string> {\n readonly _type: \"value\"\n readonly valueType: \"string\"\n}\nexport interface NumberValueShape extends Shape<number, number> {\n readonly _type: \"value\"\n readonly valueType: \"number\"\n}\nexport interface BooleanValueShape extends Shape<boolean, boolean> {\n readonly _type: \"value\"\n readonly valueType: \"boolean\"\n}\nexport interface NullValueShape extends Shape<null, null> {\n readonly _type: \"value\"\n readonly valueType: \"null\"\n}\nexport interface UndefinedValueShape extends Shape<undefined, undefined> {\n readonly _type: \"value\"\n readonly valueType: \"undefined\"\n}\nexport interface Uint8ArrayValueShape extends Shape<Uint8Array, Uint8Array> {\n readonly _type: \"value\"\n readonly valueType: \"uint8array\"\n}\n\nexport interface ObjectValueShape<\n T extends Record<string, ValueShape> = Record<string, ValueShape>,\n> extends Shape<\n { [K in keyof T]: T[K][\"_plain\"] },\n { [K in keyof T]: T[K][\"_draft\"] }\n > {\n readonly _type: \"value\"\n readonly valueType: \"object\"\n readonly shape: T\n}\n\nexport interface RecordValueShape<T extends ValueShape = ValueShape>\n extends Shape<Record<string, T[\"_plain\"]>, Record<string, T[\"_draft\"]>> {\n readonly _type: \"value\"\n readonly valueType: \"record\"\n readonly shape: T\n}\n\nexport interface ArrayValueShape<T extends ValueShape = ValueShape>\n extends Shape<T[\"_plain\"][], T[\"_draft\"][]> {\n readonly _type: \"value\"\n readonly valueType: \"array\"\n readonly shape: T\n}\n\nexport interface UnionValueShape<T extends ValueShape[] = ValueShape[]>\n extends Shape<T[number][\"_plain\"], T[number][\"_draft\"]> {\n readonly _type: \"value\"\n readonly valueType: \"union\"\n readonly shapes: T\n}\n\n// Union of all ValueShapes - these can only contain other ValueShapes, not ContainerShapes\nexport type ValueShape =\n | StringValueShape\n | NumberValueShape\n | BooleanValueShape\n | NullValueShape\n | UndefinedValueShape\n | Uint8ArrayValueShape\n | ObjectValueShape\n | RecordValueShape\n | ArrayValueShape\n | UnionValueShape\n\nexport type ContainerOrValueShape = ContainerShape | ValueShape\n\n/**\n * The LoroShape factory object\n *\n * If a container has a `shape` type variable, it refers to the shape it contains--\n * so for example, a `LoroShape.list(LoroShape.text())` would return a value of type\n * `ListContainerShape<TextContainerShape>`.\n */\nexport const Shape = {\n doc: <T extends Record<string, ContainerShape>>(shape: T): DocShape<T> => ({\n _type: \"doc\" as const,\n shapes: shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n // CRDTs are represented by Loro Containers--they converge on state using Loro's\n // various CRDT algorithms\n counter: (): CounterContainerShape => ({\n _type: \"counter\" as const,\n _plain: 0,\n _draft: {} as CounterDraftNode,\n }),\n\n list: <T extends ContainerOrValueShape>(shape: T): ListContainerShape<T> => ({\n _type: \"list\" as const,\n shape,\n _plain: [] as any,\n _draft: {} as any,\n }),\n\n map: <T extends Record<string, ContainerOrValueShape>>(\n shape: T,\n ): MapContainerShape<T> => ({\n _type: \"map\" as const,\n shapes: shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n record: <T extends ContainerOrValueShape>(\n shape: T,\n ): RecordContainerShape<T> => ({\n _type: \"record\" as const,\n shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n movableList: <T extends ContainerOrValueShape>(\n shape: T,\n ): MovableListContainerShape<T> => ({\n _type: \"movableList\" as const,\n shape,\n _plain: [] as any,\n _draft: {} as any,\n }),\n\n text: (): TextContainerShape => ({\n _type: \"text\" as const,\n _plain: \"\",\n _draft: {} as TextDraftNode,\n }),\n\n tree: <T extends MapContainerShape>(shape: T): TreeContainerShape => ({\n _type: \"tree\" as const,\n shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n // Values are represented as plain JS objects, with the limitation that they MUST be\n // representable as a Loro \"Value\"--basically JSON. The behavior of a Value is basically\n // \"Last Write Wins\", meaning there is no subtle convergent behavior here, just taking\n // the most recent value based on the current available information.\n plain: {\n string: (): StringValueShape => ({\n _type: \"value\" as const,\n valueType: \"string\" as const,\n _plain: \"\",\n _draft: \"\",\n }),\n\n number: (): NumberValueShape => ({\n _type: \"value\" as const,\n valueType: \"number\" as const,\n _plain: 0,\n _draft: 0,\n }),\n\n boolean: (): BooleanValueShape => ({\n _type: \"value\" as const,\n valueType: \"boolean\" as const,\n _plain: false,\n _draft: false,\n }),\n\n null: (): NullValueShape => ({\n _type: \"value\" as const,\n valueType: \"null\" as const,\n _plain: null,\n _draft: null,\n }),\n\n undefined: (): UndefinedValueShape => ({\n _type: \"value\" as const,\n valueType: \"undefined\" as const,\n _plain: undefined,\n _draft: undefined,\n }),\n\n uint8Array: (): Uint8ArrayValueShape => ({\n _type: \"value\" as const,\n valueType: \"uint8array\" as const,\n _plain: new Uint8Array(),\n _draft: new Uint8Array(),\n }),\n\n object: <T extends Record<string, ValueShape>>(\n shape: T,\n ): ObjectValueShape<T> => ({\n _type: \"value\" as const,\n valueType: \"object\" as const,\n shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n record: <T extends ValueShape>(shape: T): RecordValueShape<T> => ({\n _type: \"value\" as const,\n valueType: \"record\" as const,\n shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n array: <T extends ValueShape>(shape: T): ArrayValueShape<T> => ({\n _type: \"value\" as const,\n valueType: \"array\" as const,\n shape,\n _plain: [] as any,\n _draft: [] as any,\n }),\n\n // Special value type that helps make things like `string | null` representable\n // TODO(duane): should this be a more general type for containers too?\n union: <T extends ValueShape[]>(shapes: T): UnionValueShape<T> => ({\n _type: \"value\" as const,\n valueType: \"union\" as const,\n shapes,\n _plain: {} as any,\n _draft: {} as any,\n }),\n },\n}\n\n// Add this type mapping near the top of your file, after the imports\nexport type ShapeToContainer<T extends DocShape | ContainerShape> =\n T extends TextContainerShape\n ? LoroText\n : T extends CounterContainerShape\n ? LoroCounter\n : T extends ListContainerShape\n ? LoroList\n : T extends MovableListContainerShape\n ? LoroMovableList\n : T extends MapContainerShape | RecordContainerShape\n ? LoroMap\n : T extends TreeContainerShape\n ? LoroTree\n : never // not a container\n"],"mappings":";AAEA,SAAS,eAAe;;;ACQjB,IAAe,YAAf,MAAkE;AAAA,EAGvE,YAAsB,SAAiC;AAAjC;AAAA,EAAkC;AAAA,EAF9C;AAAA,EAMV,IAAc,QAAe;AAC3B,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAc,aAAgD;AAC5D,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAc,YAAqC;AACjD,QAAI,CAAC,KAAK,kBAAkB;AAC1B,YAAM,YAAY,KAAK,QAAQ,aAAa;AAC5C,WAAK,mBAAmB;AACxB,aAAO;AAAA,IACT;AACA,WAAO,KAAK;AAAA,EACd;AACF;;;AC7BO,IAAM,mBAAN,cAA+B,UAAiC;AAAA,EACrE,oBAAoB;AAAA,EAEpB;AAAA,EAEA,UAAU,OAAqB;AAC7B,SAAK,UAAU,UAAU,KAAK;AAAA,EAChC;AAAA,EAEA,UAAU,OAAqB;AAC7B,SAAK,UAAU,UAAU,KAAK;AAAA,EAChC;AAAA,EAEA,IAAI,QAAgB;AAClB,WAAO,KAAK,UAAU;AAAA,EACxB;AACF;;;ACpBA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;;;ACgBP,SAAS,aAAa,qBAAqB;AAsJpC,SAAS,iBACd,QAC0B;AAC1B,SAAO,OAAO,SAAS,OAAO,UAAU;AAC1C;AAKO,SAAS,aACd,QACsB;AACtB,SACE,OAAO,UAAU,WACjB;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IAC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P,WAAW;AAAA,MACX,QAAQ;AAAA,MACR,QAAQ;AAAA,IACV;AAAA,IAEA,MAAM,OAAuB;AAAA,MAC3B,OAAO;AAAA,MACP,WAAW;AAAA,MACX,QAAQ;AAAA,MACR,QAAQ;AAAA,IACV;AAAA,IAEA,WAAW,OAA4B;AAAA,MACrC,OAAO;AAAA,MACP,WAAW;AAAA,MACX,QAAQ;AAAA,MACR,QAAQ;AAAA,IACV;AAAA,IAEA,YAAY,OAA6B;AAAA,MACvC,OAAO;AAAA,MACP,WAAW;AAAA,MACX,QAAQ,IAAI,WAAW;AAAA,MACvB,QAAQ,IAAI,WAAW;AAAA,IACzB;AAAA,IAEA,QAAQ,CACN,WACyB;AAAA,MACzB,OAAO;AAAA,MACP,WAAW;AAAA,MACX;AAAA,MACA,QAAQ,CAAC;AAAA,MACT,QAAQ,CAAC;AAAA,IACX;AAAA,IAEA,QAAQ,CAAuB,WAAmC;AAAA,MAChE,OAAO;AAAA,MACP,WAAW;AAAA,MACX;AAAA,MACA,QAAQ,CAAC;AAAA,MACT,QAAQ,CAAC;AAAA,IACX;AAAA,IAEA,OAAO,CAAuB,WAAkC;AAAA,MAC9D,OAAO;AAAA,MACP,WAAW;AAAA,MACX;AAAA,MACA,QAAQ,CAAC;AAAA,MACT,QAAQ,CAAC;AAAA,IACX;AAAA;AAAA;AAAA,IAIA,OAAO,CAAyB,YAAmC;AAAA,MACjE,OAAO;AAAA,MACP,WAAW;AAAA,MACX;AAAA,MACA,QAAQ,CAAC;AAAA,MACT,QAAQ,CAAC;AAAA,IACX;AAAA,EACF;AACF;","names":["LoroCounter","LoroList","LoroMap","LoroMovableList","LoroText","LoroCounter","LoroList","LoroMap","LoroMovableList","LoroText","LoroCounter","LoroList","LoroMap","LoroMovableList","LoroText","LoroTree","containerConstructor","LoroCounter","LoroList","LoroMap","LoroMovableList","LoroText","LoroTree","value"]}
1
+ {"version":3,"sources":["../src/change.ts","../src/draft-nodes/base.ts","../src/draft-nodes/counter.ts","../src/conversion.ts","../src/utils/type-guards.ts","../src/draft-nodes/list-base.ts","../src/draft-nodes/list.ts","../src/draft-nodes/map.ts","../src/draft-nodes/movable-list.ts","../src/draft-nodes/record.ts","../src/draft-nodes/text.ts","../src/draft-nodes/tree.ts","../src/draft-nodes/utils.ts","../src/draft-nodes/doc.ts","../src/json-patch.ts","../src/overlay.ts","../src/validation.ts","../src/shape.ts"],"sourcesContent":["/** biome-ignore-all lint/suspicious/noExplicitAny: fix later */\n\nimport { LoroDoc } from \"loro-crdt\"\nimport { DraftDoc } from \"./draft-nodes/doc.js\"\nimport {\n type JsonPatch,\n JsonPatchApplicator,\n type JsonPatchOperation,\n normalizePath,\n} from \"./json-patch.js\"\nimport { overlayEmptyState } from \"./overlay.js\"\nimport type { DocShape } from \"./shape.js\"\nimport type { Draft, InferPlainType } from \"./types.js\"\nimport { validateEmptyState } from \"./validation.js\"\n\n// Core TypedDoc abstraction around LoroDoc\nexport class TypedDoc<Shape extends DocShape> {\n constructor(\n private shape: Shape,\n private emptyState: InferPlainType<Shape>,\n private doc: LoroDoc = new LoroDoc(),\n ) {\n validateEmptyState(emptyState, shape)\n }\n\n get value(): InferPlainType<Shape> {\n const crdtValue = this.doc.toJSON()\n return overlayEmptyState(\n this.shape,\n crdtValue,\n this.emptyState,\n ) as InferPlainType<Shape>\n }\n\n change(fn: (draft: Draft<Shape>) => void): InferPlainType<Shape> {\n // Reuse existing DocumentDraft system with empty state integration\n const draft = new DraftDoc({\n shape: this.shape,\n emptyState: this.emptyState,\n doc: this.doc,\n })\n fn(draft as unknown as Draft<Shape>)\n draft.absorbPlainValues()\n this.doc.commit()\n return this.value\n }\n\n /**\n * Apply JSON Patch operations to the document\n *\n * @param patch - Array of JSON Patch operations (RFC 6902)\n * @param pathPrefix - Optional path prefix for scoped operations\n * @returns Updated document value\n *\n * @example\n * ```typescript\n * const result = typedDoc.applyPatch([\n * { op: 'add', path: '/users/0/name', value: 'Alice' },\n * { op: 'replace', path: '/settings/theme', value: 'dark' }\n * ])\n * ```\n */\n applyPatch(\n patch: JsonPatch,\n pathPrefix?: (string | number)[],\n ): InferPlainType<Shape> {\n return this.change(draft => {\n const applicator = new JsonPatchApplicator(draft)\n\n // Apply path prefix if provided\n const prefixedPatch = pathPrefix\n ? patch.map((op: JsonPatchOperation) => ({\n ...op,\n path: [...pathPrefix, ...normalizePath(op.path)],\n }))\n : patch\n\n applicator.applyPatch(prefixedPatch)\n })\n }\n\n // Expose underlying doc for advanced use cases\n get loroDoc(): LoroDoc {\n return this.doc\n }\n\n // Expose shape for internal use\n get docShape(): Shape {\n return this.shape\n }\n\n // Get raw CRDT value without overlay\n get rawValue(): any {\n return this.doc.toJSON()\n }\n}\n\n// Factory function for TypedLoroDoc\nexport function createTypedDoc<Shape extends DocShape>(\n shape: Shape,\n emptyState: InferPlainType<Shape>,\n existingDoc?: LoroDoc,\n): TypedDoc<Shape> {\n return new TypedDoc<Shape>(shape, emptyState, existingDoc || new LoroDoc())\n}\n","import type { ContainerShape, DocShape, ShapeToContainer } from \"../shape.js\"\nimport type { InferPlainType } from \"../types.js\"\n\nexport type DraftNodeParams<Shape extends DocShape | ContainerShape> = {\n shape: Shape\n emptyState?: InferPlainType<Shape>\n getContainer: () => ShapeToContainer<Shape>\n}\n\n// Base class for all draft nodes\nexport abstract class DraftNode<Shape extends DocShape | ContainerShape> {\n protected _cachedContainer?: ShapeToContainer<Shape>\n\n constructor(protected _params: DraftNodeParams<Shape>) {}\n\n abstract absorbPlainValues(): void\n\n protected get shape(): Shape {\n return this._params.shape\n }\n\n protected get emptyState(): InferPlainType<Shape> | undefined {\n return this._params.emptyState\n }\n\n protected get container(): ShapeToContainer<Shape> {\n if (!this._cachedContainer) {\n const container = this._params.getContainer()\n this._cachedContainer = container\n return container\n }\n return this._cachedContainer\n }\n}\n","import type { CounterContainerShape } from \"../shape.js\"\nimport { DraftNode } from \"./base.js\"\n\n// Counter draft node\nexport class CounterDraftNode extends DraftNode<CounterContainerShape> {\n absorbPlainValues() {\n // no plain values contained within\n }\n\n increment(value: number): void {\n this.container.increment(value)\n }\n\n decrement(value: number): void {\n this.container.decrement(value)\n }\n\n get value(): number {\n return this.container.value\n }\n}\n","import {\n type Container,\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n type Value,\n} from \"loro-crdt\"\nimport type {\n ArrayValueShape,\n ContainerOrValueShape,\n ListContainerShape,\n MapContainerShape,\n MovableListContainerShape,\n ObjectValueShape,\n RecordContainerShape,\n RecordValueShape,\n} from \"./shape.js\"\nimport {\n isContainer,\n isContainerShape,\n isObjectValue,\n isValueShape,\n} from \"./utils/type-guards.js\"\n\n/**\n * Converts string input to LoroText container\n */\nfunction convertTextInput(value: string): LoroText {\n const text = new LoroText()\n\n text.insert(0, value)\n\n return text\n}\n\n/**\n * Converts number input to LoroCounter container\n */\nfunction convertCounterInput(value: number): LoroCounter {\n const counter = new LoroCounter()\n counter.increment(value)\n return counter\n}\n\n/**\n * Converts array input to LoroList container\n */\nfunction convertListInput(\n value: Value[],\n shape: ListContainerShape | ArrayValueShape,\n // parentPath: string[],\n): LoroList | Value[] {\n if (!isContainerShape(shape)) {\n return value\n }\n\n const list = new LoroList()\n\n for (const item of value) {\n const convertedItem = convertInputToNode(item, shape.shape)\n if (isContainer(convertedItem)) {\n list.pushContainer(convertedItem)\n } else {\n list.push(convertedItem)\n }\n }\n\n return list\n}\n\n/**\n * Converts array input to LoroMovableList container\n */\nfunction convertMovableListInput(\n value: Value[],\n shape: MovableListContainerShape | ArrayValueShape,\n // parentPath: string[],\n): LoroMovableList | Value[] {\n if (!isContainerShape(shape)) {\n return value\n }\n\n const list = new LoroMovableList()\n\n for (const item of value) {\n const convertedItem = convertInputToNode(item, shape.shape)\n if (isContainer(convertedItem)) {\n list.pushContainer(convertedItem)\n } else {\n list.push(convertedItem)\n }\n }\n\n return list\n}\n\n/**\n * Converts object input to LoroMap container\n */\nfunction convertMapInput(\n value: { [key: string]: Value },\n shape: MapContainerShape | ObjectValueShape,\n): LoroMap | { [key: string]: Value } {\n if (!isContainerShape(shape)) {\n return value\n }\n\n const map = new LoroMap()\n for (const [k, v] of Object.entries(value)) {\n const nestedSchema = shape.shapes[k]\n if (nestedSchema) {\n const convertedValue = convertInputToNode(v, nestedSchema)\n if (isContainer(convertedValue)) {\n map.setContainer(k, convertedValue)\n } else {\n map.set(k, convertedValue)\n }\n } else {\n map.set(k, value)\n }\n }\n\n return map\n}\n\n/**\n * Converts object input to LoroMap container (Record)\n */\nfunction convertRecordInput(\n value: { [key: string]: Value },\n shape: RecordContainerShape | RecordValueShape,\n): LoroMap | { [key: string]: Value } {\n if (!isContainerShape(shape)) {\n return value\n }\n\n const map = new LoroMap()\n for (const [k, v] of Object.entries(value)) {\n const convertedValue = convertInputToNode(v, shape.shape)\n if (isContainer(convertedValue)) {\n map.setContainer(k, convertedValue)\n } else {\n map.set(k, convertedValue)\n }\n }\n\n return map\n}\n\n/**\n * Main conversion function that transforms input values to appropriate CRDT containers\n * based on schema definitions\n */\nexport function convertInputToNode<Shape extends ContainerOrValueShape>(\n value: Value,\n shape: Shape,\n): Container | Value {\n switch (shape._type) {\n case \"text\": {\n if (typeof value !== \"string\") {\n throw new Error(\"string expected\")\n }\n\n return convertTextInput(value)\n }\n case \"counter\": {\n if (typeof value !== \"number\") {\n throw new Error(\"number expected\")\n }\n\n return convertCounterInput(value)\n }\n case \"list\": {\n if (!Array.isArray(value)) {\n throw new Error(\"array expected\")\n }\n\n return convertListInput(value, shape)\n }\n case \"movableList\": {\n if (!Array.isArray(value)) {\n throw new Error(\"array expected\")\n }\n\n return convertMovableListInput(value, shape)\n }\n case \"map\": {\n if (!isObjectValue(value)) {\n throw new Error(\"object expected\")\n }\n\n return convertMapInput(value, shape)\n }\n case \"record\": {\n if (!isObjectValue(value)) {\n throw new Error(\"object expected\")\n }\n\n return convertRecordInput(value, shape)\n }\n case \"value\": {\n if (!isValueShape(shape)) {\n throw new Error(\"value expected\")\n }\n\n return value\n }\n\n case \"tree\":\n throw new Error(\"tree type unimplemented\")\n\n default:\n throw new Error(`unexpected type: ${(shape as Shape)._type}`)\n }\n}\n","import type {\n Container,\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n LoroTree,\n LoroTreeNode,\n Value,\n} from \"loro-crdt\"\nimport type {\n ContainerOrValueShape,\n ContainerShape,\n CounterContainerShape,\n ListContainerShape,\n MapContainerShape,\n MovableListContainerShape,\n RecordContainerShape,\n TextContainerShape,\n TreeContainerShape,\n ValueShape,\n} from \"../shape.js\"\n\nexport { isContainer, isContainerId } from \"loro-crdt\"\n\n/**\n * Type guard to check if a container is a LoroCounter\n */\nexport function isLoroCounter(container: Container): container is LoroCounter {\n return container.kind() === \"Counter\"\n}\n\n/**\n * Type guard to check if a container is a LoroList\n */\nexport function isLoroList(container: Container): container is LoroList {\n return container.kind() === \"List\"\n}\n\n/**\n * Type guard to check if a container is a LoroMap\n */\nexport function isLoroMap(container: Container): container is LoroMap {\n return container.kind() === \"Map\"\n}\n\n/**\n * Type guard to check if a container is a LoroMovableList\n */\nexport function isLoroMovableList(\n container: Container,\n): container is LoroMovableList {\n return container.kind() === \"MovableList\"\n}\n\n/**\n * Type guard to check if a container is a LoroText\n */\nexport function isLoroText(container: Container): container is LoroText {\n return container.kind() === \"Text\"\n}\n\n/**\n * Type guard to check if a container is a LoroTree\n */\nexport function isLoroTree(container: Container): container is LoroTree {\n return container.kind() === \"Tree\"\n}\n\n/**\n * Type guard to check if an object is a LoroTreeNode\n * Note: LoroTreeNode is not a Container, so we check for its specific properties\n */\nexport function isLoroTreeNode(obj: any): obj is LoroTreeNode {\n return (\n obj &&\n typeof obj === \"object\" &&\n typeof obj.id === \"string\" &&\n typeof obj.data === \"object\" &&\n typeof obj.parent === \"function\" &&\n typeof obj.children === \"function\" &&\n typeof obj.createNode === \"function\"\n )\n}\n\n/**\n * Type guard to ensure cached container matches expected type using kind() method\n */\nexport function assertContainerType<T extends Container>(\n cached: Container,\n expected: T,\n context: string = \"container operation\",\n): asserts cached is T {\n if (cached.kind() !== expected.kind()) {\n throw new Error(\n `Type safety violation in ${context}: ` +\n `cached container kind '${cached.kind()}' does not match ` +\n `expected kind '${expected.kind()}'`,\n )\n }\n\n // Additional safety check: ensure IDs match\n if (cached.id !== expected.id) {\n throw new Error(\n `Container ID mismatch in ${context}: ` +\n `cached ID '${cached.id}' does not match expected ID '${expected.id}'`,\n )\n }\n}\n\n/**\n * Type guard to check if a schema is for TextDraftNode\n */\nexport function isTextShape(\n schema: ContainerOrValueShape,\n): schema is TextContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"text\"\n}\n\n/**\n * Type guard to check if a schema is for CounterDraftNode\n */\nexport function isCounterShape(\n schema: ContainerOrValueShape,\n): schema is CounterContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"counter\"\n}\n\n/**\n * Type guard to check if a schema is for ListDraftNode\n */\nexport function isListShape(\n schema: ContainerOrValueShape,\n): schema is ListContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"list\"\n}\n\n/**\n * Type guard to check if a schema is for MovableListDraftNode\n */\nexport function isMovableListShape(\n schema: ContainerOrValueShape,\n): schema is MovableListContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"movableList\"\n}\n\n/**\n * Type guard to check if a schema is for MapDraftNode\n */\nexport function isMapShape(\n schema: ContainerOrValueShape,\n): schema is MapContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"map\"\n}\n\n/**\n * Type guard to check if a schema is for RecordDraftNode\n */\nexport function isRecordShape(\n schema: ContainerOrValueShape,\n): schema is RecordContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"record\"\n}\n\n/**\n * Type guard to check if a schema is for TreeDraftNode\n */\nexport function isTreeShape(\n schema: ContainerOrValueShape,\n): schema is TreeContainerShape {\n return schema && typeof schema === \"object\" && schema._type === \"tree\"\n}\n\nexport function isContainerShape(\n schema: ContainerOrValueShape,\n): schema is ContainerShape {\n return schema._type && schema._type !== \"value\"\n}\n\n/**\n * Type guard to check if a schema is any of the Value shapes\n */\nexport function isValueShape(\n schema: ContainerOrValueShape,\n): schema is ValueShape {\n return (\n schema._type === \"value\" &&\n [\n \"string\",\n \"number\",\n \"boolean\",\n \"null\",\n \"undefined\",\n \"uint8array\",\n \"object\",\n \"record\",\n \"array\",\n \"union\",\n ].includes(schema.valueType)\n )\n}\n\nexport function isObjectValue(value: Value): value is { [key: string]: Value } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n !(value instanceof Uint8Array)\n )\n}\n","import type { Container, LoroList, LoroMovableList } from \"loro-crdt\"\nimport { convertInputToNode } from \"../conversion.js\"\nimport type {\n ContainerOrValueShape,\n ContainerShape,\n ListContainerShape,\n MovableListContainerShape,\n} from \"../shape.js\"\nimport { isContainer, isValueShape } from \"../utils/type-guards.js\"\nimport { DraftNode, type DraftNodeParams } from \"./base.js\"\nimport { createContainerDraftNode } from \"./utils.js\"\n\n// Shared logic for list operations\nexport abstract class ListDraftNodeBase<\n NestedShape extends ContainerOrValueShape,\n Item = NestedShape[\"_plain\"],\n DraftItem = NestedShape[\"_draft\"],\n> extends DraftNode<any> {\n // Cache for items returned by array methods to track mutations\n private itemCache = new Map<number, any>()\n\n protected get container(): LoroList | LoroMovableList {\n return super.container as LoroList | LoroMovableList\n }\n\n protected get shape():\n | ListContainerShape<NestedShape>\n | MovableListContainerShape<NestedShape> {\n return super.shape as\n | ListContainerShape<NestedShape>\n | MovableListContainerShape<NestedShape>\n }\n\n absorbPlainValues() {\n // Critical function: absorb mutated plain values back into Loro containers\n // This is called at the end of change() to persist mutations made to plain objects\n for (const [index, cachedItem] of this.itemCache.entries()) {\n if (cachedItem) {\n if (isValueShape(this.shape.shape)) {\n // For value shapes, delegate to subclass-specific absorption logic\n this.absorbValueAtIndex(index, cachedItem)\n } else {\n // For container shapes, the item should be a draft node that handles its own absorption\n if (\n cachedItem &&\n typeof cachedItem === \"object\" &&\n \"absorbPlainValues\" in cachedItem\n ) {\n ;(cachedItem as any).absorbPlainValues()\n }\n }\n }\n }\n\n // Clear the cache after absorbing values\n this.itemCache.clear()\n }\n\n // Abstract method to be implemented by subclasses\n // Each subclass knows how to handle its specific container type\n protected abstract absorbValueAtIndex(index: number, value: any): void\n\n protected insertWithConversion(index: number, item: Item): void {\n const convertedItem = convertInputToNode(item as any, this.shape.shape)\n if (isContainer(convertedItem)) {\n this.container.insertContainer(index, convertedItem)\n } else {\n this.container.insert(index, convertedItem)\n }\n }\n\n protected pushWithConversion(item: Item): void {\n const convertedItem = convertInputToNode(item as any, this.shape.shape)\n if (isContainer(convertedItem)) {\n this.container.pushContainer(convertedItem)\n } else {\n this.container.push(convertedItem)\n }\n }\n\n getDraftNodeParams(\n index: number,\n shape: ContainerShape,\n ): DraftNodeParams<ContainerShape> {\n return {\n shape,\n emptyState: undefined, // List items don't have empty state\n getContainer: () => {\n const containerItem = this.container.get(index)\n if (!containerItem || !isContainer(containerItem)) {\n throw new Error(`No container found at index ${index}`)\n }\n return containerItem\n },\n }\n }\n\n // Get item for predicate functions - always returns plain Item for filtering logic\n protected getPredicateItem(index: number): Item {\n // CRITICAL FIX: For predicates to work correctly with mutations,\n // we need to check if there's a cached (mutated) version first\n const cachedItem = this.itemCache.get(index)\n if (cachedItem && isValueShape(this.shape.shape)) {\n // For value shapes, if we have a cached item, use it so predicates see mutations\n return cachedItem as Item\n }\n\n const containerItem = this.container.get(index)\n if (containerItem === undefined) {\n return undefined as Item\n }\n\n if (isValueShape(this.shape.shape)) {\n // For value shapes, return the plain value directly\n return containerItem as Item\n } else {\n // For container shapes, we need to return the plain object representation\n // This allows predicates to access nested properties like article.metadata.author\n if (isContainer(containerItem)) {\n // Convert container to plain object for predicate logic\n // Handle different container types that may not have toJSON method\n if (\n typeof containerItem === \"object\" &&\n containerItem !== null &&\n \"toJSON\" in containerItem\n ) {\n return (containerItem as any).toJSON() as Item\n } else if (\n typeof containerItem === \"object\" &&\n containerItem !== null &&\n \"getShallowValue\" in containerItem\n ) {\n // For containers like LoroCounter that don't have toJSON but have getShallowValue\n return (containerItem as any).getShallowValue() as Item\n } else {\n // Fallback for other container types\n return containerItem as Item\n }\n }\n return containerItem as Item\n }\n }\n\n // Get item for return values - returns DraftItem that can be mutated\n protected getDraftItem(index: number): DraftItem {\n // Check if we already have a cached item for this index\n let cachedItem = this.itemCache.get(index)\n if (cachedItem) {\n return cachedItem\n }\n\n // Get the raw container item\n const containerItem = this.container.get(index)\n if (containerItem === undefined) {\n return undefined as DraftItem\n }\n\n if (isValueShape(this.shape.shape)) {\n // For value shapes, we need to ensure mutations persist\n // The key insight: we must return the SAME object for the same index\n // so that mutations to filtered/found items persist back to the cache\n if (typeof containerItem === \"object\" && containerItem !== null) {\n // Create a deep copy for objects so mutations can be tracked\n // IMPORTANT: Only create the copy once, then always return the same cached object\n cachedItem = JSON.parse(JSON.stringify(containerItem))\n } else {\n // For primitives, just use the value directly\n cachedItem = containerItem\n }\n this.itemCache.set(index, cachedItem)\n return cachedItem as DraftItem\n } else {\n // For container shapes, create a proper draft node using the new pattern\n cachedItem = createContainerDraftNode(\n this.getDraftNodeParams(index, this.shape.shape as ContainerShape),\n )\n this.itemCache.set(index, cachedItem)\n return cachedItem as DraftItem\n }\n }\n\n // Array-like methods for better developer experience\n // DUAL INTERFACE: Predicates get Item (plain data), return values are DraftItem (mutable)\n\n find(\n predicate: (item: Item, index: number) => boolean,\n ): DraftItem | undefined {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (predicate(predicateItem, i)) {\n return this.getDraftItem(i) // Return mutable draft item\n }\n }\n return undefined\n }\n\n findIndex(predicate: (item: Item, index: number) => boolean): number {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (predicate(predicateItem, i)) {\n return i\n }\n }\n return -1\n }\n\n map<ReturnType>(\n callback: (item: Item, index: number) => ReturnType,\n ): ReturnType[] {\n const result: ReturnType[] = []\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n result.push(callback(predicateItem, i))\n }\n return result\n }\n\n filter(predicate: (item: Item, index: number) => boolean): DraftItem[] {\n const result: DraftItem[] = []\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (predicate(predicateItem, i)) {\n result.push(this.getDraftItem(i)) // Return mutable draft items\n }\n }\n return result\n }\n\n forEach(callback: (item: Item, index: number) => void): void {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n callback(predicateItem, i)\n }\n }\n\n some(predicate: (item: Item, index: number) => boolean): boolean {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (predicate(predicateItem, i)) {\n return true\n }\n }\n return false\n }\n\n every(predicate: (item: Item, index: number) => boolean): boolean {\n for (let i = 0; i < this.length; i++) {\n const predicateItem = this.getPredicateItem(i)\n if (!predicate(predicateItem, i)) {\n return false\n }\n }\n return true\n }\n\n insert(index: number, item: Item): void {\n // Update cache indices before performing the insert operation\n this.updateCacheForInsert(index)\n this.insertWithConversion(index, item)\n }\n\n delete(index: number, len: number): void {\n // Update cache indices before performing the delete operation\n this.updateCacheForDelete(index, len)\n this.container.delete(index, len)\n }\n\n push(item: Item): void {\n this.pushWithConversion(item)\n }\n\n pushContainer(container: Container): Container {\n return this.container.pushContainer(container)\n }\n\n insertContainer(index: number, container: Container): Container {\n return this.container.insertContainer(index, container)\n }\n\n get(index: number): DraftItem {\n return this.getDraftItem(index)\n }\n\n toArray(): Item[] {\n return this.container.toArray() as Item[]\n }\n\n get length(): number {\n return this.container.length\n }\n\n // Update cache indices when items are deleted\n private updateCacheForDelete(deleteIndex: number, deleteLen: number): void {\n const newCache = new Map<number, any>()\n\n for (const [cachedIndex, cachedItem] of this.itemCache.entries()) {\n if (cachedIndex < deleteIndex) {\n // Items before the deletion point keep their indices\n newCache.set(cachedIndex, cachedItem)\n } else if (cachedIndex >= deleteIndex + deleteLen) {\n // Items after the deletion range shift down by deleteLen\n newCache.set(cachedIndex - deleteLen, cachedItem)\n }\n // Items within the deletion range are removed from cache\n }\n\n this.itemCache = newCache\n }\n\n // Update cache indices when items are inserted\n private updateCacheForInsert(insertIndex: number): void {\n const newCache = new Map<number, any>()\n\n for (const [cachedIndex, cachedItem] of this.itemCache.entries()) {\n if (cachedIndex < insertIndex) {\n // Items before the insertion point keep their indices\n newCache.set(cachedIndex, cachedItem)\n } else {\n // Items at or after the insertion point shift up by 1\n newCache.set(cachedIndex + 1, cachedItem)\n }\n }\n\n this.itemCache = newCache\n }\n}\n","import type { LoroList } from \"loro-crdt\"\nimport type { ContainerOrValueShape } from \"../shape.js\"\nimport { ListDraftNodeBase } from \"./list-base.js\"\n\n// List draft node\nexport class ListDraftNode<\n NestedShape extends ContainerOrValueShape,\n> extends ListDraftNodeBase<NestedShape> {\n protected get container(): LoroList {\n return super.container as LoroList\n }\n\n protected absorbValueAtIndex(index: number, value: any): void {\n // LoroList doesn't have set method, need to delete and insert\n this.container.delete(index, 1)\n this.container.insert(index, value)\n }\n}\n","import {\n type Container,\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n LoroTree,\n type Value,\n} from \"loro-crdt\"\nimport type {\n ContainerOrValueShape,\n ContainerShape,\n MapContainerShape,\n ValueShape,\n} from \"../shape.js\"\nimport { isContainerShape, isValueShape } from \"../utils/type-guards.js\"\nimport { DraftNode, type DraftNodeParams } from \"./base.js\"\nimport { createContainerDraftNode } from \"./utils.js\"\n\nconst containerConstructor = {\n counter: LoroCounter,\n list: LoroList,\n map: LoroMap,\n movableList: LoroMovableList,\n record: LoroMap,\n text: LoroText,\n tree: LoroTree,\n} as const\n\n// Map draft node\nexport class MapDraftNode<\n NestedShapes extends Record<string, ContainerOrValueShape>,\n> extends DraftNode<any> {\n private propertyCache = new Map<string, DraftNode<ContainerShape> | Value>()\n\n constructor(params: DraftNodeParams<MapContainerShape<NestedShapes>>) {\n super(params)\n this.createLazyProperties()\n }\n\n protected get shape(): MapContainerShape<NestedShapes> {\n return super.shape as MapContainerShape<NestedShapes>\n }\n\n protected get container(): LoroMap {\n return super.container as LoroMap\n }\n\n absorbPlainValues() {\n for (const [key, node] of this.propertyCache.entries()) {\n if (node instanceof DraftNode) {\n // Contains a DraftNode, not a plain Value: keep recursing\n node.absorbPlainValues()\n continue\n }\n\n // Plain value!\n this.container.set(key, node)\n }\n }\n\n getDraftNodeParams<S extends ContainerShape>(\n key: string,\n shape: S,\n ): DraftNodeParams<ContainerShape> {\n const emptyState = (this.emptyState as any)?.[key]\n\n const LoroContainer = containerConstructor[shape._type]\n\n return {\n shape,\n emptyState,\n getContainer: () =>\n this.container.getOrCreateContainer(key, new (LoroContainer as any)()),\n }\n }\n\n getOrCreateNode<Shape extends ContainerShape | ValueShape>(\n key: string,\n shape: Shape,\n ): Shape extends ContainerShape ? DraftNode<Shape> : Value {\n let node = this.propertyCache.get(key)\n if (!node) {\n if (isContainerShape(shape)) {\n node = createContainerDraftNode(this.getDraftNodeParams(key, shape))\n } else {\n // For value shapes, first try to get the value from the container\n const containerValue = this.container.get(key)\n if (containerValue !== undefined) {\n node = containerValue as Value\n } else {\n // Only fall back to empty state if the container doesn't have the value\n const emptyState = (this.emptyState as any)?.[key]\n if (!emptyState) {\n throw new Error(\"empty state required\")\n }\n node = emptyState as Value\n }\n }\n if (!node) throw new Error(\"no container made\")\n this.propertyCache.set(key, node)\n }\n\n return node as Shape extends ContainerShape ? DraftNode<Shape> : Value\n }\n\n private createLazyProperties(): void {\n for (const key in this.shape.shapes) {\n const shape = this.shape.shapes[key]\n Object.defineProperty(this, key, {\n get: () => this.getOrCreateNode(key, shape),\n set: isValueShape(shape)\n ? value => {\n // console.log(\"set value\", value)\n this.container.set(key, value)\n }\n : undefined,\n })\n }\n }\n\n // TOOD(duane): return correct type here\n get(key: string): any {\n return this.container.get(key)\n }\n\n set(key: string, value: Value): void {\n this.container.set(key, value)\n }\n\n setContainer<C extends Container>(key: string, container: C): C {\n return this.container.setContainer(key, container)\n }\n\n delete(key: string): void {\n this.container.delete(key)\n }\n\n has(key: string): boolean {\n // LoroMap doesn't have a has method, so we check if get returns undefined\n return this.container.get(key) !== undefined\n }\n\n keys(): string[] {\n return this.container.keys()\n }\n\n values(): any[] {\n return this.container.values()\n }\n\n get size(): number {\n return this.container.size\n }\n}\n","import type { Container, LoroMovableList } from \"loro-crdt\"\nimport type { ContainerOrValueShape } from \"../shape.js\"\nimport { ListDraftNodeBase } from \"./list-base.js\"\n\n// Movable list draft node\nexport class MovableListDraftNode<\n NestedShape extends ContainerOrValueShape,\n Item = NestedShape[\"_plain\"],\n> extends ListDraftNodeBase<NestedShape> {\n protected get container(): LoroMovableList {\n return super.container as LoroMovableList\n }\n\n protected absorbValueAtIndex(index: number, value: any): void {\n // LoroMovableList has set method\n this.container.set(index, value)\n }\n\n move(from: number, to: number): void {\n this.container.move(from, to)\n }\n\n set(index: number, item: Exclude<Item, Container>) {\n return this.container.set(index, item)\n }\n}\n","import {\n type Container,\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n LoroTree,\n type Value,\n} from \"loro-crdt\"\nimport type {\n ContainerOrValueShape,\n ContainerShape,\n RecordContainerShape,\n} from \"../shape.js\"\nimport type { InferDraftType } from \"../types.js\"\nimport { isContainerShape, isValueShape } from \"../utils/type-guards.js\"\nimport { DraftNode, type DraftNodeParams } from \"./base.js\"\nimport { createContainerDraftNode } from \"./utils.js\"\n\nconst containerConstructor = {\n counter: LoroCounter,\n list: LoroList,\n map: LoroMap,\n movableList: LoroMovableList,\n record: LoroMap,\n text: LoroText,\n tree: LoroTree,\n} as const\n\n// Record draft node\nexport class RecordDraftNode<\n NestedShape extends ContainerOrValueShape,\n> extends DraftNode<any> {\n private nodeCache = new Map<string, DraftNode<ContainerShape> | Value>()\n\n constructor(params: DraftNodeParams<RecordContainerShape<NestedShape>>) {\n super(params)\n // We don't need to create lazy properties because keys are dynamic\n // But we could use a Proxy if we wanted property access syntax like record.key\n // However, for now let's stick to get/set methods or maybe Proxy for better DX?\n // The requirement says \"records with uniform specific key type and value\".\n // Usually records are accessed via keys.\n // If we want `draft.record.key`, we need a Proxy.\n // biome-ignore lint/correctness/noConstructorReturn: Proxy return is intentional\n return new Proxy(this, {\n get: (target, prop) => {\n if (typeof prop === \"string\" && !(prop in target)) {\n return target.get(prop)\n }\n return Reflect.get(target, prop)\n },\n set: (target, prop, value) => {\n if (typeof prop === \"string\" && !(prop in target)) {\n target.set(prop, value)\n return true\n }\n return Reflect.set(target, prop, value)\n },\n deleteProperty: (target, prop) => {\n if (typeof prop === \"string\" && !(prop in target)) {\n target.delete(prop)\n return true\n }\n return Reflect.deleteProperty(target, prop)\n },\n ownKeys: target => {\n return target.keys()\n },\n getOwnPropertyDescriptor: (target, prop) => {\n if (typeof prop === \"string\" && target.has(prop)) {\n return {\n configurable: true,\n enumerable: true,\n value: target.get(prop),\n }\n }\n return Reflect.getOwnPropertyDescriptor(target, prop)\n },\n })\n }\n\n protected get shape(): RecordContainerShape<NestedShape> {\n return super.shape as RecordContainerShape<NestedShape>\n }\n\n protected get container(): LoroMap {\n return super.container as LoroMap\n }\n\n absorbPlainValues() {\n for (const [key, node] of this.nodeCache.entries()) {\n if (node instanceof DraftNode) {\n // Contains a DraftNode, not a plain Value: keep recursing\n node.absorbPlainValues()\n continue\n }\n\n // Plain value!\n this.container.set(key, node)\n }\n }\n\n getDraftNodeParams<S extends ContainerShape>(\n key: string,\n shape: S,\n ): DraftNodeParams<ContainerShape> {\n const emptyState = (this.emptyState as any)?.[key]\n\n const LoroContainer = containerConstructor[shape._type]\n\n return {\n shape,\n emptyState,\n getContainer: () =>\n this.container.getOrCreateContainer(key, new (LoroContainer as any)()),\n }\n }\n\n getOrCreateNode(key: string): InferDraftType<NestedShape> {\n let node = this.nodeCache.get(key)\n if (!node) {\n const shape = this.shape.shape\n if (isContainerShape(shape)) {\n node = createContainerDraftNode(\n this.getDraftNodeParams(key, shape as ContainerShape),\n )\n } else {\n // For value shapes, first try to get the value from the container\n const containerValue = this.container.get(key)\n if (containerValue !== undefined) {\n node = containerValue as Value\n } else {\n // Only fall back to empty state if the container doesn't have the value\n const emptyState = (this.emptyState as any)?.[key]\n // For records, empty state might not have the key, which is fine?\n // But if we are accessing it, maybe we expect it to exist or be created?\n // If it's a value type, we can't really \"create\" it without a value.\n // So if it's undefined in container and empty state, we return undefined?\n // But the return type expects Value.\n // Let's check MapDraftNode.\n // MapDraftNode throws \"empty state required\" if not found.\n // But for Record, keys are dynamic.\n if (emptyState === undefined) {\n // If it's a value type and not in container or empty state,\n // we should probably return undefined if the type allows it,\n // or maybe the default value for that type?\n // But we don't have a default value generator for shapes.\n // Actually Shape.plain.* factories have _plain and _draft which are defaults.\n node = (shape as any)._plain\n } else {\n node = emptyState as Value\n }\n }\n }\n if (node !== undefined) {\n this.nodeCache.set(key, node)\n }\n }\n\n return node as any\n }\n\n get(key: string): InferDraftType<NestedShape> {\n return this.getOrCreateNode(key)\n }\n\n set(key: string, value: any): void {\n if (isValueShape(this.shape.shape)) {\n this.container.set(key, value)\n // Update cache if needed?\n // MapDraftNode updates container directly for values.\n // But we also cache values in nodeCache for consistency?\n // MapDraftNode doesn't cache values in propertyCache if they are set via setter?\n // Actually MapDraftNode setter:\n // set: isValueShape(shape) ? value => this.container.set(key, value) : undefined\n // It doesn't update propertyCache.\n // But getOrCreateNode checks propertyCache first.\n // So if we set it, we should probably update propertyCache or clear it for that key.\n this.nodeCache.set(key, value)\n } else {\n // For containers, we can't set them directly usually.\n // But if the user passes a plain object that matches the shape, maybe we should convert it?\n // But typically we modify the draft node.\n throw new Error(\n \"Cannot set container directly, modify the draft node instead\",\n )\n }\n }\n\n setContainer<C extends Container>(key: string, container: C): C {\n return this.container.setContainer(key, container)\n }\n\n delete(key: string): void {\n this.container.delete(key)\n this.nodeCache.delete(key)\n }\n\n has(key: string): boolean {\n return this.container.get(key) !== undefined\n }\n\n keys(): string[] {\n return this.container.keys()\n }\n\n values(): any[] {\n return this.container.values()\n }\n\n get size(): number {\n return this.container.size\n }\n}\n","import type { TextContainerShape } from \"../shape.js\"\nimport { DraftNode } from \"./base.js\"\n\n// Text draft node\nexport class TextDraftNode extends DraftNode<TextContainerShape> {\n absorbPlainValues() {\n // no plain values contained within\n }\n\n // Text methods\n insert(index: number, content: string): void {\n this.container.insert(index, content)\n }\n\n delete(index: number, len: number): void {\n this.container.delete(index, len)\n }\n\n toString(): string {\n return this.container.toString()\n }\n\n update(text: string): void {\n this.container.update(text)\n }\n\n mark(range: { start: number; end: number }, key: string, value: any): void {\n this.container.mark(range, key, value)\n }\n\n unmark(range: { start: number; end: number }, key: string): void {\n this.container.unmark(range, key)\n }\n\n toDelta(): any[] {\n return this.container.toDelta()\n }\n\n applyDelta(delta: any[]): void {\n this.container.applyDelta(delta)\n }\n\n get length(): number {\n return this.container.length\n }\n}\n","import type { TreeContainerShape } from \"../shape.js\"\nimport { DraftNode } from \"./base.js\"\n\n// Tree draft node\nexport class TreeDraftNode<T extends TreeContainerShape> extends DraftNode<T> {\n absorbPlainValues() {\n // TODO(duane): implement for trees\n }\n\n createNode(parent?: any, index?: number): any {\n return this.container.createNode(parent, index)\n }\n\n move(target: any, parent?: any, index?: number): void {\n this.container.move(target, parent, index)\n }\n\n delete(target: any): void {\n this.container.delete(target)\n }\n\n has(target: any): boolean {\n return this.container.has(target)\n }\n\n getNodeByID(id: any): any {\n return this.container.getNodeByID\n ? this.container.getNodeByID(id)\n : undefined\n }\n}\n","import type {\n ContainerShape,\n CounterContainerShape,\n ListContainerShape,\n MapContainerShape,\n MovableListContainerShape,\n RecordContainerShape,\n TextContainerShape,\n TreeContainerShape,\n} from \"../shape.js\"\nimport type { DraftNode, DraftNodeParams } from \"./base.js\"\nimport { CounterDraftNode } from \"./counter.js\"\nimport { ListDraftNode } from \"./list.js\"\nimport { MapDraftNode } from \"./map.js\"\nimport { MovableListDraftNode } from \"./movable-list.js\"\nimport { RecordDraftNode } from \"./record.js\"\nimport { TextDraftNode } from \"./text.js\"\nimport { TreeDraftNode } from \"./tree.js\"\n\n// Generic catch-all overload\nexport function createContainerDraftNode<T extends ContainerShape>(\n params: DraftNodeParams<T>,\n): DraftNode<T>\n\n// Implementation\nexport function createContainerDraftNode(\n params: DraftNodeParams<ContainerShape>,\n): DraftNode<ContainerShape> {\n switch (params.shape._type) {\n case \"counter\":\n return new CounterDraftNode(\n params as DraftNodeParams<CounterContainerShape>,\n )\n case \"list\":\n return new ListDraftNode(params as DraftNodeParams<ListContainerShape>)\n case \"map\":\n return new MapDraftNode(params as DraftNodeParams<MapContainerShape>)\n case \"movableList\":\n return new MovableListDraftNode(\n params as DraftNodeParams<MovableListContainerShape>,\n )\n case \"record\":\n return new RecordDraftNode(\n params as DraftNodeParams<RecordContainerShape>,\n )\n case \"text\":\n return new TextDraftNode(params as DraftNodeParams<TextContainerShape>)\n case \"tree\":\n return new TreeDraftNode(params as DraftNodeParams<TreeContainerShape>)\n default:\n throw new Error(\n `Unknown container type: ${(params.shape as ContainerShape)._type}`,\n )\n }\n}\n","import type { LoroDoc } from \"loro-crdt\"\nimport type { InferPlainType } from \"../index.js\"\nimport type { ContainerShape, DocShape } from \"../shape.js\"\nimport { DraftNode, type DraftNodeParams } from \"./base.js\"\nimport { createContainerDraftNode } from \"./utils.js\"\n\nconst containerGetter = {\n counter: \"getCounter\",\n list: \"getList\",\n map: \"getMap\",\n movableList: \"getMovableList\",\n record: \"getMap\",\n text: \"getText\",\n tree: \"getTree\",\n} as const\n\n// Draft Document class -- the actual object passed to the change `mutation` function\nexport class DraftDoc<Shape extends DocShape> extends DraftNode<Shape> {\n private doc: LoroDoc\n private propertyCache = new Map<string, DraftNode<ContainerShape>>()\n private requiredEmptyState!: InferPlainType<Shape>\n\n constructor(\n _params: Omit<DraftNodeParams<Shape>, \"getContainer\"> & { doc: LoroDoc },\n ) {\n super({\n ..._params,\n getContainer: () => {\n throw new Error(\"can't get container on DraftDoc\")\n },\n })\n if (!_params.emptyState) throw new Error(\"emptyState required\")\n this.doc = _params.doc\n this.requiredEmptyState = _params.emptyState\n this.createLazyProperties()\n }\n\n getDraftNodeParams<S extends ContainerShape>(\n key: string,\n shape: S,\n ): DraftNodeParams<ContainerShape> {\n const getter = this.doc[containerGetter[shape._type]].bind(this.doc)\n\n return {\n shape,\n emptyState: this.requiredEmptyState[key],\n getContainer: () => getter(key),\n }\n }\n\n getOrCreateDraftNode(\n key: string,\n shape: ContainerShape,\n ): DraftNode<ContainerShape> {\n let node = this.propertyCache.get(key)\n\n if (!node) {\n node = createContainerDraftNode(this.getDraftNodeParams(key, shape))\n this.propertyCache.set(key, node)\n }\n\n return node\n }\n\n private createLazyProperties(): void {\n for (const key in this.shape.shapes) {\n const shape = this.shape.shapes[key]\n Object.defineProperty(this, key, {\n get: () => this.getOrCreateDraftNode(key, shape),\n })\n }\n }\n\n absorbPlainValues(): void {\n // By iterating over the propertyCache, we achieve a small optimization\n // by only absorbing values that have been 'touched' in some way\n for (const [, node] of this.propertyCache.entries()) {\n node.absorbPlainValues()\n }\n }\n}\n","/** biome-ignore-all lint/suspicious/noExplicitAny: JSON Patch values can be any type */\n\nimport type { DocShape } from \"./shape.js\"\nimport type { Draft } from \"./types.js\"\n\n// =============================================================================\n// JSON PATCH TYPES - Discriminated Union for Type Safety\n// =============================================================================\n\nexport type JsonPatchAddOperation = {\n op: \"add\"\n path: string | (string | number)[]\n value: any\n}\n\nexport type JsonPatchRemoveOperation = {\n op: \"remove\"\n path: string | (string | number)[]\n}\n\nexport type JsonPatchReplaceOperation = {\n op: \"replace\"\n path: string | (string | number)[]\n value: any\n}\n\nexport type JsonPatchMoveOperation = {\n op: \"move\"\n path: string | (string | number)[]\n from: string | (string | number)[]\n}\n\nexport type JsonPatchCopyOperation = {\n op: \"copy\"\n path: string | (string | number)[]\n from: string | (string | number)[]\n}\n\nexport type JsonPatchTestOperation = {\n op: \"test\"\n path: string | (string | number)[]\n value: any\n}\n\nexport type JsonPatchOperation =\n | JsonPatchAddOperation\n | JsonPatchRemoveOperation\n | JsonPatchReplaceOperation\n | JsonPatchMoveOperation\n | JsonPatchCopyOperation\n | JsonPatchTestOperation\n\nexport type JsonPatch = JsonPatchOperation[]\n\n// =============================================================================\n// PATH NAVIGATION UTILITIES\n// =============================================================================\n\n/**\n * Normalize JSON Pointer string to path array\n * Handles RFC 6901 escaping: ~1 -> /, ~0 -> ~\n */\nexport function normalizePath(\n path: string | (string | number)[],\n): (string | number)[] {\n if (Array.isArray(path)) {\n return path\n }\n\n // Handle JSON Pointer format (RFC 6901)\n if (path.startsWith(\"/\")) {\n return path\n .slice(1) // Remove leading slash\n .split(\"/\")\n .map(segment => {\n // Handle JSON Pointer escaping\n const unescaped = segment.replace(/~1/g, \"/\").replace(/~0/g, \"~\")\n // Try to parse as number for array indices\n const asNumber = Number(unescaped)\n return Number.isInteger(asNumber) && asNumber >= 0\n ? asNumber\n : unescaped\n })\n }\n\n // Handle simple dot notation or single segment\n return path.split(\".\").map(segment => {\n const asNumber = Number(segment)\n return Number.isInteger(asNumber) && asNumber >= 0 ? asNumber : segment\n })\n}\n\n/**\n * Navigate to a target path using natural DraftNode property access\n * This follows the existing patterns from the test suite\n */\nfunction navigateToPath<T extends DocShape>(\n draft: Draft<T>,\n path: (string | number)[],\n): { parent: any; key: string | number } {\n if (path.length === 0) {\n throw new Error(\"Cannot navigate to empty path\")\n }\n\n let current = draft as any\n\n // Navigate to parent of target\n for (let i = 0; i < path.length - 1; i++) {\n const segment = path[i]\n\n if (typeof segment === \"string\") {\n // Use natural property access - this leverages existing DraftNode lazy creation\n current = current[segment]\n if (current === undefined) {\n throw new Error(`Cannot navigate to path segment: ${segment}`)\n }\n } else if (typeof segment === \"number\") {\n // List/array access using get() method (following existing patterns)\n if (current.get && typeof current.get === \"function\") {\n current = current.get(segment)\n if (current === undefined) {\n throw new Error(`List index ${segment} does not exist`)\n }\n } else {\n throw new Error(`Cannot use numeric index ${segment} on non-list`)\n }\n } else {\n throw new Error(`Invalid path segment type: ${typeof segment}`)\n }\n }\n\n const targetKey = path[path.length - 1]\n return { parent: current, key: targetKey }\n}\n\n/**\n * Get value at path using natural DraftNode access patterns\n */\nfunction getValueAtPath<T extends DocShape>(\n draft: Draft<T>,\n path: (string | number)[],\n): any {\n if (path.length === 0) {\n return draft\n }\n\n const { parent, key } = navigateToPath(draft, path)\n\n if (typeof key === \"string\") {\n // Use natural property access or get() method\n if (parent.get && typeof parent.get === \"function\") {\n return parent.get(key)\n }\n return parent[key]\n } else if (typeof key === \"number\") {\n // List access using get() method\n if (parent.get && typeof parent.get === \"function\") {\n return parent.get(key)\n }\n throw new Error(`Cannot use numeric index ${key} on non-list`)\n }\n\n throw new Error(`Invalid key type: ${typeof key}`)\n}\n\n// =============================================================================\n// OPERATION HANDLERS - Following existing DraftNode patterns\n// =============================================================================\n\n/**\n * Handle 'add' operation using existing DraftNode methods\n */\nfunction handleAdd<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchAddOperation,\n): void {\n const path = normalizePath(operation.path)\n const { parent, key } = navigateToPath(draft, path)\n\n if (typeof key === \"string\") {\n // Map-like operations - use natural assignment or set() method\n if (parent.set && typeof parent.set === \"function\") {\n parent.set(key, operation.value)\n } else {\n // Natural property assignment (follows existing test patterns)\n parent[key] = operation.value\n }\n } else if (typeof key === \"number\") {\n // List operations - use insert() method (follows existing patterns)\n if (parent.insert && typeof parent.insert === \"function\") {\n parent.insert(key, operation.value)\n } else {\n throw new Error(`Cannot insert at numeric index ${key} on non-list`)\n }\n } else {\n throw new Error(`Invalid key type: ${typeof key}`)\n }\n}\n\n/**\n * Handle 'remove' operation using existing DraftNode methods\n */\nfunction handleRemove<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchRemoveOperation,\n): void {\n const path = normalizePath(operation.path)\n const { parent, key } = navigateToPath(draft, path)\n\n if (typeof key === \"string\") {\n // Map-like operations - use delete() method (follows existing patterns)\n if (parent.delete && typeof parent.delete === \"function\") {\n parent.delete(key)\n } else {\n delete parent[key]\n }\n } else if (typeof key === \"number\") {\n // List operations - use delete() method with count (follows existing patterns)\n if (parent.delete && typeof parent.delete === \"function\") {\n parent.delete(key, 1)\n } else {\n throw new Error(`Cannot remove at numeric index ${key} on non-list`)\n }\n } else {\n throw new Error(`Invalid key type: ${typeof key}`)\n }\n}\n\n/**\n * Handle 'replace' operation using existing DraftNode methods\n */\nfunction handleReplace<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchReplaceOperation,\n): void {\n const path = normalizePath(operation.path)\n const { parent, key } = navigateToPath(draft, path)\n\n if (typeof key === \"string\") {\n // Map-like operations - use set() method or natural assignment\n if (parent.set && typeof parent.set === \"function\") {\n parent.set(key, operation.value)\n } else {\n parent[key] = operation.value\n }\n } else if (typeof key === \"number\") {\n // List operations - delete then insert (follows existing patterns)\n if (\n parent.delete &&\n parent.insert &&\n typeof parent.delete === \"function\" &&\n typeof parent.insert === \"function\"\n ) {\n parent.delete(key, 1)\n parent.insert(key, operation.value)\n } else {\n throw new Error(`Cannot replace at numeric index ${key} on non-list`)\n }\n } else {\n throw new Error(`Invalid key type: ${typeof key}`)\n }\n}\n\n/**\n * Handle 'move' operation using existing DraftNode methods\n */\nfunction handleMove<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchMoveOperation,\n): void {\n const fromPath = normalizePath(operation.from)\n const toPath = normalizePath(operation.path)\n\n // For list moves within the same parent, we need special handling\n if (\n fromPath.length === toPath.length &&\n fromPath.slice(0, -1).every((segment, i) => segment === toPath[i])\n ) {\n // Same parent container - use list move operation if available\n const fromIndex = fromPath[fromPath.length - 1]\n const toIndex = toPath[toPath.length - 1]\n\n if (typeof fromIndex === \"number\" && typeof toIndex === \"number\") {\n const { parent } = navigateToPath(draft, fromPath.slice(0, -1))\n\n // Check if the parent has a move method (like LoroMovableList)\n if (parent.move && typeof parent.move === \"function\") {\n parent.move(fromIndex, toIndex)\n return\n }\n\n // Otherwise, get value, remove, then add at target index\n const value = getValueAtPath(draft, fromPath)\n handleRemove(draft, { op: \"remove\", path: operation.from })\n\n // For JSON Patch move semantics, the target index refers to the position\n // in the final array, not the intermediate array after removal.\n // No index adjustment needed - use the original target index.\n handleAdd(draft, { op: \"add\", path: operation.path, value })\n return\n }\n }\n\n // Different parents or non-numeric indices - standard move\n const value = getValueAtPath(draft, fromPath)\n handleRemove(draft, { op: \"remove\", path: operation.from })\n handleAdd(draft, { op: \"add\", path: operation.path, value })\n}\n\n/**\n * Handle 'copy' operation using existing DraftNode methods\n */\nfunction handleCopy<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchCopyOperation,\n): void {\n const fromPath = normalizePath(operation.from)\n\n // Get the value to copy\n const value = getValueAtPath(draft, fromPath)\n\n // Add to destination (no removal)\n handleAdd(draft, { op: \"add\", path: operation.path, value })\n}\n\n/**\n * Handle 'test' operation using existing DraftNode value access\n */\nfunction handleTest<T extends DocShape>(\n draft: Draft<T>,\n operation: JsonPatchTestOperation,\n): boolean {\n const path = normalizePath(operation.path)\n const actualValue = getValueAtPath(draft, path)\n\n // Deep equality check for test operation\n return JSON.stringify(actualValue) === JSON.stringify(operation.value)\n}\n\n// =============================================================================\n// MAIN APPLICATOR - Simple orchestration following existing patterns\n// =============================================================================\n\n/**\n * Main JSON Patch applicator - follows existing change() patterns\n */\nexport class JsonPatchApplicator<T extends DocShape> {\n constructor(private rootDraft: Draft<T>) {}\n\n /**\n * Apply a single JSON Patch operation\n */\n applyOperation(operation: JsonPatchOperation): void {\n switch (operation.op) {\n case \"add\":\n handleAdd(this.rootDraft, operation)\n break\n case \"remove\":\n handleRemove(this.rootDraft, operation)\n break\n case \"replace\":\n handleReplace(this.rootDraft, operation)\n break\n case \"move\":\n handleMove(this.rootDraft, operation)\n break\n case \"copy\":\n handleCopy(this.rootDraft, operation)\n break\n case \"test\":\n if (!handleTest(this.rootDraft, operation)) {\n throw new Error(`JSON Patch test failed at path: ${operation.path}`)\n }\n break\n default:\n // TypeScript will catch this at compile time with proper discriminated union\n throw new Error(\n `Unsupported JSON Patch operation: ${(operation as any).op}`,\n )\n }\n }\n\n /**\n * Apply multiple JSON Patch operations in sequence\n */\n applyPatch(patch: JsonPatch): void {\n for (const operation of patch) {\n this.applyOperation(operation)\n }\n }\n}\n","import type { Value } from \"loro-crdt\"\nimport type { ContainerShape, DocShape, ValueShape } from \"./shape.js\"\nimport { isObjectValue } from \"./utils/type-guards.js\"\n\n/**\n * Overlays CRDT state with empty state defaults\n */\nexport function overlayEmptyState<Shape extends DocShape>(\n shape: Shape,\n crdtValue: { [key: string]: Value },\n emptyValue: { [key: string]: Value },\n): { [key: string]: Value } {\n if (typeof crdtValue !== \"object\") {\n throw new Error(\"crdt object is required\")\n }\n\n if (typeof emptyValue !== \"object\") {\n throw new Error(\"empty object is required\")\n }\n\n const result = { ...emptyValue }\n\n for (const [key, propShape] of Object.entries(shape.shapes)) {\n const propCrdtValue = crdtValue[key]\n\n const propEmptyValue = emptyValue[key as keyof typeof emptyValue]\n\n result[key as keyof typeof result] = mergeValue(\n propShape,\n propCrdtValue,\n propEmptyValue,\n )\n }\n\n return result\n}\n\n/**\n * Merges individual CRDT values with empty state defaults\n */\nexport function mergeValue<Shape extends ContainerShape | ValueShape>(\n shape: Shape,\n crdtValue: Value,\n emptyValue: Value,\n): Value {\n if (crdtValue === undefined && emptyValue === undefined) {\n throw new Error(\"either crdt or empty value must be defined\")\n }\n\n switch (shape._type) {\n case \"text\":\n return crdtValue ?? emptyValue ?? \"\"\n case \"counter\":\n return crdtValue ?? emptyValue ?? 0\n case \"list\":\n case \"movableList\":\n return crdtValue ?? emptyValue ?? []\n case \"map\": {\n if (!isObjectValue(crdtValue) && crdtValue !== undefined) {\n throw new Error(\"map crdt must be object\")\n }\n\n const crdtMapValue = crdtValue ?? {}\n\n if (!isObjectValue(emptyValue) && emptyValue !== undefined) {\n throw new Error(\"map empty state must be object\")\n }\n\n const emptyMapValue = emptyValue ?? {}\n\n const result = { ...emptyMapValue }\n for (const [key, nestedShape] of Object.entries(shape.shapes)) {\n const nestedCrdtValue = crdtMapValue[key]\n const nestedEmptyValue = emptyMapValue[key]\n\n result[key as keyof typeof result] = mergeValue(\n nestedShape,\n nestedCrdtValue,\n nestedEmptyValue,\n )\n }\n\n return result\n }\n case \"tree\":\n return crdtValue ?? emptyValue ?? []\n default:\n return crdtValue ?? emptyValue\n }\n}\n","import type {\n ArrayValueShape,\n ContainerOrValueShape,\n DocShape,\n ListContainerShape,\n MapContainerShape,\n MovableListContainerShape,\n ObjectValueShape,\n RecordContainerShape,\n RecordValueShape,\n UnionValueShape,\n ValueShape,\n} from \"./shape.js\"\nimport type { InferPlainType } from \"./types.js\"\n\n/**\n * Validates a value against a ContainerShape or ValueShape schema\n */\nexport function validateValue(\n value: unknown,\n schema: ContainerOrValueShape,\n path: string = \"\",\n): unknown {\n if (!schema || typeof schema !== \"object\" || !(\"_type\" in schema)) {\n throw new Error(`Invalid schema at path ${path}: missing _type`)\n }\n\n const currentPath = path || \"root\"\n\n // Handle ContainerShape types\n if (schema._type === \"text\") {\n if (typeof value !== \"string\") {\n throw new Error(\n `Expected string at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n }\n\n if (schema._type === \"counter\") {\n if (typeof value !== \"number\") {\n throw new Error(\n `Expected number at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n }\n\n if (schema._type === \"list\" || schema._type === \"movableList\") {\n if (!Array.isArray(value)) {\n throw new Error(\n `Expected array at path ${currentPath}, got ${typeof value}`,\n )\n }\n const listSchema = schema as ListContainerShape | MovableListContainerShape\n return value.map((item, index) =>\n validateValue(item, listSchema.shape, `${currentPath}[${index}]`),\n )\n }\n\n if (schema._type === \"map\") {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `Expected object at path ${currentPath}, got ${typeof value}`,\n )\n }\n const mapSchema = schema as MapContainerShape\n const result: Record<string, unknown> = {}\n\n // Validate each property in the map shape\n for (const [key, nestedSchema] of Object.entries(mapSchema.shapes)) {\n const nestedPath = `${currentPath}.${key}`\n const nestedValue = (value as Record<string, unknown>)[key]\n result[key] = validateValue(nestedValue, nestedSchema, nestedPath)\n }\n return result\n }\n\n if (schema._type === \"record\") {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `Expected object at path ${currentPath}, got ${typeof value}`,\n )\n }\n const recordSchema = schema as RecordContainerShape\n const result: Record<string, unknown> = {}\n\n // Validate each property in the record\n for (const [key, nestedValue] of Object.entries(value)) {\n const nestedPath = `${currentPath}.${key}`\n result[key] = validateValue(nestedValue, recordSchema.shape, nestedPath)\n }\n return result\n }\n\n if (schema._type === \"tree\") {\n if (!Array.isArray(value)) {\n throw new Error(\n `Expected array for tree at path ${currentPath}, got ${typeof value}`,\n )\n }\n // Trees can contain any structure, so we just validate it's an array\n return value\n }\n\n // Handle ValueShape types\n if (schema._type === \"value\") {\n const valueSchema = schema as ValueShape\n\n switch (valueSchema.valueType) {\n case \"string\":\n if (typeof value !== \"string\") {\n throw new Error(\n `Expected string at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"number\":\n if (typeof value !== \"number\") {\n throw new Error(\n `Expected number at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"boolean\":\n if (typeof value !== \"boolean\") {\n throw new Error(\n `Expected boolean at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"null\":\n if (value !== null) {\n throw new Error(\n `Expected null at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"undefined\":\n if (value !== undefined) {\n throw new Error(\n `Expected undefined at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"uint8array\":\n if (!(value instanceof Uint8Array)) {\n throw new Error(\n `Expected Uint8Array at path ${currentPath}, got ${typeof value}`,\n )\n }\n return value\n\n case \"object\": {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `Expected object at path ${currentPath}, got ${typeof value}`,\n )\n }\n const objectSchema = valueSchema as ObjectValueShape\n const result: Record<string, unknown> = {}\n\n // Validate each property in the object shape\n for (const [key, nestedSchema] of Object.entries(objectSchema.shape)) {\n const nestedPath = `${currentPath}.${key}`\n const nestedValue = (value as Record<string, unknown>)[key]\n result[key] = validateValue(nestedValue, nestedSchema, nestedPath)\n }\n return result\n }\n\n case \"record\": {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `Expected object at path ${currentPath}, got ${typeof value}`,\n )\n }\n const recordSchema = valueSchema as RecordValueShape\n const result: Record<string, unknown> = {}\n\n // Validate each property in the record\n for (const [key, nestedValue] of Object.entries(value)) {\n const nestedPath = `${currentPath}.${key}`\n result[key] = validateValue(\n nestedValue,\n recordSchema.shape,\n nestedPath,\n )\n }\n return result\n }\n\n case \"array\": {\n if (!Array.isArray(value)) {\n throw new Error(\n `Expected array at path ${currentPath}, got ${typeof value}`,\n )\n }\n const arraySchema = valueSchema as ArrayValueShape\n return value.map((item, index) =>\n validateValue(item, arraySchema.shape, `${currentPath}[${index}]`),\n )\n }\n\n case \"union\": {\n const unionSchema = valueSchema as UnionValueShape\n let lastError: Error | null = null\n\n // Try to validate against each shape in the union\n for (const shape of unionSchema.shapes) {\n try {\n return validateValue(value, shape, currentPath)\n } catch (error) {\n lastError = error as Error\n }\n }\n\n throw new Error(\n `Value at path ${currentPath} does not match any union type: ${lastError?.message}`,\n )\n }\n\n default:\n throw new Error(`Unknown value type: ${(valueSchema as any).valueType}`)\n }\n }\n\n throw new Error(`Unknown schema type: ${(schema as any)._type}`)\n}\n\n/**\n * Validates empty state against schema structure without using Zod\n * Combines the functionality of createEmptyStateValidator and createValueValidator\n */\nexport function validateEmptyState<T extends DocShape>(\n emptyState: unknown,\n schema: T,\n): InferPlainType<T> {\n if (\n !emptyState ||\n typeof emptyState !== \"object\" ||\n Array.isArray(emptyState)\n ) {\n throw new Error(\"Empty state must be an object\")\n }\n\n const result: Record<string, unknown> = {}\n\n // Validate each property in the document schema\n for (const [key, schemaValue] of Object.entries(schema.shapes)) {\n const value = (emptyState as Record<string, unknown>)[key]\n result[key] = validateValue(value, schemaValue, key)\n }\n\n return result as InferPlainType<T>\n}\n","// biome-ignore-all lint/suspicious/noExplicitAny: required\n\nimport type {\n LoroCounter,\n LoroList,\n LoroMap,\n LoroMovableList,\n LoroText,\n LoroTree,\n} from \"loro-crdt\"\n\nimport type { CounterDraftNode } from \"./draft-nodes/counter.js\"\nimport type { ListDraftNode } from \"./draft-nodes/list.js\"\nimport type { MapDraftNode } from \"./draft-nodes/map.js\"\nimport type { MovableListDraftNode } from \"./draft-nodes/movable-list.js\"\nimport type { RecordDraftNode } from \"./draft-nodes/record.js\"\nimport type { TextDraftNode } from \"./draft-nodes/text.js\"\n\nexport interface Shape<Plain, Draft> {\n readonly _type: string\n readonly _plain: Plain\n readonly _draft: Draft\n}\n\nexport interface DocShape<\n NestedShapes extends Record<string, ContainerShape> = Record<\n string,\n ContainerShape\n >,\n> extends Shape<\n { [K in keyof NestedShapes]: NestedShapes[K][\"_plain\"] },\n { [K in keyof NestedShapes]: NestedShapes[K][\"_draft\"] }\n > {\n readonly _type: \"doc\"\n // A doc's root containers each separately has its own shape, hence 'shapes'\n readonly shapes: NestedShapes\n}\n\nexport interface TextContainerShape extends Shape<string, TextDraftNode> {\n readonly _type: \"text\"\n}\nexport interface CounterContainerShape extends Shape<number, CounterDraftNode> {\n readonly _type: \"counter\"\n}\nexport interface TreeContainerShape<NestedShape = ContainerOrValueShape>\n extends Shape<any, any> {\n readonly _type: \"tree\"\n // TODO(duane): What does a tree contain? One type, or many?\n readonly shape: NestedShape\n}\n\n// Container schemas using interfaces for recursive references\nexport interface ListContainerShape<\n NestedShape extends ContainerOrValueShape = ContainerOrValueShape,\n> extends Shape<NestedShape[\"_plain\"][], ListDraftNode<NestedShape>> {\n readonly _type: \"list\"\n // A list contains many elements, all of the same 'shape'\n readonly shape: NestedShape\n}\n\nexport interface MovableListContainerShape<\n NestedShape extends ContainerOrValueShape = ContainerOrValueShape,\n> extends Shape<NestedShape[\"_plain\"][], MovableListDraftNode<NestedShape>> {\n readonly _type: \"movableList\"\n // A list contains many elements, all of the same 'shape'\n readonly shape: NestedShape\n}\n\nexport interface MapContainerShape<\n NestedShapes extends Record<string, ContainerOrValueShape> = Record<\n string,\n ContainerOrValueShape\n >,\n> extends Shape<\n { [K in keyof NestedShapes]: NestedShapes[K][\"_plain\"] },\n MapDraftNode<NestedShapes> & {\n [K in keyof NestedShapes]: NestedShapes[K][\"_draft\"]\n }\n > {\n readonly _type: \"map\"\n // Each map property has its own shape, hence 'shapes'\n readonly shapes: NestedShapes\n}\n\nexport interface RecordContainerShape<\n NestedShape extends ContainerOrValueShape = ContainerOrValueShape,\n> extends Shape<\n Record<string, NestedShape[\"_plain\"]>,\n RecordDraftNode<NestedShape>\n > {\n readonly _type: \"record\"\n readonly shape: NestedShape\n}\n\nexport type ContainerShape =\n | CounterContainerShape\n | ListContainerShape\n | MapContainerShape\n | MovableListContainerShape\n | RecordContainerShape\n | TextContainerShape\n | TreeContainerShape\n\nexport type ContainerType = ContainerShape[\"_type\"]\n\n// LoroValue shape types - a shape for each of Loro's Value types\nexport interface StringValueShape extends Shape<string, string> {\n readonly _type: \"value\"\n readonly valueType: \"string\"\n}\nexport interface NumberValueShape extends Shape<number, number> {\n readonly _type: \"value\"\n readonly valueType: \"number\"\n}\nexport interface BooleanValueShape extends Shape<boolean, boolean> {\n readonly _type: \"value\"\n readonly valueType: \"boolean\"\n}\nexport interface NullValueShape extends Shape<null, null> {\n readonly _type: \"value\"\n readonly valueType: \"null\"\n}\nexport interface UndefinedValueShape extends Shape<undefined, undefined> {\n readonly _type: \"value\"\n readonly valueType: \"undefined\"\n}\nexport interface Uint8ArrayValueShape extends Shape<Uint8Array, Uint8Array> {\n readonly _type: \"value\"\n readonly valueType: \"uint8array\"\n}\n\nexport interface ObjectValueShape<\n T extends Record<string, ValueShape> = Record<string, ValueShape>,\n> extends Shape<\n { [K in keyof T]: T[K][\"_plain\"] },\n { [K in keyof T]: T[K][\"_draft\"] }\n > {\n readonly _type: \"value\"\n readonly valueType: \"object\"\n readonly shape: T\n}\n\nexport interface RecordValueShape<T extends ValueShape = ValueShape>\n extends Shape<Record<string, T[\"_plain\"]>, Record<string, T[\"_draft\"]>> {\n readonly _type: \"value\"\n readonly valueType: \"record\"\n readonly shape: T\n}\n\nexport interface ArrayValueShape<T extends ValueShape = ValueShape>\n extends Shape<T[\"_plain\"][], T[\"_draft\"][]> {\n readonly _type: \"value\"\n readonly valueType: \"array\"\n readonly shape: T\n}\n\nexport interface UnionValueShape<T extends ValueShape[] = ValueShape[]>\n extends Shape<T[number][\"_plain\"], T[number][\"_draft\"]> {\n readonly _type: \"value\"\n readonly valueType: \"union\"\n readonly shapes: T\n}\n\n// Union of all ValueShapes - these can only contain other ValueShapes, not ContainerShapes\nexport type ValueShape =\n | StringValueShape\n | NumberValueShape\n | BooleanValueShape\n | NullValueShape\n | UndefinedValueShape\n | Uint8ArrayValueShape\n | ObjectValueShape\n | RecordValueShape\n | ArrayValueShape\n | UnionValueShape\n\nexport type ContainerOrValueShape = ContainerShape | ValueShape\n\n/**\n * The LoroShape factory object\n *\n * If a container has a `shape` type variable, it refers to the shape it contains--\n * so for example, a `LoroShape.list(LoroShape.text())` would return a value of type\n * `ListContainerShape<TextContainerShape>`.\n */\nexport const Shape = {\n doc: <T extends Record<string, ContainerShape>>(shape: T): DocShape<T> => ({\n _type: \"doc\" as const,\n shapes: shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n // CRDTs are represented by Loro Containers--they converge on state using Loro's\n // various CRDT algorithms\n counter: (): CounterContainerShape => ({\n _type: \"counter\" as const,\n _plain: 0,\n _draft: {} as CounterDraftNode,\n }),\n\n list: <T extends ContainerOrValueShape>(shape: T): ListContainerShape<T> => ({\n _type: \"list\" as const,\n shape,\n _plain: [] as any,\n _draft: {} as any,\n }),\n\n map: <T extends Record<string, ContainerOrValueShape>>(\n shape: T,\n ): MapContainerShape<T> => ({\n _type: \"map\" as const,\n shapes: shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n record: <T extends ContainerOrValueShape>(\n shape: T,\n ): RecordContainerShape<T> => ({\n _type: \"record\" as const,\n shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n movableList: <T extends ContainerOrValueShape>(\n shape: T,\n ): MovableListContainerShape<T> => ({\n _type: \"movableList\" as const,\n shape,\n _plain: [] as any,\n _draft: {} as any,\n }),\n\n text: (): TextContainerShape => ({\n _type: \"text\" as const,\n _plain: \"\",\n _draft: {} as TextDraftNode,\n }),\n\n tree: <T extends MapContainerShape>(shape: T): TreeContainerShape => ({\n _type: \"tree\" as const,\n shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n // Values are represented as plain JS objects, with the limitation that they MUST be\n // representable as a Loro \"Value\"--basically JSON. The behavior of a Value is basically\n // \"Last Write Wins\", meaning there is no subtle convergent behavior here, just taking\n // the most recent value based on the current available information.\n plain: {\n string: (): StringValueShape => ({\n _type: \"value\" as const,\n valueType: \"string\" as const,\n _plain: \"\",\n _draft: \"\",\n }),\n\n number: (): NumberValueShape => ({\n _type: \"value\" as const,\n valueType: \"number\" as const,\n _plain: 0,\n _draft: 0,\n }),\n\n boolean: (): BooleanValueShape => ({\n _type: \"value\" as const,\n valueType: \"boolean\" as const,\n _plain: false,\n _draft: false,\n }),\n\n null: (): NullValueShape => ({\n _type: \"value\" as const,\n valueType: \"null\" as const,\n _plain: null,\n _draft: null,\n }),\n\n undefined: (): UndefinedValueShape => ({\n _type: \"value\" as const,\n valueType: \"undefined\" as const,\n _plain: undefined,\n _draft: undefined,\n }),\n\n uint8Array: (): Uint8ArrayValueShape => ({\n _type: \"value\" as const,\n valueType: \"uint8array\" as const,\n _plain: new Uint8Array(),\n _draft: new Uint8Array(),\n }),\n\n object: <T extends Record<string, ValueShape>>(\n shape: T,\n ): ObjectValueShape<T> => ({\n _type: \"value\" as const,\n valueType: \"object\" as const,\n shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n record: <T extends ValueShape>(shape: T): RecordValueShape<T> => ({\n _type: \"value\" as const,\n valueType: \"record\" as const,\n shape,\n _plain: {} as any,\n _draft: {} as any,\n }),\n\n array: <T extends ValueShape>(shape: T): ArrayValueShape<T> => ({\n _type: \"value\" as const,\n valueType: \"array\" as const,\n shape,\n _plain: [] as any,\n _draft: [] as any,\n }),\n\n // Special value type that helps make things like `string | null` representable\n // TODO(duane): should this be a more general type for containers too?\n union: <T extends ValueShape[]>(shapes: T): UnionValueShape<T> => ({\n _type: \"value\" as const,\n valueType: \"union\" as const,\n shapes,\n _plain: {} as any,\n _draft: {} as any,\n }),\n },\n}\n\n// Add this type mapping near the top of your file, after the imports\nexport type ShapeToContainer<T extends DocShape | ContainerShape> =\n T extends TextContainerShape\n ? LoroText\n : T extends CounterContainerShape\n ? LoroCounter\n : T extends ListContainerShape\n ? LoroList\n : T extends MovableListContainerShape\n ? LoroMovableList\n : T extends MapContainerShape | RecordContainerShape\n ? LoroMap\n : T extends TreeContainerShape\n ? LoroTree\n : never // not a container\n"],"mappings":";AAEA,SAAS,eAAe;;;ACQjB,IAAe,YAAf,MAAkE;AAAA,EAGvE,YAAsB,SAAiC;AAAjC;AAAA,EAAkC;AAAA,EAF9C;AAAA,EAMV,IAAc,QAAe;AAC3B,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAc,aAAgD;AAC5D,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,IAAc,YAAqC;AACjD,QAAI,CAAC,KAAK,kBAAkB;AAC1B,YAAM,YAAY,KAAK,QAAQ,aAAa;AAC5C,WAAK,mBAAmB;AACxB,aAAO;AAAA,IACT;AACA,WAAO,KAAK;AAAA,EACd;AACF;;;AC7BO,IAAM,mBAAN,cAA+B,UAAiC;AAAA,EACrE,oBAAoB;AAAA,EAEpB;AAAA,EAEA,UAAU,OAAqB;AAC7B,SAAK,UAAU,UAAU,KAAK;AAAA,EAChC;AAAA,EAEA,UAAU,OAAqB;AAC7B,SAAK,UAAU,UAAU,KAAK;AA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W;AAAA,MACX;AAAA,MACA,QAAQ,CAAC;AAAA,MACT,QAAQ,CAAC;AAAA,IACX;AAAA,EACF;AACF;","names":["LoroCounter","LoroList","LoroMap","LoroMovableList","LoroText","LoroCounter","LoroList","LoroMap","LoroMovableList","LoroText","LoroCounter","LoroList","LoroMap","LoroMovableList","LoroText","LoroTree","containerConstructor","LoroCounter","LoroList","LoroMap","LoroMovableList","LoroText","LoroTree","value"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loro-extended/change",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "A schema-driven, type-safe wrapper for Loro CRDT that provides natural JavaScript syntax for collaborative data mutations",
5
5
  "author": "Duane Johnson",
6
6
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "./src/*": "./src/*"
22
22
  },
23
23
  "dependencies": {
24
- "loro-crdt": "^1.9.0"
24
+ "loro-crdt": "^1.10.2"
25
25
  },
26
26
  "devDependencies": {
27
27
  "tsup": "^8.5.0",
package/src/conversion.ts CHANGED
@@ -30,10 +30,7 @@ import {
30
30
  function convertTextInput(value: string): LoroText {
31
31
  const text = new LoroText()
32
32
 
33
- // TODO(duane): condition can be removed when https://github.com/loro-dev/loro/issues/872 is addressed
34
- if (value.length > 0) {
35
- text.insert(0, value)
36
- }
33
+ text.insert(0, value)
37
34
 
38
35
  return text
39
36
  }
@@ -9,8 +9,6 @@ export class TextDraftNode extends DraftNode<TextContainerShape> {
9
9
 
10
10
  // Text methods
11
11
  insert(index: number, content: string): void {
12
- // TODO(duane): condition can be removed when https://github.com/loro-dev/loro/issues/872 is addressed
13
- if (content.length === 0) return
14
12
  this.container.insert(index, content)
15
13
  }
16
14
 
@@ -196,6 +196,7 @@ export function isValueShape(
196
196
  "object",
197
197
  "record",
198
198
  "array",
199
+ "union",
199
200
  ].includes(schema.valueType)
200
201
  )
201
202
  }