|
1 | 1 | import {deepEqual} from '../../json-equal/deepEqual'; |
2 | | -import {ObjNode, ArrNode, JsonNode} from '../nodes'; |
| 2 | +import {ObjNode, ArrNode, JsonNode, ConNode} from '../nodes'; |
3 | 3 | import {toPath, isChild} from '../../json-pointer/util'; |
4 | 4 | import {interval} from '../../json-crdt-patch/clock'; |
5 | 5 | import {PatchBuilder} from '../../json-crdt-patch/PatchBuilder'; |
@@ -102,12 +102,14 @@ export class JsonPatch<N extends JsonNode = JsonNode<any>> { |
102 | 102 | const key = steps[steps.length - 1]; |
103 | 103 | if (node instanceof ObjNode) { |
104 | 104 | const stringKey = String(key); |
105 | | - if (node.get(stringKey) === undefined) throw new Error('NOT_FOUND'); |
| 105 | + const valueNode = node.get(stringKey); |
| 106 | + if (valueNode === undefined) throw new Error('NOT_FOUND'); |
| 107 | + if (valueNode instanceof ConNode && valueNode.val === undefined) throw new Error('NOT_FOUND'); |
106 | 108 | builder.insObj(node.id, [[stringKey, builder.const(undefined)]]); |
107 | 109 | } else if (node instanceof ArrNode) { |
108 | 110 | const key = steps[steps.length - 1]; |
109 | 111 | const index = ~~key; |
110 | | - if ('' + index !== key) throw new Error('INVALID_INDEX'); |
| 112 | + if (typeof key === 'string' && '' + index !== key) throw new Error('INVALID_INDEX'); |
111 | 113 | const id = node.find(index); |
112 | 114 | if (!id) throw new Error('NOT_FOUND'); |
113 | 115 | builder.del(node.id, [interval(id, 0, 1)]); |
@@ -169,17 +171,21 @@ export class JsonPatch<N extends JsonNode = JsonNode<any>> { |
169 | 171 | const model = this.model; |
170 | 172 | if (!steps.length) return model.view(); |
171 | 173 | else { |
172 | | - const objSteps = steps.slice(0, steps.length - 1); |
173 | | - const node = model.api.find(objSteps); |
174 | | - const key = steps[steps.length - 1]; |
175 | | - if (node instanceof ObjNode) { |
176 | | - return node.get(String(key))?.view(); |
177 | | - } else if (node instanceof ArrNode) { |
178 | | - const index = ~~key; |
179 | | - if ('' + index !== key) throw new Error('INVALID_INDEX'); |
180 | | - const arrNode = node.getNode(index); |
181 | | - if (!arrNode) throw new Error('NOT_FOUND'); |
182 | | - return arrNode.view(); |
| 174 | + try { |
| 175 | + const objSteps = steps.slice(0, steps.length - 1); |
| 176 | + const node = model.api.find(objSteps); |
| 177 | + const key = steps[steps.length - 1]; |
| 178 | + if (node instanceof ObjNode) { |
| 179 | + return node.get(String(key))?.view(); |
| 180 | + } else if (node instanceof ArrNode) { |
| 181 | + const index = ~~key; |
| 182 | + if ('' + index !== key) throw new Error('INVALID_INDEX'); |
| 183 | + const arrNode = node.getNode(index); |
| 184 | + if (!arrNode) throw new Error('NOT_FOUND'); |
| 185 | + return arrNode.view(); |
| 186 | + } |
| 187 | + } catch { |
| 188 | + return; |
183 | 189 | } |
184 | 190 | } |
185 | 191 | return undefined; |
|
0 commit comments