nhb-toolbox 0.9.8 → 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
@@ -1,8 +1,8 @@
1
1
  export { capitalizeString, truncateString, generateRandomID } from './string';
2
- export { getRandomNumber } from './number';
2
+ 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
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,MAAM,UAAU,CAAC;AAE3C,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,12 +1,13 @@
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.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; } });
7
7
  Object.defineProperty(exports, "generateRandomID", { enumerable: true, get: function () { return string_1.generateRandomID; } });
8
8
  var number_1 = require("./number");
9
9
  Object.defineProperty(exports, "getRandomNumber", { enumerable: true, get: function () { return number_1.getRandomNumber; } });
10
+ Object.defineProperty(exports, "convertToDecimal", { enumerable: true, get: function () { return number_1.convertToDecimal; } });
10
11
  var colors_1 = require("./colors");
11
12
  Object.defineProperty(exports, "getColorForFirstCharacter", { enumerable: true, get: function () { return colors_1.getColorForFirstCharacter; } });
12
13
  var random_1 = require("./colors/random");
@@ -23,3 +24,9 @@ Object.defineProperty(exports, "flattenArray", { enumerable: true, get: function
23
24
  Object.defineProperty(exports, "createSelectOptions", { enumerable: true, get: function () { return array_1.createSelectOptions; } });
24
25
  var object_1 = require("./object");
25
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; } });
@@ -1,4 +1,4 @@
1
- import type { RandomNumberOptions } from './types';
1
+ import type { DecimalOptions, RandomNumberOptions } from './types';
2
2
  /**
3
3
  * * Utility to generate a random number between a given range.
4
4
  * * If no options are provided, it will generate a random number between `0` and `100` (inclusive).
@@ -8,4 +8,12 @@ import type { RandomNumberOptions } from './types';
8
8
  * @returns Random number.
9
9
  */
10
10
  export declare const getRandomNumber: (options?: RandomNumberOptions) => number;
11
+ /**
12
+ * * Utility to round a number to a given decimal places.
13
+ *
14
+ * @param num - Number to round.
15
+ * @param options - Options for rounding behavior, including decimal places and return type.
16
+ * @returns Converted number (as a `number`) or string (if `isString` is `true`).
17
+ */
18
+ export declare const convertToDecimal: (num: number, options?: DecimalOptions) => number | string;
11
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,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,aAAc,mBAAmB,KAAG,MA+C/D,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"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getRandomNumber = void 0;
3
+ exports.convertToDecimal = exports.getRandomNumber = void 0;
4
4
  /**
5
5
  * * Utility to generate a random number between a given range.
6
6
  * * If no options are provided, it will generate a random number between `0` and `100` (inclusive).
@@ -43,3 +43,18 @@ const getRandomNumber = (options) => {
43
43
  return 0;
44
44
  };
45
45
  exports.getRandomNumber = getRandomNumber;
46
+ /**
47
+ * * Utility to round a number to a given decimal places.
48
+ *
49
+ * @param num - Number to round.
50
+ * @param options - Options for rounding behavior, including decimal places and return type.
51
+ * @returns Converted number (as a `number`) or string (if `isString` is `true`).
52
+ */
53
+ const convertToDecimal = (num, options) => {
54
+ const { decimalPlaces = 2, isString = false } = options || {};
55
+ if (isString) {
56
+ return num.toFixed(decimalPlaces);
57
+ }
58
+ return parseFloat(num.toFixed(decimalPlaces));
59
+ };
60
+ exports.convertToDecimal = convertToDecimal;
@@ -9,4 +9,11 @@ export interface RandomNumberOptions {
9
9
  /** Whether to include the maximum number. */
10
10
  includeMax?: boolean;
11
11
  }
12
+ /** Decimal options for converting to decimal */
13
+ export interface DecimalOptions {
14
+ /** Number of decimal places to round to. Defaults to `2`. */
15
+ decimalPlaces?: number;
16
+ /** If the return value is in `string` or `number`. Defaults to `false`. */
17
+ isString?: boolean;
18
+ }
12
19
  //# sourceMappingURL=types.d.ts.map
@@ -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"}
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,gDAAgD;AAChD,MAAM,WAAW,cAAc;IAC9B,6DAA6D;IAC7D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB"}
@@ -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.8",
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",