nhb-toolbox 1.0.0 → 1.0.2
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 +7 -8
- package/dist/array/index.d.ts.map +1 -1
- package/dist/array/index.js +10 -10
- package/dist/array/types.d.ts +34 -6
- package/dist/array/types.d.ts.map +1 -1
- package/dist/array/types.js +0 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -4
- package/dist/number/types.d.ts +1 -1
- package/dist/number/types.d.ts.map +1 -1
- 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/array/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Flattened, ConfigOptions, OptionInput } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Flattens a nested array recursively or wraps any non-array data type in an array.
|
|
4
4
|
*
|
|
@@ -6,15 +6,14 @@ import type { Flatten, SelectOptions } from './types';
|
|
|
6
6
|
* @param input - The input value, which can be a nested array or a non-array value.
|
|
7
7
|
* @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
8
|
*/
|
|
9
|
-
export declare const flattenArray: <T>(input: T | T[]) =>
|
|
9
|
+
export declare const flattenArray: <T>(input: T | T[]) => Flattened<T>[];
|
|
10
10
|
/**
|
|
11
|
-
* * Converts an array of objects into a formatted array of
|
|
11
|
+
* * Converts an array of objects into a formatted array of options.
|
|
12
12
|
*
|
|
13
13
|
* @typeParam T - The type of each object in the `data` array.
|
|
14
|
-
* @param data - An array of objects to convert into
|
|
15
|
-
* @param
|
|
16
|
-
* @
|
|
17
|
-
* @returns An array of select options, where each option has a `value` and `label` field.
|
|
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.
|
|
18
17
|
*/
|
|
19
|
-
export declare const
|
|
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; }[];
|
|
20
19
|
//# 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,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/array/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAErE;;;;;;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"}
|
package/dist/array/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createOptionsArray = exports.flattenArray = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* * Flattens a nested array recursively or wraps any non-array data type in an array.
|
|
6
6
|
*
|
|
@@ -18,23 +18,23 @@ const flattenArray = (input) => {
|
|
|
18
18
|
};
|
|
19
19
|
exports.flattenArray = flattenArray;
|
|
20
20
|
/**
|
|
21
|
-
* * Converts an array of objects into a formatted array of
|
|
21
|
+
* * Converts an array of objects into a formatted array of options.
|
|
22
22
|
*
|
|
23
23
|
* @typeParam T - The type of each object in the `data` array.
|
|
24
|
-
* @param data - An array of objects to convert into
|
|
25
|
-
* @param
|
|
26
|
-
* @
|
|
27
|
-
* @returns An array of select options, where each option has a `value` and `label` field.
|
|
24
|
+
* @param data - An array of objects to convert into options.
|
|
25
|
+
* @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
|
|
26
|
+
* @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
27
|
*/
|
|
29
|
-
const
|
|
28
|
+
const createOptionsArray = (data, config) => {
|
|
29
|
+
const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', } = config || {};
|
|
30
30
|
if (data && data.length) {
|
|
31
31
|
return data.map((datum) => ({
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
[firstFieldName]: String(datum[firstFieldKey] ?? ''),
|
|
33
|
+
[secondFieldName]: String(datum[secondFieldKey] ?? ''),
|
|
34
34
|
}));
|
|
35
35
|
}
|
|
36
36
|
else {
|
|
37
37
|
return [];
|
|
38
38
|
}
|
|
39
39
|
};
|
|
40
|
-
exports.
|
|
40
|
+
exports.createOptionsArray = createOptionsArray;
|
package/dist/array/types.d.ts
CHANGED
|
@@ -1,9 +1,37 @@
|
|
|
1
1
|
/** - Flatten Array */
|
|
2
|
-
export type
|
|
3
|
-
/** - Select Options */
|
|
4
|
-
export interface SelectOptions {
|
|
5
|
-
value: string;
|
|
6
|
-
label: string;
|
|
7
|
-
}
|
|
2
|
+
export type Flattened<T> = T extends (infer U)[] ? Flattened<U> : T;
|
|
8
3
|
/** */
|
|
4
|
+
export type OptionInput = Record<string, string | number | null | undefined>;
|
|
5
|
+
/**
|
|
6
|
+
* - Configuration for `createOptionsArray`.
|
|
7
|
+
* - Defines the mapping between keys in the input objects and the keys in the output options.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam T - The type of the objects in the input array.
|
|
10
|
+
* @typeParam K1 - The name of the key for the first field in the output (default: `'value'`).
|
|
11
|
+
* @typeParam K2 - The name of the key for the second field in the output (default: `'label'`).
|
|
12
|
+
*/
|
|
13
|
+
export interface ConfigOptions<T extends Record<string, string | number | null | undefined>, K1 extends string = 'value', K2 extends string = 'label'> {
|
|
14
|
+
/**
|
|
15
|
+
* - The key in the input objects to use for the first field of the option.
|
|
16
|
+
* @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'}).
|
|
17
|
+
*/
|
|
18
|
+
firstFieldKey: keyof T;
|
|
19
|
+
/**
|
|
20
|
+
* - The key in the input objects to use for the second field of the option.
|
|
21
|
+
* @example If the input objects have a `name` field and you want to use it as the `label` field in the output, set createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name'}).
|
|
22
|
+
*/
|
|
23
|
+
secondFieldKey: keyof T;
|
|
24
|
+
/**
|
|
25
|
+
* - The name of the first field in the output object.
|
|
26
|
+
* - Defaults to `'value'`.
|
|
27
|
+
* @example If you want the output field to be named `'key'` instead of `'value'`, set createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name', firstFieldName: 'key'}).
|
|
28
|
+
*/
|
|
29
|
+
firstFieldName?: K1;
|
|
30
|
+
/**
|
|
31
|
+
* - The name of the second field in the output object.
|
|
32
|
+
* - Defaults to `'label'`.
|
|
33
|
+
* @example If you want the output field to be named `'title'` instead of `'label'`, set createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name', firstFieldName: 'key', secondFieldName: 'title'}).
|
|
34
|
+
*/
|
|
35
|
+
secondFieldName?: K2;
|
|
36
|
+
}
|
|
9
37
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/array/types.ts"],"names":[],"mappings":"AAAA,sBAAsB;AACtB,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/array/types.ts"],"names":[],"mappings":"AAAA,sBAAsB;AACtB,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEpE,OAAO;AACP,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;;;;OAIG;IACH,cAAc,CAAC,EAAE,EAAE,CAAC;IAEpB;;;;OAIG;IACH,eAAe,CAAC,EAAE,EAAE,CAAC;CACrB"}
|
package/dist/array/types.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ export { getRandomNumber, convertToDecimal } from './number';
|
|
|
3
3
|
export { getColorForFirstCharacter } from './colors';
|
|
4
4
|
export { generateRandomColor } from './colors/random';
|
|
5
5
|
export { convertHexToHsl, convertHexToRgb, convertHslToHex, convertHslToRgb, convertRgbToHex, convertRgbToHsl, } from './colors/convert';
|
|
6
|
-
export { flattenArray,
|
|
7
|
-
export { generateQueryParams,
|
|
6
|
+
export { flattenArray, createOptionsArray, } from './array';
|
|
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,
|
|
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,EACN,YAAY,EACZ,kBAAkB,GAClB,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"}
|
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.createOptionsArray = 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; } });
|
|
@@ -21,12 +21,13 @@ Object.defineProperty(exports, "convertRgbToHex", { enumerable: true, get: funct
|
|
|
21
21
|
Object.defineProperty(exports, "convertRgbToHsl", { enumerable: true, get: function () { return convert_1.convertRgbToHsl; } });
|
|
22
22
|
var array_1 = require("./array");
|
|
23
23
|
Object.defineProperty(exports, "flattenArray", { enumerable: true, get: function () { return array_1.flattenArray; } });
|
|
24
|
-
Object.defineProperty(exports, "
|
|
24
|
+
Object.defineProperty(exports, "createOptionsArray", { enumerable: true, get: function () { return array_1.createOptionsArray; } });
|
|
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/number/types.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export interface RandomNumberOptions {
|
|
|
9
9
|
/** Whether to include the maximum number. */
|
|
10
10
|
includeMax?: boolean;
|
|
11
11
|
}
|
|
12
|
-
/** Decimal options for converting to decimal */
|
|
12
|
+
/** - Decimal options for converting to decimal */
|
|
13
13
|
export interface DecimalOptions {
|
|
14
14
|
/** Number of decimal places to round to. Defaults to `2`. */
|
|
15
15
|
decimalPlaces?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/number/types.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,MAAM,WAAW,mBAAmB;IACnC,oCAAoC;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/number/types.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,MAAM,WAAW,mBAAmB;IACnC,oCAAoC;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,kDAAkD;AAClD,MAAM,WAAW,cAAc;IAC9B,6DAA6D;IAC7D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB"}
|
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;;;;;;;;;;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,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,KAAG,CAEvE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,OACzD,CAAC,KACJ,OAEF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,OAC7D,CAAC,KACJ,MAEF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,cACjD,CAAC,EAAE,KACb,CAuCF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,cAC3D,CAAC,EAAE,KACb,MAAM,CAAC,MAAM,EAAE,OAAO,CA4BxB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,UACtD,CAAC,KACP,MAAM,CAAC,MAAM,EAAE,OAAO,CAgCxB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAG,OAgC3C,CAAC"}
|
package/dist/object/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
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