nhb-toolbox 1.0.0 → 1.0.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/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/object/index.d.ts +12 -3
- package/dist/object/index.d.ts.map +1 -1
- package/dist/object/index.js +48 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,5 +4,5 @@ export { getColorForFirstCharacter } from './colors';
|
|
|
4
4
|
export { generateRandomColor } from './colors/random';
|
|
5
5
|
export { convertHexToHsl, convertHexToRgb, convertHslToHex, convertHslToRgb, convertRgbToHex, convertRgbToHsl, } from './colors/convert';
|
|
6
6
|
export { flattenArray, createSelectOptions } from './array';
|
|
7
|
-
export { generateQueryParams,
|
|
7
|
+
export { generateQueryParams, cloneObject, isDeepEqual, mergeObjects, flattenObject, mergeAndFlattenObjects, isEmptyObject, countObjectFields, } from './object';
|
|
8
8
|
//# 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,EAAE,gBAAgB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE9E,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAErD,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,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE5D,OAAO,EACN,mBAAmB,EACnB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE9E,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAErD,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,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE5D,OAAO,EACN,mBAAmB,EACnB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,aAAa,EACb,sBAAsB,EACtB,aAAa,EACb,iBAAiB,GACjB,MAAM,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.countObjectFields = exports.isEmptyObject = exports.flattenObject = exports.mergeObjects = exports.
|
|
3
|
+
exports.countObjectFields = exports.isEmptyObject = exports.mergeAndFlattenObjects = exports.flattenObject = exports.mergeObjects = exports.isDeepEqual = exports.cloneObject = exports.generateQueryParams = exports.createSelectOptions = exports.flattenArray = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.generateRandomColor = exports.getColorForFirstCharacter = exports.convertToDecimal = exports.getRandomNumber = 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; } });
|
|
@@ -24,9 +24,10 @@ Object.defineProperty(exports, "flattenArray", { enumerable: true, get: function
|
|
|
24
24
|
Object.defineProperty(exports, "createSelectOptions", { enumerable: true, get: function () { return array_1.createSelectOptions; } });
|
|
25
25
|
var object_1 = require("./object");
|
|
26
26
|
Object.defineProperty(exports, "generateQueryParams", { enumerable: true, get: function () { return object_1.generateQueryParams; } });
|
|
27
|
-
Object.defineProperty(exports, "
|
|
28
|
-
Object.defineProperty(exports, "
|
|
27
|
+
Object.defineProperty(exports, "cloneObject", { enumerable: true, get: function () { return object_1.cloneObject; } });
|
|
28
|
+
Object.defineProperty(exports, "isDeepEqual", { enumerable: true, get: function () { return object_1.isDeepEqual; } });
|
|
29
29
|
Object.defineProperty(exports, "mergeObjects", { enumerable: true, get: function () { return object_1.mergeObjects; } });
|
|
30
30
|
Object.defineProperty(exports, "flattenObject", { enumerable: true, get: function () { return object_1.flattenObject; } });
|
|
31
|
+
Object.defineProperty(exports, "mergeAndFlattenObjects", { enumerable: true, get: function () { return object_1.mergeAndFlattenObjects; } });
|
|
31
32
|
Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return object_1.isEmptyObject; } });
|
|
32
33
|
Object.defineProperty(exports, "countObjectFields", { enumerable: true, get: function () { return object_1.countObjectFields; } });
|
package/dist/object/index.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare const generateQueryParams: <T extends Record<string, string | num
|
|
|
16
16
|
* @param obj Object to clone.
|
|
17
17
|
* @returns Deep cloned object.
|
|
18
18
|
*/
|
|
19
|
-
export declare const
|
|
19
|
+
export declare const cloneObject: <T extends Record<string, unknown>>(obj: T) => T;
|
|
20
20
|
/**
|
|
21
21
|
* * Check if an object is empty.
|
|
22
22
|
*
|
|
@@ -40,11 +40,20 @@ export declare const countObjectFields: <T extends Record<string, unknown>>(obj:
|
|
|
40
40
|
export declare const mergeObjects: <T extends Record<string, unknown>>(...objects: T[]) => T;
|
|
41
41
|
/**
|
|
42
42
|
* * Deeply merge objects and flatten nested objects.
|
|
43
|
+
* * Useful for flattening a single object or merging multiple objects with duplicate key(s).
|
|
44
|
+
* * If keys are duplicated, the last object's value will be used.
|
|
43
45
|
*
|
|
44
46
|
* @param objects Objects to merge.
|
|
45
47
|
* @returns Merged object with flattened structure.
|
|
46
48
|
*/
|
|
47
|
-
export declare const
|
|
49
|
+
export declare const mergeAndFlattenObjects: <T extends Record<string, unknown>>(...objects: T[]) => Record<string, unknown>;
|
|
50
|
+
/**
|
|
51
|
+
* * Flattens a nested object into a dot notation format.
|
|
52
|
+
*
|
|
53
|
+
* @param object - The `object` to flatten.
|
|
54
|
+
* @returns A `flattened object` with dot notation keys.
|
|
55
|
+
*/
|
|
56
|
+
export declare const flattenObject: <T extends Record<string, unknown>>(object: T) => Record<string, unknown>;
|
|
48
57
|
/**
|
|
49
58
|
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
50
59
|
*
|
|
@@ -52,5 +61,5 @@ export declare const flattenObject: <T extends Record<string, unknown>>(...objec
|
|
|
52
61
|
* @param b Second value to compare.
|
|
53
62
|
* @returns Whether the values are deeply equal.
|
|
54
63
|
*/
|
|
55
|
-
export declare const
|
|
64
|
+
export declare const isDeepEqual: <T>(a: T, b: T) => boolean;
|
|
56
65
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA;;;;;;;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+
exports.isDeepEqual = exports.flattenObject = exports.mergeAndFlattenObjects = exports.mergeObjects = exports.countObjectFields = exports.isEmptyObject = exports.cloneObject = exports.generateQueryParams = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* * Utility function to generate query parameters from an object.
|
|
6
6
|
*
|
|
@@ -28,10 +28,10 @@ exports.generateQueryParams = generateQueryParams;
|
|
|
28
28
|
* @param obj Object to clone.
|
|
29
29
|
* @returns Deep cloned object.
|
|
30
30
|
*/
|
|
31
|
-
const
|
|
31
|
+
const cloneObject = (obj) => {
|
|
32
32
|
return JSON.parse(JSON.stringify(obj));
|
|
33
33
|
};
|
|
34
|
-
exports.
|
|
34
|
+
exports.cloneObject = cloneObject;
|
|
35
35
|
/**
|
|
36
36
|
* * Check if an object is empty.
|
|
37
37
|
*
|
|
@@ -90,18 +90,20 @@ const mergeObjects = (...objects) => {
|
|
|
90
90
|
exports.mergeObjects = mergeObjects;
|
|
91
91
|
/**
|
|
92
92
|
* * Deeply merge objects and flatten nested objects.
|
|
93
|
+
* * Useful for flattening a single object or merging multiple objects with duplicate key(s).
|
|
94
|
+
* * If keys are duplicated, the last object's value will be used.
|
|
93
95
|
*
|
|
94
96
|
* @param objects Objects to merge.
|
|
95
97
|
* @returns Merged object with flattened structure.
|
|
96
98
|
*/
|
|
97
|
-
const
|
|
99
|
+
const mergeAndFlattenObjects = (...objects) => {
|
|
98
100
|
const map = new Map();
|
|
99
|
-
const
|
|
101
|
+
const _flattenObject = (obj, parentKey = '') => {
|
|
100
102
|
for (const key in obj) {
|
|
101
|
-
const newKey = parentKey ? `${parentKey}.${key}` : key;
|
|
103
|
+
const newKey = parentKey ? `${String(parentKey)}.${key}` : key;
|
|
102
104
|
if (obj[key] instanceof Object && !Array.isArray(obj[key])) {
|
|
103
105
|
// Recursively flatten nested objects
|
|
104
|
-
|
|
106
|
+
_flattenObject(obj[key], newKey);
|
|
105
107
|
}
|
|
106
108
|
else {
|
|
107
109
|
// Set the flattened key
|
|
@@ -109,13 +111,47 @@ const flattenObject = (...objects) => {
|
|
|
109
111
|
}
|
|
110
112
|
}
|
|
111
113
|
};
|
|
112
|
-
objects.forEach((obj) =>
|
|
114
|
+
objects.forEach((obj) => _flattenObject(obj));
|
|
113
115
|
const result = {};
|
|
114
116
|
map.forEach((value, key) => {
|
|
115
117
|
result[key] = value;
|
|
116
118
|
});
|
|
117
119
|
return result;
|
|
118
120
|
};
|
|
121
|
+
exports.mergeAndFlattenObjects = mergeAndFlattenObjects;
|
|
122
|
+
/**
|
|
123
|
+
* * Flattens a nested object into a dot notation format.
|
|
124
|
+
*
|
|
125
|
+
* @param object - The `object` to flatten.
|
|
126
|
+
* @returns A `flattened object` with dot notation keys.
|
|
127
|
+
*/
|
|
128
|
+
const flattenObject = (object) => {
|
|
129
|
+
/**
|
|
130
|
+
* * Recursively flattens an object, transforming nested structures into dot-notation keys.
|
|
131
|
+
*
|
|
132
|
+
* @param source - The `object` to be flattened.
|
|
133
|
+
* @param prefix - The prefix to prepend to each key. Used for nested objects.
|
|
134
|
+
* @returns A flattened version of the input object.
|
|
135
|
+
*/
|
|
136
|
+
const _flattenObject = (source, prefix = '') => {
|
|
137
|
+
const flattened = {};
|
|
138
|
+
for (const [key, value] of Object.entries(source)) {
|
|
139
|
+
// Construct the dot-notation key
|
|
140
|
+
const newKey = prefix ? `${String(prefix)}.${key}` : key;
|
|
141
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
142
|
+
// Recursively process nested objects
|
|
143
|
+
Object.assign(flattened, _flattenObject(value, newKey));
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
// Directly assign non-object values
|
|
147
|
+
flattened[newKey] = value;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return flattened;
|
|
151
|
+
};
|
|
152
|
+
// Call the recursive function with an empty prefix initially
|
|
153
|
+
return _flattenObject(object);
|
|
154
|
+
};
|
|
119
155
|
exports.flattenObject = flattenObject;
|
|
120
156
|
/**
|
|
121
157
|
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
@@ -124,7 +160,7 @@ exports.flattenObject = flattenObject;
|
|
|
124
160
|
* @param b Second value to compare.
|
|
125
161
|
* @returns Whether the values are deeply equal.
|
|
126
162
|
*/
|
|
127
|
-
const
|
|
163
|
+
const isDeepEqual = (a, b) => {
|
|
128
164
|
// If both values are strictly equal (handles primitive types and same references)
|
|
129
165
|
if (a === b)
|
|
130
166
|
return true;
|
|
@@ -138,7 +174,7 @@ const deepEqual = (a, b) => {
|
|
|
138
174
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
139
175
|
if (a.length !== b.length)
|
|
140
176
|
return false;
|
|
141
|
-
return a.every((element, index) => (0, exports.
|
|
177
|
+
return a.every((element, index) => (0, exports.isDeepEqual)(element, b[index]));
|
|
142
178
|
}
|
|
143
179
|
// Check for object equality
|
|
144
180
|
if (typeof a === 'object' && typeof b === 'object') {
|
|
@@ -146,8 +182,8 @@ const deepEqual = (a, b) => {
|
|
|
146
182
|
const bKeys = Object.keys(b);
|
|
147
183
|
if (aKeys.length !== bKeys.length)
|
|
148
184
|
return false;
|
|
149
|
-
return aKeys.every((key) => (0, exports.
|
|
185
|
+
return aKeys.every((key) => (0, exports.isDeepEqual)(a[key], b[key]));
|
|
150
186
|
}
|
|
151
187
|
return false;
|
|
152
188
|
};
|
|
153
|
-
exports.
|
|
189
|
+
exports.isDeepEqual = isDeepEqual;
|
package/package.json
CHANGED