(query: string)
| 218 | }; |
| 219 | |
| 220 | function parseJsonPath(query: string): PathToken[] { |
| 221 | const trimmed = query.trim(); |
| 222 | if (!trimmed || trimmed === ".") { |
| 223 | return []; |
| 224 | } |
| 225 | let cursor = trimmed.startsWith(".") ? trimmed.slice(1) : trimmed; |
| 226 | const tokens: PathToken[] = []; |
| 227 | while (cursor.length > 0) { |
| 228 | if (cursor[0] === "[") { |
| 229 | const close = cursor.indexOf("]"); |
| 230 | if (close === -1) { |
| 231 | throw new Error(`Invalid JSON path: ${query}`); |
| 232 | } |
| 233 | tokens.push(Number(cursor.slice(1, close))); |
| 234 | cursor = cursor.slice(close + 1); |
| 235 | if (cursor.startsWith(".")) cursor = cursor.slice(1); |
| 236 | continue; |
| 237 | } |
| 238 | const dot = cursor.search(/[.[\]]/); |
| 239 | if (dot === -1) { |
| 240 | tokens.push(cursor); |
| 241 | break; |
| 242 | } |
| 243 | tokens.push(cursor.slice(0, dot)); |
| 244 | cursor = cursor.slice(dot); |
| 245 | if (cursor.startsWith(".")) cursor = cursor.slice(1); |
| 246 | } |
| 247 | return tokens.filter((token) => token !== ""); |
| 248 | } |
| 249 | |
| 250 | function setJsonPathValue( |
| 251 | root: unknown, |
no test coverage detected