nhb-toolbox 1.0.4 → 1.1.3
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/array/index.d.ts +10 -0
- package/dist/array/index.d.ts.map +1 -1
- package/dist/array/index.js +25 -1
- package/dist/array/types.d.ts +2 -2
- package/dist/array/types.d.ts.map +1 -1
- package/dist/colors/index.js +1 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/object/index.d.ts +8 -7
- package/dist/object/index.d.ts.map +1 -1
- package/dist/object/index.js +2 -2
- package/dist/object/sanitize.d.ts +27 -0
- package/dist/object/sanitize.d.ts.map +1 -0
- package/dist/object/sanitize.js +75 -0
- package/dist/object/types.d.ts +16 -0
- package/dist/object/types.d.ts.map +1 -0
- package/dist/object/types.js +2 -0
- package/dist/string/index.d.ts +16 -2
- package/dist/string/index.d.ts.map +1 -1
- package/dist/string/index.js +22 -2
- package/dist/string/types.d.ts +1 -1
- package/dist/string/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -1
package/dist/array/index.d.ts
CHANGED
|
@@ -51,4 +51,14 @@ export declare function sortAnArray(array: boolean[], options?: OrderOption): bo
|
|
|
51
51
|
* @returns The sorted array.
|
|
52
52
|
*/
|
|
53
53
|
export declare function sortAnArray<T extends InputObject>(array: T[], options: SortOptions<T>): T[];
|
|
54
|
+
/**
|
|
55
|
+
* * Filters an array of objects based on multiple conditions for specified keys.
|
|
56
|
+
*
|
|
57
|
+
* @template T - The type of objects in the array.
|
|
58
|
+
* @param array - The array of objects to filter.
|
|
59
|
+
* @param conditions - An object where keys represent the property names and values represent the filter conditions.
|
|
60
|
+
* The conditions can be a value, a range, or a function.
|
|
61
|
+
* @returns The filtered array of objects.
|
|
62
|
+
*/
|
|
63
|
+
export declare const filterArrayOfObjects: <T extends Record<string, unknown>>(array: T[], conditions: { [K in keyof T]?: (value: T[K]) => boolean; }) => T[];
|
|
54
64
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/array/index.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,CAAC;AAEF;;;;;;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"}
|
|
1
|
+
{"version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M,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,GAAE,KACvD,CAAC,EAgBH,CAAC"}
|
package/dist/array/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createOptionsArray = exports.flattenArray = void 0;
|
|
3
|
+
exports.filterArrayOfObjects = exports.createOptionsArray = exports.flattenArray = void 0;
|
|
4
4
|
exports.sortAnArray = sortAnArray;
|
|
5
5
|
/**
|
|
6
6
|
* * Flattens a nested array recursively or wraps any non-array data type in an array.
|
|
@@ -98,3 +98,27 @@ function sortAnArray(array, options) {
|
|
|
98
98
|
}
|
|
99
99
|
throw new Error('Invalid array or missing "sortByField" for objects.');
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* * Filters an array of objects based on multiple conditions for specified keys.
|
|
103
|
+
*
|
|
104
|
+
* @template T - The type of objects in the array.
|
|
105
|
+
* @param array - The array of objects to filter.
|
|
106
|
+
* @param conditions - An object where keys represent the property names and values represent the filter conditions.
|
|
107
|
+
* The conditions can be a value, a range, or a function.
|
|
108
|
+
* @returns The filtered array of objects.
|
|
109
|
+
*/
|
|
110
|
+
const filterArrayOfObjects = (array, conditions) => {
|
|
111
|
+
if (!Array.isArray(array)) {
|
|
112
|
+
throw new Error('The provided input is not an array!');
|
|
113
|
+
}
|
|
114
|
+
return array.filter((item) => Object.entries(conditions).every(([key, conditionFn]) => {
|
|
115
|
+
// Ensure only check the key in the object if the condition function is provided
|
|
116
|
+
if (conditionFn) {
|
|
117
|
+
// Type assertion for the value since it's unknown
|
|
118
|
+
return conditionFn(item[key]);
|
|
119
|
+
}
|
|
120
|
+
// If no condition function, include all values for the key
|
|
121
|
+
return true;
|
|
122
|
+
}));
|
|
123
|
+
};
|
|
124
|
+
exports.filterArrayOfObjects = filterArrayOfObjects;
|
package/dist/array/types.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export interface ConfigOptions<T extends Record<string, string | number | null |
|
|
|
38
38
|
*/
|
|
39
39
|
secondFieldName?: K2;
|
|
40
40
|
}
|
|
41
|
-
/** - Object type with string or number as value for each key. */
|
|
41
|
+
/** - Object type with string or number or boolean as value for each key. */
|
|
42
42
|
export type InputObject = Record<string, string | number | boolean>;
|
|
43
43
|
/** - Option for sorting order. */
|
|
44
44
|
export interface OrderOption {
|
|
@@ -53,7 +53,7 @@ export interface OrderOption {
|
|
|
53
53
|
* * Options for sorting an array of objects.
|
|
54
54
|
* @template T - The type of objects in the array.
|
|
55
55
|
*/
|
|
56
|
-
export interface SortOptions<T extends
|
|
56
|
+
export interface SortOptions<T extends Record<string, unknown>> extends OrderOption {
|
|
57
57
|
/** The field by which to sort the objects in the array. */
|
|
58
58
|
sortByField?: keyof T;
|
|
59
59
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/array/types.ts"],"names":[],"mappings":"AAAA;;;;MAIM;AACN,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEpE,wCAAwC;AACxC,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;AAE7E;;;;;;;GAOG;AACH,MAAM,WAAW,aAAa,CAC7B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAC5D,EAAE,SAAS,MAAM,GAAG,OAAO,EAC3B,EAAE,SAAS,MAAM,GAAG,OAAO;IAE3B;;;OAGG;IACH,aAAa,EAAE,MAAM,CAAC,CAAC;IAEvB;;;OAGG;IACH,cAAc,EAAE,MAAM,CAAC,CAAC;IAExB;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,CAAC,EAAE,MAAM,CAAC,CAAC;CACtB"}
|
package/dist/colors/index.js
CHANGED
|
@@ -4,8 +4,7 @@ exports.getColorForInitial = getColorForInitial;
|
|
|
4
4
|
const helpers_1 = require("./helpers");
|
|
5
5
|
const constants_1 = require("./constants");
|
|
6
6
|
/**
|
|
7
|
-
* * Generates a hex color based on the first character (initial) of a string or number;
|
|
8
|
-
* or an array of hex colors based on the first character (initial) of an array of strings/numbers or even nested arrays of strings/numbers.
|
|
7
|
+
* * Generates a hex color based on the first character (initial) of a string or number; or an array of hex colors based on the first character (initial) of an array of strings/numbers or even nested arrays of strings/numbers.
|
|
9
8
|
*
|
|
10
9
|
* - For numbers, it uses 10 predefined colors (0-9).
|
|
11
10
|
* - For letters, it uses 26 predefined colors (A-Z).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export { capitalizeString, truncateString, generateRandomID } from './string';
|
|
1
|
+
export { capitalizeString, truncateString, generateRandomID, trimString, } from './string';
|
|
2
2
|
export { getRandomNumber, convertToDecimal } from './number';
|
|
3
3
|
export { getColorForInitial } from './colors';
|
|
4
4
|
export { generateRandomColor } from './colors/random';
|
|
5
5
|
export { convertHexToHsl, convertHexToRgb, convertHslToHex, convertHslToRgb, convertRgbToHex, convertRgbToHsl, } from './colors/convert';
|
|
6
|
-
export { flattenArray, createOptionsArray, sortAnArray } from './array';
|
|
6
|
+
export { flattenArray, createOptionsArray, sortAnArray, filterArrayOfObjects, } from './array';
|
|
7
7
|
export { generateQueryParams, cloneObject, isDeepEqual, mergeObjects, flattenObject, mergeAndFlattenObjects, isEmptyObject, countObjectFields, } from './object';
|
|
8
|
+
export { sanitizeData } from './object/sanitize';
|
|
8
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,UAAU,GACV,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9C,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,EACN,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,GACf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACN,YAAY,EACZ,kBAAkB,EAClB,WAAW,EACX,oBAAoB,GACpB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACN,mBAAmB,EACnB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,aAAa,EACb,sBAAsB,EACtB,aAAa,EACb,iBAAiB,GACjB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.countObjectFields = exports.isEmptyObject = exports.mergeAndFlattenObjects = exports.flattenObject = exports.mergeObjects = exports.isDeepEqual = exports.cloneObject = exports.generateQueryParams = exports.sortAnArray = exports.createOptionsArray = exports.flattenArray = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.generateRandomColor = exports.getColorForInitial = exports.convertToDecimal = exports.getRandomNumber = exports.generateRandomID = exports.truncateString = exports.capitalizeString = void 0;
|
|
3
|
+
exports.sanitizeData = exports.countObjectFields = exports.isEmptyObject = exports.mergeAndFlattenObjects = exports.flattenObject = exports.mergeObjects = exports.isDeepEqual = exports.cloneObject = exports.generateQueryParams = exports.filterArrayOfObjects = exports.sortAnArray = exports.createOptionsArray = exports.flattenArray = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.generateRandomColor = exports.getColorForInitial = exports.convertToDecimal = exports.getRandomNumber = exports.trimString = exports.generateRandomID = exports.truncateString = exports.capitalizeString = void 0;
|
|
4
4
|
var string_1 = require("./string");
|
|
5
5
|
Object.defineProperty(exports, "capitalizeString", { enumerable: true, get: function () { return string_1.capitalizeString; } });
|
|
6
6
|
Object.defineProperty(exports, "truncateString", { enumerable: true, get: function () { return string_1.truncateString; } });
|
|
7
7
|
Object.defineProperty(exports, "generateRandomID", { enumerable: true, get: function () { return string_1.generateRandomID; } });
|
|
8
|
+
Object.defineProperty(exports, "trimString", { enumerable: true, get: function () { return string_1.trimString; } });
|
|
8
9
|
var number_1 = require("./number");
|
|
9
10
|
Object.defineProperty(exports, "getRandomNumber", { enumerable: true, get: function () { return number_1.getRandomNumber; } });
|
|
10
11
|
Object.defineProperty(exports, "convertToDecimal", { enumerable: true, get: function () { return number_1.convertToDecimal; } });
|
|
@@ -23,6 +24,7 @@ var array_1 = require("./array");
|
|
|
23
24
|
Object.defineProperty(exports, "flattenArray", { enumerable: true, get: function () { return array_1.flattenArray; } });
|
|
24
25
|
Object.defineProperty(exports, "createOptionsArray", { enumerable: true, get: function () { return array_1.createOptionsArray; } });
|
|
25
26
|
Object.defineProperty(exports, "sortAnArray", { enumerable: true, get: function () { return array_1.sortAnArray; } });
|
|
27
|
+
Object.defineProperty(exports, "filterArrayOfObjects", { enumerable: true, get: function () { return array_1.filterArrayOfObjects; } });
|
|
26
28
|
var object_1 = require("./object");
|
|
27
29
|
Object.defineProperty(exports, "generateQueryParams", { enumerable: true, get: function () { return object_1.generateQueryParams; } });
|
|
28
30
|
Object.defineProperty(exports, "cloneObject", { enumerable: true, get: function () { return object_1.cloneObject; } });
|
|
@@ -32,3 +34,5 @@ Object.defineProperty(exports, "flattenObject", { enumerable: true, get: functio
|
|
|
32
34
|
Object.defineProperty(exports, "mergeAndFlattenObjects", { enumerable: true, get: function () { return object_1.mergeAndFlattenObjects; } });
|
|
33
35
|
Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return object_1.isEmptyObject; } });
|
|
34
36
|
Object.defineProperty(exports, "countObjectFields", { enumerable: true, get: function () { return object_1.countObjectFields; } });
|
|
37
|
+
var sanitize_1 = require("./object/sanitize");
|
|
38
|
+
Object.defineProperty(exports, "sanitizeData", { enumerable: true, get: function () { return sanitize_1.sanitizeData; } });
|
package/dist/object/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { GenericObject } from './types';
|
|
1
2
|
/**
|
|
2
|
-
* * Utility
|
|
3
|
+
* * Utility to generate query parameters from an object.
|
|
3
4
|
*
|
|
4
5
|
* @template T - A generic type extending `Record<string, string | number | string[] | number[]>`.
|
|
5
6
|
* @param params - Object containing query parameters.
|
|
@@ -16,28 +17,28 @@ export declare const generateQueryParams: <T extends Record<string, string | num
|
|
|
16
17
|
* @param obj Object to clone.
|
|
17
18
|
* @returns Deep cloned object.
|
|
18
19
|
*/
|
|
19
|
-
export declare const cloneObject: <T extends
|
|
20
|
+
export declare const cloneObject: <T extends GenericObject>(obj: T) => T;
|
|
20
21
|
/**
|
|
21
22
|
* * Check if an object is empty.
|
|
22
23
|
*
|
|
23
24
|
* @param obj Object to check.
|
|
24
25
|
* @returns Whether the object is empty.
|
|
25
26
|
*/
|
|
26
|
-
export declare const isEmptyObject: <T extends
|
|
27
|
+
export declare const isEmptyObject: <T extends GenericObject>(obj: T) => boolean;
|
|
27
28
|
/**
|
|
28
29
|
* * Count the number of fields in an object.
|
|
29
30
|
*
|
|
30
31
|
* @param obj Object to check.
|
|
31
32
|
* @returns Number of fields in the object.
|
|
32
33
|
*/
|
|
33
|
-
export declare const countObjectFields: <T extends
|
|
34
|
+
export declare const countObjectFields: <T extends GenericObject>(obj: T) => number;
|
|
34
35
|
/**
|
|
35
36
|
* * Deeply merge two or more objects using `Map`.
|
|
36
37
|
*
|
|
37
38
|
* @param objects Objects to merge.
|
|
38
39
|
* @returns Merged object.
|
|
39
40
|
*/
|
|
40
|
-
export declare const mergeObjects: <T extends
|
|
41
|
+
export declare const mergeObjects: <T extends GenericObject>(...objects: T[]) => T;
|
|
41
42
|
/**
|
|
42
43
|
* * Deeply merge objects and flatten nested objects.
|
|
43
44
|
* * Useful for flattening a single object or merging multiple objects with duplicate key(s).
|
|
@@ -46,14 +47,14 @@ export declare const mergeObjects: <T extends Record<string, unknown>>(...object
|
|
|
46
47
|
* @param objects Objects to merge.
|
|
47
48
|
* @returns Merged object with flattened structure.
|
|
48
49
|
*/
|
|
49
|
-
export declare const mergeAndFlattenObjects: <T extends
|
|
50
|
+
export declare const mergeAndFlattenObjects: <T extends GenericObject>(...objects: T[]) => GenericObject;
|
|
50
51
|
/**
|
|
51
52
|
* * Flattens a nested object into a dot notation format.
|
|
52
53
|
*
|
|
53
54
|
* @param object - The `object` to flatten.
|
|
54
55
|
* @returns A `flattened object` with dot notation keys.
|
|
55
56
|
*/
|
|
56
|
-
export declare const flattenObject: <T extends
|
|
57
|
+
export declare const flattenObject: <T extends GenericObject>(object: T) => GenericObject;
|
|
57
58
|
/**
|
|
58
59
|
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
59
60
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,GAC/B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,WAEvD,CAAC,KACP,MAkBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,GAC/B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,WAEvD,CAAC,KACP,MAkBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,aAAa,OAAO,CAAC,KAAG,CAE7D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,aAAa,OAAO,CAAC,KAAG,OAE/D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,aAAa,OAAO,CAAC,KAAG,MAEnE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,aAAa,cAAc,CAAC,EAAE,KAAG,CAuCvE,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,aAAa,cACjD,CAAC,EAAE,KACb,aAyBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,aAAa,UAC5C,CAAC,KACP,aA6BF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAG,OA6B3C,CAAC"}
|
package/dist/object/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isDeepEqual = exports.flattenObject = exports.mergeAndFlattenObjects = exports.mergeObjects = exports.countObjectFields = exports.isEmptyObject = exports.cloneObject = exports.generateQueryParams = void 0;
|
|
4
4
|
/**
|
|
5
|
-
* * Utility
|
|
5
|
+
* * Utility to generate query parameters from an object.
|
|
6
6
|
*
|
|
7
7
|
* @template T - A generic type extending `Record<string, string | number | string[] | number[]>`.
|
|
8
8
|
* @param params - Object containing query parameters.
|
|
@@ -39,7 +39,7 @@ exports.cloneObject = cloneObject;
|
|
|
39
39
|
* @returns Whether the object is empty.
|
|
40
40
|
*/
|
|
41
41
|
const isEmptyObject = (obj) => {
|
|
42
|
-
return Object.keys(obj).length === 0;
|
|
42
|
+
return Object.keys(obj).length === 0 && obj.constructor === Object;
|
|
43
43
|
};
|
|
44
44
|
exports.isEmptyObject = isEmptyObject;
|
|
45
45
|
/**
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { GenericObject, SanitizeOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* * Processes an object by ignoring specified keys and trimming string values based on options provided.
|
|
4
|
+
* * Also excludes nullish values (null, undefined) if specified. Always ignores empty nested object(s).
|
|
5
|
+
*
|
|
6
|
+
* @param object - The object to process.
|
|
7
|
+
* @param options - Options that define which keys to ignore, whether to trim string values, and whether to exclude nullish values.
|
|
8
|
+
* @returns A new object with the specified modifications.
|
|
9
|
+
*/
|
|
10
|
+
export declare function sanitizeData<T extends GenericObject>(object: T, options?: SanitizeOptions<T>): T;
|
|
11
|
+
/**
|
|
12
|
+
* * Processes an array of objects by ignoring specified keys and trimming string values based on options provided.
|
|
13
|
+
* * Also excludes nullish values (null, undefined) if specified. Always ignores empty nested object(s).
|
|
14
|
+
*
|
|
15
|
+
* @param object - The object to process.
|
|
16
|
+
* @param options - Options that define which keys to ignore, whether to trim string values, and whether to exclude nullish values.
|
|
17
|
+
* @returns A new array of objects with the specified modifications.
|
|
18
|
+
*/
|
|
19
|
+
export declare function sanitizeData<T extends GenericObject>(array: T[], options?: SanitizeOptions<T>): T[];
|
|
20
|
+
/**
|
|
21
|
+
* * Trims all the words in a string or an array of strings.
|
|
22
|
+
*
|
|
23
|
+
* @param input String or array of strings.
|
|
24
|
+
* @returns Trimmed string or array of strings.
|
|
25
|
+
*/
|
|
26
|
+
export declare function sanitizeData(input: string | string[]): string | string[];
|
|
27
|
+
//# sourceMappingURL=sanitize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../src/object/sanitize.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAkB,aAAa,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE9E;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,aAAa,EACnD,MAAM,EAAE,CAAC,EACT,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAC1B,CAAC,CAAC;AAEL;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,aAAa,EACnD,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAC1B,CAAC,EAAE,CAAC;AAEP;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sanitizeData = sanitizeData;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
const string_1 = require("../string");
|
|
6
|
+
/**
|
|
7
|
+
* * Processes a string, array of strings, an object or array of objects by ignoring specified keys and trimming string values.
|
|
8
|
+
* * Also excludes nullish values (null, undefined) if specified. Always ignores empty nested object(s).
|
|
9
|
+
*
|
|
10
|
+
* @param input - The object or array of objects to process.
|
|
11
|
+
* @param options - Options for processing.
|
|
12
|
+
* @returns A new string, array of strings, object or array of objects with the specified modifications.
|
|
13
|
+
*/
|
|
14
|
+
function sanitizeData(input, options) {
|
|
15
|
+
const { ignoreKeys = [], trimStrings = true, ignoreNullish = false, } = options || {};
|
|
16
|
+
// Flatten the object keys and use the keys for comparison
|
|
17
|
+
const ignoreKeySet = new Set(ignoreKeys);
|
|
18
|
+
/**
|
|
19
|
+
* * Helper function to process a single object.
|
|
20
|
+
*
|
|
21
|
+
* @param object The object to process.
|
|
22
|
+
* @param parentPath The parent path of a key.
|
|
23
|
+
* */
|
|
24
|
+
const _processObject = (object, parentPath = '') => Object.entries(object).reduce((acc, [key, value]) => {
|
|
25
|
+
// Compute the full key path
|
|
26
|
+
const fullKeyPath = parentPath ? `${parentPath}.${key}` : key;
|
|
27
|
+
// Skip ignored keys
|
|
28
|
+
if (ignoreKeySet.has(fullKeyPath)) {
|
|
29
|
+
return acc;
|
|
30
|
+
}
|
|
31
|
+
// Exclude nullish values if specified
|
|
32
|
+
if (ignoreNullish && (value === null || value === undefined)) {
|
|
33
|
+
return acc;
|
|
34
|
+
}
|
|
35
|
+
// Trim string values if enabled
|
|
36
|
+
if (typeof value === 'string' && trimStrings) {
|
|
37
|
+
acc[key] = (0, string_1.trimString)(value);
|
|
38
|
+
}
|
|
39
|
+
else if (value &&
|
|
40
|
+
typeof value === 'object' &&
|
|
41
|
+
!Array.isArray(value)) {
|
|
42
|
+
// Recursively process nested objects
|
|
43
|
+
const processedValue = _processObject(value, fullKeyPath);
|
|
44
|
+
// Only add the property if it's not an empty object
|
|
45
|
+
if (!(0, index_1.isEmptyObject)(processedValue)) {
|
|
46
|
+
acc[key] = processedValue;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// Add other values as-is
|
|
51
|
+
acc[key] = value;
|
|
52
|
+
}
|
|
53
|
+
return acc;
|
|
54
|
+
}, {});
|
|
55
|
+
// Process strings
|
|
56
|
+
if (typeof input === 'string') {
|
|
57
|
+
return (0, string_1.trimString)(input);
|
|
58
|
+
}
|
|
59
|
+
// Process array of strings and objects
|
|
60
|
+
if (Array.isArray(input)) {
|
|
61
|
+
// Process array of strings
|
|
62
|
+
if (typeof input[0] === 'string') {
|
|
63
|
+
return (0, string_1.trimString)(input);
|
|
64
|
+
}
|
|
65
|
+
// Process array of objects
|
|
66
|
+
return input
|
|
67
|
+
.map((obj) => _processObject(obj))
|
|
68
|
+
.filter((obj) => !(0, index_1.isEmptyObject)(obj));
|
|
69
|
+
}
|
|
70
|
+
// Process object
|
|
71
|
+
if (typeof input === 'object' && input !== null) {
|
|
72
|
+
return _processObject(input);
|
|
73
|
+
}
|
|
74
|
+
return input;
|
|
75
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** - Generic object type */
|
|
2
|
+
export type GenericObject = Record<string, unknown>;
|
|
3
|
+
/** - Recursive type to generate dot-notation keys for nested objects. */
|
|
4
|
+
export type DotNotationKey<T> = T extends object ? {
|
|
5
|
+
[K in keyof T & string]: T[K] extends object ? K | `${K}.${DotNotationKey<T[K]>}` : K;
|
|
6
|
+
}[keyof T & string] : never;
|
|
7
|
+
/** - Options for `sanitizeData` */
|
|
8
|
+
export interface SanitizeOptions<T extends GenericObject> {
|
|
9
|
+
/** Keys to ignore */
|
|
10
|
+
ignoreKeys?: DotNotationKey<T>[];
|
|
11
|
+
/** Whether to trim string values. Defaults to `true` */
|
|
12
|
+
trimStrings?: boolean;
|
|
13
|
+
/** Whether to exclude nullish (null or undefined) values. Defaults to `false` */
|
|
14
|
+
ignoreNullish?: boolean;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/object/types.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEpD,yEAAyE;AACzE,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,GAC7C;KACC,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GACzC,CAAC,GAAG,GAAG,CAAC,IAAI,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,GAClC,CAAC;CACJ,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAClB,KAAK,CAAC;AAET,mCAAmC;AACnC,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,aAAa;IACvD,qBAAqB;IACrB,UAAU,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;IACjC,wDAAwD;IACxD,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,iFAAiF;IACjF,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB"}
|
package/dist/string/index.d.ts
CHANGED
|
@@ -17,10 +17,24 @@ export declare const capitalizeString: (string: string, options?: CapitalizeOpti
|
|
|
17
17
|
*/
|
|
18
18
|
export declare const truncateString: (string: string, maxLength: number) => string;
|
|
19
19
|
/**
|
|
20
|
-
* * Generates a
|
|
20
|
+
* * Generates a random alphanumeric (16 characters long, this length is customizable in the options) ID string composed of an optional `prefix`, `suffix`, a `timestamp`, and a customizable separator.
|
|
21
21
|
*
|
|
22
22
|
* @param options Configuration options for random ID generation.
|
|
23
|
-
* @returns The generated ID string composed of the random alphanumeric string
|
|
23
|
+
* @returns The generated ID string composed of the random alphanumeric string of specified length with optional `timeStamp`, `prefix`, and `suffix`, `caseOption` and `separator`.
|
|
24
24
|
*/
|
|
25
25
|
export declare const generateRandomID: (options?: RandomIdOptions) => string;
|
|
26
|
+
/**
|
|
27
|
+
* * Trims all the words in a string.
|
|
28
|
+
*
|
|
29
|
+
* @param input The string to trim.
|
|
30
|
+
* @returns Trimmed string.
|
|
31
|
+
*/
|
|
32
|
+
export declare function trimString(input: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* * Trims all the words in an array of strings.
|
|
35
|
+
*
|
|
36
|
+
* @param input The array of strings to trim.
|
|
37
|
+
* @returns Trimmed array of strings.
|
|
38
|
+
*/
|
|
39
|
+
export declare function trimString(input: string[]): string[];
|
|
26
40
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/string/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAElE;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,WACpB,MAAM,YACJ,iBAAiB,KACzB,MA0CF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,WAAY,MAAM,aAAa,MAAM,KAAG,MAYlE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,aAAc,eAAe,KAAG,MAkC5D,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/string/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAElE;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,WACpB,MAAM,YACJ,iBAAiB,KACzB,MA0CF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,WAAY,MAAM,aAAa,MAAM,KAAG,MAYlE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,aAAc,eAAe,KAAG,MAkC5D,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;AAElD;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC"}
|
package/dist/string/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateRandomID = exports.truncateString = exports.capitalizeString = void 0;
|
|
4
|
+
exports.trimString = trimString;
|
|
4
5
|
/**
|
|
5
6
|
* * Utility to convert the first letter of any string to uppercase and the rest lowercase (unless specified).
|
|
6
7
|
* Handles surrounding symbols like quotes or parentheses.
|
|
@@ -61,10 +62,10 @@ const truncateString = (string, maxLength) => {
|
|
|
61
62
|
};
|
|
62
63
|
exports.truncateString = truncateString;
|
|
63
64
|
/**
|
|
64
|
-
* * Generates a
|
|
65
|
+
* * Generates a random alphanumeric (16 characters long, this length is customizable in the options) ID string composed of an optional `prefix`, `suffix`, a `timestamp`, and a customizable separator.
|
|
65
66
|
*
|
|
66
67
|
* @param options Configuration options for random ID generation.
|
|
67
|
-
* @returns The generated ID string composed of the random alphanumeric string
|
|
68
|
+
* @returns The generated ID string composed of the random alphanumeric string of specified length with optional `timeStamp`, `prefix`, and `suffix`, `caseOption` and `separator`.
|
|
68
69
|
*/
|
|
69
70
|
const generateRandomID = (options) => {
|
|
70
71
|
const { prefix = '', suffix = '', timeStamp = false, length = 16, separator = '', caseOption = null, } = options || {};
|
|
@@ -91,3 +92,22 @@ const generateRandomID = (options) => {
|
|
|
91
92
|
}
|
|
92
93
|
};
|
|
93
94
|
exports.generateRandomID = generateRandomID;
|
|
95
|
+
/**
|
|
96
|
+
* * Trims all the words in a string or an array of strings.
|
|
97
|
+
*
|
|
98
|
+
* @param input String or array of strings.
|
|
99
|
+
* @returns Trimmed string or array of strings.
|
|
100
|
+
*/
|
|
101
|
+
function trimString(input) {
|
|
102
|
+
if (!input)
|
|
103
|
+
return '';
|
|
104
|
+
// If the input is a string, trim each word
|
|
105
|
+
if (typeof input === 'string' && !Array.isArray(input)) {
|
|
106
|
+
return input.trim().replace(/\s+/g, ' ');
|
|
107
|
+
}
|
|
108
|
+
// If the input is an array of strings, trim each string in the array
|
|
109
|
+
if (Array.isArray(input)) {
|
|
110
|
+
return input.map((str) => typeof str === 'string' ? str.trim().replace(/\s+/g, ' ') : str);
|
|
111
|
+
}
|
|
112
|
+
throw new Error('Invalid input type. Expected string or array of strings!');
|
|
113
|
+
}
|
package/dist/string/types.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/string/types.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/string/types.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,MAAM,WAAW,iBAAiB;IACjC,iGAAiG;IACjG,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,kFAAkF;IAClF,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,qFAAqF;IACrF,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,iDAAiD;AACjD,MAAM,WAAW,eAAe;IAC/B,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,+DAA+D;IAC/D,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,8EAA8E;IAC9E,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,gFAAgF;IAChF,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,6DAA6D;IAC7D,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,IAAI,CAAC;CACtC"}
|
package/package.json
CHANGED
package/dist/types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
|
package/dist/types/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|