nhb-toolbox 1.6.9 → 1.7.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.
@@ -1,4 +1,5 @@
1
- import type { Flattened, ConfigOptions, OptionInput, SortOptions, InputObject, OrderOption } from './types';
1
+ import type { GenericObject } from '../object/types';
2
+ import type { Flattened } from './types';
2
3
  /**
3
4
  * * Flattens a nested array recursively or wraps any non-array data type in an array.
4
5
  *
@@ -7,50 +8,6 @@ import type { Flattened, ConfigOptions, OptionInput, SortOptions, InputObject, O
7
8
  * @returns A fully flattened array of type `Flatten<T>`. If the input is not an array, it wraps it in a single-element array.
8
9
  */
9
10
  export declare const flattenArray: <T>(input: T | T[]) => Flattened<T>[];
10
- /**
11
- * * Converts an array of objects into a formatted array of options.
12
- *
13
- * @typeParam T - The type of each object in the `data` array.
14
- * @param data - An array of objects to convert into options.
15
- * @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
16
- * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
17
- */
18
- export declare const createOptionsArray: <T extends OptionInput, K1 extends string = "value", K2 extends string = "label">(data: T[], config: ConfigOptions<T, K1, K2>) => { [P in K1 | K2]: string; }[];
19
- /**
20
- * * Sorts an array of strings.
21
- *
22
- * @param array - The array of strings to sort.
23
- * @param options - Sorting options.
24
- * @returns The sorted array.
25
- */
26
- export declare function sortAnArray(array: string[], options?: OrderOption): string[];
27
- /**
28
- * * Sorts an array of numbers.
29
- *
30
- * @param array - The array of numbers to sort.
31
- * @param options - Sorting options.
32
- * @returns The sorted array.
33
- */
34
- export declare function sortAnArray(array: number[], options?: OrderOption): number[];
35
- /**
36
- * * Sorts an array of booleans.
37
- *
38
- * @param array - The array of booleans to sort.
39
- * @param options - Sorting options.
40
- * @returns The sorted array.
41
- */
42
- export declare function sortAnArray(array: boolean[], options?: OrderOption): boolean[];
43
- /**
44
- * * Sorts an array of objects.
45
- *
46
- * - Sorts array by the specified field.
47
- *
48
- * @template T - The type of objects in the array.
49
- * @param array - The array of objects to sort.
50
- * @param options - Sorting options.
51
- * @returns The sorted array.
52
- */
53
- export declare function sortAnArray<T extends InputObject>(array: T[], options: SortOptions<T>): T[];
54
11
  /**
55
12
  * * Filters an array of objects based on multiple conditions for specified keys.
56
13
  *
@@ -60,5 +17,12 @@ export declare function sortAnArray<T extends InputObject>(array: T[], options:
60
17
  * The conditions can be a value, a range, or a function.
61
18
  * @returns The filtered array of objects.
62
19
  */
63
- export declare const filterArrayOfObjects: <T extends Record<string, unknown>>(array: T[], conditions: { [K in keyof T]?: (value: T[K]) => boolean; }) => T[];
20
+ export declare const filterArrayOfObjects: <T extends GenericObject>(array: T[], conditions: { [K in keyof T]?: (value: T[K]) => boolean; }) => T[];
21
+ /**
22
+ * * Check if an array is empty.
23
+ *
24
+ * @param array Array to check.
25
+ * @returns Whether the array is empty.
26
+ */
27
+ export declare const isValidButEmptyArray: <T>(array: T[] | unknown) => boolean;
64
28
  //# sourceMappingURL=basics.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"basics.d.ts","sourceRoot":"","sources":["../../src/array/basics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,SAAS,EACT,aAAa,EACb,WAAW,EACX,WAAW,EACX,WAAW,EACX,WAAW,EACX,MAAM,SAAS,CAAC;AAEjB;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,KAAG,SAAS,CAAC,CAAC,CAAC,EAO5D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,GAC9B,CAAC,SAAS,WAAW,EACrB,EAAE,SAAS,MAAM,YACjB,EAAE,SAAS,MAAM,kBAEX,CAAC,EAAE,UACD,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,KAC9B,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,GAAE,EAe5B,CA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AC,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,GAAE,KACvD,CAAC,EAgBH,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,SAAS,CAAC,EAAE,GAAG,OAAO,KAAG,OAE9D,CAAC"}
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.filterArrayOfObjects = exports.createOptionsArray = exports.flattenArray = void 0;
4
- exports.sortAnArray = sortAnArray;
3
+ exports.isValidButEmptyArray = exports.filterArrayOfObjects = exports.flattenArray = void 0;
5
4
  /**
6
5
  * * Flattens a nested array recursively or wraps any non-array data type in an array.
7
6
  *
@@ -18,86 +17,6 @@ const flattenArray = (input) => {
18
17
  }, []);
19
18
  };
20
19
  exports.flattenArray = flattenArray;
21
- /**
22
- * * Converts an array of objects into a formatted array of options.
23
- *
24
- * @typeParam T - The type of each object in the `data` array.
25
- * @param data - An array of objects to convert into options.
26
- * @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
27
- * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
28
- */
29
- const createOptionsArray = (data, config) => {
30
- const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', } = config || {};
31
- if (data && data.length) {
32
- return data.map((datum) => ({
33
- [firstFieldName]: String(datum[firstFieldKey] ?? ''),
34
- [secondFieldName]: String(datum[secondFieldKey] ?? ''),
35
- }));
36
- }
37
- else {
38
- return [];
39
- }
40
- };
41
- exports.createOptionsArray = createOptionsArray;
42
- /**
43
- * * Sorts an array of strings, numbers, booleans, or objects based on the provided options.
44
- *
45
- * - If the array contains strings, it sorts them alphabetically.
46
- * - If the array contains numbers, it sorts them numerically.
47
- * - If the array contains booleans, it sorts them by their boolean value.
48
- * - If the array contains objects, it sorts them by the specified field.
49
- * @template T - The type of objects in the array.
50
- * @param array - The array to sort.
51
- * @param options - Sorting options for objects.
52
- * @returns The sorted array.
53
- */
54
- function sortAnArray(array, options) {
55
- if (!Array.isArray(array) || array.length === 0)
56
- return array;
57
- // Check if the array contains strings
58
- if (typeof array[0] === 'string') {
59
- return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
60
- b.localeCompare(a)
61
- : a.localeCompare(b));
62
- }
63
- // Check if the array contains numbers
64
- if (typeof array[0] === 'number') {
65
- return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
66
- b - a
67
- : a - b);
68
- }
69
- // Check if the array contains booleans
70
- if (typeof array[0] === 'boolean') {
71
- return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
72
- Number(b) - Number(a)
73
- : Number(a) - Number(b));
74
- }
75
- // Handle object arrays
76
- if (options?.sortByField) {
77
- return [...array].sort((a, b) => {
78
- const key = options.sortByField;
79
- const keyA = a[key];
80
- const keyB = b[key];
81
- if (typeof keyA === 'string' && typeof keyB === 'string') {
82
- return options?.sortOrder === 'desc' ?
83
- keyB.localeCompare(keyA)
84
- : keyA.localeCompare(keyB);
85
- }
86
- if (typeof keyA === 'number' && typeof keyB === 'number') {
87
- return options?.sortOrder === 'desc' ?
88
- keyB - keyA
89
- : keyA - keyB;
90
- }
91
- if (typeof keyA === 'boolean' && typeof keyB === 'boolean') {
92
- return options?.sortOrder === 'desc' ?
93
- Number(keyB) - Number(keyA)
94
- : Number(keyA) - Number(keyB);
95
- }
96
- throw new Error('Cannot compare non-string/non-number/non-boolean properties.');
97
- });
98
- }
99
- throw new Error('Invalid array or missing "sortByField" for objects.');
100
- }
101
20
  /**
102
21
  * * Filters an array of objects based on multiple conditions for specified keys.
103
22
  *
@@ -122,3 +41,13 @@ const filterArrayOfObjects = (array, conditions) => {
122
41
  }));
123
42
  };
124
43
  exports.filterArrayOfObjects = filterArrayOfObjects;
44
+ /**
45
+ * * Check if an array is empty.
46
+ *
47
+ * @param array Array to check.
48
+ * @returns Whether the array is empty.
49
+ */
50
+ const isValidButEmptyArray = (array) => {
51
+ return Array.isArray(array) && array.length === 0;
52
+ };
53
+ exports.isValidButEmptyArray = isValidButEmptyArray;
@@ -0,0 +1,37 @@
1
+ import type { InputObject, OrderOption, SortOptions } from "./types";
2
+ /**
3
+ * * Sorts an array of strings.
4
+ *
5
+ * @param array - The array of strings to sort.
6
+ * @param options - Sorting options.
7
+ * @returns The sorted array.
8
+ */
9
+ export declare function sortAnArray(array: string[], options?: OrderOption): string[];
10
+ /**
11
+ * * Sorts an array of numbers.
12
+ *
13
+ * @param array - The array of numbers to sort.
14
+ * @param options - Sorting options.
15
+ * @returns The sorted array.
16
+ */
17
+ export declare function sortAnArray(array: number[], options?: OrderOption): number[];
18
+ /**
19
+ * * Sorts an array of booleans.
20
+ *
21
+ * @param array - The array of booleans to sort.
22
+ * @param options - Sorting options.
23
+ * @returns The sorted array.
24
+ */
25
+ export declare function sortAnArray(array: boolean[], options?: OrderOption): boolean[];
26
+ /**
27
+ * * Sorts an array of objects.
28
+ *
29
+ * - Sorts array by the specified field.
30
+ *
31
+ * @template T - The type of objects in the array.
32
+ * @param array - The array of objects to sort.
33
+ * @param options - Sorting options.
34
+ * @returns The sorted array.
35
+ */
36
+ export declare function sortAnArray<T extends InputObject>(array: T[], options: SortOptions<T>): T[];
37
+ //# sourceMappingURL=sort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sort.d.ts","sourceRoot":"","sources":["../../src/array/sort.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAErE;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,MAAM,EAAE,CAAC;AAE9E;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,MAAM,EAAE,CAAC;AAE9E;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,EAAE,CAAC;AAEhF;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,WAAW,EAChD,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,GACrB,CAAC,EAAE,CAAC"}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortAnArray = sortAnArray;
4
+ /**
5
+ * * Sorts an array of strings, numbers, booleans, or objects based on the provided options.
6
+ *
7
+ * - If the array contains strings, it sorts them alphabetically.
8
+ * - If the array contains numbers, it sorts them numerically.
9
+ * - If the array contains booleans, it sorts them by their boolean value.
10
+ * - If the array contains objects, it sorts them by the specified field.
11
+ * @template T - The type of objects in the array.
12
+ * @param array - The array to sort.
13
+ * @param options - Sorting options for objects.
14
+ * @returns The sorted array.
15
+ */
16
+ function sortAnArray(array, options) {
17
+ if (!Array.isArray(array) || array.length === 0)
18
+ return array;
19
+ // Check if the array contains strings
20
+ if (typeof array[0] === 'string') {
21
+ return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
22
+ b.localeCompare(a)
23
+ : a.localeCompare(b));
24
+ }
25
+ // Check if the array contains numbers
26
+ if (typeof array[0] === 'number') {
27
+ return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
28
+ b - a
29
+ : a - b);
30
+ }
31
+ // Check if the array contains booleans
32
+ if (typeof array[0] === 'boolean') {
33
+ return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
34
+ Number(b) - Number(a)
35
+ : Number(a) - Number(b));
36
+ }
37
+ // Handle object arrays
38
+ if (options?.sortByField) {
39
+ return [...array].sort((a, b) => {
40
+ const key = options.sortByField;
41
+ const keyA = a[key];
42
+ const keyB = b[key];
43
+ if (typeof keyA === 'string' && typeof keyB === 'string') {
44
+ return options?.sortOrder === 'desc' ?
45
+ keyB.localeCompare(keyA)
46
+ : keyA.localeCompare(keyB);
47
+ }
48
+ if (typeof keyA === 'number' && typeof keyB === 'number') {
49
+ return options?.sortOrder === 'desc' ?
50
+ keyB - keyA
51
+ : keyA - keyB;
52
+ }
53
+ if (typeof keyA === 'boolean' && typeof keyB === 'boolean') {
54
+ return options?.sortOrder === 'desc' ?
55
+ Number(keyB) - Number(keyA)
56
+ : Number(keyA) - Number(keyB);
57
+ }
58
+ throw new Error('Cannot compare non-string/non-number/non-boolean properties.');
59
+ });
60
+ }
61
+ throw new Error('Invalid array or missing "sortByField" for objects.');
62
+ }
@@ -0,0 +1,11 @@
1
+ import type { OptionInput, OptionsConfig } from './types';
2
+ /**
3
+ * * Converts an array of objects into a formatted array of options.
4
+ *
5
+ * @typeParam T - The type of each object in the `data` array.
6
+ * @param data - An array of objects to convert into options.
7
+ * @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
8
+ * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
9
+ */
10
+ export declare const createOptionsArray: <T extends OptionInput, K1 extends string = "value", K2 extends string = "label">(data: T[], config: OptionsConfig<T, K1, K2>) => { [P in K1 | K2]: string; }[];
11
+ //# sourceMappingURL=transform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/array/transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE1D;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,GAC9B,CAAC,SAAS,WAAW,EACrB,EAAE,SAAS,MAAM,YACjB,EAAE,SAAS,MAAM,kBAEX,CAAC,EAAE,UACD,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,KAC9B,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,GAAE,EAe5B,CAAC"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createOptionsArray = void 0;
4
+ /**
5
+ * * Converts an array of objects into a formatted array of options.
6
+ *
7
+ * @typeParam T - The type of each object in the `data` array.
8
+ * @param data - An array of objects to convert into options.
9
+ * @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
10
+ * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
11
+ */
12
+ const createOptionsArray = (data, config) => {
13
+ const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', } = config || {};
14
+ if (data && data.length) {
15
+ return data.map((datum) => ({
16
+ [firstFieldName]: String(datum[firstFieldKey] ?? ''),
17
+ [secondFieldName]: String(datum[secondFieldKey] ?? ''),
18
+ }));
19
+ }
20
+ else {
21
+ return [];
22
+ }
23
+ };
24
+ exports.createOptionsArray = createOptionsArray;
@@ -14,7 +14,7 @@ export type OptionInput = Record<string, string | number | null | undefined>;
14
14
  * @typeParam K1 - The name of the key for the first field in the output (default: `'value'`).
15
15
  * @typeParam K2 - The name of the key for the second field in the output (default: `'label'`).
16
16
  */
17
- export interface ConfigOptions<T extends Record<string, string | number | null | undefined>, K1 extends string = 'value', K2 extends string = 'label'> {
17
+ export interface OptionsConfig<T extends Record<string, string | number | null | undefined>, K1 extends string = 'value', K2 extends string = 'label'> {
18
18
  /**
19
19
  * - The key in the input objects to use for the first field of the option.
20
20
  * @example If the input objects have an `id` field and you want to use it as the `value` field in the output, set createOptionsArray(data, {firstFieldKey: 'id'}).
@@ -0,0 +1,15 @@
1
+ /**
2
+ * * Utility to convert object into FormData.
3
+ *
4
+ * @param data Data to convert into FormData.
5
+ * @returns Converted FormData.
6
+ */
7
+ export declare const convertIntoFormData: <T extends object>(data: T) => FormData;
8
+ /**
9
+ * * Check if a formdata object is empty.
10
+ *
11
+ * @param obj FormData to check.
12
+ * @returns Whether the formdata is empty.
13
+ */
14
+ export declare const isEmptyFormData: (data: FormData) => boolean;
15
+ //# sourceMappingURL=convert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/form/convert.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,MAAM,QAAQ,CAAC,KAAG,QAY/D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,SAAU,QAAQ,KAAG,OAOhD,CAAC"}
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isEmptyFormData = exports.convertIntoFormData = void 0;
4
+ const basics_1 = require("../array/basics");
5
+ /**
6
+ * * Utility to convert object into FormData.
7
+ *
8
+ * @param data Data to convert into FormData.
9
+ * @returns Converted FormData.
10
+ */
11
+ const convertIntoFormData = (data) => {
12
+ const formData = new FormData();
13
+ Object.entries(data).forEach(([key, value]) => {
14
+ if (!(0, basics_1.isValidButEmptyArray)(value) && value[0]?.originFileObj) {
15
+ formData.append(key, value[0].originFileObj);
16
+ }
17
+ else if (value !== undefined && value !== null && value !== '') {
18
+ formData.append(key, value);
19
+ }
20
+ });
21
+ return formData;
22
+ };
23
+ exports.convertIntoFormData = convertIntoFormData;
24
+ /**
25
+ * * Check if a formdata object is empty.
26
+ *
27
+ * @param obj FormData to check.
28
+ * @returns Whether the formdata is empty.
29
+ */
30
+ const isEmptyFormData = (data) => {
31
+ if ('entries' in data && typeof data.entries === 'function') {
32
+ return Array.from(data.entries()).length === 0;
33
+ }
34
+ throw new Error('`FormData.entries()` is not supported in this environment.');
35
+ };
36
+ exports.isEmptyFormData = isEmptyFormData;
@@ -0,0 +1,12 @@
1
+ import type { LooseObject } from '../object/types';
2
+ import type { FormDataConfigs } from './types';
3
+ /**
4
+ * * Utility to convert object into FormData in a controlled way.
5
+ *
6
+ * @param data - The source object to control and convert to FormData.
7
+ * @param configs - Configuration options to control the formData.
8
+ *
9
+ * @returns FormData instance containing the sanitized and transformed data
10
+ */
11
+ export declare const createControlledFormData: <T extends LooseObject>(data: T, configs?: FormDataConfigs<T>) => FormData;
12
+ //# sourceMappingURL=transform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/form/transform.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,GAAI,CAAC,SAAS,WAAW,QACvD,CAAC,YACG,eAAe,CAAC,CAAC,CAAC,KAC1B,QAkDF,CAAC"}
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createControlledFormData = void 0;
4
+ const basics_1 = require("../object/basics");
5
+ /**
6
+ * * Utility to convert object into FormData in a controlled way.
7
+ *
8
+ * @param data - The source object to control and convert to FormData.
9
+ * @param configs - Configuration options to control the formData.
10
+ *
11
+ * @returns FormData instance containing the sanitized and transformed data
12
+ */
13
+ const createControlledFormData = (data, configs) => {
14
+ const formData = new FormData();
15
+ const addToFormData = (key, value) => {
16
+ if (value instanceof File) {
17
+ formData.append(key, value);
18
+ }
19
+ else if (Array.isArray(value)) {
20
+ value.forEach((item, index) => {
21
+ addToFormData(`${key}[${index}]`, item);
22
+ });
23
+ }
24
+ else if (typeof value === 'object' && !(0, basics_1.isEmptyObject)(value)) {
25
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
26
+ addToFormData(`${key}.${nestedKey}`, nestedValue);
27
+ });
28
+ }
29
+ else {
30
+ formData.append(key, value);
31
+ }
32
+ };
33
+ const isPathPreserved = (key) => {
34
+ return configs?.preservePaths?.some((path) => key.startsWith(path));
35
+ };
36
+ Object.entries(data).forEach(([key, value]) => {
37
+ // 1. Skip keys that are in ignoreKeys
38
+ if (configs?.ignoreKeys?.includes(key))
39
+ return;
40
+ // 2. Trim string values if trimStrings is enabled
41
+ if (configs?.trimStrings && typeof value === 'string') {
42
+ value = value.trim();
43
+ }
44
+ // 3. Ensure requiredKeys are included even if falsy
45
+ if (configs?.requiredKeys?.includes(key) && value == null) {
46
+ value = ''; // Make sure it's an empty string for falsy values
47
+ }
48
+ // 4. Check preservePaths, if true, preserve values for nested paths
49
+ if (isPathPreserved(key)) {
50
+ addToFormData(key, value);
51
+ }
52
+ else {
53
+ const transformedKey = configs?.lowerCaseKeys?.includes(key) ?
54
+ key.toLowerCase()
55
+ : key;
56
+ addToFormData(transformedKey, value);
57
+ }
58
+ });
59
+ return formData;
60
+ };
61
+ exports.createControlledFormData = createControlledFormData;
@@ -0,0 +1,8 @@
1
+ export interface FormDataConfigs<T> {
2
+ ignoreKeys?: (keyof T)[];
3
+ requiredKeys?: (keyof T)[];
4
+ lowerCaseKeys?: (keyof T)[];
5
+ preservePaths?: string[];
6
+ trimStrings?: boolean;
7
+ }
8
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/form/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe,CAAC,CAAC;IAEjC,UAAU,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;IAEzB,YAAY,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;IAE3B,aAAa,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;IAE5B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IAEzB,WAAW,CAAC,EAAE,OAAO,CAAC;CACtB"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/index.d.ts CHANGED
@@ -7,7 +7,11 @@ export { getColorForInitial } from './colors/initials';
7
7
  export { generateRandomColorInHexRGB, generateRandomHSLColor, } from './colors/random';
8
8
  export { convertColorCode, convertHexToHsl, convertHexToRgb, convertHslToHex, convertHslToRgb, convertRgbToHex, convertRgbToHsl, } from './colors/convert';
9
9
  export { extractNumbersFromColor } from './colors/helpers';
10
- export { createOptionsArray, filterArrayOfObjects, flattenArray, sortAnArray, } from './array/basics';
10
+ export { filterArrayOfObjects, flattenArray, isValidButEmptyArray, } from './array/basics';
11
+ export { sortAnArray } from './array/sort';
12
+ export { createOptionsArray } from './array/transform';
13
+ export { convertIntoFormData, isEmptyFormData } from './form/convert';
14
+ export { createControlledFormData } from './form/transform';
11
15
  export { cloneObject, countObjectFields, generateQueryParams, isEmptyObject, isObject, } from './object/basics';
12
16
  export { extractNewFields, extractUpdatedAndNewFields, extractUpdatedFields, flattenObject, mergeAndFlattenObjects, mergeObjects, } from './object/objectify';
13
17
  export { sanitizeData } from './object/sanitize';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,cAAc,GACd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEpE,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAE3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EACN,2BAA2B,EAC3B,sBAAsB,GACtB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,GACf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAE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ExB,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtE,OAAO,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAE5D,OAAO,EACN,WAAW,EACX,iBAAiB,EACjB,mBAAmB,EACnB,aAAa,EACb,QAAQ,GACR,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,gBAAgB,EAChB,0BAA0B,EAC1B,oBAAoB,EACpB,aAAa,EACb,sBAAsB,EACtB,YAAY,GACZ,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isDeepEqual = exports.convertObjectValues = exports.sanitizeData = exports.mergeObjects = exports.mergeAndFlattenObjects = exports.flattenObject = exports.extractUpdatedFields = exports.extractUpdatedAndNewFields = exports.extractNewFields = exports.isObject = exports.isEmptyObject = exports.generateQueryParams = exports.countObjectFields = exports.cloneObject = exports.sortAnArray = exports.flattenArray = exports.filterArrayOfObjects = exports.createOptionsArray = exports.extractNumbersFromColor = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.convertColorCode = exports.generateRandomHSLColor = exports.generateRandomColorInHexRGB = exports.getColorForInitial = exports.isPrime = exports.findPrimeNumbers = exports.numberToWords = exports.getRandomNumber = exports.convertToDecimal = exports.generateAnagrams = exports.truncateString = exports.trimString = exports.generateRandomID = exports.capitalizeString = void 0;
3
+ exports.isDeepEqual = exports.convertObjectValues = exports.sanitizeData = exports.mergeObjects = exports.mergeAndFlattenObjects = exports.flattenObject = exports.extractUpdatedFields = exports.extractUpdatedAndNewFields = exports.extractNewFields = exports.isObject = exports.isEmptyObject = exports.generateQueryParams = exports.countObjectFields = exports.cloneObject = exports.createControlledFormData = exports.isEmptyFormData = exports.convertIntoFormData = exports.createOptionsArray = exports.sortAnArray = exports.isValidButEmptyArray = exports.flattenArray = exports.filterArrayOfObjects = exports.extractNumbersFromColor = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.convertColorCode = exports.generateRandomHSLColor = exports.generateRandomColorInHexRGB = exports.getColorForInitial = exports.isPrime = exports.findPrimeNumbers = exports.numberToWords = exports.getRandomNumber = exports.convertToDecimal = exports.generateAnagrams = exports.truncateString = exports.trimString = exports.generateRandomID = exports.capitalizeString = void 0;
4
4
  var basics_1 = require("./string/basics");
5
5
  Object.defineProperty(exports, "capitalizeString", { enumerable: true, get: function () { return basics_1.capitalizeString; } });
6
6
  Object.defineProperty(exports, "generateRandomID", { enumerable: true, get: function () { return basics_1.generateRandomID; } });
@@ -32,10 +32,18 @@ Object.defineProperty(exports, "convertRgbToHsl", { enumerable: true, get: funct
32
32
  var helpers_1 = require("./colors/helpers");
33
33
  Object.defineProperty(exports, "extractNumbersFromColor", { enumerable: true, get: function () { return helpers_1.extractNumbersFromColor; } });
34
34
  var basics_3 = require("./array/basics");
35
- Object.defineProperty(exports, "createOptionsArray", { enumerable: true, get: function () { return basics_3.createOptionsArray; } });
36
35
  Object.defineProperty(exports, "filterArrayOfObjects", { enumerable: true, get: function () { return basics_3.filterArrayOfObjects; } });
37
36
  Object.defineProperty(exports, "flattenArray", { enumerable: true, get: function () { return basics_3.flattenArray; } });
38
- Object.defineProperty(exports, "sortAnArray", { enumerable: true, get: function () { return basics_3.sortAnArray; } });
37
+ Object.defineProperty(exports, "isValidButEmptyArray", { enumerable: true, get: function () { return basics_3.isValidButEmptyArray; } });
38
+ var sort_1 = require("./array/sort");
39
+ Object.defineProperty(exports, "sortAnArray", { enumerable: true, get: function () { return sort_1.sortAnArray; } });
40
+ var transform_1 = require("./array/transform");
41
+ Object.defineProperty(exports, "createOptionsArray", { enumerable: true, get: function () { return transform_1.createOptionsArray; } });
42
+ var convert_3 = require("./form/convert");
43
+ Object.defineProperty(exports, "convertIntoFormData", { enumerable: true, get: function () { return convert_3.convertIntoFormData; } });
44
+ Object.defineProperty(exports, "isEmptyFormData", { enumerable: true, get: function () { return convert_3.isEmptyFormData; } });
45
+ var transform_2 = require("./form/transform");
46
+ Object.defineProperty(exports, "createControlledFormData", { enumerable: true, get: function () { return transform_2.createControlledFormData; } });
39
47
  var basics_4 = require("./object/basics");
40
48
  Object.defineProperty(exports, "cloneObject", { enumerable: true, get: function () { return basics_4.cloneObject; } });
41
49
  Object.defineProperty(exports, "countObjectFields", { enumerable: true, get: function () { return basics_4.countObjectFields; } });
@@ -51,7 +59,7 @@ Object.defineProperty(exports, "mergeAndFlattenObjects", { enumerable: true, get
51
59
  Object.defineProperty(exports, "mergeObjects", { enumerable: true, get: function () { return objectify_1.mergeObjects; } });
52
60
  var sanitize_1 = require("./object/sanitize");
53
61
  Object.defineProperty(exports, "sanitizeData", { enumerable: true, get: function () { return sanitize_1.sanitizeData; } });
54
- var convert_3 = require("./object/convert");
55
- Object.defineProperty(exports, "convertObjectValues", { enumerable: true, get: function () { return convert_3.convertObjectValues; } });
62
+ var convert_4 = require("./object/convert");
63
+ Object.defineProperty(exports, "convertObjectValues", { enumerable: true, get: function () { return convert_4.convertObjectValues; } });
56
64
  var utils_1 = require("./utils");
57
65
  Object.defineProperty(exports, "isDeepEqual", { enumerable: true, get: function () { return utils_1.isDeepEqual; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "1.6.9",
3
+ "version": "1.7.1",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions for everyday development needs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",