nhb-toolbox 4.0.46 → 4.0.48
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/form/convert.js +0 -12
- package/dist/cjs/object/sanitize.js +80 -17
- package/dist/dts/form/convert.d.ts.map +1 -1
- package/dist/dts/object/sanitize.d.ts +6 -6
- package/dist/dts/object/sanitize.d.ts.map +1 -1
- package/dist/dts/object/types.d.ts +17 -5
- package/dist/dts/object/types.d.ts.map +1 -1
- package/dist/esm/form/convert.js +0 -12
- package/dist/esm/object/sanitize.js +81 -18
- package/package.json +1 -1
package/dist/cjs/form/convert.js
CHANGED
|
@@ -212,18 +212,6 @@ const createControlledFormData = (data, configs) => {
|
|
|
212
212
|
_addToFormData(key, JSON.stringify(value));
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
|
-
// else if (Array.isArray(value) && value.every(isNotEmptyObject)) {
|
|
216
|
-
// if (shouldDotNotate(fullKey)) {
|
|
217
|
-
// value.forEach((item, index) =>
|
|
218
|
-
// _addToFormData(`${fullKey}[${index}]`, item),
|
|
219
|
-
// );
|
|
220
|
-
// } else if (shouldStringify(fullKey)) {
|
|
221
|
-
// _addToFormData(
|
|
222
|
-
// key,
|
|
223
|
-
// value.map((item) => _processObject(item, parentKey)),
|
|
224
|
-
// );
|
|
225
|
-
// }
|
|
226
|
-
// }
|
|
227
215
|
else {
|
|
228
216
|
// * For other cases, just append as key-value
|
|
229
217
|
_addToFormData(key, value);
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.sanitizeData = sanitizeData;
|
|
4
4
|
exports.parseObjectValues = parseObjectValues;
|
|
5
|
+
const guards_1 = require("../form/guards");
|
|
5
6
|
const non_primitives_1 = require("../guards/non-primitives");
|
|
7
|
+
const primitives_1 = require("../guards/primitives");
|
|
6
8
|
const basics_1 = require("../string/basics");
|
|
7
9
|
/**
|
|
8
10
|
* * Sanitizes a string, array of strings, an object or array of objects by ignoring specified keys and trimming string values.
|
|
@@ -13,9 +15,46 @@ const basics_1 = require("../string/basics");
|
|
|
13
15
|
* @returns A new string, object or array of strings or objects with the specified modifications.
|
|
14
16
|
*/
|
|
15
17
|
function sanitizeData(input, options) {
|
|
16
|
-
const { keysToIgnore
|
|
18
|
+
const { keysToIgnore = [], requiredKeys = [], trimStrings = true, ignoreNullish = false, ignoreFalsy = false, } = options || {};
|
|
17
19
|
// Flatten the object keys and use the keys for comparison
|
|
18
|
-
const ignoreKeySet = new Set(
|
|
20
|
+
const ignoreKeySet = new Set(keysToIgnore);
|
|
21
|
+
const isRequiredKey = (key) => {
|
|
22
|
+
return Array.isArray(requiredKeys) ?
|
|
23
|
+
requiredKeys.some((path) => key === path || key.startsWith(`${path}.`))
|
|
24
|
+
: requiredKeys === '*';
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* * Recursively process an array and its nested content(s).
|
|
28
|
+
* @param arr Array to process.
|
|
29
|
+
* @param path Full path as dot notation if needed.
|
|
30
|
+
* @returns Processed array.
|
|
31
|
+
*/
|
|
32
|
+
const _processArray = (arr, path) => {
|
|
33
|
+
return arr
|
|
34
|
+
.map((item) => {
|
|
35
|
+
if ((0, primitives_1.isString)(item) && trimStrings) {
|
|
36
|
+
return (0, basics_1.trimString)(item);
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(item)) {
|
|
39
|
+
// Recursive sanitize
|
|
40
|
+
return _processArray(item, path);
|
|
41
|
+
}
|
|
42
|
+
if ((0, non_primitives_1.isObject)(item)) {
|
|
43
|
+
return _processObject(item, path);
|
|
44
|
+
}
|
|
45
|
+
return item;
|
|
46
|
+
})
|
|
47
|
+
.filter((v) => {
|
|
48
|
+
if (ignoreNullish && v == null)
|
|
49
|
+
return false;
|
|
50
|
+
if (ignoreFalsy && !v)
|
|
51
|
+
return false;
|
|
52
|
+
if (ignoreFalsy && (0, non_primitives_1.isObject)(v) && !(0, non_primitives_1.isNotEmptyObject)(v)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
});
|
|
57
|
+
};
|
|
19
58
|
/**
|
|
20
59
|
* * Helper function to process a single object.
|
|
21
60
|
*
|
|
@@ -30,23 +69,38 @@ function sanitizeData(input, options) {
|
|
|
30
69
|
return acc;
|
|
31
70
|
}
|
|
32
71
|
// Exclude nullish values if specified
|
|
33
|
-
if (ignoreNullish && (
|
|
72
|
+
if (ignoreNullish && !isRequiredKey(fullKeyPath) && value == null) {
|
|
34
73
|
return acc;
|
|
35
74
|
}
|
|
36
|
-
//
|
|
37
|
-
if (
|
|
75
|
+
// Exclude falsy values `0`, `false`, `null` and `undefined`
|
|
76
|
+
if (ignoreFalsy && !value && !isRequiredKey(fullKeyPath)) {
|
|
77
|
+
return acc;
|
|
78
|
+
}
|
|
79
|
+
if ((0, primitives_1.isString)(value) && trimStrings) {
|
|
80
|
+
// Trim string values if enabled
|
|
38
81
|
acc[key] = (0, basics_1.trimString)(value);
|
|
39
82
|
}
|
|
40
|
-
else if (value &&
|
|
41
|
-
typeof value === 'object' &&
|
|
42
|
-
!Array.isArray(value)) {
|
|
83
|
+
else if (value && (0, non_primitives_1.isObject)(value)) {
|
|
43
84
|
// Recursively process nested objects
|
|
44
85
|
const processedValue = _processObject(value, fullKeyPath);
|
|
45
|
-
//
|
|
46
|
-
if (
|
|
86
|
+
// Add the property conditionally if it's not an empty object
|
|
87
|
+
if (!ignoreFalsy ||
|
|
88
|
+
isRequiredKey(fullKeyPath) ||
|
|
89
|
+
(0, non_primitives_1.isNotEmptyObject)(processedValue)) {
|
|
47
90
|
acc[key] = processedValue;
|
|
48
91
|
}
|
|
49
92
|
}
|
|
93
|
+
else if (value && Array.isArray(value)) {
|
|
94
|
+
// Keep file arrays untouched
|
|
95
|
+
if ((0, guards_1.isFileArray)(value) || (0, guards_1.isCustomFileArray)(value)) {
|
|
96
|
+
acc[key] = value;
|
|
97
|
+
}
|
|
98
|
+
// acc[key as keyof T] = value.map(sanitizeData) as T[keyof T];
|
|
99
|
+
const sanitizedArray = _processArray(value, fullKeyPath);
|
|
100
|
+
if (!ignoreFalsy || sanitizedArray.length > 0) {
|
|
101
|
+
acc[key] = sanitizedArray;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
50
104
|
else {
|
|
51
105
|
// Add other values as-is
|
|
52
106
|
acc[key] = value;
|
|
@@ -54,22 +108,31 @@ function sanitizeData(input, options) {
|
|
|
54
108
|
return acc;
|
|
55
109
|
}, {});
|
|
56
110
|
// Process strings
|
|
57
|
-
if (
|
|
111
|
+
if ((0, primitives_1.isString)(input)) {
|
|
58
112
|
return (0, basics_1.trimString)(input);
|
|
59
113
|
}
|
|
60
114
|
// Process array of strings and objects
|
|
61
115
|
if (Array.isArray(input)) {
|
|
62
116
|
// Process array of strings
|
|
63
|
-
if (
|
|
117
|
+
if ((0, non_primitives_1.isArrayOfType)(input, primitives_1.isString)) {
|
|
64
118
|
return (0, basics_1.trimString)(input);
|
|
65
119
|
}
|
|
66
|
-
//
|
|
120
|
+
// * Handle arrays with nested strings/arrays/objects
|
|
67
121
|
return input
|
|
68
|
-
.map((
|
|
69
|
-
.filter((
|
|
122
|
+
.map((item) => sanitizeData(item, options))
|
|
123
|
+
.filter((val) => {
|
|
124
|
+
if (ignoreNullish && val == null)
|
|
125
|
+
return false;
|
|
126
|
+
if (ignoreFalsy && !val)
|
|
127
|
+
return false;
|
|
128
|
+
if (ignoreFalsy && (0, non_primitives_1.isObject)(val) && !(0, non_primitives_1.isNotEmptyObject)(val)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
});
|
|
70
133
|
}
|
|
71
134
|
// Process object
|
|
72
|
-
if (
|
|
135
|
+
if ((0, non_primitives_1.isObject)(input)) {
|
|
73
136
|
return _processObject(input);
|
|
74
137
|
}
|
|
75
138
|
return input;
|
|
@@ -87,7 +150,7 @@ function parseObjectValues(object) {
|
|
|
87
150
|
const parsedBody = {};
|
|
88
151
|
if ((0, non_primitives_1.isNotEmptyObject)(object)) {
|
|
89
152
|
Object.entries(object).forEach(([key, value]) => {
|
|
90
|
-
if (
|
|
153
|
+
if (!(0, primitives_1.isString)(value)) {
|
|
91
154
|
parsedBody[key] = value;
|
|
92
155
|
return;
|
|
93
156
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../../src/form/convert.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAEX,aAAa,EAEb,MAAM,iBAAiB,CAAC;AAQzB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,GAAI,CAAC,SAAS,aAAa,QACzD,CAAC,YACG,eAAe,CAAC,CAAC,CAAC,KAC1B,
|
|
1
|
+
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../../src/form/convert.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAEX,aAAa,EAEb,MAAM,iBAAiB,CAAC;AAQzB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,GAAI,CAAC,SAAS,aAAa,QACzD,CAAC,YACG,eAAe,CAAC,CAAC,CAAC,KAC1B,QAqPF,CAAC"}
|
|
@@ -10,14 +10,14 @@ import type { GenericObject, SanitizeOptions, StrictObject } from './types';
|
|
|
10
10
|
*/
|
|
11
11
|
export declare function sanitizeData<T extends GenericObject>(object: T, options?: SanitizeOptions<T>): FlattenPartial<T>;
|
|
12
12
|
/**
|
|
13
|
-
* * Sanitizes
|
|
14
|
-
* *
|
|
13
|
+
* * Sanitizes a deeply nested array that may contain arrays, objects or other (mixed) data types.
|
|
14
|
+
* * Preserves structure while removing empty values and trimming strings and other operations.
|
|
15
15
|
*
|
|
16
|
-
* @param
|
|
17
|
-
* @param options - Options
|
|
18
|
-
* @returns A new array
|
|
16
|
+
* @param array - A mixed array that may contain arrays, objects or other data types.
|
|
17
|
+
* @param options - Options to trim and filter values.
|
|
18
|
+
* @returns A new sanitized array with the specified modifications.
|
|
19
19
|
*/
|
|
20
|
-
export declare function sanitizeData<T
|
|
20
|
+
export declare function sanitizeData<T>(array: T[], options?: SanitizeOptions<T>): FlattenPartial<T>[];
|
|
21
21
|
/**
|
|
22
22
|
* * Trims all the words in a string.
|
|
23
23
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../../src/object/sanitize.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../../src/object/sanitize.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,KAAK,EAEX,aAAa,EACb,eAAe,EACf,YAAY,EACZ,MAAM,SAAS,CAAC;AAEjB;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,aAAa,EACnD,MAAM,EAAE,CAAC,EACT,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAC1B,cAAc,CAAC,CAAC,CAAC,CAAC;AAErB;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC7B,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAC1B,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;AAEvB;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;AAEpD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;AAgKxD;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,aAAa,GAAG,YAAY,CAgCrE"}
|
|
@@ -41,14 +41,26 @@ export type NestedKeyString<T> = T extends AdvancedTypes ? never : T extends Gen
|
|
|
41
41
|
export type NestedPrimitiveKey<T> = T extends AdvancedTypes ? never : T extends GenericObject ? {
|
|
42
42
|
[K in keyof T & string]: NonNullable<T[K]> extends Primitive ? K : NonNullable<T[K]> extends GenericObject ? `${K}.${NestedPrimitiveKey<NonNullable<T[K]>>}` : never;
|
|
43
43
|
}[keyof T & string] : never;
|
|
44
|
-
/** - Options for `sanitizeData` */
|
|
45
|
-
export interface SanitizeOptions<T
|
|
46
|
-
/**
|
|
44
|
+
/** - Options for `sanitizeData` utility. */
|
|
45
|
+
export interface SanitizeOptions<T> {
|
|
46
|
+
/**
|
|
47
|
+
* An array of dot-notation keys to exclude from the sanitized output.
|
|
48
|
+
* This is only applicable when sanitizing plain objects or arrays of objects.
|
|
49
|
+
* When applied to nested or irregular array structures, behavior may be inconsistent or partially ignored.
|
|
50
|
+
*/
|
|
47
51
|
keysToIgnore?: DotNotationKey<T>[];
|
|
48
|
-
/** Whether to trim string values. Defaults to `true
|
|
52
|
+
/** Whether to trim string values. Defaults to `true`. */
|
|
49
53
|
trimStrings?: boolean;
|
|
50
|
-
/** Whether to exclude nullish (null or undefined) values. Defaults to `false
|
|
54
|
+
/** Whether to exclude nullish (`null` or `undefined`) values. Defaults to `false`. */
|
|
51
55
|
ignoreNullish?: boolean;
|
|
56
|
+
/** Whether to exclude all falsy values (`false`, `0`, `empty string: ''`, `null`, `undefined` and `empty object and arrays` (`{}`, `[]`)). Defaults to `false`. */
|
|
57
|
+
ignoreFalsy?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* An array of dot-notation key paths that must be preserved in the sanitized output.
|
|
60
|
+
* Use `"*"` to retain all keys. This applies primarily to plain or nested objects and arrays of objects.
|
|
61
|
+
* When applied to nested or irregular array structures, behavior may be inconsistent or partially ignored.
|
|
62
|
+
*/
|
|
63
|
+
requiredKeys?: '*' | DotNotationKey<T>[];
|
|
52
64
|
}
|
|
53
65
|
/** -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C3B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,GACzD,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1D,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GACrC,CAAC,CAAC,CAAC,CAAC;CACN,CAAC;AAEF,iDAAiD;AACjD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;KAC3B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,GACzD,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1D,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAC5B,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAC1B,MAAM;CACR,CAAC;AAEF,uFAAuF;AACvF,MAAM,MAAM,QAAQ,CAAC,MAAM,EAAE,MAAM,IAAI;KACrC,CAAC,IAAI,MAAM,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC;CACnD,CAAC"}
|
package/dist/esm/form/convert.js
CHANGED
|
@@ -209,18 +209,6 @@ export const createControlledFormData = (data, configs) => {
|
|
|
209
209
|
_addToFormData(key, JSON.stringify(value));
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
|
-
// else if (Array.isArray(value) && value.every(isNotEmptyObject)) {
|
|
213
|
-
// if (shouldDotNotate(fullKey)) {
|
|
214
|
-
// value.forEach((item, index) =>
|
|
215
|
-
// _addToFormData(`${fullKey}[${index}]`, item),
|
|
216
|
-
// );
|
|
217
|
-
// } else if (shouldStringify(fullKey)) {
|
|
218
|
-
// _addToFormData(
|
|
219
|
-
// key,
|
|
220
|
-
// value.map((item) => _processObject(item, parentKey)),
|
|
221
|
-
// );
|
|
222
|
-
// }
|
|
223
|
-
// }
|
|
224
212
|
else {
|
|
225
213
|
// * For other cases, just append as key-value
|
|
226
214
|
_addToFormData(key, value);
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isCustomFileArray, isFileArray } from '../form/guards';
|
|
2
|
+
import { isArrayOfType, isNotEmptyObject, isObject, } from '../guards/non-primitives';
|
|
3
|
+
import { isString } from '../guards/primitives';
|
|
2
4
|
import { trimString } from '../string/basics';
|
|
3
5
|
/**
|
|
4
6
|
* * Sanitizes a string, array of strings, an object or array of objects by ignoring specified keys and trimming string values.
|
|
@@ -9,9 +11,46 @@ import { trimString } from '../string/basics';
|
|
|
9
11
|
* @returns A new string, object or array of strings or objects with the specified modifications.
|
|
10
12
|
*/
|
|
11
13
|
export function sanitizeData(input, options) {
|
|
12
|
-
const { keysToIgnore
|
|
14
|
+
const { keysToIgnore = [], requiredKeys = [], trimStrings = true, ignoreNullish = false, ignoreFalsy = false, } = options || {};
|
|
13
15
|
// Flatten the object keys and use the keys for comparison
|
|
14
|
-
const ignoreKeySet = new Set(
|
|
16
|
+
const ignoreKeySet = new Set(keysToIgnore);
|
|
17
|
+
const isRequiredKey = (key) => {
|
|
18
|
+
return Array.isArray(requiredKeys) ?
|
|
19
|
+
requiredKeys.some((path) => key === path || key.startsWith(`${path}.`))
|
|
20
|
+
: requiredKeys === '*';
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* * Recursively process an array and its nested content(s).
|
|
24
|
+
* @param arr Array to process.
|
|
25
|
+
* @param path Full path as dot notation if needed.
|
|
26
|
+
* @returns Processed array.
|
|
27
|
+
*/
|
|
28
|
+
const _processArray = (arr, path) => {
|
|
29
|
+
return arr
|
|
30
|
+
.map((item) => {
|
|
31
|
+
if (isString(item) && trimStrings) {
|
|
32
|
+
return trimString(item);
|
|
33
|
+
}
|
|
34
|
+
if (Array.isArray(item)) {
|
|
35
|
+
// Recursive sanitize
|
|
36
|
+
return _processArray(item, path);
|
|
37
|
+
}
|
|
38
|
+
if (isObject(item)) {
|
|
39
|
+
return _processObject(item, path);
|
|
40
|
+
}
|
|
41
|
+
return item;
|
|
42
|
+
})
|
|
43
|
+
.filter((v) => {
|
|
44
|
+
if (ignoreNullish && v == null)
|
|
45
|
+
return false;
|
|
46
|
+
if (ignoreFalsy && !v)
|
|
47
|
+
return false;
|
|
48
|
+
if (ignoreFalsy && isObject(v) && !isNotEmptyObject(v)) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
});
|
|
53
|
+
};
|
|
15
54
|
/**
|
|
16
55
|
* * Helper function to process a single object.
|
|
17
56
|
*
|
|
@@ -26,23 +65,38 @@ export function sanitizeData(input, options) {
|
|
|
26
65
|
return acc;
|
|
27
66
|
}
|
|
28
67
|
// Exclude nullish values if specified
|
|
29
|
-
if (ignoreNullish && (
|
|
68
|
+
if (ignoreNullish && !isRequiredKey(fullKeyPath) && value == null) {
|
|
30
69
|
return acc;
|
|
31
70
|
}
|
|
32
|
-
//
|
|
33
|
-
if (
|
|
71
|
+
// Exclude falsy values `0`, `false`, `null` and `undefined`
|
|
72
|
+
if (ignoreFalsy && !value && !isRequiredKey(fullKeyPath)) {
|
|
73
|
+
return acc;
|
|
74
|
+
}
|
|
75
|
+
if (isString(value) && trimStrings) {
|
|
76
|
+
// Trim string values if enabled
|
|
34
77
|
acc[key] = trimString(value);
|
|
35
78
|
}
|
|
36
|
-
else if (value &&
|
|
37
|
-
typeof value === 'object' &&
|
|
38
|
-
!Array.isArray(value)) {
|
|
79
|
+
else if (value && isObject(value)) {
|
|
39
80
|
// Recursively process nested objects
|
|
40
81
|
const processedValue = _processObject(value, fullKeyPath);
|
|
41
|
-
//
|
|
42
|
-
if (
|
|
82
|
+
// Add the property conditionally if it's not an empty object
|
|
83
|
+
if (!ignoreFalsy ||
|
|
84
|
+
isRequiredKey(fullKeyPath) ||
|
|
85
|
+
isNotEmptyObject(processedValue)) {
|
|
43
86
|
acc[key] = processedValue;
|
|
44
87
|
}
|
|
45
88
|
}
|
|
89
|
+
else if (value && Array.isArray(value)) {
|
|
90
|
+
// Keep file arrays untouched
|
|
91
|
+
if (isFileArray(value) || isCustomFileArray(value)) {
|
|
92
|
+
acc[key] = value;
|
|
93
|
+
}
|
|
94
|
+
// acc[key as keyof T] = value.map(sanitizeData) as T[keyof T];
|
|
95
|
+
const sanitizedArray = _processArray(value, fullKeyPath);
|
|
96
|
+
if (!ignoreFalsy || sanitizedArray.length > 0) {
|
|
97
|
+
acc[key] = sanitizedArray;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
46
100
|
else {
|
|
47
101
|
// Add other values as-is
|
|
48
102
|
acc[key] = value;
|
|
@@ -50,22 +104,31 @@ export function sanitizeData(input, options) {
|
|
|
50
104
|
return acc;
|
|
51
105
|
}, {});
|
|
52
106
|
// Process strings
|
|
53
|
-
if (
|
|
107
|
+
if (isString(input)) {
|
|
54
108
|
return trimString(input);
|
|
55
109
|
}
|
|
56
110
|
// Process array of strings and objects
|
|
57
111
|
if (Array.isArray(input)) {
|
|
58
112
|
// Process array of strings
|
|
59
|
-
if (
|
|
113
|
+
if (isArrayOfType(input, isString)) {
|
|
60
114
|
return trimString(input);
|
|
61
115
|
}
|
|
62
|
-
//
|
|
116
|
+
// * Handle arrays with nested strings/arrays/objects
|
|
63
117
|
return input
|
|
64
|
-
.map((
|
|
65
|
-
.filter((
|
|
118
|
+
.map((item) => sanitizeData(item, options))
|
|
119
|
+
.filter((val) => {
|
|
120
|
+
if (ignoreNullish && val == null)
|
|
121
|
+
return false;
|
|
122
|
+
if (ignoreFalsy && !val)
|
|
123
|
+
return false;
|
|
124
|
+
if (ignoreFalsy && isObject(val) && !isNotEmptyObject(val)) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
});
|
|
66
129
|
}
|
|
67
130
|
// Process object
|
|
68
|
-
if (
|
|
131
|
+
if (isObject(input)) {
|
|
69
132
|
return _processObject(input);
|
|
70
133
|
}
|
|
71
134
|
return input;
|
|
@@ -83,7 +146,7 @@ export function parseObjectValues(object) {
|
|
|
83
146
|
const parsedBody = {};
|
|
84
147
|
if (isNotEmptyObject(object)) {
|
|
85
148
|
Object.entries(object).forEach(([key, value]) => {
|
|
86
|
-
if (
|
|
149
|
+
if (!isString(value)) {
|
|
87
150
|
parsedBody[key] = value;
|
|
88
151
|
return;
|
|
89
152
|
}
|
package/package.json
CHANGED