@splendidlabz/utils 1.14.0 → 1.15.1
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/cjs/dom/actions/index.cjs +18 -6
- package/dist/cjs/dom/actions/prefer-horizontal-scroll.cjs +18 -6
- package/dist/cjs/dom/index.cjs +43 -25
- package/dist/cjs/dom/pkce.cjs +16 -19
- package/dist/cjs/dom/sanitize.cjs +8 -1
- package/dist/cjs/lib/checks.cjs +7 -0
- package/dist/cjs/lib/form/form-data.cjs +10 -1
- package/dist/cjs/lib/form/index.cjs +11 -11
- package/dist/cjs/lib/form/sanitize.cjs +12 -10
- package/dist/cjs/lib/index.cjs +62 -16
- package/dist/cjs/lib/objects/index.cjs +58 -7
- package/dist/cjs/lib/objects/normalize-object.cjs +16 -5
- package/dist/cjs/lib/objects/omit-empty.cjs +18 -5
- package/dist/cjs/lib/objects/remove-invalid.cjs +61 -0
- package/dist/cjs/lib/objects/trim-values.cjs +47 -0
- package/dist/cjs/lib/promises/index.cjs +16 -5
- package/dist/cjs/lib/promises/reject.cjs +16 -5
- package/dist/cjs/lib/sse.cjs +16 -5
- package/dist/cjs/node/index.cjs +20 -17
- package/dist/cjs/node/pkce.cjs +5 -26
- package/dist/cjs/node/sanitize.cjs +8 -1
- package/dist/esm/dom/actions/prefer-horizontal-scroll.js +2 -1
- package/dist/esm/dom/pkce.js +14 -10
- package/dist/esm/lib/checks.js +6 -0
- package/dist/esm/lib/form/form-data.js +2 -1
- package/dist/esm/lib/form/sanitize.js +4 -10
- package/dist/esm/lib/objects/index.js +2 -0
- package/dist/esm/lib/objects/omit-empty.js +10 -5
- package/dist/esm/lib/objects/remove-invalid.js +28 -0
- package/dist/esm/lib/objects/trim-values.js +14 -0
- package/dist/esm/node/pkce.js +5 -8
- package/dist/types/dom/index.d.cts +1 -1
- package/dist/types/dom/pkce.d.cts +29 -4
- package/dist/types/lib/checks.d.cts +12 -2
- package/dist/types/lib/form/sanitize.d.cts +2 -2
- package/dist/types/lib/index.d.cts +3 -1
- package/dist/types/lib/objects/index.d.cts +2 -0
- package/dist/types/lib/objects/omit-empty.d.cts +26 -1
- package/dist/types/lib/objects/remove-invalid.d.cts +12 -0
- package/dist/types/lib/objects/trim-values.d.cts +8 -0
- package/dist/types/node/index.d.cts +1 -1
- package/dist/types/node/pkce.d.cts +29 -10
- package/package.json +1 -1
|
@@ -164,16 +164,27 @@ function nativeMasonrySupport(node) {
|
|
|
164
164
|
return getComputedStyle(node).gridTemplateRows === "masonry";
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
// src/lib/checks.js
|
|
168
|
+
function isPlainObject(x) {
|
|
169
|
+
if (typeof x !== "object" || x === null) return false;
|
|
170
|
+
const prototype = Object.getPrototypeOf(x);
|
|
171
|
+
return prototype === Object.prototype || prototype === null;
|
|
172
|
+
}
|
|
173
|
+
|
|
167
174
|
// src/lib/objects/omit-empty.js
|
|
168
|
-
function omitEmpty(obj,
|
|
169
|
-
|
|
170
|
-
if (obj
|
|
175
|
+
function omitEmpty(obj, options = {}) {
|
|
176
|
+
const { shallow = false, omitFalsey = false } = typeof options === "boolean" ? { shallow: options } : options;
|
|
177
|
+
if (!isPlainObject(obj) && !Array.isArray(obj)) return obj;
|
|
171
178
|
const result = Array.isArray(obj) ? [] : {};
|
|
172
179
|
for (const [key, value] of Object.entries(obj)) {
|
|
173
|
-
if (value === null || value === void 0 || value === ""
|
|
180
|
+
if (value === null || value === void 0 || value === "") continue;
|
|
181
|
+
if (omitFalsey && !value) continue;
|
|
182
|
+
if ((isPlainObject(value) || Array.isArray(value)) && Object.keys(value).length === 0) {
|
|
174
183
|
continue;
|
|
175
184
|
}
|
|
176
|
-
|
|
185
|
+
const filteredValue = shallow ? value : omitEmpty(value, { omitFalsey });
|
|
186
|
+
if (Array.isArray(result)) result.push(filteredValue);
|
|
187
|
+
else result[key] = filteredValue;
|
|
177
188
|
}
|
|
178
189
|
return result;
|
|
179
190
|
}
|
|
@@ -199,10 +210,11 @@ function preferHorizontalScroll(node, props = {}) {
|
|
|
199
210
|
currentSnapType: null,
|
|
200
211
|
snapTimeoutID: null
|
|
201
212
|
};
|
|
213
|
+
const cssSnapDelay = parseFloat(getCSSVar(node, "--scroll-snap-delay"));
|
|
202
214
|
const options = {
|
|
203
215
|
...DEFAULT_OPTIONS,
|
|
204
216
|
...omitEmpty({
|
|
205
|
-
scrollSnapDelay:
|
|
217
|
+
scrollSnapDelay: Number.isNaN(cssSnapDelay) ? void 0 : cssSnapDelay
|
|
206
218
|
}),
|
|
207
219
|
...omitEmpty(props)
|
|
208
220
|
};
|
|
@@ -23,16 +23,27 @@ __export(prefer_horizontal_scroll_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(prefer_horizontal_scroll_exports);
|
|
25
25
|
|
|
26
|
+
// src/lib/checks.js
|
|
27
|
+
function isPlainObject(x) {
|
|
28
|
+
if (typeof x !== "object" || x === null) return false;
|
|
29
|
+
const prototype = Object.getPrototypeOf(x);
|
|
30
|
+
return prototype === Object.prototype || prototype === null;
|
|
31
|
+
}
|
|
32
|
+
|
|
26
33
|
// src/lib/objects/omit-empty.js
|
|
27
|
-
function omitEmpty(obj,
|
|
28
|
-
|
|
29
|
-
if (obj
|
|
34
|
+
function omitEmpty(obj, options = {}) {
|
|
35
|
+
const { shallow = false, omitFalsey = false } = typeof options === "boolean" ? { shallow: options } : options;
|
|
36
|
+
if (!isPlainObject(obj) && !Array.isArray(obj)) return obj;
|
|
30
37
|
const result = Array.isArray(obj) ? [] : {};
|
|
31
38
|
for (const [key, value] of Object.entries(obj)) {
|
|
32
|
-
if (value === null || value === void 0 || value === ""
|
|
39
|
+
if (value === null || value === void 0 || value === "") continue;
|
|
40
|
+
if (omitFalsey && !value) continue;
|
|
41
|
+
if ((isPlainObject(value) || Array.isArray(value)) && Object.keys(value).length === 0) {
|
|
33
42
|
continue;
|
|
34
43
|
}
|
|
35
|
-
|
|
44
|
+
const filteredValue = shallow ? value : omitEmpty(value, { omitFalsey });
|
|
45
|
+
if (Array.isArray(result)) result.push(filteredValue);
|
|
46
|
+
else result[key] = filteredValue;
|
|
36
47
|
}
|
|
37
48
|
return result;
|
|
38
49
|
}
|
|
@@ -77,10 +88,11 @@ function preferHorizontalScroll(node, props = {}) {
|
|
|
77
88
|
currentSnapType: null,
|
|
78
89
|
snapTimeoutID: null
|
|
79
90
|
};
|
|
91
|
+
const cssSnapDelay = parseFloat(getCSSVar(node, "--scroll-snap-delay"));
|
|
80
92
|
const options = {
|
|
81
93
|
...DEFAULT_OPTIONS,
|
|
82
94
|
...omitEmpty({
|
|
83
|
-
scrollSnapDelay:
|
|
95
|
+
scrollSnapDelay: Number.isNaN(cssSnapDelay) ? void 0 : cssSnapDelay
|
|
84
96
|
}),
|
|
85
97
|
...omitEmpty(props)
|
|
86
98
|
};
|
package/dist/cjs/dom/index.cjs
CHANGED
|
@@ -44,6 +44,7 @@ __export(dom_exports, {
|
|
|
44
44
|
getCSSValue: () => getCSSValue,
|
|
45
45
|
getCSSVar: () => getCSSVar,
|
|
46
46
|
getChildrenElements: () => getChildrenElements,
|
|
47
|
+
getCodeChallenge: () => getCodeChallenge,
|
|
47
48
|
getCookie: () => getCookie,
|
|
48
49
|
getElement: () => getElement,
|
|
49
50
|
getFocusableElements: () => getFocusableElements,
|
|
@@ -305,16 +306,27 @@ function nativeMasonrySupport(node) {
|
|
|
305
306
|
return getComputedStyle(node).gridTemplateRows === "masonry";
|
|
306
307
|
}
|
|
307
308
|
|
|
309
|
+
// src/lib/checks.js
|
|
310
|
+
function isPlainObject(x) {
|
|
311
|
+
if (typeof x !== "object" || x === null) return false;
|
|
312
|
+
const prototype = Object.getPrototypeOf(x);
|
|
313
|
+
return prototype === Object.prototype || prototype === null;
|
|
314
|
+
}
|
|
315
|
+
|
|
308
316
|
// src/lib/objects/omit-empty.js
|
|
309
|
-
function omitEmpty(obj,
|
|
310
|
-
|
|
311
|
-
if (obj
|
|
317
|
+
function omitEmpty(obj, options = {}) {
|
|
318
|
+
const { shallow = false, omitFalsey = false } = typeof options === "boolean" ? { shallow: options } : options;
|
|
319
|
+
if (!isPlainObject(obj) && !Array.isArray(obj)) return obj;
|
|
312
320
|
const result = Array.isArray(obj) ? [] : {};
|
|
313
321
|
for (const [key, value] of Object.entries(obj)) {
|
|
314
|
-
if (value === null || value === void 0 || value === ""
|
|
322
|
+
if (value === null || value === void 0 || value === "") continue;
|
|
323
|
+
if (omitFalsey && !value) continue;
|
|
324
|
+
if ((isPlainObject(value) || Array.isArray(value)) && Object.keys(value).length === 0) {
|
|
315
325
|
continue;
|
|
316
326
|
}
|
|
317
|
-
|
|
327
|
+
const filteredValue = shallow ? value : omitEmpty(value, { omitFalsey });
|
|
328
|
+
if (Array.isArray(result)) result.push(filteredValue);
|
|
329
|
+
else result[key] = filteredValue;
|
|
318
330
|
}
|
|
319
331
|
return result;
|
|
320
332
|
}
|
|
@@ -346,10 +358,11 @@ function preferHorizontalScroll(node, props = {}) {
|
|
|
346
358
|
currentSnapType: null,
|
|
347
359
|
snapTimeoutID: null
|
|
348
360
|
};
|
|
361
|
+
const cssSnapDelay = parseFloat(getCSSVar(node, "--scroll-snap-delay"));
|
|
349
362
|
const options = {
|
|
350
363
|
...DEFAULT_OPTIONS,
|
|
351
364
|
...omitEmpty({
|
|
352
|
-
scrollSnapDelay:
|
|
365
|
+
scrollSnapDelay: Number.isNaN(cssSnapDelay) ? void 0 : cssSnapDelay
|
|
353
366
|
}),
|
|
354
367
|
...omitEmpty(props)
|
|
355
368
|
};
|
|
@@ -989,32 +1002,26 @@ function scrollObserver(node, options = {}) {
|
|
|
989
1002
|
};
|
|
990
1003
|
}
|
|
991
1004
|
|
|
992
|
-
// src/dom/random-string.js
|
|
993
|
-
function randomString(length = 10) {
|
|
994
|
-
const firstLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
|
|
995
|
-
const others = Math.random().toString(36).substring(2, 2 + length - 1);
|
|
996
|
-
return firstLetter + others;
|
|
997
|
-
}
|
|
998
|
-
function uuid() {
|
|
999
|
-
return crypto.randomUUID();
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
1005
|
// src/dom/pkce.js
|
|
1003
1006
|
async function PKCE() {
|
|
1004
|
-
const codeVerifier =
|
|
1005
|
-
const codeChallenge = await getCodeChallenge(codeVerifier);
|
|
1007
|
+
const codeVerifier = randomVerifier();
|
|
1006
1008
|
return {
|
|
1007
|
-
state:
|
|
1009
|
+
state: randomVerifier(),
|
|
1008
1010
|
code_verifier: codeVerifier,
|
|
1009
|
-
code_challenge:
|
|
1011
|
+
code_challenge: await getCodeChallenge(codeVerifier),
|
|
1010
1012
|
code_challenge_method: "S256"
|
|
1011
1013
|
};
|
|
1012
1014
|
}
|
|
1013
1015
|
async function getCodeChallenge(verifier) {
|
|
1014
|
-
const
|
|
1015
|
-
const
|
|
1016
|
-
|
|
1017
|
-
|
|
1016
|
+
const data = new TextEncoder().encode(verifier);
|
|
1017
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
1018
|
+
return toBase64Url(new Uint8Array(hash));
|
|
1019
|
+
}
|
|
1020
|
+
function randomVerifier() {
|
|
1021
|
+
return toBase64Url(crypto.getRandomValues(new Uint8Array(32)));
|
|
1022
|
+
}
|
|
1023
|
+
function toBase64Url(bytes) {
|
|
1024
|
+
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
1018
1025
|
}
|
|
1019
1026
|
|
|
1020
1027
|
// src/dom/query-params.js
|
|
@@ -1034,6 +1041,16 @@ var queryParams = {
|
|
|
1034
1041
|
}
|
|
1035
1042
|
};
|
|
1036
1043
|
|
|
1044
|
+
// src/dom/random-string.js
|
|
1045
|
+
function randomString(length = 10) {
|
|
1046
|
+
const firstLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
|
|
1047
|
+
const others = Math.random().toString(36).substring(2, 2 + length - 1);
|
|
1048
|
+
return firstLetter + others;
|
|
1049
|
+
}
|
|
1050
|
+
function uuid() {
|
|
1051
|
+
return crypto.randomUUID();
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1037
1054
|
// src/dom/sanitize.js
|
|
1038
1055
|
var import_dompurify = __toESM(require("dompurify"), 1);
|
|
1039
1056
|
|
|
@@ -1043,7 +1060,7 @@ function sanitize(value, options = {}) {
|
|
|
1043
1060
|
if (!sanitizer) throw new Error("sanitizer function is required");
|
|
1044
1061
|
if (typeof value === "string") return sanitizer(value, rest);
|
|
1045
1062
|
if (Array.isArray(value)) return sanitizeArray(value, { sanitizer, ...rest });
|
|
1046
|
-
if (value
|
|
1063
|
+
if (isPlainObject(value)) {
|
|
1047
1064
|
return Object.fromEntries(
|
|
1048
1065
|
Object.entries(value).map(([key, val]) => [
|
|
1049
1066
|
key,
|
|
@@ -1221,6 +1238,7 @@ function scrambleText(text) {
|
|
|
1221
1238
|
getCSSValue,
|
|
1222
1239
|
getCSSVar,
|
|
1223
1240
|
getChildrenElements,
|
|
1241
|
+
getCodeChallenge,
|
|
1224
1242
|
getCookie,
|
|
1225
1243
|
getElement,
|
|
1226
1244
|
getFocusableElements,
|
package/dist/cjs/dom/pkce.cjs
CHANGED
|
@@ -19,35 +19,32 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/dom/pkce.js
|
|
20
20
|
var pkce_exports = {};
|
|
21
21
|
__export(pkce_exports, {
|
|
22
|
-
PKCE: () => PKCE
|
|
22
|
+
PKCE: () => PKCE,
|
|
23
|
+
getCodeChallenge: () => getCodeChallenge
|
|
23
24
|
});
|
|
24
25
|
module.exports = __toCommonJS(pkce_exports);
|
|
25
|
-
|
|
26
|
-
// src/dom/random-string.js
|
|
27
|
-
function randomString(length = 10) {
|
|
28
|
-
const firstLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
|
|
29
|
-
const others = Math.random().toString(36).substring(2, 2 + length - 1);
|
|
30
|
-
return firstLetter + others;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// src/dom/pkce.js
|
|
34
26
|
async function PKCE() {
|
|
35
|
-
const codeVerifier =
|
|
36
|
-
const codeChallenge = await getCodeChallenge(codeVerifier);
|
|
27
|
+
const codeVerifier = randomVerifier();
|
|
37
28
|
return {
|
|
38
|
-
state:
|
|
29
|
+
state: randomVerifier(),
|
|
39
30
|
code_verifier: codeVerifier,
|
|
40
|
-
code_challenge:
|
|
31
|
+
code_challenge: await getCodeChallenge(codeVerifier),
|
|
41
32
|
code_challenge_method: "S256"
|
|
42
33
|
};
|
|
43
34
|
}
|
|
44
35
|
async function getCodeChallenge(verifier) {
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
36
|
+
const data = new TextEncoder().encode(verifier);
|
|
37
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
38
|
+
return toBase64Url(new Uint8Array(hash));
|
|
39
|
+
}
|
|
40
|
+
function randomVerifier() {
|
|
41
|
+
return toBase64Url(crypto.getRandomValues(new Uint8Array(32)));
|
|
42
|
+
}
|
|
43
|
+
function toBase64Url(bytes) {
|
|
44
|
+
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
49
45
|
}
|
|
50
46
|
// Annotate the CommonJS export names for ESM import in node:
|
|
51
47
|
0 && (module.exports = {
|
|
52
|
-
PKCE
|
|
48
|
+
PKCE,
|
|
49
|
+
getCodeChallenge
|
|
53
50
|
});
|
|
@@ -34,13 +34,20 @@ __export(sanitize_exports, {
|
|
|
34
34
|
module.exports = __toCommonJS(sanitize_exports);
|
|
35
35
|
var import_dompurify = __toESM(require("dompurify"), 1);
|
|
36
36
|
|
|
37
|
+
// src/lib/checks.js
|
|
38
|
+
function isPlainObject(x) {
|
|
39
|
+
if (typeof x !== "object" || x === null) return false;
|
|
40
|
+
const prototype = Object.getPrototypeOf(x);
|
|
41
|
+
return prototype === Object.prototype || prototype === null;
|
|
42
|
+
}
|
|
43
|
+
|
|
37
44
|
// src/lib/form/sanitize.js
|
|
38
45
|
function sanitize(value, options = {}) {
|
|
39
46
|
const { sanitizer, ...rest } = options;
|
|
40
47
|
if (!sanitizer) throw new Error("sanitizer function is required");
|
|
41
48
|
if (typeof value === "string") return sanitizer(value, rest);
|
|
42
49
|
if (Array.isArray(value)) return sanitizeArray(value, { sanitizer, ...rest });
|
|
43
|
-
if (value
|
|
50
|
+
if (isPlainObject(value)) {
|
|
44
51
|
return Object.fromEntries(
|
|
45
52
|
Object.entries(value).map(([key, val]) => [
|
|
46
53
|
key,
|
package/dist/cjs/lib/checks.cjs
CHANGED
|
@@ -23,12 +23,18 @@ __export(checks_exports, {
|
|
|
23
23
|
isArray: () => isArray,
|
|
24
24
|
isFunction: () => isFunction,
|
|
25
25
|
isObject: () => isObject,
|
|
26
|
+
isPlainObject: () => isPlainObject,
|
|
26
27
|
notObject: () => notObject
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(checks_exports);
|
|
29
30
|
function isObject(x) {
|
|
30
31
|
return typeof x === "object" && !Array.isArray(x) && x !== null;
|
|
31
32
|
}
|
|
33
|
+
function isPlainObject(x) {
|
|
34
|
+
if (typeof x !== "object" || x === null) return false;
|
|
35
|
+
const prototype = Object.getPrototypeOf(x);
|
|
36
|
+
return prototype === Object.prototype || prototype === null;
|
|
37
|
+
}
|
|
32
38
|
function isArray(x) {
|
|
33
39
|
return Array.isArray(x);
|
|
34
40
|
}
|
|
@@ -59,5 +65,6 @@ function isFunction(x) {
|
|
|
59
65
|
isArray,
|
|
60
66
|
isFunction,
|
|
61
67
|
isObject,
|
|
68
|
+
isPlainObject,
|
|
62
69
|
notObject
|
|
63
70
|
});
|
|
@@ -23,6 +23,15 @@ __export(form_data_exports, {
|
|
|
23
23
|
formDataToObject: () => formDataToObject
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(form_data_exports);
|
|
26
|
+
|
|
27
|
+
// src/lib/checks.js
|
|
28
|
+
function isPlainObject(x) {
|
|
29
|
+
if (typeof x !== "object" || x === null) return false;
|
|
30
|
+
const prototype = Object.getPrototypeOf(x);
|
|
31
|
+
return prototype === Object.prototype || prototype === null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/lib/form/form-data.js
|
|
26
35
|
function formDataToObject(formData) {
|
|
27
36
|
const obj = {};
|
|
28
37
|
for (let [key, value] of formData) {
|
|
@@ -63,7 +72,7 @@ function flattenArrayFields(inputObject) {
|
|
|
63
72
|
}
|
|
64
73
|
}
|
|
65
74
|
for (const key in result) {
|
|
66
|
-
if (
|
|
75
|
+
if (isPlainObject(result[key]) || Array.isArray(result[key])) {
|
|
67
76
|
if (Object.keys(result[key]).every((k) => !isNaN(parseInt(k)))) {
|
|
68
77
|
result[key] = Object.values(result[key]).filter((item) => {
|
|
69
78
|
if (typeof item === "object") {
|
|
@@ -27,6 +27,13 @@ __export(form_exports, {
|
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(form_exports);
|
|
29
29
|
|
|
30
|
+
// src/lib/checks.js
|
|
31
|
+
function isPlainObject(x) {
|
|
32
|
+
if (typeof x !== "object" || x === null) return false;
|
|
33
|
+
const prototype = Object.getPrototypeOf(x);
|
|
34
|
+
return prototype === Object.prototype || prototype === null;
|
|
35
|
+
}
|
|
36
|
+
|
|
30
37
|
// src/lib/form/form-data.js
|
|
31
38
|
function formDataToObject(formData) {
|
|
32
39
|
const obj = {};
|
|
@@ -68,7 +75,7 @@ function flattenArrayFields(inputObject) {
|
|
|
68
75
|
}
|
|
69
76
|
}
|
|
70
77
|
for (const key in result) {
|
|
71
|
-
if (
|
|
78
|
+
if (isPlainObject(result[key]) || Array.isArray(result[key])) {
|
|
72
79
|
if (Object.keys(result[key]).every((k) => !isNaN(parseInt(k)))) {
|
|
73
80
|
result[key] = Object.values(result[key]).filter((item) => {
|
|
74
81
|
if (typeof item === "object") {
|
|
@@ -88,7 +95,7 @@ function sanitize(value, options = {}) {
|
|
|
88
95
|
if (!sanitizer) throw new Error("sanitizer function is required");
|
|
89
96
|
if (typeof value === "string") return sanitizer(value, rest);
|
|
90
97
|
if (Array.isArray(value)) return sanitizeArray(value, { sanitizer, ...rest });
|
|
91
|
-
if (value
|
|
98
|
+
if (isPlainObject(value)) {
|
|
92
99
|
return Object.fromEntries(
|
|
93
100
|
Object.entries(value).map(([key, val]) => [
|
|
94
101
|
key,
|
|
@@ -102,15 +109,8 @@ function sanitizeArray(arr, { sanitizer, ...rest }) {
|
|
|
102
109
|
if (!sanitizer) throw new Error("sanitizer function is required");
|
|
103
110
|
return arr.map((item) => sanitize(item, { sanitizer, ...rest }));
|
|
104
111
|
}
|
|
105
|
-
function sanitizeObject(obj,
|
|
106
|
-
|
|
107
|
-
if (Array.isArray(obj)) return sanitizeArray(obj, { sanitizer, ...options });
|
|
108
|
-
return Object.fromEntries(
|
|
109
|
-
Object.entries(obj).map(([key, value]) => [
|
|
110
|
-
key,
|
|
111
|
-
sanitize(value, { sanitizer, ...options })
|
|
112
|
-
])
|
|
113
|
-
);
|
|
112
|
+
function sanitizeObject(obj, options = {}) {
|
|
113
|
+
return sanitize(obj, options);
|
|
114
114
|
}
|
|
115
115
|
// Annotate the CommonJS export names for ESM import in node:
|
|
116
116
|
0 && (module.exports = {
|
|
@@ -24,12 +24,21 @@ __export(sanitize_exports, {
|
|
|
24
24
|
sanitizeObject: () => sanitizeObject
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(sanitize_exports);
|
|
27
|
+
|
|
28
|
+
// src/lib/checks.js
|
|
29
|
+
function isPlainObject(x) {
|
|
30
|
+
if (typeof x !== "object" || x === null) return false;
|
|
31
|
+
const prototype = Object.getPrototypeOf(x);
|
|
32
|
+
return prototype === Object.prototype || prototype === null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/lib/form/sanitize.js
|
|
27
36
|
function sanitize(value, options = {}) {
|
|
28
37
|
const { sanitizer, ...rest } = options;
|
|
29
38
|
if (!sanitizer) throw new Error("sanitizer function is required");
|
|
30
39
|
if (typeof value === "string") return sanitizer(value, rest);
|
|
31
40
|
if (Array.isArray(value)) return sanitizeArray(value, { sanitizer, ...rest });
|
|
32
|
-
if (value
|
|
41
|
+
if (isPlainObject(value)) {
|
|
33
42
|
return Object.fromEntries(
|
|
34
43
|
Object.entries(value).map(([key, val]) => [
|
|
35
44
|
key,
|
|
@@ -43,15 +52,8 @@ function sanitizeArray(arr, { sanitizer, ...rest }) {
|
|
|
43
52
|
if (!sanitizer) throw new Error("sanitizer function is required");
|
|
44
53
|
return arr.map((item) => sanitize(item, { sanitizer, ...rest }));
|
|
45
54
|
}
|
|
46
|
-
function sanitizeObject(obj,
|
|
47
|
-
|
|
48
|
-
if (Array.isArray(obj)) return sanitizeArray(obj, { sanitizer, ...options });
|
|
49
|
-
return Object.fromEntries(
|
|
50
|
-
Object.entries(obj).map(([key, value]) => [
|
|
51
|
-
key,
|
|
52
|
-
sanitize(value, { sanitizer, ...options })
|
|
53
|
-
])
|
|
54
|
-
);
|
|
55
|
+
function sanitizeObject(obj, options = {}) {
|
|
56
|
+
return sanitize(obj, options);
|
|
55
57
|
}
|
|
56
58
|
// Annotate the CommonJS export names for ESM import in node:
|
|
57
59
|
0 && (module.exports = {
|
package/dist/cjs/lib/index.cjs
CHANGED
|
@@ -65,6 +65,7 @@ __export(lib_exports, {
|
|
|
65
65
|
isHashedValue: () => isHashedValue,
|
|
66
66
|
isLastItem: () => isLastItem,
|
|
67
67
|
isObject: () => isObject,
|
|
68
|
+
isPlainObject: () => isPlainObject,
|
|
68
69
|
isPlural: () => isPlural,
|
|
69
70
|
isSingular: () => isSingular,
|
|
70
71
|
joinWithConjunction: () => joinWithConjunction,
|
|
@@ -91,6 +92,7 @@ __export(lib_exports, {
|
|
|
91
92
|
plural: () => plural,
|
|
92
93
|
pluralize: () => pluralize,
|
|
93
94
|
reject: () => reject,
|
|
95
|
+
removeInvalid: () => removeInvalid,
|
|
94
96
|
sanitize: () => sanitize,
|
|
95
97
|
sanitizeArray: () => sanitizeArray,
|
|
96
98
|
sanitizeObject: () => sanitizeObject,
|
|
@@ -128,6 +130,7 @@ __export(lib_exports, {
|
|
|
128
130
|
toUpper: () => toUpper,
|
|
129
131
|
toWeeks: () => toWeeks,
|
|
130
132
|
treatMarkdownWhitespace: () => treatMarkdownWhitespace,
|
|
133
|
+
trimValues: () => trimValues,
|
|
131
134
|
uniqueArray: () => uniqueArray,
|
|
132
135
|
wait: () => wait
|
|
133
136
|
});
|
|
@@ -267,6 +270,11 @@ function isMatch(routeConfig, pathname) {
|
|
|
267
270
|
function isObject(x) {
|
|
268
271
|
return typeof x === "object" && !Array.isArray(x) && x !== null;
|
|
269
272
|
}
|
|
273
|
+
function isPlainObject(x) {
|
|
274
|
+
if (typeof x !== "object" || x === null) return false;
|
|
275
|
+
const prototype = Object.getPrototypeOf(x);
|
|
276
|
+
return prototype === Object.prototype || prototype === null;
|
|
277
|
+
}
|
|
270
278
|
function isArray(x) {
|
|
271
279
|
return Array.isArray(x);
|
|
272
280
|
}
|
|
@@ -525,7 +533,7 @@ function flattenArrayFields(inputObject) {
|
|
|
525
533
|
}
|
|
526
534
|
}
|
|
527
535
|
for (const key in result) {
|
|
528
|
-
if (
|
|
536
|
+
if (isPlainObject(result[key]) || Array.isArray(result[key])) {
|
|
529
537
|
if (Object.keys(result[key]).every((k) => !isNaN(parseInt(k)))) {
|
|
530
538
|
result[key] = Object.values(result[key]).filter((item) => {
|
|
531
539
|
if (typeof item === "object") {
|
|
@@ -545,7 +553,7 @@ function sanitize(value, options = {}) {
|
|
|
545
553
|
if (!sanitizer) throw new Error("sanitizer function is required");
|
|
546
554
|
if (typeof value === "string") return sanitizer(value, rest);
|
|
547
555
|
if (Array.isArray(value)) return sanitizeArray(value, { sanitizer, ...rest });
|
|
548
|
-
if (value
|
|
556
|
+
if (isPlainObject(value)) {
|
|
549
557
|
return Object.fromEntries(
|
|
550
558
|
Object.entries(value).map(([key, val]) => [
|
|
551
559
|
key,
|
|
@@ -559,15 +567,8 @@ function sanitizeArray(arr, { sanitizer, ...rest }) {
|
|
|
559
567
|
if (!sanitizer) throw new Error("sanitizer function is required");
|
|
560
568
|
return arr.map((item) => sanitize(item, { sanitizer, ...rest }));
|
|
561
569
|
}
|
|
562
|
-
function sanitizeObject(obj,
|
|
563
|
-
|
|
564
|
-
if (Array.isArray(obj)) return sanitizeArray(obj, { sanitizer, ...options });
|
|
565
|
-
return Object.fromEntries(
|
|
566
|
-
Object.entries(obj).map(([key, value]) => [
|
|
567
|
-
key,
|
|
568
|
-
sanitize(value, { sanitizer, ...options })
|
|
569
|
-
])
|
|
570
|
-
);
|
|
570
|
+
function sanitizeObject(obj, options = {}) {
|
|
571
|
+
return sanitize(obj, options);
|
|
571
572
|
}
|
|
572
573
|
|
|
573
574
|
// src/lib/functions/debounce.js
|
|
@@ -972,15 +973,19 @@ function parseJSON(value, defaultValue = {}) {
|
|
|
972
973
|
}
|
|
973
974
|
|
|
974
975
|
// src/lib/objects/omit-empty.js
|
|
975
|
-
function omitEmpty(obj,
|
|
976
|
-
|
|
977
|
-
if (obj
|
|
976
|
+
function omitEmpty(obj, options = {}) {
|
|
977
|
+
const { shallow = false, omitFalsey = false } = typeof options === "boolean" ? { shallow: options } : options;
|
|
978
|
+
if (!isPlainObject(obj) && !Array.isArray(obj)) return obj;
|
|
978
979
|
const result = Array.isArray(obj) ? [] : {};
|
|
979
980
|
for (const [key, value] of Object.entries(obj)) {
|
|
980
|
-
if (value === null || value === void 0 || value === ""
|
|
981
|
+
if (value === null || value === void 0 || value === "") continue;
|
|
982
|
+
if (omitFalsey && !value) continue;
|
|
983
|
+
if ((isPlainObject(value) || Array.isArray(value)) && Object.keys(value).length === 0) {
|
|
981
984
|
continue;
|
|
982
985
|
}
|
|
983
|
-
|
|
986
|
+
const filteredValue = shallow ? value : omitEmpty(value, { omitFalsey });
|
|
987
|
+
if (Array.isArray(result)) result.push(filteredValue);
|
|
988
|
+
else result[key] = filteredValue;
|
|
984
989
|
}
|
|
985
990
|
return result;
|
|
986
991
|
}
|
|
@@ -990,6 +995,32 @@ function normalizeObject(object) {
|
|
|
990
995
|
return pipe(omitEmpty, camelCaseKeys)(object);
|
|
991
996
|
}
|
|
992
997
|
|
|
998
|
+
// src/lib/objects/remove-invalid.js
|
|
999
|
+
function removeInvalid(values, schema) {
|
|
1000
|
+
if (!schema) return values;
|
|
1001
|
+
if (Array.isArray(values)) {
|
|
1002
|
+
const kept2 = [];
|
|
1003
|
+
for (const item of values) {
|
|
1004
|
+
if (!isValid(item, schema)) continue;
|
|
1005
|
+
kept2.push(removeInvalid(item, schema));
|
|
1006
|
+
}
|
|
1007
|
+
return kept2;
|
|
1008
|
+
}
|
|
1009
|
+
if (!isPlainObject(values) || !isPlainObject(schema)) return values;
|
|
1010
|
+
const kept = {};
|
|
1011
|
+
for (const [key, value] of Object.entries(values)) {
|
|
1012
|
+
const keySchema = schema[key];
|
|
1013
|
+
if (!isValid(value, keySchema)) continue;
|
|
1014
|
+
kept[key] = removeInvalid(value, keySchema);
|
|
1015
|
+
}
|
|
1016
|
+
return kept;
|
|
1017
|
+
}
|
|
1018
|
+
function isValid(value, pattern) {
|
|
1019
|
+
if (typeof value !== "string") return true;
|
|
1020
|
+
if (typeof pattern !== "string" && !(pattern instanceof RegExp)) return true;
|
|
1021
|
+
return new RegExp(pattern).test(value);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
993
1024
|
// src/lib/objects/size.js
|
|
994
1025
|
function sizeOf(obj) {
|
|
995
1026
|
let bytes = 0;
|
|
@@ -1040,6 +1071,18 @@ function splitObject(obj, keys) {
|
|
|
1040
1071
|
return { picked, omitted, p: picked, o: omitted };
|
|
1041
1072
|
}
|
|
1042
1073
|
|
|
1074
|
+
// src/lib/objects/trim-values.js
|
|
1075
|
+
function trimValues(values) {
|
|
1076
|
+
if (typeof values === "string") return values.trim();
|
|
1077
|
+
if (Array.isArray(values)) return values.map(trimValues);
|
|
1078
|
+
if (!isPlainObject(values)) return values;
|
|
1079
|
+
const trimmed = {};
|
|
1080
|
+
for (const [key, value] of Object.entries(values)) {
|
|
1081
|
+
trimmed[key] = trimValues(value);
|
|
1082
|
+
}
|
|
1083
|
+
return trimmed;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1043
1086
|
// src/lib/promises/reject.js
|
|
1044
1087
|
function reject(props, { includeStack = true } = {}) {
|
|
1045
1088
|
const { status, statusText: statusText2, body, message } = props;
|
|
@@ -1715,6 +1758,7 @@ function getSymbolValue(object, description) {
|
|
|
1715
1758
|
isHashedValue,
|
|
1716
1759
|
isLastItem,
|
|
1717
1760
|
isObject,
|
|
1761
|
+
isPlainObject,
|
|
1718
1762
|
isPlural,
|
|
1719
1763
|
isSingular,
|
|
1720
1764
|
joinWithConjunction,
|
|
@@ -1741,6 +1785,7 @@ function getSymbolValue(object, description) {
|
|
|
1741
1785
|
plural,
|
|
1742
1786
|
pluralize,
|
|
1743
1787
|
reject,
|
|
1788
|
+
removeInvalid,
|
|
1744
1789
|
sanitize,
|
|
1745
1790
|
sanitizeArray,
|
|
1746
1791
|
sanitizeObject,
|
|
@@ -1778,6 +1823,7 @@ function getSymbolValue(object, description) {
|
|
|
1778
1823
|
toUpper,
|
|
1779
1824
|
toWeeks,
|
|
1780
1825
|
treatMarkdownWhitespace,
|
|
1826
|
+
trimValues,
|
|
1781
1827
|
uniqueArray,
|
|
1782
1828
|
wait
|
|
1783
1829
|
});
|