UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

40 lines (39 loc) 1.32 kB
import { ConNode, ValNode, ObjNode, VecNode, StrNode, BinNode, ArrNode } from '../nodes'; import { s } from '../../json-crdt-patch'; /** * Converts any JSON CRDT node to a schema representation. The schema can be * used to copy the structure of the JSON CRDT node to another document or * another location in the same document. * * @param node JSON CRDT node to recursively convert to schema. * @returns Schema representation of the JSON CRDT node. */ export const toSchema = (node) => { if (node instanceof ConNode) return s.con(node.val); if (node instanceof ValNode) return s.val(toSchema(node.node())); if (node instanceof ObjNode) { const obj = {}; node.nodes((child, key) => (obj[key] = toSchema(child))); return s.obj(obj); } if (node instanceof VecNode) { const arr = []; node.children((child) => arr.push(toSchema(child))); return s.vec(...arr); } if (node instanceof StrNode) return s.str(node.view()); if (node instanceof BinNode) return s.bin(node.view()); if (node instanceof ArrNode) { const arr = []; node.children((child) => { if (child) arr.push(toSchema(child)); }); return s.arr(arr); } return s.con(undefined); };