@uniqu/url 0.1.3 → 0.1.5
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 +8 -8
- package/dist/builder.mjs +8 -8
- package/package.json +2 -2
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);
|
|
@@ -37,7 +37,7 @@ function serializeFilter(expr) {
|
|
|
37
37
|
}
|
|
38
38
|
let result = "";
|
|
39
39
|
for (const [field, value] of Object.entries(expr)) if (value instanceof RegExp) {
|
|
40
|
-
const part = `${field}
|
|
40
|
+
const part = `${field}~=${serializeValue(value)}`;
|
|
41
41
|
result = result ? result + "&" + part : part;
|
|
42
42
|
} else if ((0, _uniqu_core.isPrimitive)(value)) {
|
|
43
43
|
const part = `${field}=${serializeValue(value)}`;
|
|
@@ -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) {
|
|
@@ -82,12 +82,12 @@ function serializeValue(value) {
|
|
|
82
82
|
if (value === true) return "true";
|
|
83
83
|
if (value === false) return "false";
|
|
84
84
|
if (typeof value === "number") return String(value);
|
|
85
|
-
if (value instanceof RegExp) return value.toString()
|
|
85
|
+
if (value instanceof RegExp) return `'${value.toString()}'`;
|
|
86
86
|
if (value instanceof Date) return `'${value.toISOString()}'`;
|
|
87
87
|
const str = String(value);
|
|
88
88
|
if (str === "null" || str === "true" || str === "false") return `'${str}'`;
|
|
89
89
|
if (/^\d/.test(str) && !isNaN(Number(str)) && !str.startsWith("0")) return `'${str}'`;
|
|
90
|
-
if (str.startsWith("/") && /\/[gimsuy]*$/.test(str)) return str
|
|
90
|
+
if (str.startsWith("/") && /\/[gimsuy]*$/.test(str)) return `'${str}'`;
|
|
91
91
|
if (/[&^=!<>~(){},\s'\\]/.test(str)) return `'${str.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
92
92
|
return str;
|
|
93
93
|
}
|
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);
|
|
@@ -36,7 +36,7 @@ function serializeFilter(expr) {
|
|
|
36
36
|
}
|
|
37
37
|
let result = "";
|
|
38
38
|
for (const [field, value] of Object.entries(expr)) if (value instanceof RegExp) {
|
|
39
|
-
const part = `${field}
|
|
39
|
+
const part = `${field}~=${serializeValue(value)}`;
|
|
40
40
|
result = result ? result + "&" + part : part;
|
|
41
41
|
} else if (isPrimitive(value)) {
|
|
42
42
|
const part = `${field}=${serializeValue(value)}`;
|
|
@@ -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) {
|
|
@@ -81,12 +81,12 @@ function serializeValue(value) {
|
|
|
81
81
|
if (value === true) return "true";
|
|
82
82
|
if (value === false) return "false";
|
|
83
83
|
if (typeof value === "number") return String(value);
|
|
84
|
-
if (value instanceof RegExp) return value.toString()
|
|
84
|
+
if (value instanceof RegExp) return `'${value.toString()}'`;
|
|
85
85
|
if (value instanceof Date) return `'${value.toISOString()}'`;
|
|
86
86
|
const str = String(value);
|
|
87
87
|
if (str === "null" || str === "true" || str === "false") return `'${str}'`;
|
|
88
88
|
if (/^\d/.test(str) && !isNaN(Number(str)) && !str.startsWith("0")) return `'${str}'`;
|
|
89
|
-
if (str.startsWith("/") && /\/[gimsuy]*$/.test(str)) return str
|
|
89
|
+
if (str.startsWith("/") && /\/[gimsuy]*$/.test(str)) return `'${str}'`;
|
|
90
90
|
if (/[&^=!<>~(){},\s'\\]/.test(str)) return `'${str.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
91
91
|
return str;
|
|
92
92
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniqu/url",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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.
|
|
48
|
+
"@uniqu/core": "^0.1.5"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"pub": "pnpm publish --access public",
|