|
| 1 | +// deno-lint-ignore-file no-explicit-any |
| 2 | + |
| 3 | +// Modernized version of Stefan Goessner's original JSON Path implementation. |
| 4 | +// Copyright (c) 2007 Stefan Goessner (goessner.net) |
| 5 | +// Licensed under the MIT (MIT-LICENSE.txt) licence. |
| 6 | + |
| 7 | +export function* trace<T = any>(expr: string, val: unknown, path: string): IterableIterator<[string, T]> { |
| 8 | + if (expr) { |
| 9 | + const [loc, ...rest] = expr.split(";"); |
| 10 | + const x = rest.join(";"); |
| 11 | + |
| 12 | + if (val !== null && typeof val === 'object' && loc in val) { |
| 13 | + yield* trace(x, (<any>val)[loc], path + ";" + loc); |
| 14 | + } |
| 15 | + else if (loc === "*") { |
| 16 | + for (const [m, _l, v, p] of walk(loc, val, path)) { |
| 17 | + yield* trace(m + ";" + x, v, p) |
| 18 | + } |
| 19 | + } |
| 20 | + else if (loc === "..") { |
| 21 | + yield* trace(x, val, path); |
| 22 | + for (const [m, _l, v, p] of walk(loc, val, path)) { |
| 23 | + if (typeof (<any>v)[m] === "object") |
| 24 | + yield* trace("..;" + x, (<any>v)[m], p + ";" + m); |
| 25 | + } |
| 26 | + } |
| 27 | + else if (/,/.test(loc)) { // [name1,name2,...] |
| 28 | + for (let s = loc.split(/'?,'?/), i = 0, n = s.length; i < n; i++) |
| 29 | + yield* trace(s[i] + ";" + x, val, path); |
| 30 | + } |
| 31 | + else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) { // [start:end:step] slice syntax |
| 32 | + yield* slice(loc, x, val, path); |
| 33 | + } |
| 34 | + // eval is bad, not doing this anymore |
| 35 | + // else if (/^\(.*?\)$/.test(loc)) { // [(expr)] |
| 36 | + // trace(eval(loc, val, path.substr(path.lastIndexOf(";") + 1)) + ";" + x, val, path); |
| 37 | + // } else if (/^\?\(.*?\)$/.test(loc)) { // [?(expr)] |
| 38 | + // walk(loc, x, val, path, (m, l, x, v, p) => { if (eval(l.replace(/^\?\((.*?)\)$/, "$1"), v[m], m)) trace(m + ";" + x, v, p); }); |
| 39 | + // } |
| 40 | + } |
| 41 | + else yield [path, val as T] |
| 42 | +} |
| 43 | + |
| 44 | +function* slice<T>(loc: string, expr: string, val: unknown, path: string): IterableIterator<[string, T]> { |
| 45 | + if (val instanceof Array) { |
| 46 | + const len = val.length; |
| 47 | + let start = 0, end = len, step = 1; |
| 48 | + loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, (_$0, $1, $2, $3) => { |
| 49 | + start = parseInt($1 || start); |
| 50 | + end = parseInt($2 || end); |
| 51 | + step = parseInt($3 || step); |
| 52 | + return '' |
| 53 | + }); |
| 54 | + start = (start < 0) ? Math.max(0, start + len) : Math.min(len, start); |
| 55 | + end = (end < 0) ? Math.max(0, end + len) : Math.min(len, end); |
| 56 | + for (let i = start; i < end; i += step) |
| 57 | + yield* trace(i + ";" + expr, val, path); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function* walk(loc: string, val: unknown, path: string) { |
| 62 | + if (val instanceof Array) { |
| 63 | + for (let i = 0, n = val.length; i < n; i++) |
| 64 | + if (i in val) |
| 65 | + yield [i, loc, val, path] as const |
| 66 | + } |
| 67 | + else if (typeof val === "object") { |
| 68 | + for (const m in val) |
| 69 | + if (val.hasOwnProperty(m)) |
| 70 | + yield [m, loc, val, path] as const |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export function normalize(expr: string) { |
| 75 | + const subX: string[] = []; |
| 76 | + if (!expr.startsWith('$')) expr = '$' + expr |
| 77 | + return expr |
| 78 | + .replace(/[\['](\??\(.*?\))[\]']/g, (_$0, $1) => { return "[#" + (subX.push($1) - 1) + "]"; }) |
| 79 | + .replace(/'?\.'?|\['?/g, ";") |
| 80 | + .replace(/;;;|;;/g, ";..;") |
| 81 | + .replace(/;$|'?\]|'$/g, "") |
| 82 | + .replace(/#([0-9]+)/g, (_$0, $1) => { return subX[$1]; }); |
| 83 | +} |
| 84 | + |
| 85 | +// FIXME: avoid repeated split/join/regex.test |
| 86 | +export function match(expr: string, path: string): boolean { |
| 87 | + if (expr && path) { |
| 88 | + const [loc, ...restLoc] = expr.split(";"); |
| 89 | + const [val, ...restVal] = path.split(";"); |
| 90 | + const exprRest = restLoc.join(";"); |
| 91 | + const pathRest = restVal.join(';') |
| 92 | + |
| 93 | + if (loc === val) { |
| 94 | + return match(exprRest, pathRest) |
| 95 | + } |
| 96 | + else if (loc === "*") { |
| 97 | + return match(exprRest, pathRest) |
| 98 | + } |
| 99 | + else if (loc === "..") { |
| 100 | + return match(exprRest, path) || match("..;" + exprRest, pathRest); |
| 101 | + } |
| 102 | + else if (/,/.test(loc)) { // [name1,name2,...] |
| 103 | + if (loc.split(/'?,'?/).some(v => v === val)) return match(exprRest, pathRest) |
| 104 | + else return false |
| 105 | + } |
| 106 | + else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) { // [start:end:step] slice syntax |
| 107 | + let start = 0, end = Number.MAX_SAFE_INTEGER, step = 1; |
| 108 | + loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, (_$0, $1, $2, $3) => { |
| 109 | + start = parseInt($1 || start); |
| 110 | + end = parseInt($2 || end); |
| 111 | + step = parseInt($3 || step); |
| 112 | + return '' |
| 113 | + }); |
| 114 | + const idx = Number(val) |
| 115 | + if (start < 0 || end < 0 || step < 0) |
| 116 | + throw TypeError('Negative numbers not supported. Can\'t know length ahead of time when stream parsing'); |
| 117 | + if (idx >= start && idx < end && start + idx % step === 0) return match(exprRest, pathRest) |
| 118 | + else return false |
| 119 | + } |
| 120 | + } |
| 121 | + else if (!expr && !path) return true |
| 122 | + return false; |
| 123 | +} |
0 commit comments