nhb-toolbox 0.9.9 → 1.0.0

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 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 } from './object';
7
+ export { generateQueryParams, deepClone, deepEqual, mergeObjects, flattenObject, isEmptyObject, countObjectFields, } from './object';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -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,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC"}
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,SAAS,EACT,SAAS,EACT,YAAY,EACZ,aAAa,EACb,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.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;
3
+ exports.countObjectFields = exports.isEmptyObject = exports.flattenObject = exports.mergeObjects = exports.deepEqual = exports.deepClone = 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,3 +24,9 @@ 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, "deepClone", { enumerable: true, get: function () { return object_1.deepClone; } });
28
+ Object.defineProperty(exports, "deepEqual", { enumerable: true, get: function () { return object_1.deepEqual; } });
29
+ Object.defineProperty(exports, "mergeObjects", { enumerable: true, get: function () { return object_1.mergeObjects; } });
30
+ Object.defineProperty(exports, "flattenObject", { enumerable: true, get: function () { return object_1.flattenObject; } });
31
+ Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return object_1.isEmptyObject; } });
32
+ Object.defineProperty(exports, "countObjectFields", { enumerable: true, get: function () { return object_1.countObjectFields; } });
@@ -13,9 +13,7 @@ export declare const getRandomNumber: (options?: RandomNumberOptions) => number;
13
13
  *
14
14
  * @param num - Number to round.
15
15
  * @param options - Options for rounding behavior, including decimal places and return type.
16
- * @param options.decimalPlaces - Number of decimal places to round to.
17
- * @param options.isString - Flag to determine if the result should be returned as a string (default: `false`).
18
- * @returns - Rounded number (as a `number`) or string (if `isString` is `true`).
16
+ * @returns Converted number (as a `number`) or string (if `isString` is `true`).
19
17
  */
20
18
  export declare const convertToDecimal: (num: number, options?: DecimalOptions) => number | string;
21
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/number/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnE;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,aAAc,mBAAmB,KAAG,MA+C/D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,QACvB,MAAM,YACD,cAAc,KACtB,MAAM,GAAG,MAQX,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/number/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnE;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,aAAc,mBAAmB,KAAG,MA+C/D,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,QACvB,MAAM,YACD,cAAc,KACtB,MAAM,GAAG,MAQX,CAAC"}
@@ -48,15 +48,13 @@ exports.getRandomNumber = getRandomNumber;
48
48
  *
49
49
  * @param num - Number to round.
50
50
  * @param options - Options for rounding behavior, including decimal places and return type.
51
- * @param options.decimalPlaces - Number of decimal places to round to.
52
- * @param options.isString - Flag to determine if the result should be returned as a string (default: `false`).
53
- * @returns - Rounded number (as a `number`) or string (if `isString` is `true`).
51
+ * @returns Converted number (as a `number`) or string (if `isString` is `true`).
54
52
  */
55
53
  const convertToDecimal = (num, options) => {
56
54
  const { decimalPlaces = 2, isString = false } = options || {};
57
55
  if (isString) {
58
- return num.toFixed(decimalPlaces); // Return as string
56
+ return num.toFixed(decimalPlaces);
59
57
  }
60
- return parseFloat(num.toFixed(decimalPlaces)); // Return as number
58
+ return parseFloat(num.toFixed(decimalPlaces));
61
59
  };
62
60
  exports.convertToDecimal = convertToDecimal;
@@ -10,4 +10,47 @@
10
10
  * generateQueryParams({ key1: ['value1', 'value2'], key2: 42 }); // "?key1=value1&key1=value2&key2=42"
11
11
  */
12
12
  export declare const generateQueryParams: <T extends Record<string, string | number | string[] | number[]>>(params?: T) => string;
13
+ /**
14
+ * * Deep clone an object.
15
+ *
16
+ * @param obj Object to clone.
17
+ * @returns Deep cloned object.
18
+ */
19
+ export declare const deepClone: <T extends Record<string, unknown>>(obj: T) => T;
20
+ /**
21
+ * * Check if an object is empty.
22
+ *
23
+ * @param obj Object to check.
24
+ * @returns Whether the object is empty.
25
+ */
26
+ export declare const isEmptyObject: <T extends Record<string, unknown>>(obj: T) => boolean;
27
+ /**
28
+ * * Count the number of fields in an object.
29
+ *
30
+ * @param obj Object to check.
31
+ * @returns Number of fields in the object.
32
+ */
33
+ export declare const countObjectFields: <T extends Record<string, unknown>>(obj: T) => number;
34
+ /**
35
+ * * Deeply merge two or more objects using `Map`.
36
+ *
37
+ * @param objects Objects to merge.
38
+ * @returns Merged object.
39
+ */
40
+ export declare const mergeObjects: <T extends Record<string, unknown>>(...objects: T[]) => T;
41
+ /**
42
+ * * Deeply merge objects and flatten nested objects.
43
+ *
44
+ * @param objects Objects to merge.
45
+ * @returns Merged object with flattened structure.
46
+ */
47
+ export declare const flattenObject: <T extends Record<string, unknown>>(...objects: T[]) => T;
48
+ /**
49
+ * * Deeply compare two values (arrays, objects, or primitive values).
50
+ *
51
+ * @param a First value to compare.
52
+ * @param b Second value to compare.
53
+ * @returns Whether the values are deeply equal.
54
+ */
55
+ export declare const deepEqual: <T>(a: T, b: T) => boolean;
13
56
  //# 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"}
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,SAAS,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,KAAG,CAErE,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;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,cAClD,CAAC,EAAE,KACb,CAyBF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAG,OAgCzC,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateQueryParams = void 0;
3
+ exports.deepEqual = exports.flattenObject = exports.mergeObjects = exports.countObjectFields = exports.isEmptyObject = exports.deepClone = exports.generateQueryParams = void 0;
4
4
  /**
5
5
  * * Utility function to generate query parameters from an object.
6
6
  *
@@ -22,3 +22,132 @@ const generateQueryParams = (params = {}) => {
22
22
  return queryParams ? `?${queryParams}` : '';
23
23
  };
24
24
  exports.generateQueryParams = generateQueryParams;
25
+ /**
26
+ * * Deep clone an object.
27
+ *
28
+ * @param obj Object to clone.
29
+ * @returns Deep cloned object.
30
+ */
31
+ const deepClone = (obj) => {
32
+ return JSON.parse(JSON.stringify(obj));
33
+ };
34
+ exports.deepClone = deepClone;
35
+ /**
36
+ * * Check if an object is empty.
37
+ *
38
+ * @param obj Object to check.
39
+ * @returns Whether the object is empty.
40
+ */
41
+ const isEmptyObject = (obj) => {
42
+ return Object.keys(obj).length === 0;
43
+ };
44
+ exports.isEmptyObject = isEmptyObject;
45
+ /**
46
+ * * Count the number of fields in an object.
47
+ *
48
+ * @param obj Object to check.
49
+ * @returns Number of fields in the object.
50
+ */
51
+ const countObjectFields = (obj) => {
52
+ return Object.keys(obj).length;
53
+ };
54
+ exports.countObjectFields = countObjectFields;
55
+ /**
56
+ * * Deeply merge two or more objects using `Map`.
57
+ *
58
+ * @param objects Objects to merge.
59
+ * @returns Merged object.
60
+ */
61
+ const mergeObjects = (...objects) => {
62
+ const map = new Map();
63
+ objects.forEach((obj) => {
64
+ for (const key in obj) {
65
+ const existingValue = map.get(key);
66
+ if (obj[key] instanceof Object && !Array.isArray(obj[key])) {
67
+ // If the key already exists in the map and both are objects, merge them
68
+ if (existingValue &&
69
+ existingValue instanceof Object &&
70
+ !Array.isArray(existingValue)) {
71
+ map.set(key, (0, exports.mergeObjects)(existingValue, obj[key]));
72
+ }
73
+ else {
74
+ // Otherwise, just set the value
75
+ map.set(key, obj[key]);
76
+ }
77
+ }
78
+ else {
79
+ // If it's not an object, just set the value
80
+ map.set(key, obj[key]);
81
+ }
82
+ }
83
+ });
84
+ const result = {};
85
+ map.forEach((value, key) => {
86
+ result[key] = value;
87
+ });
88
+ return result;
89
+ };
90
+ exports.mergeObjects = mergeObjects;
91
+ /**
92
+ * * Deeply merge objects and flatten nested objects.
93
+ *
94
+ * @param objects Objects to merge.
95
+ * @returns Merged object with flattened structure.
96
+ */
97
+ const flattenObject = (...objects) => {
98
+ const map = new Map();
99
+ const flattenObject = (obj, parentKey = '') => {
100
+ for (const key in obj) {
101
+ const newKey = parentKey ? `${parentKey}.${key}` : key;
102
+ if (obj[key] instanceof Object && !Array.isArray(obj[key])) {
103
+ // Recursively flatten nested objects
104
+ flattenObject(obj[key], newKey);
105
+ }
106
+ else {
107
+ // Set the flattened key
108
+ map.set(newKey, obj[key]);
109
+ }
110
+ }
111
+ };
112
+ objects.forEach((obj) => flattenObject(obj));
113
+ const result = {};
114
+ map.forEach((value, key) => {
115
+ result[key] = value;
116
+ });
117
+ return result;
118
+ };
119
+ exports.flattenObject = flattenObject;
120
+ /**
121
+ * * Deeply compare two values (arrays, objects, or primitive values).
122
+ *
123
+ * @param a First value to compare.
124
+ * @param b Second value to compare.
125
+ * @returns Whether the values are deeply equal.
126
+ */
127
+ const deepEqual = (a, b) => {
128
+ // If both values are strictly equal (handles primitive types and same references)
129
+ if (a === b)
130
+ return true;
131
+ // If the types of the two values are different
132
+ if (typeof a !== typeof b)
133
+ return false;
134
+ // If either is null or undefined, they must both be null or undefined
135
+ if (a === null || b === null)
136
+ return a === b;
137
+ // Check for array equality
138
+ if (Array.isArray(a) && Array.isArray(b)) {
139
+ if (a.length !== b.length)
140
+ return false;
141
+ return a.every((element, index) => (0, exports.deepEqual)(element, b[index]));
142
+ }
143
+ // Check for object equality
144
+ if (typeof a === 'object' && typeof b === 'object') {
145
+ const aKeys = Object.keys(a);
146
+ const bKeys = Object.keys(b);
147
+ if (aKeys.length !== bKeys.length)
148
+ return false;
149
+ return aKeys.every((key) => (0, exports.deepEqual)(a[key], b[key]));
150
+ }
151
+ return false;
152
+ };
153
+ exports.deepEqual = deepEqual;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "0.9.9",
3
+ "version": "1.0.0",
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",