@f5io/jsonpath
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.paths = undefined; | ||
@@ -23,16 +24,20 @@ var _tokenizer = require('./tokenizer'); | ||
const cache = new Map(); | ||
const withPaths = ['path']; | ||
exports.default = query; | ||
const paths = exports.paths = factory('path'); | ||
exports.default = factory('data'); | ||
function query(q) { | ||
let data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
function factory(type) { | ||
return function (q) { | ||
let data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
if (!cache.has(q)) cache.set(q, (0, _transformer2.default)((0, _parser2.default)((0, _tokenizer2.default)(q)))); | ||
const fn = cache.get(q); | ||
if (data === null) return function (d) { | ||
return fn(d); | ||
if (!cache.has(q)) cache.set(q, (0, _transformer2.default)((0, _parser2.default)((0, _tokenizer2.default)(q)))); | ||
const fn = cache.get(q); | ||
const paths = withPaths.includes(type); | ||
if (data === null) return function (d) { | ||
return fn(d, paths)[type].get(); | ||
}; | ||
return fn(data, paths)[type].get(); | ||
}; | ||
return fn(data); | ||
}; | ||
module.exports = exports['default']; | ||
} |
@@ -23,8 +23,15 @@ "use strict"; | ||
value: function bind(f) { | ||
return this.value ? f(this.value) : this; | ||
return this.value != null ? f(this.value) : this; | ||
} | ||
}, { | ||
key: "chain", | ||
value: function chain(f) { | ||
return this.map(f).bind(function (x) { | ||
return x; | ||
}); | ||
} | ||
}, { | ||
key: "map", | ||
value: function map(f) { | ||
return this.value ? maybe(f(this.value)) : this; | ||
return this.value != null ? maybe(f(this.value)) : this; | ||
} | ||
@@ -36,2 +43,7 @@ }, { | ||
} | ||
}, { | ||
key: "getOr", | ||
value: function getOr(val) { | ||
return this.value != null ? this.value : val; | ||
} | ||
}]); | ||
@@ -38,0 +50,0 @@ |
@@ -29,3 +29,3 @@ 'use strict'; | ||
return ops; | ||
}; | ||
} | ||
module.exports = exports['default']; |
@@ -41,15 +41,19 @@ 'use strict'; | ||
function search(x, p) { | ||
let a = []; | ||
for (const k in x) { | ||
if (k === p) a = a.concat(x[k]); | ||
if ((0, _utils.isPlainObject)(x[k]) || Array.isArray(x[k])) a = a.concat(search(x[k], p)); | ||
} | ||
return a; | ||
} | ||
const next = arr[i + 1]; | ||
if (next && next.type === 'prop') return acc.concat((0, _utils.map)(function (x) { | ||
return search(x, next.value); | ||
}));else return acc; | ||
if (next && next.type === 'prop') return acc.concat(function (_ref2) { | ||
let data = _ref2.data, | ||
path = _ref2.path, | ||
withPaths = _ref2.withPaths; | ||
return { | ||
data: data.map(function (x) { | ||
return (0, _utils.searchFor)(x, next.value); | ||
}), | ||
path: withPaths ? path.chain(function (p) { | ||
return data.map(function (x) { | ||
return (0, _utils.join)((0, _utils.searchForPath)(x, next.value), p); | ||
}); | ||
}) : path, | ||
withPaths: withPaths | ||
}; | ||
});else return acc; | ||
} | ||
@@ -59,8 +63,15 @@ }), _defineProperty(_rules, 5, { | ||
re: /^([_A-Za-z][^\W]*)/, | ||
parse: function prop(acc, _ref2, i, arr) { | ||
let value = _ref2.value; | ||
parse: function prop(acc, _ref3, i, arr) { | ||
let value = _ref3.value; | ||
const prev = arr[i - 1]; | ||
let offset = 1; | ||
let prev = arr[i - offset]; | ||
if (prev && prev.type === 'recurse') return acc; | ||
while (prev.type === 'dot') { | ||
prev = arr[i - ++offset]; | ||
} | ||
const ignored = ['union', 'array']; | ||
const getProps = function (x) { | ||
@@ -72,3 +83,26 @@ return Array.isArray(x) ? (0, _utils.mapFilter)(function (y) { | ||
return acc.concat((0, _utils.map)(getProps)); | ||
const getPaths = function (x) { | ||
return Array.isArray(x) && !ignored.includes(prev.type) ? (0, _utils.join)([[value]], (0, _utils.pathFilter)(function (y) { | ||
return y[value]; | ||
})(x)) : [[value]]; | ||
}; | ||
return acc.concat(function (_ref4) { | ||
let data = _ref4.data, | ||
path = _ref4.path, | ||
withPaths = _ref4.withPaths; | ||
const d = data.map(getProps); | ||
return { | ||
data: d, | ||
path: withPaths ? path.chain(function (p) { | ||
return d.chain(function (_) { | ||
return data.map(function (x) { | ||
return (0, _utils.join)(getPaths(x), p); | ||
}); | ||
}); | ||
}) : path, | ||
withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -81,6 +115,7 @@ }), _defineProperty(_rules, 7, { | ||
}, | ||
parse: function array(acc, _ref3) { | ||
let value = _ref3.value; | ||
parse: function array(acc, _ref5, i, arr) { | ||
let value = _ref5.value; | ||
const output = (0, _parser2.default)(value); | ||
const prev = arr[i - 1]; | ||
const output = (0, _parser2.default)([{ type: prev.type, parse: _utils.Identity }].concat(_toConsumableArray(value))); | ||
return acc.concat.apply(acc, _toConsumableArray(output)); | ||
@@ -94,9 +129,26 @@ } | ||
}, | ||
parse: function expr(acc, _ref4) { | ||
let value = _ref4.value; | ||
parse: function expr(acc, _ref6) { | ||
let value = _ref6.value; | ||
const fn = new Function('y', 'return ' + value.replace(/@/g, 'y')); | ||
return acc.concat((0, _utils.map)(function (x) { | ||
return x[fn(x)]; | ||
})); | ||
return acc.concat(function (_ref7) { | ||
let data = _ref7.data, | ||
path = _ref7.path, | ||
withPaths = _ref7.withPaths; | ||
const d = data.map(function (x) { | ||
return x[fn(x)]; | ||
}); | ||
return { | ||
data: d, | ||
path: withPaths ? path.chain(function (p) { | ||
return d.chain(function (_) { | ||
return data.map(function (x) { | ||
return (0, _utils.join)([[fn(x)]], p); | ||
}); | ||
}); | ||
}) : path, | ||
withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -109,6 +161,7 @@ }), _defineProperty(_rules, 11, { | ||
}, | ||
parse: function mexpr(acc, _ref5) { | ||
let value = _ref5.value; | ||
parse: function mexpr(acc, _ref8) { | ||
let value = _ref8.value; | ||
const fn = new Function('y', 'return ' + value.replace(/@/g, 'y')); | ||
const getProps = function (x) { | ||
@@ -119,3 +172,11 @@ return Array.isArray(x) ? x.map(function (y, i) { | ||
}; | ||
return acc.concat((0, _utils.map)(getProps)); | ||
return acc.concat(function (_ref9) { | ||
let data = _ref9.data, | ||
path = _ref9.path, | ||
withPaths = _ref9.withPaths; | ||
return { | ||
data: data.map(getProps), path: path, withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -128,9 +189,26 @@ }), _defineProperty(_rules, 13, { | ||
}, | ||
parse: function fexpr(acc, _ref6) { | ||
let value = _ref6.value; | ||
parse: function fexpr(acc, _ref10) { | ||
let value = _ref10.value; | ||
const fn = new Function('y', 'return ' + value.replace(/@/g, 'y')); | ||
return acc.concat((0, _utils.map)(function (x) { | ||
return x.filter(fn); | ||
})); | ||
return acc.concat(function (_ref11) { | ||
let data = _ref11.data, | ||
path = _ref11.path, | ||
withPaths = _ref11.withPaths; | ||
const d = data.map(function (x) { | ||
return x.filter(fn); | ||
}); | ||
return { | ||
data: d, | ||
path: withPaths ? path.chain(function (p) { | ||
return d.chain(function (_) { | ||
return data.map(function (x) { | ||
return (0, _utils.join)((0, _utils.pathFilter)(fn)(x), p); | ||
}); | ||
}); | ||
}) : path, | ||
withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -143,4 +221,4 @@ }), _defineProperty(_rules, 15, { | ||
}, | ||
parse: function coerce(acc, _ref7) { | ||
let value = _ref7.value; | ||
parse: function coerce(acc, _ref12) { | ||
let value = _ref12.value; | ||
@@ -167,3 +245,11 @@ let fn; | ||
}; | ||
return acc.concat((0, _utils.map)(getProps)); | ||
return acc.concat(function (_ref13) { | ||
let data = _ref13.data, | ||
path = _ref13.path, | ||
withPaths = _ref13.withPaths; | ||
return { | ||
data: data.map(getProps), path: path, withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -175,9 +261,9 @@ }), _defineProperty(_rules, 17, { | ||
return { | ||
start: m[1] || '0', | ||
end: m[2], | ||
step: (m[3] || ':1').slice(1) | ||
start: Number(m[1] || '0'), | ||
end: Number(m[2]), | ||
step: Number((m[3] || ':1').slice(1)) | ||
}; | ||
}, | ||
parse: function slice(acc, _ref8) { | ||
let value = _ref8.value; | ||
parse: function slice(acc, _ref14) { | ||
let value = _ref14.value; | ||
@@ -187,5 +273,18 @@ // TODO: Support step option | ||
if (value.end) args.push(value.end); | ||
return acc.concat((0, _utils.map)(function (x) { | ||
return x.slice.apply(x, args); | ||
})); | ||
return acc.concat(function (_ref15) { | ||
let data = _ref15.data, | ||
path = _ref15.path, | ||
withPaths = _ref15.withPaths; | ||
return { | ||
data: data.map(function (x) { | ||
return x.slice.apply(x, args); | ||
}), | ||
path: withPaths ? path.chain(function (p) { | ||
return data.map(function (x) { | ||
return (0, _utils.join)(_utils.slicePaths.apply(undefined, [x].concat(args)), p); | ||
}); | ||
}) : path, | ||
withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -196,12 +295,29 @@ }), _defineProperty(_rules, 22, { | ||
map: function (m) { | ||
return m[0].split(',').filter(_utils.Identity); | ||
return m[0].split(',').filter(_utils.Identity).map(Number); | ||
}, | ||
parse: function union(acc, _ref9) { | ||
let value = _ref9.value; | ||
parse: function union(acc, _ref16) { | ||
let value = _ref16.value; | ||
return acc.concat((0, _utils.map)(function (x) { | ||
return value.map(function (y) { | ||
return x[y]; | ||
return acc.concat(function (_ref17) { | ||
let data = _ref17.data, | ||
path = _ref17.path, | ||
withPaths = _ref17.withPaths; | ||
const d = data.map(function (x) { | ||
return value.map(function (y) { | ||
return x[y]; | ||
}); | ||
}); | ||
})); | ||
return { | ||
data: d, | ||
path: withPaths ? path.chain(function (p) { | ||
return d.map(function (_) { | ||
return (0, _utils.join)(value.map(function (x) { | ||
return [x]; | ||
}), p); | ||
}); | ||
}) : path, | ||
withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -216,5 +332,7 @@ }), _defineProperty(_rules, 25, { | ||
}, | ||
parse: function member(acc, _ref10) { | ||
let value = _ref10.value; | ||
parse: function member(acc, _ref18, i, arr) { | ||
let value = _ref18.value; | ||
const prev = arr[i - 1]; | ||
const getProps = function (x) { | ||
@@ -229,3 +347,31 @@ return Array.isArray(x) ? x.map(function (y) { | ||
}; | ||
return acc.concat((0, _utils.map)(getProps)); | ||
const getPaths = function (x) { | ||
return Array.isArray(x) && prev.type !== 'array' ? (0, _utils.join)(value.map(function (x) { | ||
return [x]; | ||
}), Array.from(x, function (_, i) { | ||
return [i]; | ||
})) : value.map(function (x) { | ||
return [x]; | ||
}); | ||
}; | ||
return acc.concat(function (_ref19) { | ||
let data = _ref19.data, | ||
path = _ref19.path, | ||
withPaths = _ref19.withPaths; | ||
const d = data.map(getProps); | ||
return { | ||
data: d, | ||
path: withPaths ? path.chain(function (p) { | ||
return d.chain(function (_) { | ||
return data.map(function (x) { | ||
return (0, _utils.join)(getPaths(x), p); | ||
}); | ||
}); | ||
}) : path, | ||
withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -240,5 +386,7 @@ }), _defineProperty(_rules, 26, { | ||
}, | ||
parse: function omember(acc, _ref11) { | ||
let value = _ref11.value; | ||
parse: function omember(acc, _ref20, i, arr) { | ||
let value = _ref20.value; | ||
const prev = arr[i - 1]; | ||
const getProps = function (x) { | ||
@@ -253,3 +401,31 @@ return Array.isArray(x) ? x.map(function (y) { | ||
}; | ||
return acc.concat((0, _utils.map)(getProps)); | ||
const getPaths = function (x) { | ||
return Array.isArray(x) && prev.type !== 'array' ? (0, _utils.join)(value.map(function (x) { | ||
return [x]; | ||
}), Array.from(x, function (_, i) { | ||
return [i]; | ||
})) : value.map(function (x) { | ||
return [x]; | ||
}); | ||
}; | ||
return acc.concat(function (_ref21) { | ||
let data = _ref21.data, | ||
path = _ref21.path, | ||
withPaths = _ref21.withPaths; | ||
const d = data.map(getProps); | ||
return { | ||
data: d, | ||
path: withPaths ? path.chain(function (p) { | ||
return d.chain(function (_) { | ||
return data.map(function (x) { | ||
return (0, _utils.join)(getPaths(x), p); | ||
}); | ||
}); | ||
}) : path, | ||
withPaths: withPaths | ||
}; | ||
}); | ||
} | ||
@@ -256,0 +432,0 @@ }), _rules); |
@@ -30,3 +30,3 @@ 'use strict'; | ||
} | ||
}; | ||
} | ||
module.exports = exports['default']; |
@@ -17,8 +17,8 @@ 'use strict'; | ||
function transformer(ops) { | ||
return function (data) { | ||
return function (data, withPaths) { | ||
return ops.reduce(function (acc, f) { | ||
return f(acc); | ||
}, (0, _maybe2.default)(data)).get(); | ||
}, { data: (0, _maybe2.default)(data), path: (0, _maybe2.default)([]), withPaths: withPaths }); | ||
}; | ||
}; | ||
} | ||
module.exports = exports['default']; |
@@ -6,2 +6,5 @@ 'use strict'; | ||
}); | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | ||
const map = exports.map = function (f) { | ||
@@ -48,2 +51,62 @@ return function (x) { | ||
}; | ||
}; | ||
const pathFilter = exports.pathFilter = function (f) { | ||
return function (x) { | ||
const output = []; | ||
let i = 0, | ||
xi; | ||
while (xi = x[i]) { | ||
const result = f(xi); | ||
if (result) output.push([i]); | ||
i++; | ||
} | ||
return output; | ||
}; | ||
}; | ||
const searchFor = exports.searchFor = function (o, p) { | ||
let result = []; | ||
for (const k in o) { | ||
if (k === p) result = result.concat(o[k]); | ||
if (isPlainObject(o[k]) || Array.isArray(o[k])) result = result.concat(searchFor(o[k], p)); | ||
} | ||
return result; | ||
}; | ||
const searchForPath = exports.searchForPath = function (o, p) { | ||
let ps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
let result = []; | ||
const coerce = Array.isArray(o) ? Number : function (x) { | ||
return x; | ||
}; | ||
for (const k in o) { | ||
if (k === p) result = result.concat([ps.concat(coerce(k))]); | ||
if (isPlainObject(o[k]) || Array.isArray(o[k])) result = result.concat(searchForPath(o[k], p, ps.concat(coerce(k)))); | ||
} | ||
return result; | ||
}; | ||
const join = exports.join = function (paths, to) { | ||
if (!to.length) return paths; | ||
const result = []; | ||
for (const p of paths) { | ||
for (const t of to) { | ||
result.push([].concat(_toConsumableArray(t), _toConsumableArray(p))); | ||
} | ||
} | ||
return result; | ||
}; | ||
const slicePaths = exports.slicePaths = function (x, start) { | ||
let end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : x.length; | ||
let i = start; | ||
const output = []; | ||
while (i < end) { | ||
output.push([i]); | ||
i++; | ||
} | ||
return output; | ||
}; |
{ | ||
"name": "@f5io/jsonpath", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Performant JSONPath implementation", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -6,3 +6,4 @@ # jsonpath | ||
</p> | ||
--- | ||
---- | ||
Performant JSONPath implementation that focusses on results | ||
@@ -9,0 +10,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
143834
30
1037
45
4