@uniqu/url 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/builder.cjs CHANGED
@@ -14,11 +14,11 @@ let _uniqu_core = require("@uniqu/core");
14
14
  if (filterStr && controlStr) return filterStr + "&" + controlStr;
15
15
  return filterStr || controlStr;
16
16
  }
17
- function serializeFilter(expr) {
17
+ function serializeFilter(expr, parentOp) {
18
18
  if ("$and" in expr && expr.$and !== void 0) {
19
19
  let result = "";
20
20
  for (const child of expr.$and) {
21
- const s = serializeFilter(child);
21
+ const s = serializeFilter(child, "$and");
22
22
  if (s) result = result ? result + "&" + s : s;
23
23
  }
24
24
  return result;
@@ -26,10 +26,10 @@ function serializeFilter(expr) {
26
26
  if ("$or" in expr && expr.$or !== void 0) {
27
27
  let result = "";
28
28
  for (const child of expr.$or) {
29
- const s = serializeFilter(child);
29
+ const s = serializeFilter(child, "$or");
30
30
  if (s) result = result ? result + "^" + s : s;
31
31
  }
32
- return result;
32
+ return parentOp === "$and" && result ? `(${result})` : result;
33
33
  }
34
34
  if ("$not" in expr && expr.$not !== void 0) {
35
35
  const inner = serializeFilter(expr.$not);
@@ -46,7 +46,7 @@ function serializeFilter(expr) {
46
46
  const part = serializeComparison(field, op, opValue);
47
47
  result = result ? result + "&" + part : part;
48
48
  }
49
- return result;
49
+ return parentOp === "$or" && result.includes("&") ? `(${result})` : result;
50
50
  }
51
51
  function serializeComparison(field, op, value) {
52
52
  switch (op) {
@@ -77,18 +77,20 @@ function serializeComparison(field, op, value) {
77
77
  default: return `${field}${op}${serializeValue(value)}`;
78
78
  }
79
79
  }
80
+ const WORD_RE = /^[A-Za-z0-9_.]+$/u;
81
+ const NUMBER_RE = /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/u;
82
+ function quote(str) {
83
+ return `'${str.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/%/g, "%25")}'`;
84
+ }
80
85
  function serializeValue(value) {
81
86
  if (value === null) return "null";
82
87
  if (value === true) return "true";
83
88
  if (value === false) return "false";
84
89
  if (typeof value === "number") return String(value);
85
- if (value instanceof RegExp) return `'${value.toString()}'`;
86
- if (value instanceof Date) return `'${value.toISOString()}'`;
90
+ if (value instanceof RegExp) return quote(value.toString());
91
+ if (value instanceof Date) return quote(value.toISOString());
87
92
  const str = String(value);
88
- if (str === "null" || str === "true" || str === "false") return `'${str}'`;
89
- if (/^\d/.test(str) && !isNaN(Number(str)) && !str.startsWith("0")) return `'${str}'`;
90
- if (str.startsWith("/") && /\/[gimsuy]*$/.test(str)) return `'${str}'`;
91
- if (/[&^=!<>~(){},\s'\\]/.test(str)) return `'${str.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
93
+ if (str === "" || str === "null" || str === "true" || str === "false" || NUMBER_RE.test(str) || !WORD_RE.test(str)) return quote(str);
92
94
  return str;
93
95
  }
94
96
  const KNOWN_CONTROL_KEYS = new Set([
package/dist/builder.mjs CHANGED
@@ -13,11 +13,11 @@ import { isPrimitive } from "@uniqu/core";
13
13
  if (filterStr && controlStr) return filterStr + "&" + controlStr;
14
14
  return filterStr || controlStr;
15
15
  }
16
- function serializeFilter(expr) {
16
+ function serializeFilter(expr, parentOp) {
17
17
  if ("$and" in expr && expr.$and !== void 0) {
18
18
  let result = "";
19
19
  for (const child of expr.$and) {
20
- const s = serializeFilter(child);
20
+ const s = serializeFilter(child, "$and");
21
21
  if (s) result = result ? result + "&" + s : s;
22
22
  }
23
23
  return result;
@@ -25,10 +25,10 @@ function serializeFilter(expr) {
25
25
  if ("$or" in expr && expr.$or !== void 0) {
26
26
  let result = "";
27
27
  for (const child of expr.$or) {
28
- const s = serializeFilter(child);
28
+ const s = serializeFilter(child, "$or");
29
29
  if (s) result = result ? result + "^" + s : s;
30
30
  }
31
- return result;
31
+ return parentOp === "$and" && result ? `(${result})` : result;
32
32
  }
33
33
  if ("$not" in expr && expr.$not !== void 0) {
34
34
  const inner = serializeFilter(expr.$not);
@@ -45,7 +45,7 @@ function serializeFilter(expr) {
45
45
  const part = serializeComparison(field, op, opValue);
46
46
  result = result ? result + "&" + part : part;
47
47
  }
48
- return result;
48
+ return parentOp === "$or" && result.includes("&") ? `(${result})` : result;
49
49
  }
50
50
  function serializeComparison(field, op, value) {
51
51
  switch (op) {
@@ -76,18 +76,20 @@ function serializeComparison(field, op, value) {
76
76
  default: return `${field}${op}${serializeValue(value)}`;
77
77
  }
78
78
  }
79
+ const WORD_RE = /^[A-Za-z0-9_.]+$/u;
80
+ const NUMBER_RE = /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/u;
81
+ function quote(str) {
82
+ return `'${str.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/%/g, "%25")}'`;
83
+ }
79
84
  function serializeValue(value) {
80
85
  if (value === null) return "null";
81
86
  if (value === true) return "true";
82
87
  if (value === false) return "false";
83
88
  if (typeof value === "number") return String(value);
84
- if (value instanceof RegExp) return `'${value.toString()}'`;
85
- if (value instanceof Date) return `'${value.toISOString()}'`;
89
+ if (value instanceof RegExp) return quote(value.toString());
90
+ if (value instanceof Date) return quote(value.toISOString());
86
91
  const str = String(value);
87
- if (str === "null" || str === "true" || str === "false") return `'${str}'`;
88
- if (/^\d/.test(str) && !isNaN(Number(str)) && !str.startsWith("0")) return `'${str}'`;
89
- if (str.startsWith("/") && /\/[gimsuy]*$/.test(str)) return `'${str}'`;
90
- if (/[&^=!<>~(){},\s'\\]/.test(str)) return `'${str.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
92
+ if (str === "" || str === "null" || str === "true" || str === "false" || NUMBER_RE.test(str) || !WORD_RE.test(str)) return quote(str);
91
93
  return str;
92
94
  }
93
95
  const KNOWN_CONTROL_KEYS = new Set([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniqu/url",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "URL query string parser producing the Uniqu canonical query format",
5
5
  "license": "MIT",
6
6
  "author": "Artem Maltsev",
@@ -45,7 +45,7 @@
45
45
  "dist"
46
46
  ],
47
47
  "dependencies": {
48
- "@uniqu/core": "^0.1.4"
48
+ "@uniqu/core": "^0.1.6"
49
49
  },
50
50
  "scripts": {
51
51
  "pub": "pnpm publish --access public",