Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jsonquerylang/jsonquery

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jsonquerylang/jsonquery - npm Package Compare versions

Comparing version 4.0.0 to 4.1.0

399

lib/jsonquery.js

@@ -1,15 +0,15 @@

const E = (t) => Array.isArray(t), I = (t) => typeof t == "string";
function m(t) {
const A = (t) => Array.isArray(t), L = (t) => t && typeof t == "object" && !A(t), W = (t) => typeof t == "string";
function p(t) {
return (...e) => {
const n = e.map((c) => $(c)), r = n[0], s = n[1];
return n.length === 1 ? (c) => t(r(c)) : n.length === 2 ? (c) => t(r(c), s(c)) : (c) => t(...n.map((x) => x(c)));
const n = e.map((c) => m(c)), r = n[0], s = n[1];
return n.length === 1 ? (c) => t(r(c)) : n.length === 2 ? (c) => t(r(c), s(c)) : (c) => t(...n.map(($) => $(c)));
};
}
const k = {
const x = {
pipe: (...t) => {
const e = t.map((n) => $(n));
const e = t.map((n) => m(n));
return (n) => e.reduce((r, s) => s(r), n);
},
object: (t) => {
const e = Object.keys(t).map((n) => [n, $(t[n])]);
const e = Object.keys(t).map((n) => [n, m(t[n])]);
return (n) => {

@@ -23,3 +23,3 @@ const r = {};

array: (...t) => {
const e = t.map((n) => $(n));
const e = t.map((n) => m(n));
return (n) => e.map((r) => r(n));

@@ -42,30 +42,62 @@ },

map: (t) => {
const e = $(t);
const e = m(t);
return (n) => n.map(e);
},
mapObject: (t) => {
const e = m(t);
return (n) => {
const r = {};
for (const s of Object.keys(n)) {
const c = e({ key: s, value: n[s] });
r[c.key] = c.value;
}
return r;
};
},
mapKeys: (t) => {
const e = m(t);
return (n) => {
const r = {};
for (const s of Object.keys(n)) {
const c = e(s);
r[c] = n[s];
}
return r;
};
},
mapValues: (t) => {
const e = m(t);
return (n) => {
const r = {};
for (const s of Object.keys(n))
r[s] = e(n[s]);
return r;
};
},
filter: (t) => {
const e = $(t);
return (n) => n.filter(e);
const e = m(t);
return (n) => n.filter((r) => E(e(r)));
},
sort: (t = ["get"], e) => {
const n = $(t), r = e === "desc" ? -1 : 1;
function s(c, x) {
const w = n(c), y = n(x);
return w > y ? r : w < y ? -r : 0;
const n = m(t), r = e === "desc" ? -1 : 1;
function s(c, $) {
const k = n(c), O = n($);
return k > O ? r : k < O ? -r : 0;
}
return (c) => c.slice().sort(s);
},
reverse: () => (t) => t.toReversed(),
pick: (...t) => {
const e = t.map(
([r, ...s]) => [s[s.length - 1], k.get(...s)]
([r, ...s]) => [s[s.length - 1], x.get(...s)]
), n = (r, s) => {
const c = {};
for (const [x, w] of s)
c[x] = w(r);
for (const [$, k] of s)
c[$] = k(r);
return c;
};
return (r) => E(r) ? r.map((s) => n(s, e)) : n(r, e);
return (r) => A(r) ? r.map((s) => n(s, e)) : n(r, e);
},
groupBy: (t) => {
const e = $(t);
const e = m(t);
return (n) => {

@@ -81,3 +113,3 @@ const r = {};

keyBy: (t) => {
const e = $(t);
const e = m(t);
return (n) => {

@@ -87,3 +119,3 @@ const r = {};

const c = e(s);
r[c] = r[c] ?? s;
c in r || (r[c] = s);
}

@@ -94,4 +126,9 @@ return r;

flatten: () => (t) => t.flat(),
join: (t = "") => (e) => e.join(t),
split: p(
(t, e) => e !== void 0 ? t.split(e) : t.trim().split(/\s+/)
),
substring: (t, e) => (n) => n.slice(Math.max(t, 0), e),
uniq: () => (t) => [...new Set(t)],
uniqBy: (t) => (e) => Object.values(k.groupBy(t)(e)).map((n) => n[0]),
uniqBy: (t) => (e) => Object.values(x.keyBy(t)(e)),
limit: (t) => (e) => e.slice(0, Math.max(t, 0)),

@@ -103,50 +140,57 @@ size: () => (t) => t.length,

sum: () => (t) => t.reduce((e, n) => e + n),
average: () => (t) => k.sum()(t) / t.length,
average: () => (t) => x.sum()(t) / t.length,
min: () => (t) => Math.min(...t),
max: () => (t) => Math.max(...t),
and: p((t, e) => !!(t && e)),
or: p((t, e) => !!(t || e)),
not: p((t) => !t),
exists: (t) => {
const e = t.slice(1), n = e.pop(), r = x.get(...e);
return (s) => {
const c = r(s);
return !!c && Object.hasOwnProperty.call(c, n);
};
},
if: (t, e, n) => {
const r = m(t), s = m(e), c = m(n);
return ($) => E(r($)) ? s($) : c($);
},
in: (t, e) => {
const n = $(t), r = $(e);
const n = m(t), r = m(e);
return (s) => r(s).includes(n(s));
},
"not in": (t, e) => {
const n = k.in(t, e);
const n = x.in(t, e);
return (r) => !n(r);
},
regex: (t, e, n) => {
const r = new RegExp(e, n), s = $(t);
const r = new RegExp(e, n), s = m(t);
return (c) => r.test(s(c));
},
and: m((t, e) => !!(t && e)),
or: m((t, e) => !!(t || e)),
not: m((t) => !t),
exists: (t) => {
const e = t.slice(1), n = e.pop(), r = k.get(...e);
return (s) => {
const c = r(s);
return !!c && Object.hasOwnProperty.call(c, n);
};
},
if: (t, e, n) => {
const r = $(t), s = $(e), c = $(n);
return (x) => r(x) ? s(x) : c(x);
},
eq: m((t, e) => t === e),
gt: m((t, e) => t > e),
gte: m((t, e) => t >= e),
lt: m((t, e) => t < e),
lte: m((t, e) => t <= e),
ne: m((t, e) => t !== e),
add: m((t, e) => t + e),
subtract: m((t, e) => t - e),
multiply: m((t, e) => t * e),
divide: m((t, e) => t / e),
pow: m((t, e) => t ** e),
mod: m((t, e) => t % e),
abs: m(Math.abs),
round: m((t, e = 0) => +`${Math.round(+`${t}e${e}`)}e${-e}`)
}, S = [];
function $(t, e) {
S.unshift({ ...k, ...S[0], ...e == null ? void 0 : e.functions });
eq: p((t, e) => t === e),
gt: p((t, e) => t > e),
gte: p((t, e) => t >= e),
lt: p((t, e) => t < e),
lte: p((t, e) => t <= e),
ne: p((t, e) => t !== e),
add: p((t, e) => t + e),
subtract: p((t, e) => t - e),
multiply: p((t, e) => t * e),
divide: p((t, e) => t / e),
pow: p((t, e) => t ** e),
mod: p((t, e) => t % e),
abs: p(Math.abs),
round: p((t, e = 0) => +`${Math.round(+`${t}e${e}`)}e${-e}`),
number: p((t) => {
const e = Number(t);
return Number.isNaN(Number(t)) ? null : e;
}),
string: p(String)
}, E = (t) => t !== null && t !== 0 && t !== !1, v = [];
function m(t, e) {
v.unshift({ ...x, ...v[0], ...e == null ? void 0 : e.functions });
try {
const n = E(t) ? R(t, S[0]) : () => t;
const n = A(t) ? J(t, v[0]) : L(t) ? P(
`Function notation ["object", {...}] expected but got ${JSON.stringify(t)}`
) : () => t;
return (r) => {

@@ -160,12 +204,13 @@ try {

} finally {
S.shift();
v.shift();
}
}
function R(t, e) {
function J(t, e) {
const [n, ...r] = t, s = e[n];
if (!s)
throw new Error(`Unknown function '${n}'`);
return s(...r);
return s || P(`Unknown function '${n}'`), s(...r);
}
const P = {
function P(t) {
throw new Error(t);
}
const R = {
and: "and",

@@ -187,104 +232,104 @@ or: "or",

"not in": "not in"
}, L = /^[a-zA-Z_$][a-zA-Z\d_$]*$/, W = /^[a-zA-Z_$][a-zA-Z\d_$]*/, F = /^"(?:[^"\\]|\\.)*"/, U = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/, J = /^(0|[1-9][0-9]*)/, M = /^(true|false|null)/, T = /^[ \n\t\r]+/;
function z(t, e) {
const n = () => {
h();
const u = r();
if (h(), t[o] === "|") {
const g = [u];
for (; t[o] === "|"; )
o++, h(), g.push(r());
return ["pipe", ...g];
}, M = /^[a-zA-Z_$][a-zA-Z\d_$]*$/, T = /^[a-zA-Z_$][a-zA-Z\d_$]*/, U = /^"(?:[^"\\]|\\.)*"/, z = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/, B = /^(0|[1-9][0-9]*)/, K = /^(true|false|null)/, Z = /^[ \n\t\r]+/;
function D(t, e) {
const n = { ...R, ...e == null ? void 0 : e.operators }, r = Object.keys(n).sort((o, a) => a.length - o.length), s = () => {
g();
const o = c();
if (g(), t[u] === "|") {
const a = [o];
for (; t[u] === "|"; )
u++, g(), a.push(c());
return ["pipe", ...a];
}
return u;
}, r = () => {
const u = { ...P, ...e == null ? void 0 : e.operators }, g = s();
h();
for (const j of Object.keys(u).sort((N, v) => v.length - N.length)) {
const N = u[j];
if (t.substring(o, o + N.length) === N) {
o += N.length, h();
const v = s();
return [j, g, v];
return o;
}, c = () => {
const o = $();
g();
for (const a of r) {
const y = n[a];
if (t.substring(u, u + y.length) === y) {
u += y.length, g();
const F = $();
return [a, o, F];
}
}
return g;
}, s = () => {
if (t[o] === "(") {
o++;
const u = n();
return p(")"), u;
return o;
}, $ = () => {
if (t[u] === "(") {
u++;
const o = s();
return d(")"), o;
}
return c();
}, c = () => {
const u = [];
if (t[o] === ".") {
for (; t[o] === "."; )
o++, u.push(
a() ?? i() ?? f() ?? b("Property expected")
return k();
}, k = () => {
if (t[u] === ".") {
const o = [];
for (; t[u] === "."; )
u++, o.push(
l() ?? f() ?? j() ?? _("Property expected")
);
return ["get", ...u];
return ["get", ...o];
}
return x();
}, x = () => {
const u = o, g = i();
if (h(), !g || t[o] !== "(")
return o = u, w();
o++, !(e != null && e.functions[g]) && !k[g] && b(`Unknown function '${g}'`), h();
const j = t[o] !== ")" ? [n()] : [];
for (; o < t.length && t[o] !== ")"; )
h(), p(","), j.push(n());
return p(")"), [g, ...j];
}, w = () => {
if (t[o] === "{") {
o++, h();
const u = {};
let g = !0;
for (; o < t.length && t[o] !== "}"; ) {
g ? g = !1 : (p(","), h());
const j = a() ?? i() ?? f() ?? b("Key expected");
h(), p(":"), u[j] = n();
return O();
}, O = () => {
const o = u, a = f();
if (g(), !a || t[u] !== "(")
return u = o, h();
u++, !(e != null && e.functions[a]) && !x[a] && _(`Unknown function '${a}'`), g();
const y = t[u] !== ")" ? [s()] : [];
for (; u < t.length && t[u] !== ")"; )
g(), d(","), y.push(s());
return d(")"), [a, ...y];
}, h = () => {
if (t[u] === "{") {
u++, g();
const o = {};
let a = !0;
for (; u < t.length && t[u] !== "}"; ) {
a ? a = !1 : (d(","), g());
const y = l() ?? f() ?? j() ?? _("Key expected");
g(), d(":"), o[y] = s();
}
return p("}"), ["object", u];
return d("}"), ["object", o];
}
return y();
}, y = () => {
if (t[o] === "[") {
o++, h();
const u = [];
let g = !0;
for (; o < t.length && t[o] !== "]"; )
g ? g = !1 : (p(","), h()), u.push(n());
return p("]"), ["array", ...u];
return i();
}, i = () => {
if (t[u] === "[") {
u++, g();
const o = [];
let a = !0;
for (; u < t.length && t[u] !== "]"; )
a ? a = !1 : (d(","), g()), o.push(s());
return d("]"), ["array", ...o];
}
return a() ?? l() ?? O();
}, a = () => d(F, JSON.parse), i = () => d(W, (u) => u), l = () => d(U, JSON.parse), f = () => d(J, JSON.parse), O = () => {
const u = d(M, JSON.parse);
if (u !== void 0)
return u;
b("Value expected");
}, _ = () => {
h(), o < t.length && b(`Unexpected part '${t.substring(o)}'`);
}, d = (u, g) => {
const j = t.substring(o).match(u);
if (j)
return o += j[0].length, g(j[0]);
}, h = () => d(T, (u) => u), p = (u) => {
t[o] !== u && b(`Character '${u}' expected`), o++;
}, b = (u, g = o) => {
throw new SyntaxError(`${u} (pos: ${g})`);
return l() ?? N() ?? w();
}, l = () => b(U, JSON.parse), f = () => b(T, (o) => o), N = () => b(z, JSON.parse), j = () => b(B, JSON.parse), w = () => {
const o = b(K, JSON.parse);
if (o !== void 0)
return o;
_("Value expected");
}, S = () => {
g(), u < t.length && _(`Unexpected part '${t.substring(u)}'`);
}, b = (o, a) => {
const y = t.substring(u).match(o);
if (y)
return u += y[0].length, a(y[0]);
}, g = () => b(Z, (o) => o), d = (o) => {
t[u] !== o && _(`Character '${o}' expected`), u++;
}, _ = (o, a = u) => {
throw new SyntaxError(`${o} (pos: ${a})`);
};
let o = 0;
const A = n();
return _(), A;
let u = 0;
const I = s();
return S(), I;
}
const B = 40, Z = " ", D = (t, e) => {
const n = (e == null ? void 0 : e.indentation) ?? Z, r = (a, i) => E(a) ? s(a, i) : JSON.stringify(a), s = (a, i) => {
var h;
const [l, ...f] = a;
const C = 40, V = " ", G = (t, e) => {
const n = (e == null ? void 0 : e.indentation) ?? V, r = (h, i) => A(h) ? s(h, i) : JSON.stringify(h), s = (h, i) => {
var S;
const [l, ...f] = h;
if (l === "get" && f.length > 0)
return x(f);
return $(f);
if (l === "pipe") {
const p = f.map((b) => r(b, i + n));
return y(p, ["", " | ", ""], ["", `
const b = f.map((g) => r(g, i + n));
return O(b, ["", " | ", ""], ["", `
${i + n}| `, ""]);

@@ -295,5 +340,5 @@ }

if (l === "array") {
const p = f.map((b) => r(b, i));
return y(
p,
const b = f.map((g) => r(g, i));
return O(
b,
["[", ", ", "]"],

@@ -306,20 +351,20 @@ [`[

}
const O = ((h = e == null ? void 0 : e.operators) == null ? void 0 : h[l]) ?? P[l];
if (O && f.length === 2) {
const [p, b] = f, o = r(p, i), A = r(b, i);
return `(${o} ${O} ${A})`;
const N = ((S = e == null ? void 0 : e.operators) == null ? void 0 : S[l]) ?? R[l];
if (N && f.length === 2) {
const [b, g] = f, d = r(b, i), _ = r(g, i);
return `(${d} ${N} ${_})`;
}
const _ = f.length === 1 ? i : i + n, d = f.map((p) => r(p, _));
return f.length === 1 && d[0][0] === "(" ? `${l}${d}` : y(
d,
const j = f.length === 1 ? i : i + n, w = f.map((b) => r(b, j));
return f.length === 1 && w[0][0] === "(" ? `${l}${w[0]}` : O(
w,
[`${l}(`, ", ", ")"],
f.length === 1 ? [`${l}(`, `,
${i}`, ")"] : [`${l}(
${_}`, `,
${_}`, `
${j}`, `,
${j}`, `
${i})`]
);
}, c = (a, i) => {
const l = i + n, f = Object.entries(a).map(([O, _]) => `${w(O)}: ${r(_, l)}`);
return y(
}, c = (h, i) => {
const l = i + n, f = Object.entries(h).map(([N, j]) => `${k(N)}: ${r(j, l)}`);
return O(
f,

@@ -332,15 +377,15 @@ ["{ ", ", ", " }"],

);
}, x = (a) => a.map((i) => `.${w(i)}`).join(""), w = (a) => L.test(a) ? a : JSON.stringify(a), y = (a, [i, l, f], [O, _, d]) => i.length + a.reduce((p, b) => p + b.length + l.length, 0) - l.length + f.length <= ((e == null ? void 0 : e.maxLineLength) ?? B) ? i + a.join(l) + f : O + a.join(_) + d;
}, $ = (h) => h.map((i) => `.${k(i)}`).join(""), k = (h) => M.test(h) ? h : JSON.stringify(h), O = (h, [i, l, f], [N, j, w]) => i.length + h.reduce((b, g) => b + g.length + l.length, 0) - l.length + f.length <= ((e == null ? void 0 : e.maxLineLength) ?? C) ? i + h.join(l) + f : N + h.join(j) + w;
return r(t, "");
};
function K(t, e, n) {
return $(I(e) ? z(e, n) : e, n)(t);
function H(t, e, n) {
return m(W(e) ? D(e, n) : e, n)(t);
}
export {
m as buildFunction,
$ as compile,
K as jsonquery,
z as parse,
D as stringify
p as buildFunction,
m as compile,
H as jsonquery,
D as parse,
G as stringify
};
//# sourceMappingURL=jsonquery.js.map

@@ -31,2 +31,6 @@ export type JSONQueryPipe = JSONQuery[];

export type Getter = [key: string, Fun];
export interface Entry<T> {
key: string;
value: T;
}
//# sourceMappingURL=types.d.ts.map
{
"name": "@jsonquerylang/jsonquery",
"version": "4.0.0",
"version": "4.1.0",
"description": "A small, flexible, and expandable JSON query language",

@@ -56,11 +56,11 @@ "keywords": [

"devDependencies": {
"@biomejs/biome": "1.9.3",
"@vitest/coverage-v8": "2.1.2",
"@biomejs/biome": "1.9.4",
"@vitest/coverage-v8": "2.1.4",
"ajv": "8.17.1",
"npm-run-all": "4.1.5",
"semantic-release": "24.1.2",
"semantic-release": "24.2.0",
"typescript": "5.6.3",
"vite": "5.4.8",
"vitest": "2.1.2"
"vite": "5.4.11",
"vitest": "2.1.4"
}
}

@@ -13,4 +13,4 @@ # JSON Query

- Small: just `3.0 kB` when minified and gzipped! The JSON query engine without parse/stringify is only `1.4 kB`.
- Feature rich (40+ powerful functions)
- Small: just `3.3 kB` when minified and gzipped! The JSON query engine without parse/stringify is only `1.7 kB`.
- Feature rich (50+ powerful functions and operators)
- Easy to interoperate with thanks to the intermediate JSON format.

@@ -42,2 +42,4 @@ - Expressive

Install the JavaScript library via [npm](https://www.npmjs.com/):
```text

@@ -47,2 +49,4 @@ npm install @jsonquerylang/jsonquery

A Python implementation can be found here: https://github.com/jsonquerylang/jsonquery-python
## Usage

@@ -67,3 +71,3 @@

// sort them by age, and pick just the name and age out of the objects.
const names = jsonquery(data, `
const output = jsonquery(data, `
.friends

@@ -74,3 +78,3 @@ | filter(.city == "New York")

`)
// names = [
// output = [
// { "name": "Chris", "age": 23 },

@@ -81,6 +85,6 @@ // { "name": "Sarah", "age": 31 },

// The same query can be written in JSON format.
// The functions `parse` and `stringify` can be used
// The same query can be written in JSON format instead of the text format.
// Note that the functions `parse` and `stringify` can be used
// to convert from text format to JSON format and vice versa.
jsonquery(shoppingCart, [
jsonquery(data, [
"pipe",

@@ -87,0 +91,0 @@ ["get", "friends"],

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc