|
1 | 1 | import {printTree} from 'tree-dump/lib/printTree'; |
| 2 | +import {get} from '@jsonjoy.com/json-pointer/lib/get'; |
| 3 | +import {toPath} from '@jsonjoy.com/json-pointer/lib/util'; |
2 | 4 | import {find} from './find'; |
3 | 5 | import {type ITimestampStruct, Timestamp} from '../../../json-crdt-patch/clock'; |
4 | 6 | import {ObjNode, ArrNode, BinNode, ConNode, VecNode, ValNode, StrNode, RootNode} from '../../nodes'; |
5 | 7 | import {NodeEvents} from './NodeEvents'; |
6 | 8 | import {ExtNode} from '../../extensions/ExtNode'; |
7 | | -import {toPath} from '@jsonjoy.com/json-pointer'; |
8 | 9 | import type {Path} from '@jsonjoy.com/json-pointer'; |
9 | 10 | import type {Extension} from '../../extensions/Extension'; |
10 | 11 | import type {ExtApi} from '../../extensions/types'; |
@@ -192,17 +193,18 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable { |
192 | 193 | } |
193 | 194 |
|
194 | 195 | public read(path?: ApiPath): unknown { |
195 | | - try { |
196 | | - return !path ? this.view() : this.in(path).view(); |
197 | | - } catch { |
198 | | - return; |
199 | | - } |
| 196 | + const view = this.view(); |
| 197 | + if (Array.isArray(path)) return get(view, path); |
| 198 | + if (!path) return view; |
| 199 | + let path2: string = path + ''; |
| 200 | + if (path && path2[0] !== '/') path2 = '/' + path2; |
| 201 | + return get(view, toPath(path2)); |
200 | 202 | } |
201 | 203 |
|
202 | 204 | public add(path: ApiPath, value: unknown): boolean { |
203 | 205 | const [parent, key] = breakPath(path); |
204 | 206 | try { |
205 | | - let node: unknown = this.in(parent); |
| 207 | + let node: unknown = parent ? this.in(parent) : this; |
206 | 208 | while (node instanceof ValApi) node = node.in(); |
207 | 209 | if (node instanceof ObjApi) { |
208 | 210 | node.set({[key]: value}); |
|
0 commit comments