@voidhash/mimic 0.0.2 → 0.0.3

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.
Files changed (273) hide show
  1. package/.turbo/turbo-build.log +257 -42
  2. package/dist/Document.cjs +152 -0
  3. package/dist/Document.d.cts +67 -0
  4. package/dist/Document.d.cts.map +1 -0
  5. package/dist/Document.d.mts +68 -0
  6. package/dist/Document.d.mts.map +1 -0
  7. package/dist/Document.mjs +147 -0
  8. package/dist/Document.mjs.map +1 -0
  9. package/dist/EffectSchema.cjs +180 -0
  10. package/dist/EffectSchema.d.cts +84 -0
  11. package/dist/EffectSchema.d.cts.map +1 -0
  12. package/dist/EffectSchema.d.mts +84 -0
  13. package/dist/EffectSchema.d.mts.map +1 -0
  14. package/dist/EffectSchema.mjs +176 -0
  15. package/dist/EffectSchema.mjs.map +1 -0
  16. package/dist/FractionalIndex.cjs +365 -0
  17. package/dist/FractionalIndex.mjs +364 -0
  18. package/dist/FractionalIndex.mjs.map +1 -0
  19. package/dist/Operation.cjs +53 -0
  20. package/dist/Operation.d.cts +39 -0
  21. package/dist/Operation.d.cts.map +1 -0
  22. package/dist/Operation.d.mts +39 -0
  23. package/dist/Operation.d.mts.map +1 -0
  24. package/dist/Operation.mjs +46 -0
  25. package/dist/Operation.mjs.map +1 -0
  26. package/dist/OperationDefinition.cjs +13 -0
  27. package/dist/OperationDefinition.d.cts +12 -0
  28. package/dist/OperationDefinition.d.cts.map +1 -0
  29. package/dist/OperationDefinition.d.mts +12 -0
  30. package/dist/OperationDefinition.d.mts.map +1 -0
  31. package/dist/OperationDefinition.mjs +13 -0
  32. package/dist/OperationDefinition.mjs.map +1 -0
  33. package/dist/OperationPath.cjs +148 -0
  34. package/dist/OperationPath.d.cts +60 -0
  35. package/dist/OperationPath.d.cts.map +1 -0
  36. package/dist/OperationPath.d.mts +60 -0
  37. package/dist/OperationPath.d.mts.map +1 -0
  38. package/dist/OperationPath.mjs +138 -0
  39. package/dist/OperationPath.mjs.map +1 -0
  40. package/dist/{Presence-gWrmGBeu.cjs → Presence.cjs} +4 -39
  41. package/dist/{Presence-N8u7Eppr.d.mts → Presence.d.cts} +2 -2
  42. package/dist/Presence.d.cts.map +1 -0
  43. package/dist/{Presence-DKKP4v5X.d.cts → Presence.d.mts} +2 -2
  44. package/dist/Presence.d.mts.map +1 -0
  45. package/dist/{Presence-DdMVKcOv.mjs → Presence.mjs} +3 -28
  46. package/dist/Presence.mjs.map +1 -0
  47. package/dist/Primitive.cjs +52 -0
  48. package/dist/Primitive.d.cts +20 -0
  49. package/dist/Primitive.d.cts.map +1 -0
  50. package/dist/Primitive.d.mts +20 -0
  51. package/dist/Primitive.d.mts.map +1 -0
  52. package/dist/Primitive.mjs +48 -0
  53. package/dist/Primitive.mjs.map +1 -0
  54. package/dist/ProxyEnvironment.cjs +34 -0
  55. package/dist/ProxyEnvironment.d.cts +31 -0
  56. package/dist/ProxyEnvironment.d.cts.map +1 -0
  57. package/dist/ProxyEnvironment.d.mts +31 -0
  58. package/dist/ProxyEnvironment.d.mts.map +1 -0
  59. package/dist/ProxyEnvironment.mjs +29 -0
  60. package/dist/ProxyEnvironment.mjs.map +1 -0
  61. package/dist/Transaction.cjs +66 -0
  62. package/dist/Transaction.d.cts +56 -0
  63. package/dist/Transaction.d.cts.map +1 -0
  64. package/dist/Transaction.d.mts +56 -0
  65. package/dist/Transaction.d.mts.map +1 -0
  66. package/dist/Transaction.mjs +58 -0
  67. package/dist/Transaction.mjs.map +1 -0
  68. package/dist/Transform.cjs +11 -0
  69. package/dist/Transform.d.cts +21 -0
  70. package/dist/Transform.d.cts.map +1 -0
  71. package/dist/Transform.d.mts +21 -0
  72. package/dist/Transform.d.mts.map +1 -0
  73. package/dist/Transform.mjs +6 -0
  74. package/dist/Transform.mjs.map +1 -0
  75. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs +14 -0
  76. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs +14 -0
  77. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs +27 -0
  78. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs +27 -0
  79. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.cjs +16 -0
  80. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.mjs +16 -0
  81. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.cjs +11 -0
  82. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.mjs +11 -0
  83. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.cjs +18 -0
  84. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.mjs +12 -0
  85. package/dist/_virtual/rolldown_runtime.cjs +43 -0
  86. package/dist/{chunk-CLMFDpHK.mjs → _virtual/rolldown_runtime.mjs} +1 -1
  87. package/dist/client/ClientDocument.cjs +590 -0
  88. package/dist/client/ClientDocument.d.cts +161 -0
  89. package/dist/client/ClientDocument.d.cts.map +1 -0
  90. package/dist/client/ClientDocument.d.mts +162 -0
  91. package/dist/client/ClientDocument.d.mts.map +1 -0
  92. package/dist/client/ClientDocument.mjs +586 -0
  93. package/dist/client/ClientDocument.mjs.map +1 -0
  94. package/dist/client/Rebase.cjs +204 -0
  95. package/dist/client/Rebase.d.cts +87 -0
  96. package/dist/client/Rebase.d.cts.map +1 -0
  97. package/dist/client/Rebase.d.mts +88 -0
  98. package/dist/client/Rebase.d.mts.map +1 -0
  99. package/dist/client/Rebase.mjs +198 -0
  100. package/dist/client/Rebase.mjs.map +1 -0
  101. package/dist/client/StateMonitor.cjs +133 -0
  102. package/dist/client/StateMonitor.d.cts +122 -0
  103. package/dist/client/StateMonitor.d.cts.map +1 -0
  104. package/dist/client/StateMonitor.d.mts +122 -0
  105. package/dist/client/StateMonitor.d.mts.map +1 -0
  106. package/dist/client/StateMonitor.mjs +129 -0
  107. package/dist/client/StateMonitor.mjs.map +1 -0
  108. package/dist/client/Transport.cjs +11 -0
  109. package/dist/client/Transport.d.cts +237 -0
  110. package/dist/client/Transport.d.cts.map +1 -0
  111. package/dist/client/Transport.d.mts +237 -0
  112. package/dist/client/Transport.d.mts.map +1 -0
  113. package/dist/client/Transport.mjs +6 -0
  114. package/dist/client/Transport.mjs.map +1 -0
  115. package/dist/client/WebSocketTransport.cjs +396 -0
  116. package/dist/client/WebSocketTransport.d.cts +29 -0
  117. package/dist/client/WebSocketTransport.d.cts.map +1 -0
  118. package/dist/client/WebSocketTransport.d.mts +29 -0
  119. package/dist/client/WebSocketTransport.d.mts.map +1 -0
  120. package/dist/client/WebSocketTransport.mjs +392 -0
  121. package/dist/client/WebSocketTransport.mjs.map +1 -0
  122. package/dist/client/errors.cjs +135 -0
  123. package/dist/client/errors.d.cts +87 -0
  124. package/dist/client/errors.d.cts.map +1 -0
  125. package/dist/client/errors.d.mts +87 -0
  126. package/dist/client/errors.d.mts.map +1 -0
  127. package/dist/client/errors.mjs +127 -0
  128. package/dist/client/errors.mjs.map +1 -0
  129. package/dist/client/index.cjs +22 -1424
  130. package/dist/client/index.d.cts +8 -692
  131. package/dist/client/index.d.mts +8 -692
  132. package/dist/client/index.mjs +9 -1413
  133. package/dist/index.cjs +20 -2973
  134. package/dist/index.d.cts +12 -419
  135. package/dist/index.d.mts +12 -419
  136. package/dist/index.mjs +13 -2968
  137. package/dist/primitives/Array.cjs +302 -0
  138. package/dist/primitives/Array.d.cts +95 -0
  139. package/dist/primitives/Array.d.cts.map +1 -0
  140. package/dist/primitives/Array.d.mts +95 -0
  141. package/dist/primitives/Array.d.mts.map +1 -0
  142. package/dist/primitives/Array.mjs +301 -0
  143. package/dist/primitives/Array.mjs.map +1 -0
  144. package/dist/primitives/Boolean.cjs +95 -0
  145. package/dist/primitives/Boolean.d.cts +44 -0
  146. package/dist/primitives/Boolean.d.cts.map +1 -0
  147. package/dist/primitives/Boolean.d.mts +44 -0
  148. package/dist/primitives/Boolean.d.mts.map +1 -0
  149. package/dist/primitives/Boolean.mjs +94 -0
  150. package/dist/primitives/Boolean.mjs.map +1 -0
  151. package/dist/primitives/Either.cjs +200 -0
  152. package/dist/primitives/Either.d.cts +113 -0
  153. package/dist/primitives/Either.d.cts.map +1 -0
  154. package/dist/primitives/Either.d.mts +113 -0
  155. package/dist/primitives/Either.d.mts.map +1 -0
  156. package/dist/primitives/Either.mjs +199 -0
  157. package/dist/primitives/Either.mjs.map +1 -0
  158. package/dist/primitives/Lazy.cjs +46 -0
  159. package/dist/primitives/Lazy.d.cts +46 -0
  160. package/dist/primitives/Lazy.d.cts.map +1 -0
  161. package/dist/primitives/Lazy.d.mts +46 -0
  162. package/dist/primitives/Lazy.d.mts.map +1 -0
  163. package/dist/primitives/Lazy.mjs +46 -0
  164. package/dist/primitives/Lazy.mjs.map +1 -0
  165. package/dist/primitives/Literal.cjs +91 -0
  166. package/dist/primitives/Literal.d.cts +46 -0
  167. package/dist/primitives/Literal.d.cts.map +1 -0
  168. package/dist/primitives/Literal.d.mts +46 -0
  169. package/dist/primitives/Literal.d.mts.map +1 -0
  170. package/dist/primitives/Literal.mjs +90 -0
  171. package/dist/primitives/Literal.mjs.map +1 -0
  172. package/dist/primitives/Number.cjs +115 -0
  173. package/dist/primitives/Number.d.cts +54 -0
  174. package/dist/primitives/Number.d.cts.map +1 -0
  175. package/dist/primitives/Number.d.mts +54 -0
  176. package/dist/primitives/Number.d.mts.map +1 -0
  177. package/dist/primitives/Number.mjs +114 -0
  178. package/dist/primitives/Number.mjs.map +1 -0
  179. package/dist/primitives/String.cjs +127 -0
  180. package/dist/primitives/String.d.cts +56 -0
  181. package/dist/primitives/String.d.cts.map +1 -0
  182. package/dist/primitives/String.d.mts +56 -0
  183. package/dist/primitives/String.d.mts.map +1 -0
  184. package/dist/primitives/String.mjs +126 -0
  185. package/dist/primitives/String.mjs.map +1 -0
  186. package/dist/primitives/Struct.cjs +207 -0
  187. package/dist/primitives/Struct.d.cts +96 -0
  188. package/dist/primitives/Struct.d.cts.map +1 -0
  189. package/dist/primitives/Struct.d.mts +97 -0
  190. package/dist/primitives/Struct.d.mts.map +1 -0
  191. package/dist/primitives/Struct.mjs +206 -0
  192. package/dist/primitives/Struct.mjs.map +1 -0
  193. package/dist/primitives/Tree.cjs +575 -0
  194. package/dist/primitives/Tree.d.cts +185 -0
  195. package/dist/primitives/Tree.d.cts.map +1 -0
  196. package/dist/primitives/Tree.d.mts +185 -0
  197. package/dist/primitives/Tree.d.mts.map +1 -0
  198. package/dist/primitives/Tree.mjs +574 -0
  199. package/dist/primitives/Tree.mjs.map +1 -0
  200. package/dist/primitives/TreeNode.cjs +73 -0
  201. package/dist/primitives/TreeNode.d.cts +92 -0
  202. package/dist/primitives/TreeNode.d.cts.map +1 -0
  203. package/dist/primitives/TreeNode.d.mts +93 -0
  204. package/dist/primitives/TreeNode.d.mts.map +1 -0
  205. package/dist/primitives/TreeNode.mjs +72 -0
  206. package/dist/primitives/TreeNode.mjs.map +1 -0
  207. package/dist/primitives/Union.cjs +170 -0
  208. package/dist/primitives/Union.d.cts +81 -0
  209. package/dist/primitives/Union.d.cts.map +1 -0
  210. package/dist/primitives/Union.d.mts +81 -0
  211. package/dist/primitives/Union.d.mts.map +1 -0
  212. package/dist/primitives/Union.mjs +169 -0
  213. package/dist/primitives/Union.mjs.map +1 -0
  214. package/dist/primitives/shared.cjs +60 -0
  215. package/dist/primitives/shared.d.cts +147 -0
  216. package/dist/primitives/shared.d.cts.map +1 -0
  217. package/dist/primitives/shared.d.mts +147 -0
  218. package/dist/primitives/shared.d.mts.map +1 -0
  219. package/dist/primitives/shared.mjs +58 -0
  220. package/dist/primitives/shared.mjs.map +1 -0
  221. package/dist/server/ServerDocument.cjs +110 -0
  222. package/dist/server/ServerDocument.d.cts +98 -0
  223. package/dist/server/ServerDocument.d.cts.map +1 -0
  224. package/dist/server/ServerDocument.d.mts +99 -0
  225. package/dist/server/ServerDocument.d.mts.map +1 -0
  226. package/dist/server/ServerDocument.mjs +106 -0
  227. package/dist/server/ServerDocument.mjs.map +1 -0
  228. package/dist/server/errors.cjs +85 -0
  229. package/dist/server/errors.d.cts +53 -0
  230. package/dist/server/errors.d.cts.map +1 -0
  231. package/dist/server/errors.d.mts +53 -0
  232. package/dist/server/errors.d.mts.map +1 -0
  233. package/dist/server/errors.mjs +81 -0
  234. package/dist/server/errors.mjs.map +1 -0
  235. package/dist/server/index.cjs +9 -185
  236. package/dist/server/index.d.cts +3 -148
  237. package/dist/server/index.d.mts +3 -148
  238. package/dist/server/index.mjs +3 -181
  239. package/dist/types/index.cjs +16 -0
  240. package/dist/types/index.d.cts +16 -0
  241. package/dist/types/index.d.cts.map +1 -0
  242. package/dist/types/index.d.mts +16 -0
  243. package/dist/types/index.d.mts.map +1 -0
  244. package/dist/types/index.mjs +12 -0
  245. package/dist/types/index.mjs.map +1 -0
  246. package/dist/utils/tree-helpers.cjs +443 -0
  247. package/dist/utils/tree-helpers.d.cts +280 -0
  248. package/dist/utils/tree-helpers.d.cts.map +1 -0
  249. package/dist/utils/tree-helpers.d.mts +280 -0
  250. package/dist/utils/tree-helpers.d.mts.map +1 -0
  251. package/dist/utils/tree-helpers.mjs +439 -0
  252. package/dist/utils/tree-helpers.mjs.map +1 -0
  253. package/package.json +2 -2
  254. package/tsdown.config.ts +1 -1
  255. package/dist/Document-ChuFrTk1.cjs +0 -571
  256. package/dist/Document-CwiAFTIq.mjs +0 -438
  257. package/dist/Document-CwiAFTIq.mjs.map +0 -1
  258. package/dist/Presence-DKKP4v5X.d.cts.map +0 -1
  259. package/dist/Presence-DdMVKcOv.mjs.map +0 -1
  260. package/dist/Presence-N8u7Eppr.d.mts.map +0 -1
  261. package/dist/Primitive-DqQFc3Gu.d.mts +0 -1180
  262. package/dist/Primitive-DqQFc3Gu.d.mts.map +0 -1
  263. package/dist/Primitive-awpEjnKL.d.cts +0 -1180
  264. package/dist/Primitive-awpEjnKL.d.cts.map +0 -1
  265. package/dist/client/index.d.cts.map +0 -1
  266. package/dist/client/index.d.mts.map +0 -1
  267. package/dist/client/index.mjs.map +0 -1
  268. package/dist/index.d.cts.map +0 -1
  269. package/dist/index.d.mts.map +0 -1
  270. package/dist/index.mjs.map +0 -1
  271. package/dist/server/index.d.cts.map +0 -1
  272. package/dist/server/index.d.mts.map +0 -1
  273. package/dist/server/index.mjs.map +0 -1
@@ -0,0 +1,439 @@
1
+ import { __export } from "../_virtual/rolldown_runtime.mjs";
2
+
3
+ //#region src/utils/tree-helpers.ts
4
+ var tree_helpers_exports = /* @__PURE__ */ __export({
5
+ buildNodeTypeMap: () => buildNodeTypeMap,
6
+ canAddChildType: () => canAddChildType,
7
+ findNode: () => findNode,
8
+ findNodeById: () => findNodeById,
9
+ findNodeWithPath: () => findNodeWithPath,
10
+ findNodes: () => findNodes,
11
+ flattenTree: () => flattenTree,
12
+ getAllNodeTypes: () => getAllNodeTypes,
13
+ getAllowedChildTypes: () => getAllowedChildTypes,
14
+ getAllowedChildTypesForNode: () => getAllowedChildTypesForNode,
15
+ getAncestors: () => getAncestors,
16
+ getDescendants: () => getDescendants,
17
+ getNodeDepth: () => getNodeDepth,
18
+ getNodePrimitive: () => getNodePrimitive,
19
+ getNodeTypeByName: () => getNodeTypeByName,
20
+ getParent: () => getParent,
21
+ getSiblings: () => getSiblings,
22
+ getSubtree: () => getSubtree,
23
+ isAncestorOf: () => isAncestorOf,
24
+ isChildTypeAllowed: () => isChildTypeAllowed,
25
+ mapTree: () => mapTree,
26
+ traverse: () => traverse
27
+ });
28
+ /**
29
+ * Find a node by ID in a tree snapshot.
30
+ *
31
+ * @param snapshot - The root tree snapshot
32
+ * @param id - The node ID to find
33
+ * @returns The node snapshot if found, undefined otherwise
34
+ */
35
+ function findNodeById(snapshot, id) {
36
+ if (!snapshot) return void 0;
37
+ if (snapshot.id === id) return snapshot;
38
+ for (const child of snapshot.children) {
39
+ const found = findNodeById(child, id);
40
+ if (found) return found;
41
+ }
42
+ }
43
+ /**
44
+ * Find a node by ID with full path information.
45
+ *
46
+ * @param snapshot - The root tree snapshot
47
+ * @param id - The node ID to find
48
+ * @returns NodeSearchResult with node, path, and depth, or undefined if not found
49
+ */
50
+ function findNodeWithPath(snapshot, id) {
51
+ if (!snapshot) return void 0;
52
+ const search = (node, currentPath, depth) => {
53
+ const path = [...currentPath, node.id];
54
+ if (node.id === id) return {
55
+ node,
56
+ path,
57
+ depth
58
+ };
59
+ for (const child of node.children) {
60
+ const result = search(child, path, depth + 1);
61
+ if (result) return result;
62
+ }
63
+ };
64
+ return search(snapshot, [], 0);
65
+ }
66
+ /**
67
+ * Get the parent node of a given node ID.
68
+ *
69
+ * @param snapshot - The root tree snapshot
70
+ * @param nodeId - The ID of the node whose parent we want
71
+ * @returns ParentSearchResult with parent node and child index, or undefined
72
+ */
73
+ function getParent(snapshot, nodeId) {
74
+ if (!snapshot) return void 0;
75
+ for (let i = 0; i < snapshot.children.length; i++) {
76
+ const child = snapshot.children[i];
77
+ if (child && child.id === nodeId) return {
78
+ parent: snapshot,
79
+ childIndex: i
80
+ };
81
+ }
82
+ for (const child of snapshot.children) {
83
+ const result = getParent(child, nodeId);
84
+ if (result) return result;
85
+ }
86
+ }
87
+ /**
88
+ * Get a subtree rooted at a specific node ID.
89
+ *
90
+ * @param snapshot - The root tree snapshot
91
+ * @param nodeId - The ID of the node to use as new root
92
+ * @returns The subtree snapshot, or undefined if node not found
93
+ */
94
+ function getSubtree(snapshot, nodeId) {
95
+ return findNodeById(snapshot, nodeId);
96
+ }
97
+ /**
98
+ * Get all ancestor nodes from a node up to the root.
99
+ *
100
+ * @param snapshot - The root tree snapshot
101
+ * @param nodeId - The ID of the node
102
+ * @returns Array of ancestor snapshots from immediate parent to root, or empty array
103
+ */
104
+ function getAncestors(snapshot, nodeId) {
105
+ if (!snapshot) return [];
106
+ const result = findNodeWithPath(snapshot, nodeId);
107
+ if (!result || result.path.length <= 1) return [];
108
+ const ancestorIds = result.path.slice(0, -1);
109
+ const ancestors = [];
110
+ for (let i = ancestorIds.length - 1; i >= 0; i--) {
111
+ const id = ancestorIds[i];
112
+ if (id) {
113
+ const ancestor = findNodeById(snapshot, id);
114
+ if (ancestor) ancestors.push(ancestor);
115
+ }
116
+ }
117
+ return ancestors;
118
+ }
119
+ /**
120
+ * Get all descendant nodes of a given node (flat array).
121
+ *
122
+ * @param snapshot - The root tree snapshot
123
+ * @param nodeId - The ID of the node (if undefined, returns all descendants of root)
124
+ * @returns Flat array of all descendant node snapshots
125
+ */
126
+ function getDescendants(snapshot, nodeId) {
127
+ if (!snapshot) return [];
128
+ const startNode = nodeId ? findNodeById(snapshot, nodeId) : snapshot;
129
+ if (!startNode) return [];
130
+ const descendants = [];
131
+ const collect = (node) => {
132
+ for (const child of node.children) {
133
+ descendants.push(child);
134
+ collect(child);
135
+ }
136
+ };
137
+ collect(startNode);
138
+ return descendants;
139
+ }
140
+ /**
141
+ * Get siblings of a node (nodes with the same parent).
142
+ *
143
+ * @param snapshot - The root tree snapshot
144
+ * @param nodeId - The ID of the node
145
+ * @param includeSelf - Whether to include the node itself (default: false)
146
+ * @returns Array of sibling snapshots
147
+ */
148
+ function getSiblings(snapshot, nodeId, includeSelf = false) {
149
+ if (!snapshot) return [];
150
+ const parentResult = getParent(snapshot, nodeId);
151
+ if (!parentResult) return [];
152
+ const siblings = parentResult.parent.children;
153
+ if (includeSelf) return siblings;
154
+ return siblings.filter((s) => s.id !== nodeId);
155
+ }
156
+ /**
157
+ * Find the first node matching a predicate.
158
+ *
159
+ * @param snapshot - The root tree snapshot
160
+ * @param predicate - Function to test each node
161
+ * @returns First matching node snapshot, or undefined
162
+ */
163
+ function findNode(snapshot, predicate) {
164
+ if (!snapshot) return void 0;
165
+ if (predicate(snapshot)) return snapshot;
166
+ for (const child of snapshot.children) {
167
+ const found = findNode(child, predicate);
168
+ if (found) return found;
169
+ }
170
+ }
171
+ /**
172
+ * Find all nodes matching a predicate.
173
+ *
174
+ * @param snapshot - The root tree snapshot
175
+ * @param predicate - Function to test each node
176
+ * @returns Array of matching node snapshots
177
+ */
178
+ function findNodes(snapshot, predicate) {
179
+ if (!snapshot) return [];
180
+ const results = [];
181
+ const search = (node) => {
182
+ if (predicate(node)) results.push(node);
183
+ for (const child of node.children) search(child);
184
+ };
185
+ search(snapshot);
186
+ return results;
187
+ }
188
+ /**
189
+ * Get the depth of a specific node (0 = root).
190
+ *
191
+ * @param snapshot - The root tree snapshot
192
+ * @param nodeId - The ID of the node
193
+ * @returns The depth, or -1 if not found
194
+ */
195
+ function getNodeDepth(snapshot, nodeId) {
196
+ if (!snapshot) return -1;
197
+ const result = findNodeWithPath(snapshot, nodeId);
198
+ return result ? result.depth : -1;
199
+ }
200
+ /**
201
+ * Check if one node is an ancestor of another.
202
+ *
203
+ * @param snapshot - The root tree snapshot
204
+ * @param ancestorId - Potential ancestor node ID
205
+ * @param descendantId - Potential descendant node ID
206
+ * @returns true if ancestorId is an ancestor of descendantId
207
+ */
208
+ function isAncestorOf(snapshot, ancestorId, descendantId) {
209
+ if (!snapshot) return false;
210
+ const result = findNodeWithPath(snapshot, descendantId);
211
+ if (!result) return false;
212
+ return result.path.slice(0, -1).includes(ancestorId);
213
+ }
214
+ /**
215
+ * Traverse the tree and call a visitor function for each node.
216
+ * Return false from visitor to stop traversal.
217
+ *
218
+ * @param snapshot - The root tree snapshot
219
+ * @param visitor - Function called for each node (return false to stop)
220
+ * @param options - Traversal options (order: 'pre' | 'post')
221
+ */
222
+ function traverse(snapshot, visitor, options = {}) {
223
+ if (!snapshot) return;
224
+ const { order = "pre" } = options;
225
+ const visit = (node, depth) => {
226
+ if (order === "pre") {
227
+ if (visitor(node, depth) === false) return false;
228
+ }
229
+ for (const child of node.children) if (!visit(child, depth + 1)) return false;
230
+ if (order === "post") {
231
+ if (visitor(node, depth) === false) return false;
232
+ }
233
+ return true;
234
+ };
235
+ visit(snapshot, 0);
236
+ }
237
+ /**
238
+ * Flatten the tree into an array with parent information.
239
+ *
240
+ * @param snapshot - The root tree snapshot
241
+ * @returns Array of { node, parentId, depth } objects
242
+ */
243
+ function flattenTree(snapshot) {
244
+ if (!snapshot) return [];
245
+ const result = [];
246
+ const flatten = (node, parentId, depth) => {
247
+ result.push({
248
+ node,
249
+ parentId,
250
+ depth
251
+ });
252
+ for (const child of node.children) flatten(child, node.id, depth + 1);
253
+ };
254
+ flatten(snapshot, null, 0);
255
+ return result;
256
+ }
257
+ /**
258
+ * Map over all nodes in the tree, transforming each node's data.
259
+ * Preserves tree structure while transforming node content.
260
+ *
261
+ * @param snapshot - The root tree snapshot
262
+ * @param mapper - Function to transform each node
263
+ * @returns New tree structure with transformed nodes, or undefined
264
+ */
265
+ function mapTree(snapshot, mapper) {
266
+ if (!snapshot) return void 0;
267
+ const map = (node, depth) => {
268
+ return {
269
+ value: mapper(node, depth),
270
+ children: node.children.map((child) => map(child, depth + 1))
271
+ };
272
+ };
273
+ return map(snapshot, 0);
274
+ }
275
+ /**
276
+ * Build a lookup map from node type strings to their TreeNodePrimitive definitions.
277
+ * Useful for resolving snapshot node types back to their schema definitions.
278
+ *
279
+ * @param tree - The TreePrimitive to analyze
280
+ * @returns Map from type string to TreeNodePrimitive
281
+ *
282
+ * @example
283
+ * ```ts
284
+ * const typeMap = buildNodeTypeMap(fileTree);
285
+ * const folderPrimitive = typeMap.get("folder"); // FolderNode
286
+ * ```
287
+ */
288
+ function buildNodeTypeMap(tree) {
289
+ const map = /* @__PURE__ */ new Map();
290
+ const visited = /* @__PURE__ */ new Set();
291
+ const visit = (node) => {
292
+ if (visited.has(node.type)) return;
293
+ visited.add(node.type);
294
+ map.set(node.type, node);
295
+ for (const child of node.children) visit(child);
296
+ };
297
+ visit(tree.root);
298
+ return map;
299
+ }
300
+ /**
301
+ * Get the TreeNodePrimitive definition for a snapshot node.
302
+ * Requires the tree schema to resolve the type string to its primitive.
303
+ *
304
+ * @param tree - The TreePrimitive schema
305
+ * @param snapshot - The node snapshot to get the primitive for
306
+ * @returns The TreeNodePrimitive, or undefined if not found
307
+ *
308
+ * @example
309
+ * ```ts
310
+ * const node = findNodeById(treeSnapshot, "some-id");
311
+ * const primitive = getNodePrimitive(fileTree, node);
312
+ * // primitive is FolderNode or FileNode depending on node.type
313
+ * ```
314
+ */
315
+ function getNodePrimitive(tree, snapshot) {
316
+ return getNodeTypeByName(tree, snapshot.type);
317
+ }
318
+ /**
319
+ * Get the allowed child types for a snapshot node.
320
+ * Combines schema lookup with the node's allowed children.
321
+ *
322
+ * @param tree - The TreePrimitive schema
323
+ * @param snapshot - The node snapshot to get allowed children for
324
+ * @returns Array of allowed child TreeNodePrimitives, or empty array if not found
325
+ *
326
+ * @example
327
+ * ```ts
328
+ * const node = findNodeById(treeSnapshot, "folder-id");
329
+ * const allowedChildren = getAllowedChildTypesForNode(fileTree, node);
330
+ * // Returns [FolderNode, FileNode] if the folder can contain both
331
+ * ```
332
+ */
333
+ function getAllowedChildTypesForNode(tree, snapshot) {
334
+ const primitive = getNodePrimitive(tree, snapshot);
335
+ if (!primitive) return [];
336
+ return primitive.children;
337
+ }
338
+ /**
339
+ * Check if a child type is allowed for a specific snapshot node.
340
+ *
341
+ * @param tree - The TreePrimitive schema
342
+ * @param parentSnapshot - The parent node snapshot
343
+ * @param childTypeName - The type string of the potential child
344
+ * @returns true if the child type is allowed
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * const folder = findNodeById(treeSnapshot, "folder-id");
349
+ * canAddChildType(fileTree, folder, "file"); // true
350
+ * canAddChildType(fileTree, folder, "unknown"); // false
351
+ * ```
352
+ */
353
+ function canAddChildType(tree, parentSnapshot, childTypeName) {
354
+ const primitive = getNodePrimitive(tree, parentSnapshot);
355
+ if (!primitive) return false;
356
+ return primitive.isChildAllowed(childTypeName);
357
+ }
358
+ /**
359
+ * Get all allowed child node types for a specific node type.
360
+ *
361
+ * @param nodeType - The TreeNodePrimitive to get children for
362
+ * @returns Array of allowed child TreeNodePrimitives
363
+ *
364
+ * @example
365
+ * ```ts
366
+ * const FolderNode = TreeNode("folder", { ... });
367
+ * const FileNode = TreeNode("file", { ... });
368
+ *
369
+ * // Get allowed children for FolderNode
370
+ * const allowedChildren = getAllowedChildTypes(FolderNode);
371
+ * // Returns [FolderNode, FileNode] if folder can contain both
372
+ * ```
373
+ */
374
+ function getAllowedChildTypes(nodeType) {
375
+ return nodeType.children;
376
+ }
377
+ /**
378
+ * Get all unique node types reachable in a tree schema.
379
+ * Recursively traverses the tree structure starting from the root.
380
+ *
381
+ * @param tree - The TreePrimitive to analyze
382
+ * @returns Array of all unique TreeNodePrimitives in the tree schema
383
+ *
384
+ * @example
385
+ * ```ts
386
+ * const fileTree = Tree({ root: FolderNode });
387
+ * const allNodeTypes = getAllNodeTypes(fileTree);
388
+ * // Returns [FolderNode, FileNode] - all possible node types
389
+ * ```
390
+ */
391
+ function getAllNodeTypes(tree) {
392
+ const visited = /* @__PURE__ */ new Set();
393
+ const result = [];
394
+ const visit = (node) => {
395
+ if (visited.has(node.type)) return;
396
+ visited.add(node.type);
397
+ result.push(node);
398
+ for (const child of node.children) visit(child);
399
+ };
400
+ visit(tree.root);
401
+ return result;
402
+ }
403
+ /**
404
+ * Get the node type primitive by its type string from a tree schema.
405
+ *
406
+ * @param tree - The TreePrimitive to search
407
+ * @param typeName - The type string to find (e.g., "folder", "file")
408
+ * @returns The matching TreeNodePrimitive, or undefined if not found
409
+ *
410
+ * @example
411
+ * ```ts
412
+ * const fileTree = Tree({ root: FolderNode });
413
+ * const folderType = getNodeTypeByName(fileTree, "folder");
414
+ * // Returns FolderNode
415
+ * ```
416
+ */
417
+ function getNodeTypeByName(tree, typeName) {
418
+ return getAllNodeTypes(tree).find((node) => node.type === typeName);
419
+ }
420
+ /**
421
+ * Check if a child type is allowed under a parent type.
422
+ *
423
+ * @param parentType - The parent TreeNodePrimitive
424
+ * @param childTypeName - The type string of the potential child
425
+ * @returns true if the child type is allowed
426
+ *
427
+ * @example
428
+ * ```ts
429
+ * isChildTypeAllowed(FolderNode, "file"); // true if folder can contain files
430
+ * isChildTypeAllowed(FileNode, "folder"); // false - files can't have children
431
+ * ```
432
+ */
433
+ function isChildTypeAllowed(parentType, childTypeName) {
434
+ return parentType.isChildAllowed(childTypeName);
435
+ }
436
+
437
+ //#endregion
438
+ export { tree_helpers_exports };
439
+ //# sourceMappingURL=tree-helpers.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tree-helpers.mjs","names":["ancestors: TreeNodeSnapshot<TNode>[]","descendants: TreeNodeSnapshot<TNode>[]","results: TreeNodeSnapshot<TNode>[]","result: FlattenedNode<TNode>[]","result: AnyTreeNodePrimitive[]"],"sources":["../../src/utils/tree-helpers.ts"],"sourcesContent":["/**\n * Tree snapshot utility functions.\n *\n * Standalone helpers for traversing and querying tree snapshots.\n * All functions handle undefined snapshots gracefully.\n *\n * @since 0.0.1\n */\n\nimport type { TreeNodeSnapshot, TreePrimitive } from \"../primitives/Tree.js\";\nimport type { AnyTreeNodePrimitive } from \"../primitives/TreeNode.js\";\n\n// =============================================================================\n// Result Types\n// =============================================================================\n\n/**\n * Result of finding a node with path information.\n */\nexport interface NodeSearchResult<TNode extends AnyTreeNodePrimitive> {\n readonly node: TreeNodeSnapshot<TNode>;\n readonly path: readonly string[]; // Array of node IDs from root to this node\n readonly depth: number;\n}\n\n/**\n * Result of finding a parent node.\n */\nexport interface ParentSearchResult<TNode extends AnyTreeNodePrimitive> {\n readonly parent: TreeNodeSnapshot<TNode>;\n readonly childIndex: number; // Index of the target node in parent's children\n}\n\n/**\n * Flattened node with parent information.\n */\nexport interface FlattenedNode<TNode extends AnyTreeNodePrimitive> {\n readonly node: TreeNodeSnapshot<TNode>;\n readonly parentId: string | null;\n readonly depth: number;\n}\n\n/**\n * Options for tree traversal.\n */\nexport interface TraverseOptions {\n readonly order?: \"pre\" | \"post\";\n}\n\n// =============================================================================\n// Core Functions\n// =============================================================================\n\n/**\n * Find a node by ID in a tree snapshot.\n *\n * @param snapshot - The root tree snapshot\n * @param id - The node ID to find\n * @returns The node snapshot if found, undefined otherwise\n */\nexport function findNodeById<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n id: string\n): TreeNodeSnapshot<TNode> | undefined {\n if (!snapshot) return undefined;\n if (snapshot.id === id) return snapshot;\n\n for (const child of snapshot.children) {\n const found = findNodeById(child as TreeNodeSnapshot<TNode>, id);\n if (found) return found;\n }\n\n return undefined;\n}\n\n/**\n * Find a node by ID with full path information.\n *\n * @param snapshot - The root tree snapshot\n * @param id - The node ID to find\n * @returns NodeSearchResult with node, path, and depth, or undefined if not found\n */\nexport function findNodeWithPath<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n id: string\n): NodeSearchResult<TNode> | undefined {\n if (!snapshot) return undefined;\n\n const search = (\n node: TreeNodeSnapshot<TNode>,\n currentPath: string[],\n depth: number\n ): NodeSearchResult<TNode> | undefined => {\n const path = [...currentPath, node.id];\n\n if (node.id === id) {\n return { node, path, depth };\n }\n\n for (const child of node.children) {\n const result = search(child as TreeNodeSnapshot<TNode>, path, depth + 1);\n if (result) return result;\n }\n\n return undefined;\n };\n\n return search(snapshot, [], 0);\n}\n\n/**\n * Get the parent node of a given node ID.\n *\n * @param snapshot - The root tree snapshot\n * @param nodeId - The ID of the node whose parent we want\n * @returns ParentSearchResult with parent node and child index, or undefined\n */\nexport function getParent<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n nodeId: string\n): ParentSearchResult<TNode> | undefined {\n if (!snapshot) return undefined;\n\n // Check if any direct child is the target\n for (let i = 0; i < snapshot.children.length; i++) {\n const child = snapshot.children[i];\n if (child && child.id === nodeId) {\n return {\n parent: snapshot,\n childIndex: i,\n };\n }\n }\n\n // Recursively search children\n for (const child of snapshot.children) {\n const result = getParent(child as TreeNodeSnapshot<TNode>, nodeId);\n if (result) return result;\n }\n\n return undefined;\n}\n\n/**\n * Get a subtree rooted at a specific node ID.\n *\n * @param snapshot - The root tree snapshot\n * @param nodeId - The ID of the node to use as new root\n * @returns The subtree snapshot, or undefined if node not found\n */\nexport function getSubtree<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n nodeId: string\n): TreeNodeSnapshot<TNode> | undefined {\n return findNodeById(snapshot, nodeId);\n}\n\n/**\n * Get all ancestor nodes from a node up to the root.\n *\n * @param snapshot - The root tree snapshot\n * @param nodeId - The ID of the node\n * @returns Array of ancestor snapshots from immediate parent to root, or empty array\n */\nexport function getAncestors<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n nodeId: string\n): readonly TreeNodeSnapshot<TNode>[] {\n if (!snapshot) return [];\n\n const result = findNodeWithPath(snapshot, nodeId);\n if (!result || result.path.length <= 1) return [];\n\n // Path includes the node itself, so we need all nodes except the last one\n const ancestorIds = result.path.slice(0, -1);\n const ancestors: TreeNodeSnapshot<TNode>[] = [];\n\n // Collect ancestors in reverse order (parent first, root last)\n for (let i = ancestorIds.length - 1; i >= 0; i--) {\n const id = ancestorIds[i];\n if (id) {\n const ancestor = findNodeById(snapshot, id);\n if (ancestor) {\n ancestors.push(ancestor);\n }\n }\n }\n\n return ancestors;\n}\n\n/**\n * Get all descendant nodes of a given node (flat array).\n *\n * @param snapshot - The root tree snapshot\n * @param nodeId - The ID of the node (if undefined, returns all descendants of root)\n * @returns Flat array of all descendant node snapshots\n */\nexport function getDescendants<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n nodeId?: string\n): readonly TreeNodeSnapshot<TNode>[] {\n if (!snapshot) return [];\n\n const startNode = nodeId ? findNodeById(snapshot, nodeId) : snapshot;\n if (!startNode) return [];\n\n const descendants: TreeNodeSnapshot<TNode>[] = [];\n\n const collect = (node: TreeNodeSnapshot<TNode>) => {\n for (const child of node.children) {\n descendants.push(child as TreeNodeSnapshot<TNode>);\n collect(child as TreeNodeSnapshot<TNode>);\n }\n };\n\n collect(startNode);\n return descendants;\n}\n\n/**\n * Get siblings of a node (nodes with the same parent).\n *\n * @param snapshot - The root tree snapshot\n * @param nodeId - The ID of the node\n * @param includeSelf - Whether to include the node itself (default: false)\n * @returns Array of sibling snapshots\n */\nexport function getSiblings<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n nodeId: string,\n includeSelf: boolean = false\n): readonly TreeNodeSnapshot<TNode>[] {\n if (!snapshot) return [];\n\n const parentResult = getParent(snapshot, nodeId);\n\n // If no parent, this is the root - root has no siblings\n if (!parentResult) return [];\n\n const siblings = parentResult.parent.children as readonly TreeNodeSnapshot<TNode>[];\n\n if (includeSelf) {\n return siblings;\n }\n\n return siblings.filter((s) => s.id !== nodeId);\n}\n\n// =============================================================================\n// Query Functions\n// =============================================================================\n\n/**\n * Find the first node matching a predicate.\n *\n * @param snapshot - The root tree snapshot\n * @param predicate - Function to test each node\n * @returns First matching node snapshot, or undefined\n */\nexport function findNode<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n predicate: (node: TreeNodeSnapshot<TNode>) => boolean\n): TreeNodeSnapshot<TNode> | undefined {\n if (!snapshot) return undefined;\n\n if (predicate(snapshot)) return snapshot;\n\n for (const child of snapshot.children) {\n const found = findNode(child as TreeNodeSnapshot<TNode>, predicate);\n if (found) return found;\n }\n\n return undefined;\n}\n\n/**\n * Find all nodes matching a predicate.\n *\n * @param snapshot - The root tree snapshot\n * @param predicate - Function to test each node\n * @returns Array of matching node snapshots\n */\nexport function findNodes<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n predicate: (node: TreeNodeSnapshot<TNode>) => boolean\n): readonly TreeNodeSnapshot<TNode>[] {\n if (!snapshot) return [];\n\n const results: TreeNodeSnapshot<TNode>[] = [];\n\n const search = (node: TreeNodeSnapshot<TNode>) => {\n if (predicate(node)) {\n results.push(node);\n }\n for (const child of node.children) {\n search(child as TreeNodeSnapshot<TNode>);\n }\n };\n\n search(snapshot);\n return results;\n}\n\n/**\n * Get the depth of a specific node (0 = root).\n *\n * @param snapshot - The root tree snapshot\n * @param nodeId - The ID of the node\n * @returns The depth, or -1 if not found\n */\nexport function getNodeDepth<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n nodeId: string\n): number {\n if (!snapshot) return -1;\n\n const result = findNodeWithPath(snapshot, nodeId);\n return result ? result.depth : -1;\n}\n\n/**\n * Check if one node is an ancestor of another.\n *\n * @param snapshot - The root tree snapshot\n * @param ancestorId - Potential ancestor node ID\n * @param descendantId - Potential descendant node ID\n * @returns true if ancestorId is an ancestor of descendantId\n */\nexport function isAncestorOf<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n ancestorId: string,\n descendantId: string\n): boolean {\n if (!snapshot) return false;\n\n const result = findNodeWithPath(snapshot, descendantId);\n if (!result) return false;\n\n // Check if ancestorId is in the path (excluding the descendant itself)\n return result.path.slice(0, -1).includes(ancestorId);\n}\n\n// =============================================================================\n// Traversal Functions\n// =============================================================================\n\n/**\n * Traverse the tree and call a visitor function for each node.\n * Return false from visitor to stop traversal.\n *\n * @param snapshot - The root tree snapshot\n * @param visitor - Function called for each node (return false to stop)\n * @param options - Traversal options (order: 'pre' | 'post')\n */\nexport function traverse<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n visitor: (node: TreeNodeSnapshot<TNode>, depth: number) => void | false,\n options: TraverseOptions = {}\n): void {\n if (!snapshot) return;\n\n const { order = \"pre\" } = options;\n\n const visit = (node: TreeNodeSnapshot<TNode>, depth: number): boolean => {\n if (order === \"pre\") {\n const result = visitor(node, depth);\n if (result === false) return false;\n }\n\n for (const child of node.children) {\n const shouldContinue = visit(child as TreeNodeSnapshot<TNode>, depth + 1);\n if (!shouldContinue) return false;\n }\n\n if (order === \"post\") {\n const result = visitor(node, depth);\n if (result === false) return false;\n }\n\n return true;\n };\n\n visit(snapshot, 0);\n}\n\n/**\n * Flatten the tree into an array with parent information.\n *\n * @param snapshot - The root tree snapshot\n * @returns Array of { node, parentId, depth } objects\n */\nexport function flattenTree<TNode extends AnyTreeNodePrimitive>(\n snapshot: TreeNodeSnapshot<TNode> | undefined\n): readonly FlattenedNode<TNode>[] {\n if (!snapshot) return [];\n\n const result: FlattenedNode<TNode>[] = [];\n\n const flatten = (\n node: TreeNodeSnapshot<TNode>,\n parentId: string | null,\n depth: number\n ) => {\n result.push({ node, parentId, depth });\n\n for (const child of node.children) {\n flatten(child as TreeNodeSnapshot<TNode>, node.id, depth + 1);\n }\n };\n\n flatten(snapshot, null, 0);\n return result;\n}\n\n/**\n * Map over all nodes in the tree, transforming each node's data.\n * Preserves tree structure while transforming node content.\n *\n * @param snapshot - The root tree snapshot\n * @param mapper - Function to transform each node\n * @returns New tree structure with transformed nodes, or undefined\n */\nexport function mapTree<TNode extends AnyTreeNodePrimitive, R>(\n snapshot: TreeNodeSnapshot<TNode> | undefined,\n mapper: (node: TreeNodeSnapshot<TNode>, depth: number) => R\n): { value: R; children: ReturnType<typeof mapTree<TNode, R>>[] } | undefined {\n if (!snapshot) return undefined;\n\n const map = (\n node: TreeNodeSnapshot<TNode>,\n depth: number\n ): { value: R; children: ReturnType<typeof mapTree<TNode, R>>[] } => {\n const value = mapper(node, depth);\n const children = node.children.map((child) =>\n map(child as TreeNodeSnapshot<TNode>, depth + 1)\n );\n return { value, children };\n };\n\n return map(snapshot, 0);\n}\n\n// =============================================================================\n// Schema Functions (work with TreeNodePrimitive, not snapshots)\n// =============================================================================\n\n/**\n * Build a lookup map from node type strings to their TreeNodePrimitive definitions.\n * Useful for resolving snapshot node types back to their schema definitions.\n *\n * @param tree - The TreePrimitive to analyze\n * @returns Map from type string to TreeNodePrimitive\n *\n * @example\n * ```ts\n * const typeMap = buildNodeTypeMap(fileTree);\n * const folderPrimitive = typeMap.get(\"folder\"); // FolderNode\n * ```\n */\nexport function buildNodeTypeMap<TRoot extends AnyTreeNodePrimitive>(\n tree: TreePrimitive<TRoot>\n): Map<string, AnyTreeNodePrimitive> {\n const map = new Map<string, AnyTreeNodePrimitive>();\n const visited = new Set<string>();\n\n const visit = (node: AnyTreeNodePrimitive) => {\n if (visited.has(node.type)) return;\n visited.add(node.type);\n map.set(node.type, node);\n\n for (const child of node.children) {\n visit(child);\n }\n };\n\n visit(tree.root);\n return map;\n}\n\n/**\n * Get the TreeNodePrimitive definition for a snapshot node.\n * Requires the tree schema to resolve the type string to its primitive.\n *\n * @param tree - The TreePrimitive schema\n * @param snapshot - The node snapshot to get the primitive for\n * @returns The TreeNodePrimitive, or undefined if not found\n *\n * @example\n * ```ts\n * const node = findNodeById(treeSnapshot, \"some-id\");\n * const primitive = getNodePrimitive(fileTree, node);\n * // primitive is FolderNode or FileNode depending on node.type\n * ```\n */\nexport function getNodePrimitive<TRoot extends AnyTreeNodePrimitive>(\n tree: TreePrimitive<TRoot>,\n snapshot: TreeNodeSnapshot<AnyTreeNodePrimitive>\n): AnyTreeNodePrimitive | undefined {\n return getNodeTypeByName(tree, snapshot.type);\n}\n\n/**\n * Get the allowed child types for a snapshot node.\n * Combines schema lookup with the node's allowed children.\n *\n * @param tree - The TreePrimitive schema\n * @param snapshot - The node snapshot to get allowed children for\n * @returns Array of allowed child TreeNodePrimitives, or empty array if not found\n *\n * @example\n * ```ts\n * const node = findNodeById(treeSnapshot, \"folder-id\");\n * const allowedChildren = getAllowedChildTypesForNode(fileTree, node);\n * // Returns [FolderNode, FileNode] if the folder can contain both\n * ```\n */\nexport function getAllowedChildTypesForNode<TRoot extends AnyTreeNodePrimitive>(\n tree: TreePrimitive<TRoot>,\n snapshot: TreeNodeSnapshot<AnyTreeNodePrimitive>\n): readonly AnyTreeNodePrimitive[] {\n const primitive = getNodePrimitive(tree, snapshot);\n if (!primitive) return [];\n return primitive.children;\n}\n\n/**\n * Check if a child type is allowed for a specific snapshot node.\n *\n * @param tree - The TreePrimitive schema\n * @param parentSnapshot - The parent node snapshot\n * @param childTypeName - The type string of the potential child\n * @returns true if the child type is allowed\n *\n * @example\n * ```ts\n * const folder = findNodeById(treeSnapshot, \"folder-id\");\n * canAddChildType(fileTree, folder, \"file\"); // true\n * canAddChildType(fileTree, folder, \"unknown\"); // false\n * ```\n */\nexport function canAddChildType<TRoot extends AnyTreeNodePrimitive>(\n tree: TreePrimitive<TRoot>,\n parentSnapshot: TreeNodeSnapshot<AnyTreeNodePrimitive>,\n childTypeName: string\n): boolean {\n const primitive = getNodePrimitive(tree, parentSnapshot);\n if (!primitive) return false;\n return primitive.isChildAllowed(childTypeName);\n}\n\n/**\n * Get all allowed child node types for a specific node type.\n *\n * @param nodeType - The TreeNodePrimitive to get children for\n * @returns Array of allowed child TreeNodePrimitives\n *\n * @example\n * ```ts\n * const FolderNode = TreeNode(\"folder\", { ... });\n * const FileNode = TreeNode(\"file\", { ... });\n *\n * // Get allowed children for FolderNode\n * const allowedChildren = getAllowedChildTypes(FolderNode);\n * // Returns [FolderNode, FileNode] if folder can contain both\n * ```\n */\nexport function getAllowedChildTypes<TNode extends AnyTreeNodePrimitive>(\n nodeType: TNode\n): readonly AnyTreeNodePrimitive[] {\n return nodeType.children;\n}\n\n/**\n * Get all unique node types reachable in a tree schema.\n * Recursively traverses the tree structure starting from the root.\n *\n * @param tree - The TreePrimitive to analyze\n * @returns Array of all unique TreeNodePrimitives in the tree schema\n *\n * @example\n * ```ts\n * const fileTree = Tree({ root: FolderNode });\n * const allNodeTypes = getAllNodeTypes(fileTree);\n * // Returns [FolderNode, FileNode] - all possible node types\n * ```\n */\nexport function getAllNodeTypes<TRoot extends AnyTreeNodePrimitive>(\n tree: TreePrimitive<TRoot>\n): readonly AnyTreeNodePrimitive[] {\n const visited = new Set<string>();\n const result: AnyTreeNodePrimitive[] = [];\n\n const visit = (node: AnyTreeNodePrimitive) => {\n if (visited.has(node.type)) return;\n visited.add(node.type);\n result.push(node);\n\n for (const child of node.children) {\n visit(child);\n }\n };\n\n visit(tree.root);\n return result;\n}\n\n/**\n * Get the node type primitive by its type string from a tree schema.\n *\n * @param tree - The TreePrimitive to search\n * @param typeName - The type string to find (e.g., \"folder\", \"file\")\n * @returns The matching TreeNodePrimitive, or undefined if not found\n *\n * @example\n * ```ts\n * const fileTree = Tree({ root: FolderNode });\n * const folderType = getNodeTypeByName(fileTree, \"folder\");\n * // Returns FolderNode\n * ```\n */\nexport function getNodeTypeByName<TRoot extends AnyTreeNodePrimitive>(\n tree: TreePrimitive<TRoot>,\n typeName: string\n): AnyTreeNodePrimitive | undefined {\n const allTypes = getAllNodeTypes(tree);\n return allTypes.find((node) => node.type === typeName);\n}\n\n/**\n * Check if a child type is allowed under a parent type.\n *\n * @param parentType - The parent TreeNodePrimitive\n * @param childTypeName - The type string of the potential child\n * @returns true if the child type is allowed\n *\n * @example\n * ```ts\n * isChildTypeAllowed(FolderNode, \"file\"); // true if folder can contain files\n * isChildTypeAllowed(FileNode, \"folder\"); // false - files can't have children\n * ```\n */\nexport function isChildTypeAllowed(\n parentType: AnyTreeNodePrimitive,\n childTypeName: string\n): boolean {\n return parentType.isChildAllowed(childTypeName);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4DA,SAAgB,aACd,UACA,IACqC;AACrC,KAAI,CAAC,SAAU,QAAO;AACtB,KAAI,SAAS,OAAO,GAAI,QAAO;AAE/B,MAAK,MAAM,SAAS,SAAS,UAAU;EACrC,MAAM,QAAQ,aAAa,OAAkC,GAAG;AAChE,MAAI,MAAO,QAAO;;;;;;;;;;AAatB,SAAgB,iBACd,UACA,IACqC;AACrC,KAAI,CAAC,SAAU,QAAO;CAEtB,MAAM,UACJ,MACA,aACA,UACwC;EACxC,MAAM,OAAO,CAAC,GAAG,aAAa,KAAK,GAAG;AAEtC,MAAI,KAAK,OAAO,GACd,QAAO;GAAE;GAAM;GAAM;GAAO;AAG9B,OAAK,MAAM,SAAS,KAAK,UAAU;GACj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AgB,iBACd,MACA,UACkC;AAClC,QAAO,kBAAkB,MAAM,SAAS,KAAK;;;;;;;;;;;;;;;;;AAkB/C,SAAgB,4BACd,MACA,UACiC;CACjC,MAAM,YAAY,iBAAiB,MAAM,SAAS;AAClD,KAAI,CAAC,UAAW,QAAO,EAAE;AACzB,QAAO,UAAU;;;;;;;;;;;;;;;;;AAkBnB,SAAgB,gBACd,MACA,gBACA,eACS;CACT,MAAM,YAAY,iBAAiB,MAAM,eAAe;AACxD,KAAI,CAAC,UAAW,QAAO;AACvB,QAAO,UAAU,eAAe,cAAc;;;;;;;;;;;;;;;;;;AAmBhD,SAAgB,qBACd,UACiC;AACjC,QAAO,SAAS;;;;;;;;;;;;;;;;AAiBlB,SAAgB,gBACd,MACiC;CACjC,MAAM,0BAAU,IAAI,KAAa;CACjC,MAAMC,SAAiC,EAAE;CAEzC,MAAM,SAAS,SAA+B;AAC5C,MAAI,QAAQ,IAAI,KAAK,KAAK,CAAE;AAC5B,UAAQ,IAAI,KAAK,KAAK;AACtB,SAAO,KAAK,KAAK;AAEjB,OAAK,MAAM,SAAS,KAAK,SACvB,OAAM,MAAM;;AAIhB,OAAM,KAAK,KAAK;AAChB,QAAO;;;;;;;;;;;;;;;;AAiBT,SAAgB,kBACd,MACA,UACkC;AAElC,QADiB,gBAAgB,KAAK,CACtB,MAAM,SAAS,KAAK,SAAS,SAAS;;;;;;;;;;;;;;;AAgBxD,SAAgB,mBACd,YACA,eACS;AACT,QAAO,WAAW,eAAe,cAAc"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voidhash/mimic",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,7 +31,7 @@
31
31
  "typescript": "5.8.3",
32
32
  "vite-tsconfig-paths": "^5.1.4",
33
33
  "vitest": "^3.2.4",
34
- "@voidhash/tsconfig": "0.0.2"
34
+ "@voidhash/tsconfig": "0.0.3"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "effect": "^3.19.12"
package/tsdown.config.ts CHANGED
@@ -9,7 +9,7 @@ export default defineConfig({
9
9
  sourcemap: true,
10
10
  tsconfig: "./tsconfig.build.json",
11
11
  },
12
- // unbundle: true,
12
+ unbundle: true,
13
13
  format: ["cjs", "esm"],
14
14
  outExtensions: (ctx) => ({
15
15
  dts: ctx.format === "cjs" ? ".d.cts" : ".d.mts",