nhb-toolbox 1.5.1 → 1.6.6
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/colors/convert.d.ts +3 -10
- package/dist/colors/convert.d.ts.map +1 -1
- package/dist/colors/convert.js +17 -22
- package/dist/colors/helpers.d.ts +13 -5
- package/dist/colors/helpers.d.ts.map +1 -1
- package/dist/colors/helpers.js +20 -9
- package/dist/colors/initials.d.ts.map +1 -1
- package/dist/colors/initials.js +10 -10
- package/dist/colors/random.d.ts +15 -3
- package/dist/colors/random.d.ts.map +1 -1
- package/dist/colors/random.js +19 -7
- package/dist/colors/types.d.ts +1 -1
- package/dist/colors/types.d.ts.map +1 -1
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -9
- package/dist/object/basics.d.ts +4 -28
- package/dist/object/basics.d.ts.map +1 -1
- package/dist/object/basics.js +8 -133
- package/dist/object/objectify.d.ts +49 -0
- package/dist/object/objectify.d.ts.map +1 -0
- package/dist/object/objectify.js +186 -0
- package/dist/object/types.d.ts +3 -1
- package/dist/object/types.d.ts.map +1 -1
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +37 -0
- package/package.json +1 -1
package/dist/colors/convert.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Hex, HSL, RGB } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Converts HSL to RGB color format.
|
|
4
4
|
*
|
|
@@ -39,9 +39,10 @@ export declare const convertHexToHsl: (hex: Hex | string) => HSL;
|
|
|
39
39
|
* @param r - The red component of the RGB color, in the range 0 to 255.
|
|
40
40
|
* @param g - The green component of the RGB color, in the range 0 to 255.
|
|
41
41
|
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
42
|
+
* @param a - The alpha opacity of the RGB color, in the range 0 to 255.
|
|
42
43
|
* @returns A string representing the color in Hex format (e.g., `#FF0000`).
|
|
43
44
|
*/
|
|
44
|
-
export declare const convertRgbToHex: (r: number, g: number, b: number) => Hex;
|
|
45
|
+
export declare const convertRgbToHex: (r: number, g: number, b: number, a?: number) => Hex;
|
|
45
46
|
/**
|
|
46
47
|
* * Converts Hex to RGB color format.
|
|
47
48
|
*
|
|
@@ -49,14 +50,6 @@ export declare const convertRgbToHex: (r: number, g: number, b: number) => Hex;
|
|
|
49
50
|
* @returns A string representing the color in RGB format (e.g., `rgb(255, 0, 0)`).
|
|
50
51
|
*/
|
|
51
52
|
export declare const convertHexToRgb: (hex: Hex | string) => RGB;
|
|
52
|
-
/**
|
|
53
|
-
* * Extracts numbers from a color string like `rgb(66, 103, 69)` or `hsl(120, 42.86%, 41.18%)`.
|
|
54
|
-
* * Converts percentage values to decimal (e.g., `42.86%` → `42.86`).
|
|
55
|
-
*
|
|
56
|
-
* @param colorString The color string in RGB or HSL format.
|
|
57
|
-
* @returns An array of extracted numbers.
|
|
58
|
-
*/
|
|
59
|
-
export declare const extractNumbersFromColor: (colorString: HSL | RGB) => ColorNumbers;
|
|
60
53
|
/**
|
|
61
54
|
* * Converts a `Hex` color code to `RGB` and `HSL` formats.
|
|
62
55
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/colors/convert.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/colors/convert.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAA0B,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAErE;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,MAAO,MAAM,KAAK,MAAM,KAAK,MAAM,KAAG,GAwBjE,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,MAAO,MAAM,KAAK,MAAM,KAAK,MAAM,KAAG,GAkCjE,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,MAAO,MAAM,KAAK,MAAM,KAAK,MAAM,KAAG,GAIjE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,QAAS,GAAG,GAAG,MAAM,KAAG,GAenD,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,MACxB,MAAM,KACN,MAAM,KACN,MAAM,MACL,MAAM,KACR,GAcF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,QAAS,GAAG,GAAG,MAAM,KAAG,GAgBnD,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,GAAG,GAAG;IAC7C,GAAG,EAAE,GAAG,CAAC;IACT,GAAG,EAAE,GAAG,CAAC;CACT,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,GAAG,GAAG;IAC7C,GAAG,EAAE,GAAG,CAAC;IACT,GAAG,EAAE,GAAG,CAAC;CACT,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,GAAG,GAAG;IAC7C,GAAG,EAAE,GAAG,CAAC;IACT,GAAG,EAAE,GAAG,CAAC;CACT,CAAC"}
|
package/dist/colors/convert.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.convertHexToRgb = exports.convertRgbToHex = exports.convertHexToHsl = exports.convertHslToHex = exports.convertRgbToHsl = exports.convertHslToRgb = void 0;
|
|
4
4
|
exports.convertColorCode = convertColorCode;
|
|
5
|
+
const helpers_1 = require("./helpers");
|
|
5
6
|
/**
|
|
6
7
|
* * Converts HSL to RGB color format.
|
|
7
8
|
*
|
|
@@ -110,13 +111,21 @@ exports.convertHexToHsl = convertHexToHsl;
|
|
|
110
111
|
* @param r - The red component of the RGB color, in the range 0 to 255.
|
|
111
112
|
* @param g - The green component of the RGB color, in the range 0 to 255.
|
|
112
113
|
* @param b - The blue component of the RGB color, in the range 0 to 255.
|
|
114
|
+
* @param a - The alpha opacity of the RGB color, in the range 0 to 255.
|
|
113
115
|
* @returns A string representing the color in Hex format (e.g., `#FF0000`).
|
|
114
116
|
*/
|
|
115
|
-
const convertRgbToHex = (r, g, b) => {
|
|
116
|
-
|
|
117
|
+
const convertRgbToHex = (r, g, b, a) => {
|
|
118
|
+
let hex = [r, g, b]
|
|
117
119
|
.map((v) => v.toString(16).padStart(2, '0'))
|
|
118
120
|
.join('')
|
|
119
|
-
.toUpperCase()
|
|
121
|
+
.toUpperCase();
|
|
122
|
+
if (a !== undefined) {
|
|
123
|
+
const alphaHex = Math.round(a * 255)
|
|
124
|
+
.toString(16)
|
|
125
|
+
.padStart(2, '0');
|
|
126
|
+
hex += alphaHex;
|
|
127
|
+
}
|
|
128
|
+
return `#${hex}`;
|
|
120
129
|
};
|
|
121
130
|
exports.convertRgbToHex = convertRgbToHex;
|
|
122
131
|
/**
|
|
@@ -140,17 +149,6 @@ const convertHexToRgb = (hex) => {
|
|
|
140
149
|
return `rgb(${r}, ${g}, ${b})`;
|
|
141
150
|
};
|
|
142
151
|
exports.convertHexToRgb = convertHexToRgb;
|
|
143
|
-
/**
|
|
144
|
-
* * Extracts numbers from a color string like `rgb(66, 103, 69)` or `hsl(120, 42.86%, 41.18%)`.
|
|
145
|
-
* * Converts percentage values to decimal (e.g., `42.86%` → `42.86`).
|
|
146
|
-
*
|
|
147
|
-
* @param colorString The color string in RGB or HSL format.
|
|
148
|
-
* @returns An array of extracted numbers.
|
|
149
|
-
*/
|
|
150
|
-
const extractNumbersFromColor = (colorString) => {
|
|
151
|
-
return (colorString.match(/[\d.]+%?/g) || []).map((value) => parseFloat(value));
|
|
152
|
-
};
|
|
153
|
-
exports.extractNumbersFromColor = extractNumbersFromColor;
|
|
154
152
|
/**
|
|
155
153
|
* * Converts a color from `Hex`, `RGB`, or `HSL` format to its equivalent representations.
|
|
156
154
|
*
|
|
@@ -167,18 +165,15 @@ function convertColorCode(color) {
|
|
|
167
165
|
}
|
|
168
166
|
if (color.startsWith('rgb')) {
|
|
169
167
|
return {
|
|
170
|
-
hex: (0, exports.convertRgbToHex)(...(0,
|
|
171
|
-
hsl: (0, exports.convertRgbToHsl)(...(0,
|
|
168
|
+
hex: (0, exports.convertRgbToHex)(...(0, helpers_1.extractNumbersFromColor)(color)),
|
|
169
|
+
hsl: (0, exports.convertRgbToHsl)(...(0, helpers_1.extractNumbersFromColor)(color)),
|
|
172
170
|
};
|
|
173
171
|
}
|
|
174
172
|
if (color.startsWith('hsl')) {
|
|
175
173
|
return {
|
|
176
|
-
hex: (0, exports.convertHslToHex)(...(0,
|
|
177
|
-
rgb: (0, exports.convertHslToRgb)(...(0,
|
|
174
|
+
hex: (0, exports.convertHslToHex)(...(0, helpers_1.extractNumbersFromColor)(color)),
|
|
175
|
+
rgb: (0, exports.convertHslToRgb)(...(0, helpers_1.extractNumbersFromColor)(color)),
|
|
178
176
|
};
|
|
179
177
|
}
|
|
180
178
|
throw new Error(`Unrecognized Color Format: ${color}`);
|
|
181
179
|
}
|
|
182
|
-
console.info(convertColorCode('#3c6945'));
|
|
183
|
-
console.info(convertColorCode('rgb(60, 105, 69)'));
|
|
184
|
-
console.info(convertColorCode('hsl(132, 27.27%, 32.35%)'));
|
package/dist/colors/helpers.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import type { OpacityValue } from './types';
|
|
1
|
+
import type { ColorNumbers, HSL, OpacityValue, RGB } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Converts opacity percentage (0-100) to a 2-digit hex string.
|
|
4
4
|
*
|
|
5
5
|
* @param opacity - The opacity value as a percentage (0-100).
|
|
6
6
|
* @returns A 2-digit hex string representing the alpha value.
|
|
7
7
|
*/
|
|
8
|
-
export declare const
|
|
8
|
+
export declare const _convertOpacityToHex: (opacity: OpacityValue) => string;
|
|
9
9
|
/**
|
|
10
10
|
* * Applies an opacity value to a color string.
|
|
11
11
|
* @param color The color string to apply opacity to.
|
|
12
12
|
* @param opacity The opacity value as a percentage (0-100).
|
|
13
13
|
* @returns The color string with the opacity value applied.
|
|
14
14
|
*/
|
|
15
|
-
export declare const
|
|
15
|
+
export declare const _applyOpacity: (color: string, opacity: string) => string;
|
|
16
16
|
/**
|
|
17
17
|
* * Helper function to generate a random HSL color.
|
|
18
18
|
*
|
|
19
19
|
* @returns A random HSL color string.
|
|
20
20
|
*/
|
|
21
|
-
export declare const
|
|
21
|
+
export declare const _generateRandomHSL: () => HSL;
|
|
22
22
|
/**
|
|
23
23
|
* * Helper function to check if the new color is visually too similar to the previous one.
|
|
24
24
|
* * It compares the hue, saturation, and lightness difference between the new color and the last one generated.
|
|
@@ -27,5 +27,13 @@ export declare const generateColorHSL: () => string;
|
|
|
27
27
|
* @param newColor - The new color to compare.
|
|
28
28
|
* @returns `Boolean` : `true` if the new color is similar to the previous one.
|
|
29
29
|
*/
|
|
30
|
-
export declare const
|
|
30
|
+
export declare const _isSimilarToLast: (recentColors: string[], newColor: string) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* * Extracts numbers from a color string like `rgb(66, 103, 69)` or `hsl(120, 42.86%, 41.18%)`.
|
|
33
|
+
* * Converts percentage values to decimal (e.g., `42.86%` → `42.86`).
|
|
34
|
+
*
|
|
35
|
+
* @param colorString The color string in RGB or HSL format.
|
|
36
|
+
* @returns An array of extracted numbers.
|
|
37
|
+
*/
|
|
38
|
+
export declare const extractNumbersFromColor: (colorString: HSL | RGB) => ColorNumbers;
|
|
31
39
|
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/colors/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/colors/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAEpE;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,YAAa,YAAY,KAAG,MAO5D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,UAAW,MAAM,WAAW,MAAM,KAAG,MAE9D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,QAAO,GAKrC,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,iBACd,MAAM,EAAE,YACZ,MAAM,KACd,OA2BF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,gBACtB,GAAG,GAAG,GAAG,KACpB,YAIF,CAAC"}
|
package/dist/colors/helpers.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.extractNumbersFromColor = exports._isSimilarToLast = exports._generateRandomHSL = exports._applyOpacity = exports._convertOpacityToHex = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* * Converts opacity percentage (0-100) to a 2-digit hex string.
|
|
6
6
|
*
|
|
7
7
|
* @param opacity - The opacity value as a percentage (0-100).
|
|
8
8
|
* @returns A 2-digit hex string representing the alpha value.
|
|
9
9
|
*/
|
|
10
|
-
const
|
|
10
|
+
const _convertOpacityToHex = (opacity) => {
|
|
11
11
|
// Ensure opacity is between 0 and 100
|
|
12
12
|
const validOpacity = Math.min(100, Math.max(0, opacity));
|
|
13
13
|
// Convert to a value between 0 and 255, then to a hex string
|
|
@@ -15,29 +15,29 @@ const convertOpacityToHex = (opacity) => {
|
|
|
15
15
|
// Ensure it's 2 digits (e.g., 0x0A instead of 0xA)
|
|
16
16
|
return alpha.toString(16).padStart(2, '0').toUpperCase();
|
|
17
17
|
};
|
|
18
|
-
exports.
|
|
18
|
+
exports._convertOpacityToHex = _convertOpacityToHex;
|
|
19
19
|
/**
|
|
20
20
|
* * Applies an opacity value to a color string.
|
|
21
21
|
* @param color The color string to apply opacity to.
|
|
22
22
|
* @param opacity The opacity value as a percentage (0-100).
|
|
23
23
|
* @returns The color string with the opacity value applied.
|
|
24
24
|
*/
|
|
25
|
-
const
|
|
25
|
+
const _applyOpacity = (color, opacity) => {
|
|
26
26
|
return color.concat(opacity);
|
|
27
27
|
};
|
|
28
|
-
exports.
|
|
28
|
+
exports._applyOpacity = _applyOpacity;
|
|
29
29
|
/**
|
|
30
30
|
* * Helper function to generate a random HSL color.
|
|
31
31
|
*
|
|
32
32
|
* @returns A random HSL color string.
|
|
33
33
|
*/
|
|
34
|
-
const
|
|
34
|
+
const _generateRandomHSL = () => {
|
|
35
35
|
const hue = Math.floor(Math.random() * 360);
|
|
36
36
|
const saturation = 75 + Math.floor(Math.random() * 25);
|
|
37
37
|
const lightness = 50 + Math.floor(Math.random() * 15);
|
|
38
38
|
return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
|
|
39
39
|
};
|
|
40
|
-
exports.
|
|
40
|
+
exports._generateRandomHSL = _generateRandomHSL;
|
|
41
41
|
/**
|
|
42
42
|
* * Helper function to check if the new color is visually too similar to the previous one.
|
|
43
43
|
* * It compares the hue, saturation, and lightness difference between the new color and the last one generated.
|
|
@@ -46,7 +46,7 @@ exports.generateColorHSL = generateColorHSL;
|
|
|
46
46
|
* @param newColor - The new color to compare.
|
|
47
47
|
* @returns `Boolean` : `true` if the new color is similar to the previous one.
|
|
48
48
|
*/
|
|
49
|
-
const
|
|
49
|
+
const _isSimilarToLast = (recentColors, newColor) => {
|
|
50
50
|
if (recentColors.length === 0)
|
|
51
51
|
return false;
|
|
52
52
|
const newHSL = newColor.match(/hsl\((\d+), (\d+)%, (\d+)%\)/);
|
|
@@ -66,4 +66,15 @@ const isSimilarToLast = (recentColors, newColor) => {
|
|
|
66
66
|
saturationDifference < 24 &&
|
|
67
67
|
lightnessDifference < 16);
|
|
68
68
|
};
|
|
69
|
-
exports.
|
|
69
|
+
exports._isSimilarToLast = _isSimilarToLast;
|
|
70
|
+
/**
|
|
71
|
+
* * Extracts numbers from a color string like `rgb(66, 103, 69)` or `hsl(120, 42.86%, 41.18%)`.
|
|
72
|
+
* * Converts percentage values to decimal (e.g., `42.86%` → `42.86`).
|
|
73
|
+
*
|
|
74
|
+
* @param colorString The color string in RGB or HSL format.
|
|
75
|
+
* @returns An array of extracted numbers.
|
|
76
|
+
*/
|
|
77
|
+
const extractNumbersFromColor = (colorString) => {
|
|
78
|
+
return (colorString.match(/[\d.]+%?/g) || []).map((value) => parseFloat(value));
|
|
79
|
+
};
|
|
80
|
+
exports.extractNumbersFromColor = extractNumbersFromColor;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initials.d.ts","sourceRoot":"","sources":["../../src/colors/initials.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"initials.d.ts","sourceRoot":"","sources":["../../src/colors/initials.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEzE;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,UAAU,EACjB,OAAO,CAAC,EAAE,YAAY,GACpB,MAAM,CAAC;AAEV;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,eAAe,EACtB,OAAO,CAAC,EAAE,YAAY,GACpB,MAAM,EAAE,CAAC"}
|
package/dist/colors/initials.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getColorForInitial = getColorForInitial;
|
|
4
|
-
const helpers_1 = require("./helpers");
|
|
5
4
|
const constants_1 = require("./constants");
|
|
5
|
+
const helpers_1 = require("./helpers");
|
|
6
6
|
/**
|
|
7
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.
|
|
8
8
|
*
|
|
@@ -16,38 +16,38 @@ const constants_1 = require("./constants");
|
|
|
16
16
|
*/
|
|
17
17
|
function getColorForInitial(input = '', opacity = 100) {
|
|
18
18
|
let initial;
|
|
19
|
-
const hexOpacity = (0, helpers_1.
|
|
19
|
+
const hexOpacity = (0, helpers_1._convertOpacityToHex)(opacity);
|
|
20
20
|
const numbers = '0123456789';
|
|
21
21
|
// Handle empty string case
|
|
22
22
|
if (!input)
|
|
23
|
-
return (0, helpers_1.
|
|
23
|
+
return (0, helpers_1._applyOpacity)('#010514', hexOpacity);
|
|
24
24
|
// Handle string input
|
|
25
25
|
if (typeof input === 'string') {
|
|
26
26
|
initial = input[0];
|
|
27
27
|
// Handle number as string
|
|
28
28
|
if (numbers.includes(initial)) {
|
|
29
|
-
return (0, helpers_1.
|
|
29
|
+
return (0, helpers_1._applyOpacity)(constants_1.numberColorPalette[parseInt(initial, 10)], hexOpacity);
|
|
30
30
|
}
|
|
31
31
|
const upperInitial = initial.toUpperCase();
|
|
32
32
|
const index = upperInitial.charCodeAt(0) - 'A'.charCodeAt(0);
|
|
33
33
|
// Validate alphabet
|
|
34
34
|
if (index >= 0 && index < constants_1.alphabetColorPalette.length) {
|
|
35
|
-
return (0, helpers_1.
|
|
35
|
+
return (0, helpers_1._applyOpacity)(constants_1.alphabetColorPalette[index], hexOpacity);
|
|
36
36
|
}
|
|
37
|
-
return (0, helpers_1.
|
|
37
|
+
return (0, helpers_1._applyOpacity)('#010514', hexOpacity);
|
|
38
38
|
// Handle number input
|
|
39
39
|
}
|
|
40
40
|
else if (typeof input === 'number' && !isNaN(input)) {
|
|
41
41
|
initial = input.toString()[0];
|
|
42
42
|
if (numbers.includes(initial)) {
|
|
43
|
-
return (0, helpers_1.
|
|
43
|
+
return (0, helpers_1._applyOpacity)(constants_1.numberColorPalette[parseInt(initial, 10)], hexOpacity);
|
|
44
44
|
}
|
|
45
|
-
return (0, helpers_1.
|
|
45
|
+
return (0, helpers_1._applyOpacity)('#010514', hexOpacity);
|
|
46
46
|
// Handle array of strings/numbers
|
|
47
47
|
}
|
|
48
48
|
else if (Array.isArray(input)) {
|
|
49
49
|
if (input.length < 1)
|
|
50
|
-
return [...constants_1.alphabetColorPalette, ...constants_1.numberColorPalette].map((color) => (0, helpers_1.
|
|
50
|
+
return [...constants_1.alphabetColorPalette, ...constants_1.numberColorPalette].map((color) => (0, helpers_1._applyOpacity)(color, hexOpacity));
|
|
51
51
|
return input
|
|
52
52
|
.map((el) => {
|
|
53
53
|
if (Array.isArray(el)) {
|
|
@@ -57,5 +57,5 @@ function getColorForInitial(input = '', opacity = 100) {
|
|
|
57
57
|
})
|
|
58
58
|
.flat();
|
|
59
59
|
}
|
|
60
|
-
return (0, helpers_1.
|
|
60
|
+
return (0, helpers_1._applyOpacity)('#010514', hexOpacity);
|
|
61
61
|
}
|
package/dist/colors/random.d.ts
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
+
import type { Hex, HSL, RGB } from './types';
|
|
1
2
|
/**
|
|
2
3
|
* * Utility to generate a unique random HSL color.
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
4
|
+
*
|
|
5
|
+
* @param maxColors - The maximum number of recent colors to store in memory. Default is `16`.
|
|
6
|
+
* @returns Generated unique random color in `HSL` format.
|
|
5
7
|
*/
|
|
6
|
-
export declare const
|
|
8
|
+
export declare const generateRandomHSLColor: (maxColors?: number) => HSL;
|
|
9
|
+
/**
|
|
10
|
+
* * Utility to generate a unique random color in Hex and RGB format.
|
|
11
|
+
*
|
|
12
|
+
* @param maxColors - The maximum number of recent colors to store in memory. Default is `16`.
|
|
13
|
+
* @returns An object of generated unique random color in both `Hex` and `RGB` formats.
|
|
14
|
+
*/
|
|
15
|
+
export declare const generateRandomColorInHexRGB: (maxColors?: number) => {
|
|
16
|
+
hex: Hex;
|
|
17
|
+
rgb: RGB;
|
|
18
|
+
};
|
|
7
19
|
//# sourceMappingURL=random.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../../src/colors/random.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../../src/colors/random.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAQ7C;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,eAAe,MAAM,KAAQ,GAqB/D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,2BAA2B,eAC5B,MAAM,KACf;IACF,GAAG,EAAE,GAAG,CAAC;IACT,GAAG,EAAE,GAAG,CAAC;CAGT,CAAC"}
|
package/dist/colors/random.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.generateRandomColorInHexRGB = exports.generateRandomHSLColor = void 0;
|
|
4
|
+
const convert_1 = require("./convert");
|
|
4
5
|
const helpers_1 = require("./helpers");
|
|
5
6
|
/** Track previously generated colors. */
|
|
6
7
|
const generatedColors = new Set();
|
|
@@ -8,16 +9,17 @@ const generatedColors = new Set();
|
|
|
8
9
|
const recentColors = [];
|
|
9
10
|
/**
|
|
10
11
|
* * Utility to generate a unique random HSL color.
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
12
|
+
*
|
|
13
|
+
* @param maxColors - The maximum number of recent colors to store in memory. Default is `16`.
|
|
14
|
+
* @returns Generated unique random color in `HSL` format.
|
|
13
15
|
*/
|
|
14
|
-
const
|
|
16
|
+
const generateRandomHSLColor = (maxColors = 16) => {
|
|
15
17
|
let color;
|
|
16
18
|
// Keep generating until a unique color is found that is also different from the last one
|
|
17
19
|
do {
|
|
18
|
-
color = (0, helpers_1.
|
|
20
|
+
color = (0, helpers_1._generateRandomHSL)();
|
|
19
21
|
} while (generatedColors.has(color) ||
|
|
20
|
-
(0, helpers_1.
|
|
22
|
+
(0, helpers_1._isSimilarToLast)(recentColors, color));
|
|
21
23
|
// Add the newly generated color to the set and recent colors
|
|
22
24
|
generatedColors.add(color);
|
|
23
25
|
recentColors.push(color);
|
|
@@ -27,4 +29,14 @@ const generateRandomColor = (maxColors = 16) => {
|
|
|
27
29
|
}
|
|
28
30
|
return color;
|
|
29
31
|
};
|
|
30
|
-
exports.
|
|
32
|
+
exports.generateRandomHSLColor = generateRandomHSLColor;
|
|
33
|
+
/**
|
|
34
|
+
* * Utility to generate a unique random color in Hex and RGB format.
|
|
35
|
+
*
|
|
36
|
+
* @param maxColors - The maximum number of recent colors to store in memory. Default is `16`.
|
|
37
|
+
* @returns An object of generated unique random color in both `Hex` and `RGB` formats.
|
|
38
|
+
*/
|
|
39
|
+
const generateRandomColorInHexRGB = (maxColors = 16) => {
|
|
40
|
+
return (0, convert_1.convertColorCode)((0, exports.generateRandomHSLColor)(maxColors));
|
|
41
|
+
};
|
|
42
|
+
exports.generateRandomColorInHexRGB = generateRandomColorInHexRGB;
|
package/dist/colors/types.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export interface ColorInputArray extends Array<ColorInput | ColorInputArray> {
|
|
|
7
7
|
export type OpacityValue = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100;
|
|
8
8
|
/**
|
|
9
9
|
* * Represents a hexadecimal color code.
|
|
10
|
-
* Format: `#3C6945`
|
|
10
|
+
* * Format: `#3C6945`
|
|
11
11
|
*/
|
|
12
12
|
export type Hex = `#${string}`;
|
|
13
13
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/colors/types.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEzC,+FAA+F;AAC/F,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,UAAU,GAAG,eAAe,CAAC;CAAG;AAE/E,wBAAwB;AACxB,MAAM,MAAM,YAAY,GACrB,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,GAAG,CAAC;AAEP;;;GAGG;AACH,MAAM,MAAM,GAAG,GAAG,IAAI,MAAM,EAAE,CAAC;AAE/B;;;;;;;GAOG;AACH,MAAM,MAAM,GAAG,GAAG,OAAO,MAAM,KAAK,MAAM,KAAK,MAAM,GAAG,CAAC;AAEzD;;;;;;;GAOG;AACH,MAAM,MAAM,GAAG,GAAG,OAAO,MAAM,KAAK,MAAM,MAAM,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/colors/types.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEzC,+FAA+F;AAC/F,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,UAAU,GAAG,eAAe,CAAC;CAAG;AAE/E,wBAAwB;AACxB,MAAM,MAAM,YAAY,GACrB,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,GAAG,CAAC;AAEP;;;GAGG;AACH,MAAM,MAAM,GAAG,GAAG,IAAI,MAAM,EAAE,CAAC;AAE/B;;;;;;;GAOG;AACH,MAAM,MAAM,GAAG,GAAG,OAAO,MAAM,KAAK,MAAM,KAAK,MAAM,GAAG,CAAC;AAEzD;;;;;;;GAOG;AACH,MAAM,MAAM,GAAG,GAAG,OAAO,MAAM,KAAK,MAAM,MAAM,MAAM,IAAI,CAAC;AAM3D,oEAAoE;AACpE,MAAM,MAAM,KAAK,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AAEpC,mGAAmG;AACnG,MAAM,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEpD;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,KAAK,CAC/C,SAAQ,MAAM,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;IACvC,uEAAuE;IACvE,GAAG,EAAE,CAAC,SAAS,GAAG,GAAG,KAAK,GAAG,GAAG,CAAC;IACjC,uEAAuE;IACvE,GAAG,EAAE,CAAC,SAAS,GAAG,GAAG,KAAK,GAAG,GAAG,CAAC;IACjC,uEAAuE;IACvE,GAAG,EAAE,CAAC,SAAS,GAAG,GAAG,KAAK,GAAG,GAAG,CAAC;CACjC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,12 +2,15 @@ export { capitalizeString, generateRandomID, trimString, truncateString, } from
|
|
|
2
2
|
export { generateAnagrams } from './string/anagram';
|
|
3
3
|
export { convertToDecimal, getRandomNumber } from './number/basics';
|
|
4
4
|
export { numberToWords } from './number/convert';
|
|
5
|
-
export {
|
|
5
|
+
export { findPrimeNumbers, isPrime } from './number/prime';
|
|
6
6
|
export { getColorForInitial } from './colors/initials';
|
|
7
|
-
export {
|
|
8
|
-
export { convertHexToHsl, convertHexToRgb, convertHslToHex, convertHslToRgb, convertRgbToHex, convertRgbToHsl,
|
|
7
|
+
export { generateRandomColorInHexRGB, generateRandomHSLColor, } from './colors/random';
|
|
8
|
+
export { convertColorCode, convertHexToHsl, convertHexToRgb, convertHslToHex, convertHslToRgb, convertRgbToHex, convertRgbToHsl, } from './colors/convert';
|
|
9
|
+
export { extractNumbersFromColor } from './colors/helpers';
|
|
9
10
|
export { createOptionsArray, filterArrayOfObjects, flattenArray, sortAnArray, } from './array/basics';
|
|
10
|
-
export { cloneObject, countObjectFields,
|
|
11
|
+
export { cloneObject, countObjectFields, generateQueryParams, isEmptyObject, isObject, } from './object/basics';
|
|
12
|
+
export { extractNewFields, extractUpdatedAndNewFields, extractUpdatedFields, flattenObject, mergeAndFlattenObjects, mergeObjects, } from './object/objectify';
|
|
11
13
|
export { sanitizeData } from './object/sanitize';
|
|
12
14
|
export { convertObjectValues } from './object/convert';
|
|
15
|
+
export { isDeepEqual } from './utils';
|
|
13
16
|
//# 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,EACN,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,cAAc,GACd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEpE,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,cAAc,GACd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEpE,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAE3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EACN,2BAA2B,EAC3B,sBAAsB,GACtB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,GACf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAE3D,OAAO,EACN,kBAAkB,EAClB,oBAAoB,EACpB,YAAY,EACZ,WAAW,GACX,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACN,WAAW,EACX,iBAAiB,EACjB,mBAAmB,EACnB,aAAa,EACb,QAAQ,GACR,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,gBAAgB,EAChB,0BAA0B,EAC1B,oBAAoB,EACpB,aAAa,EACb,sBAAsB,EACtB,YAAY,GACZ,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertObjectValues = exports.sanitizeData = exports.mergeObjects = exports.mergeAndFlattenObjects = exports.
|
|
3
|
+
exports.isDeepEqual = exports.convertObjectValues = exports.sanitizeData = exports.mergeObjects = exports.mergeAndFlattenObjects = exports.flattenObject = exports.extractUpdatedFields = exports.extractUpdatedAndNewFields = exports.extractNewFields = exports.isObject = exports.isEmptyObject = exports.generateQueryParams = exports.countObjectFields = exports.cloneObject = exports.sortAnArray = exports.flattenArray = exports.filterArrayOfObjects = exports.createOptionsArray = exports.extractNumbersFromColor = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.convertColorCode = exports.generateRandomHSLColor = exports.generateRandomColorInHexRGB = exports.getColorForInitial = exports.isPrime = exports.findPrimeNumbers = exports.numberToWords = exports.getRandomNumber = exports.convertToDecimal = exports.generateAnagrams = exports.truncateString = exports.trimString = exports.generateRandomID = exports.capitalizeString = void 0;
|
|
4
4
|
var basics_1 = require("./string/basics");
|
|
5
5
|
Object.defineProperty(exports, "capitalizeString", { enumerable: true, get: function () { return basics_1.capitalizeString; } });
|
|
6
6
|
Object.defineProperty(exports, "generateRandomID", { enumerable: true, get: function () { return basics_1.generateRandomID; } });
|
|
@@ -14,21 +14,23 @@ Object.defineProperty(exports, "getRandomNumber", { enumerable: true, get: funct
|
|
|
14
14
|
var convert_1 = require("./number/convert");
|
|
15
15
|
Object.defineProperty(exports, "numberToWords", { enumerable: true, get: function () { return convert_1.numberToWords; } });
|
|
16
16
|
var prime_1 = require("./number/prime");
|
|
17
|
-
Object.defineProperty(exports, "isPrime", { enumerable: true, get: function () { return prime_1.isPrime; } });
|
|
18
17
|
Object.defineProperty(exports, "findPrimeNumbers", { enumerable: true, get: function () { return prime_1.findPrimeNumbers; } });
|
|
18
|
+
Object.defineProperty(exports, "isPrime", { enumerable: true, get: function () { return prime_1.isPrime; } });
|
|
19
19
|
var initials_1 = require("./colors/initials");
|
|
20
20
|
Object.defineProperty(exports, "getColorForInitial", { enumerable: true, get: function () { return initials_1.getColorForInitial; } });
|
|
21
21
|
var random_1 = require("./colors/random");
|
|
22
|
-
Object.defineProperty(exports, "
|
|
22
|
+
Object.defineProperty(exports, "generateRandomColorInHexRGB", { enumerable: true, get: function () { return random_1.generateRandomColorInHexRGB; } });
|
|
23
|
+
Object.defineProperty(exports, "generateRandomHSLColor", { enumerable: true, get: function () { return random_1.generateRandomHSLColor; } });
|
|
23
24
|
var convert_2 = require("./colors/convert");
|
|
25
|
+
Object.defineProperty(exports, "convertColorCode", { enumerable: true, get: function () { return convert_2.convertColorCode; } });
|
|
24
26
|
Object.defineProperty(exports, "convertHexToHsl", { enumerable: true, get: function () { return convert_2.convertHexToHsl; } });
|
|
25
27
|
Object.defineProperty(exports, "convertHexToRgb", { enumerable: true, get: function () { return convert_2.convertHexToRgb; } });
|
|
26
28
|
Object.defineProperty(exports, "convertHslToHex", { enumerable: true, get: function () { return convert_2.convertHslToHex; } });
|
|
27
29
|
Object.defineProperty(exports, "convertHslToRgb", { enumerable: true, get: function () { return convert_2.convertHslToRgb; } });
|
|
28
30
|
Object.defineProperty(exports, "convertRgbToHex", { enumerable: true, get: function () { return convert_2.convertRgbToHex; } });
|
|
29
31
|
Object.defineProperty(exports, "convertRgbToHsl", { enumerable: true, get: function () { return convert_2.convertRgbToHsl; } });
|
|
30
|
-
|
|
31
|
-
Object.defineProperty(exports, "extractNumbersFromColor", { enumerable: true, get: function () { return
|
|
32
|
+
var helpers_1 = require("./colors/helpers");
|
|
33
|
+
Object.defineProperty(exports, "extractNumbersFromColor", { enumerable: true, get: function () { return helpers_1.extractNumbersFromColor; } });
|
|
32
34
|
var basics_3 = require("./array/basics");
|
|
33
35
|
Object.defineProperty(exports, "createOptionsArray", { enumerable: true, get: function () { return basics_3.createOptionsArray; } });
|
|
34
36
|
Object.defineProperty(exports, "filterArrayOfObjects", { enumerable: true, get: function () { return basics_3.filterArrayOfObjects; } });
|
|
@@ -37,13 +39,19 @@ Object.defineProperty(exports, "sortAnArray", { enumerable: true, get: function
|
|
|
37
39
|
var basics_4 = require("./object/basics");
|
|
38
40
|
Object.defineProperty(exports, "cloneObject", { enumerable: true, get: function () { return basics_4.cloneObject; } });
|
|
39
41
|
Object.defineProperty(exports, "countObjectFields", { enumerable: true, get: function () { return basics_4.countObjectFields; } });
|
|
40
|
-
Object.defineProperty(exports, "flattenObject", { enumerable: true, get: function () { return basics_4.flattenObject; } });
|
|
41
42
|
Object.defineProperty(exports, "generateQueryParams", { enumerable: true, get: function () { return basics_4.generateQueryParams; } });
|
|
42
|
-
Object.defineProperty(exports, "isDeepEqual", { enumerable: true, get: function () { return basics_4.isDeepEqual; } });
|
|
43
43
|
Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return basics_4.isEmptyObject; } });
|
|
44
|
-
Object.defineProperty(exports, "
|
|
45
|
-
|
|
44
|
+
Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return basics_4.isObject; } });
|
|
45
|
+
var objectify_1 = require("./object/objectify");
|
|
46
|
+
Object.defineProperty(exports, "extractNewFields", { enumerable: true, get: function () { return objectify_1.extractNewFields; } });
|
|
47
|
+
Object.defineProperty(exports, "extractUpdatedAndNewFields", { enumerable: true, get: function () { return objectify_1.extractUpdatedAndNewFields; } });
|
|
48
|
+
Object.defineProperty(exports, "extractUpdatedFields", { enumerable: true, get: function () { return objectify_1.extractUpdatedFields; } });
|
|
49
|
+
Object.defineProperty(exports, "flattenObject", { enumerable: true, get: function () { return objectify_1.flattenObject; } });
|
|
50
|
+
Object.defineProperty(exports, "mergeAndFlattenObjects", { enumerable: true, get: function () { return objectify_1.mergeAndFlattenObjects; } });
|
|
51
|
+
Object.defineProperty(exports, "mergeObjects", { enumerable: true, get: function () { return objectify_1.mergeObjects; } });
|
|
46
52
|
var sanitize_1 = require("./object/sanitize");
|
|
47
53
|
Object.defineProperty(exports, "sanitizeData", { enumerable: true, get: function () { return sanitize_1.sanitizeData; } });
|
|
48
54
|
var convert_3 = require("./object/convert");
|
|
49
55
|
Object.defineProperty(exports, "convertObjectValues", { enumerable: true, get: function () { return convert_3.convertObjectValues; } });
|
|
56
|
+
var utils_1 = require("./utils");
|
|
57
|
+
Object.defineProperty(exports, "isDeepEqual", { enumerable: true, get: function () { return utils_1.isDeepEqual; } });
|
package/dist/object/basics.d.ts
CHANGED
|
@@ -34,34 +34,10 @@ export declare const isEmptyObject: <T extends GenericObject>(obj: T) => boolean
|
|
|
34
34
|
*/
|
|
35
35
|
export declare const countObjectFields: <T extends GenericObject>(obj: T) => number;
|
|
36
36
|
/**
|
|
37
|
-
* *
|
|
37
|
+
* * Check whether data is object and not array.
|
|
38
38
|
*
|
|
39
|
-
* @param
|
|
40
|
-
* @returns
|
|
39
|
+
* @param data Data to check if its an object and not array.
|
|
40
|
+
* @returns Boolean: `true` if it's an object, `false` if not.
|
|
41
41
|
*/
|
|
42
|
-
export declare const
|
|
43
|
-
/**
|
|
44
|
-
* * Deeply merge objects and flatten nested objects.
|
|
45
|
-
* * Useful for flattening a single object or merging multiple objects with duplicate key(s).
|
|
46
|
-
* * If keys are duplicated, the last object's value will be used.
|
|
47
|
-
*
|
|
48
|
-
* @param objects Objects to merge.
|
|
49
|
-
* @returns Merged object with flattened structure.
|
|
50
|
-
*/
|
|
51
|
-
export declare const mergeAndFlattenObjects: <T extends GenericObject>(...objects: T[]) => GenericObject;
|
|
52
|
-
/**
|
|
53
|
-
* * Flattens a nested object into a dot notation format.
|
|
54
|
-
*
|
|
55
|
-
* @param object - The `object` to flatten.
|
|
56
|
-
* @returns A `flattened object` with dot notation keys.
|
|
57
|
-
*/
|
|
58
|
-
export declare const flattenObject: <T extends GenericObject>(object: T) => GenericObject;
|
|
59
|
-
/**
|
|
60
|
-
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
61
|
-
*
|
|
62
|
-
* @param a First value to compare.
|
|
63
|
-
* @param b Second value to compare.
|
|
64
|
-
* @returns Whether the values are deeply equal.
|
|
65
|
-
*/
|
|
66
|
-
export declare const isDeepEqual: <T>(a: T, b: T) => boolean;
|
|
42
|
+
export declare const isObject: (data: unknown) => boolean;
|
|
67
43
|
//# sourceMappingURL=basics.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"basics.d.ts","sourceRoot":"","sources":["../../src/object/basics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE1D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,WAAW,WAChD,CAAC,KACP,MA4BF,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,
|
|
1
|
+
{"version":3,"file":"basics.d.ts","sourceRoot":"","sources":["../../src/object/basics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE1D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,WAAW,WAChD,CAAC,KACP,MA4BF,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,QAAQ,SAAU,OAAO,KAAG,OAExC,CAAC"}
|
package/dist/object/basics.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.isObject = exports.countObjectFields = exports.isEmptyObject = exports.cloneObject = exports.generateQueryParams = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* * Utility to generate query parameters from an object.
|
|
6
6
|
*
|
|
@@ -46,7 +46,7 @@ exports.cloneObject = cloneObject;
|
|
|
46
46
|
* @returns Whether the object is empty.
|
|
47
47
|
*/
|
|
48
48
|
const isEmptyObject = (obj) => {
|
|
49
|
-
return
|
|
49
|
+
return (0, exports.countObjectFields)(obj) === 0;
|
|
50
50
|
};
|
|
51
51
|
exports.isEmptyObject = isEmptyObject;
|
|
52
52
|
/**
|
|
@@ -60,137 +60,12 @@ const countObjectFields = (obj) => {
|
|
|
60
60
|
};
|
|
61
61
|
exports.countObjectFields = countObjectFields;
|
|
62
62
|
/**
|
|
63
|
-
* *
|
|
63
|
+
* * Check whether data is object and not array.
|
|
64
64
|
*
|
|
65
|
-
* @param
|
|
66
|
-
* @returns
|
|
65
|
+
* @param data Data to check if its an object and not array.
|
|
66
|
+
* @returns Boolean: `true` if it's an object, `false` if not.
|
|
67
67
|
*/
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
objects.forEach((obj) => {
|
|
71
|
-
for (const key in obj) {
|
|
72
|
-
const existingValue = map.get(key);
|
|
73
|
-
if (obj[key] instanceof Object && !Array.isArray(obj[key])) {
|
|
74
|
-
// If the key already exists in the map and both are objects, merge them
|
|
75
|
-
if (existingValue &&
|
|
76
|
-
existingValue instanceof Object &&
|
|
77
|
-
!Array.isArray(existingValue)) {
|
|
78
|
-
map.set(key, (0, exports.mergeObjects)(existingValue, obj[key]));
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
// Otherwise, just set the value
|
|
82
|
-
map.set(key, obj[key]);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
// If it's not an object, just set the value
|
|
87
|
-
map.set(key, obj[key]);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
const result = {};
|
|
92
|
-
map.forEach((value, key) => {
|
|
93
|
-
result[key] = value;
|
|
94
|
-
});
|
|
95
|
-
return result;
|
|
68
|
+
const isObject = (data) => {
|
|
69
|
+
return typeof data === 'object' && !Array.isArray(data);
|
|
96
70
|
};
|
|
97
|
-
exports.
|
|
98
|
-
/**
|
|
99
|
-
* * Deeply merge objects and flatten nested objects.
|
|
100
|
-
* * Useful for flattening a single object or merging multiple objects with duplicate key(s).
|
|
101
|
-
* * If keys are duplicated, the last object's value will be used.
|
|
102
|
-
*
|
|
103
|
-
* @param objects Objects to merge.
|
|
104
|
-
* @returns Merged object with flattened structure.
|
|
105
|
-
*/
|
|
106
|
-
const mergeAndFlattenObjects = (...objects) => {
|
|
107
|
-
const map = new Map();
|
|
108
|
-
const _flattenObject = (obj, parentKey = '') => {
|
|
109
|
-
for (const key in obj) {
|
|
110
|
-
const newKey = parentKey ? `${String(parentKey)}.${key}` : key;
|
|
111
|
-
if (obj[key] instanceof Object && !Array.isArray(obj[key])) {
|
|
112
|
-
// Recursively flatten nested objects
|
|
113
|
-
_flattenObject(obj[key], newKey);
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
// Set the flattened key
|
|
117
|
-
map.set(newKey, obj[key]);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
objects.forEach((obj) => _flattenObject(obj));
|
|
122
|
-
const result = {};
|
|
123
|
-
map.forEach((value, key) => {
|
|
124
|
-
result[key] = value;
|
|
125
|
-
});
|
|
126
|
-
return result;
|
|
127
|
-
};
|
|
128
|
-
exports.mergeAndFlattenObjects = mergeAndFlattenObjects;
|
|
129
|
-
/**
|
|
130
|
-
* * Flattens a nested object into a dot notation format.
|
|
131
|
-
*
|
|
132
|
-
* @param object - The `object` to flatten.
|
|
133
|
-
* @returns A `flattened object` with dot notation keys.
|
|
134
|
-
*/
|
|
135
|
-
const flattenObject = (object) => {
|
|
136
|
-
/**
|
|
137
|
-
* * Recursively flattens an object, transforming nested structures into dot-notation keys.
|
|
138
|
-
*
|
|
139
|
-
* @param source - The `object` to be flattened.
|
|
140
|
-
* @param prefix - The prefix to prepend to each key. Used for nested objects.
|
|
141
|
-
* @returns A flattened version of the input object.
|
|
142
|
-
*/
|
|
143
|
-
const _flattenObject = (source, prefix = '') => {
|
|
144
|
-
const flattened = {};
|
|
145
|
-
for (const [key, value] of Object.entries(source)) {
|
|
146
|
-
// Construct the dot-notation key
|
|
147
|
-
const newKey = prefix ? `${String(prefix)}.${key}` : key;
|
|
148
|
-
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
149
|
-
// Recursively process nested objects
|
|
150
|
-
Object.assign(flattened, _flattenObject(value, newKey));
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
// Directly assign non-object values
|
|
154
|
-
flattened[newKey] = value;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return flattened;
|
|
158
|
-
};
|
|
159
|
-
// Call the recursive function with an empty prefix initially
|
|
160
|
-
return _flattenObject(object);
|
|
161
|
-
};
|
|
162
|
-
exports.flattenObject = flattenObject;
|
|
163
|
-
/**
|
|
164
|
-
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
165
|
-
*
|
|
166
|
-
* @param a First value to compare.
|
|
167
|
-
* @param b Second value to compare.
|
|
168
|
-
* @returns Whether the values are deeply equal.
|
|
169
|
-
*/
|
|
170
|
-
const isDeepEqual = (a, b) => {
|
|
171
|
-
// If both values are strictly equal (handles primitive types and same references)
|
|
172
|
-
if (a === b)
|
|
173
|
-
return true;
|
|
174
|
-
// If the types of the two values are different
|
|
175
|
-
if (typeof a !== typeof b)
|
|
176
|
-
return false;
|
|
177
|
-
// If either is null or undefined, they must both be null or undefined
|
|
178
|
-
if (a === null || b === null)
|
|
179
|
-
return a === b;
|
|
180
|
-
// Check for array equality
|
|
181
|
-
if (Array.isArray(a) && Array.isArray(b)) {
|
|
182
|
-
if (a.length !== b.length)
|
|
183
|
-
return false;
|
|
184
|
-
return a.every((element, index) => (0, exports.isDeepEqual)(element, b[index]));
|
|
185
|
-
}
|
|
186
|
-
// Check for object equality
|
|
187
|
-
if (typeof a === 'object' && typeof b === 'object') {
|
|
188
|
-
const aKeys = Object.keys(a);
|
|
189
|
-
const bKeys = Object.keys(b);
|
|
190
|
-
if (aKeys.length !== bKeys.length)
|
|
191
|
-
return false;
|
|
192
|
-
return aKeys.every((key) => (0, exports.isDeepEqual)(a[key], b[key]));
|
|
193
|
-
}
|
|
194
|
-
return false;
|
|
195
|
-
};
|
|
196
|
-
exports.isDeepEqual = isDeepEqual;
|
|
71
|
+
exports.isObject = isObject;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { GenericObject, LooseObject } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* * Deeply merge two or more objects using `Map`.
|
|
4
|
+
*
|
|
5
|
+
* @param objects Objects to merge.
|
|
6
|
+
* @returns Merged object.
|
|
7
|
+
*/
|
|
8
|
+
export declare const mergeObjects: <T extends GenericObject>(...objects: T[]) => T;
|
|
9
|
+
/**
|
|
10
|
+
* * Deeply merge objects and flatten nested objects.
|
|
11
|
+
* * Useful for flattening a single object or merging multiple objects with duplicate key(s).
|
|
12
|
+
* * If keys are duplicated, the last object's value will be used.
|
|
13
|
+
*
|
|
14
|
+
* @param objects Objects to merge.
|
|
15
|
+
* @returns Merged object with flattened structure.
|
|
16
|
+
*/
|
|
17
|
+
export declare const mergeAndFlattenObjects: <T extends GenericObject>(...objects: T[]) => GenericObject;
|
|
18
|
+
/**
|
|
19
|
+
* * Flattens a nested object into a dot notation format.
|
|
20
|
+
*
|
|
21
|
+
* @param object - The `object` to flatten.
|
|
22
|
+
* @returns A `flattened object` with dot notation keys.
|
|
23
|
+
*/
|
|
24
|
+
export declare const flattenObject: <T extends GenericObject>(object: T) => GenericObject;
|
|
25
|
+
/**
|
|
26
|
+
* * Extracts only the fields that have changed between the original and updated object.
|
|
27
|
+
*
|
|
28
|
+
* @param baseObject The original object to compare against.
|
|
29
|
+
* @param updatedObject The modified object containing potential updates.
|
|
30
|
+
* @returns A new object containing only the changed fields.
|
|
31
|
+
*/
|
|
32
|
+
export declare const extractUpdatedFields: <T extends LooseObject>(baseObject: T, updatedObject: Partial<T>) => Partial<T>;
|
|
33
|
+
/**
|
|
34
|
+
* * Extracts only new fields that exist in updatedObject but not in baseObject.
|
|
35
|
+
*
|
|
36
|
+
* @param baseObject The original object to compare against.
|
|
37
|
+
* @param updatedObject The modified object containing potential new fields.
|
|
38
|
+
* @returns A new object containing only the new fields.
|
|
39
|
+
*/
|
|
40
|
+
export declare const extractNewFields: <T extends LooseObject>(baseObject: T, updatedObject: Partial<T>) => Partial<T>;
|
|
41
|
+
/**
|
|
42
|
+
* * Extracts changed fields from the updated object while also identifying newly added keys.
|
|
43
|
+
*
|
|
44
|
+
* @param baseObject The original object to compare against.
|
|
45
|
+
* @param updatedObject The modified object containing potential updates.
|
|
46
|
+
* @returns An object containing modified fields and new fields separately.
|
|
47
|
+
*/
|
|
48
|
+
export declare const extractUpdatedAndNewFields: <T extends LooseObject>(baseObject: T, updatedObject: Partial<T> & LooseObject) => Partial<T> & LooseObject;
|
|
49
|
+
//# sourceMappingURL=objectify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objectify.d.ts","sourceRoot":"","sources":["../../src/object/objectify.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE1D;;;;;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,oBAAoB,GAAI,CAAC,SAAS,WAAW,cAC7C,CAAC,iBACE,OAAO,CAAC,CAAC,CAAC,KACvB,OAAO,CAAC,CAAC,CAwBX,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,WAAW,cACzC,CAAC,iBACE,OAAO,CAAC,CAAC,CAAC,KACvB,OAAO,CAAC,CAAC,CAqBX,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,GAAI,CAAC,SAAS,WAAW,cACnD,CAAC,iBACE,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,KACrC,OAAO,CAAC,CAAC,CAAC,GAAG,WAwBf,CAAC"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractUpdatedAndNewFields = exports.extractNewFields = exports.extractUpdatedFields = exports.flattenObject = exports.mergeAndFlattenObjects = exports.mergeObjects = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const basics_1 = require("./basics");
|
|
6
|
+
/**
|
|
7
|
+
* * Deeply merge two or more objects using `Map`.
|
|
8
|
+
*
|
|
9
|
+
* @param objects Objects to merge.
|
|
10
|
+
* @returns Merged object.
|
|
11
|
+
*/
|
|
12
|
+
const mergeObjects = (...objects) => {
|
|
13
|
+
const map = new Map();
|
|
14
|
+
objects.forEach((obj) => {
|
|
15
|
+
for (const key in obj) {
|
|
16
|
+
const existingValue = map.get(key);
|
|
17
|
+
if (obj[key] instanceof Object && !Array.isArray(obj[key])) {
|
|
18
|
+
// If the key already exists in the map and both are objects, merge them
|
|
19
|
+
if (existingValue &&
|
|
20
|
+
existingValue instanceof Object &&
|
|
21
|
+
!Array.isArray(existingValue)) {
|
|
22
|
+
map.set(key, (0, exports.mergeObjects)(existingValue, obj[key]));
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// Otherwise, just set the value
|
|
26
|
+
map.set(key, obj[key]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// If it's not an object, just set the value
|
|
31
|
+
map.set(key, obj[key]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
const result = {};
|
|
36
|
+
map.forEach((value, key) => {
|
|
37
|
+
result[key] = value;
|
|
38
|
+
});
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
exports.mergeObjects = mergeObjects;
|
|
42
|
+
/**
|
|
43
|
+
* * Deeply merge objects and flatten nested objects.
|
|
44
|
+
* * Useful for flattening a single object or merging multiple objects with duplicate key(s).
|
|
45
|
+
* * If keys are duplicated, the last object's value will be used.
|
|
46
|
+
*
|
|
47
|
+
* @param objects Objects to merge.
|
|
48
|
+
* @returns Merged object with flattened structure.
|
|
49
|
+
*/
|
|
50
|
+
const mergeAndFlattenObjects = (...objects) => {
|
|
51
|
+
const map = new Map();
|
|
52
|
+
const _flattenObject = (obj, parentKey = '') => {
|
|
53
|
+
for (const key in obj) {
|
|
54
|
+
const newKey = parentKey ? `${String(parentKey)}.${key}` : key;
|
|
55
|
+
if (obj[key] instanceof Object && !Array.isArray(obj[key])) {
|
|
56
|
+
// Recursively flatten nested objects
|
|
57
|
+
_flattenObject(obj[key], newKey);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Set the flattened key
|
|
61
|
+
map.set(newKey, obj[key]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
objects.forEach((obj) => _flattenObject(obj));
|
|
66
|
+
const result = {};
|
|
67
|
+
map.forEach((value, key) => {
|
|
68
|
+
result[key] = value;
|
|
69
|
+
});
|
|
70
|
+
return result;
|
|
71
|
+
};
|
|
72
|
+
exports.mergeAndFlattenObjects = mergeAndFlattenObjects;
|
|
73
|
+
/**
|
|
74
|
+
* * Flattens a nested object into a dot notation format.
|
|
75
|
+
*
|
|
76
|
+
* @param object - The `object` to flatten.
|
|
77
|
+
* @returns A `flattened object` with dot notation keys.
|
|
78
|
+
*/
|
|
79
|
+
const flattenObject = (object) => {
|
|
80
|
+
/**
|
|
81
|
+
* * Recursively flattens an object, transforming nested structures into dot-notation keys.
|
|
82
|
+
*
|
|
83
|
+
* @param source - The `object` to be flattened.
|
|
84
|
+
* @param prefix - The prefix to prepend to each key. Used for nested objects.
|
|
85
|
+
* @returns A flattened version of the input object.
|
|
86
|
+
*/
|
|
87
|
+
const _flattenObject = (source, prefix = '') => {
|
|
88
|
+
const flattened = {};
|
|
89
|
+
for (const [key, value] of Object.entries(source)) {
|
|
90
|
+
// Construct the dot-notation key
|
|
91
|
+
const newKey = prefix ? `${String(prefix)}.${key}` : key;
|
|
92
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
93
|
+
// Recursively process nested objects
|
|
94
|
+
Object.assign(flattened, _flattenObject(value, newKey));
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// Directly assign non-object values
|
|
98
|
+
flattened[newKey] = value;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return flattened;
|
|
102
|
+
};
|
|
103
|
+
// Call the recursive function with an empty prefix initially
|
|
104
|
+
return _flattenObject(object);
|
|
105
|
+
};
|
|
106
|
+
exports.flattenObject = flattenObject;
|
|
107
|
+
/**
|
|
108
|
+
* * Extracts only the fields that have changed between the original and updated object.
|
|
109
|
+
*
|
|
110
|
+
* @param baseObject The original object to compare against.
|
|
111
|
+
* @param updatedObject The modified object containing potential updates.
|
|
112
|
+
* @returns A new object containing only the changed fields.
|
|
113
|
+
*/
|
|
114
|
+
const extractUpdatedFields = (baseObject, updatedObject) => {
|
|
115
|
+
const updatedFields = {};
|
|
116
|
+
for (const key in updatedObject) {
|
|
117
|
+
if (key in baseObject &&
|
|
118
|
+
!(0, utils_1.isDeepEqual)(updatedObject[key], baseObject[key])) {
|
|
119
|
+
if (updatedObject[key] && (0, basics_1.isObject)(updatedObject[key])) {
|
|
120
|
+
updatedFields[key] = (0, exports.extractUpdatedFields)(baseObject[key], updatedObject[key]);
|
|
121
|
+
if (updatedFields[key] && (0, basics_1.isEmptyObject)(updatedFields[key])) {
|
|
122
|
+
delete updatedFields[key];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
updatedFields[key] = updatedObject[key];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return updatedFields;
|
|
131
|
+
};
|
|
132
|
+
exports.extractUpdatedFields = extractUpdatedFields;
|
|
133
|
+
/**
|
|
134
|
+
* * Extracts only new fields that exist in updatedObject but not in baseObject.
|
|
135
|
+
*
|
|
136
|
+
* @param baseObject The original object to compare against.
|
|
137
|
+
* @param updatedObject The modified object containing potential new fields.
|
|
138
|
+
* @returns A new object containing only the new fields.
|
|
139
|
+
*/
|
|
140
|
+
const extractNewFields = (baseObject, updatedObject) => {
|
|
141
|
+
const newFields = {};
|
|
142
|
+
for (const key in updatedObject) {
|
|
143
|
+
if (!(key in baseObject)) {
|
|
144
|
+
// Directly assign new fields
|
|
145
|
+
newFields[key] = updatedObject[key];
|
|
146
|
+
}
|
|
147
|
+
else if ((0, basics_1.isObject)(updatedObject[key]) && (0, basics_1.isObject)(baseObject[key])) {
|
|
148
|
+
// Recursively extract new fields inside nested objects
|
|
149
|
+
const nestedNewFields = (0, exports.extractNewFields)(baseObject[key], updatedObject[key]);
|
|
150
|
+
if (!(0, basics_1.isEmptyObject)(nestedNewFields)) {
|
|
151
|
+
newFields[key] = nestedNewFields;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return newFields;
|
|
156
|
+
};
|
|
157
|
+
exports.extractNewFields = extractNewFields;
|
|
158
|
+
/**
|
|
159
|
+
* * Extracts changed fields from the updated object while also identifying newly added keys.
|
|
160
|
+
*
|
|
161
|
+
* @param baseObject The original object to compare against.
|
|
162
|
+
* @param updatedObject The modified object containing potential updates.
|
|
163
|
+
* @returns An object containing modified fields and new fields separately.
|
|
164
|
+
*/
|
|
165
|
+
const extractUpdatedAndNewFields = (baseObject, updatedObject) => {
|
|
166
|
+
const updatedFields = {};
|
|
167
|
+
const newFields = {};
|
|
168
|
+
for (const key in updatedObject) {
|
|
169
|
+
if (!(key in baseObject)) {
|
|
170
|
+
newFields[key] = updatedObject[key];
|
|
171
|
+
}
|
|
172
|
+
else if (!(0, utils_1.isDeepEqual)(updatedObject[key], baseObject[key])) {
|
|
173
|
+
if (updatedObject[key] && (0, basics_1.isObject)(updatedObject[key])) {
|
|
174
|
+
updatedFields[key] = (0, exports.extractUpdatedAndNewFields)(baseObject[key], updatedObject[key]);
|
|
175
|
+
if (updatedFields[key] && (0, basics_1.isEmptyObject)(updatedFields[key])) {
|
|
176
|
+
delete updatedFields[key];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
updatedFields[key] = updatedObject[key];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { ...updatedFields, ...newFields };
|
|
185
|
+
};
|
|
186
|
+
exports.extractUpdatedAndNewFields = extractUpdatedAndNewFields;
|
package/dist/object/types.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
/** - Generic object
|
|
1
|
+
/** - Generic object with `unknown` value */
|
|
2
2
|
export type GenericObject = Record<string, unknown>;
|
|
3
|
+
/** - Generic object but with `any` value */
|
|
4
|
+
export type LooseObject = Record<string, any>;
|
|
3
5
|
/** - Query object type `Record<string, string | number | string[] | number[]` */
|
|
4
6
|
export type QueryObject = Record<string, string | number | null | undefined | (string | number | null | undefined)[]>;
|
|
5
7
|
/** - Dot-notation keys for nested objects */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/object/types.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/object/types.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEpD,4CAA4C;AAC5C,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE9C,iFAAiF;AACjF,MAAM,MAAM,WAAW,GAAG,MAAM,CAC/B,MAAM,EACN,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE,CAC3E,CAAC;AAEF,6CAA6C;AAC7C,MAAM,MAAM,cAAc,CAAC,CAAC,IAC3B,CAAC,SAAS,aAAa,GACtB;KACE,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,aAAa,GAClD,GAAG,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,GACtC,GAAG,CAAC,EAAE;CACR,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAClB,KAAK,CAAC;AAET,mCAAmC;AACnC,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,aAAa;IACvD,qBAAqB;IACrB,YAAY,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;IACnC,wDAAwD;IACxD,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,iFAAiF;IACjF,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,iCAAiC;AACjC,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;KAC7B,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,aAAa,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACxE,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAC1B,CAAC,SAAS,aAAa,GACtB;KACE,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,SAAS,MAAM,GACxC,CAAC,CAAC,CAAC,CAAC,SAAS,aAAa,GACzB,GAAG,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,GACrC,GAAG,CAAC,EAAE,GACP,KAAK;CACP,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAClB,KAAK,CAAC;AAET;;;;;GAKG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,EAAE,CAAC,SAAS,QAAQ,GAAG,QAAQ,IACzD,CAAC,SAAS,QAAQ,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;AAElE,iDAAiD;AACjD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;KAC3B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,GACzD,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1D,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GACrC,CAAC,CAAC,CAAC,CAAC;CACN,CAAC;AAEF,iDAAiD;AACjD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;KAC3B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,GACzD,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1D,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAC5B,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAC1B,MAAM;CACR,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
3
|
+
*
|
|
4
|
+
* @param a First value to compare.
|
|
5
|
+
* @param b Second value to compare.
|
|
6
|
+
* @returns Whether the values are deeply equal.
|
|
7
|
+
*/
|
|
8
|
+
export declare const isDeepEqual: <T>(a: T, b: T) => boolean;
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAG,OA6B3C,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isDeepEqual = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
6
|
+
*
|
|
7
|
+
* @param a First value to compare.
|
|
8
|
+
* @param b Second value to compare.
|
|
9
|
+
* @returns Whether the values are deeply equal.
|
|
10
|
+
*/
|
|
11
|
+
const isDeepEqual = (a, b) => {
|
|
12
|
+
// If both values are strictly equal (handles primitive types and same references)
|
|
13
|
+
if (a === b)
|
|
14
|
+
return true;
|
|
15
|
+
// If the types of the two values are different
|
|
16
|
+
if (typeof a !== typeof b)
|
|
17
|
+
return false;
|
|
18
|
+
// If either is null or undefined, they must both be null or undefined
|
|
19
|
+
if (a === null || b === null)
|
|
20
|
+
return a === b;
|
|
21
|
+
// Check for array equality
|
|
22
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
23
|
+
if (a.length !== b.length)
|
|
24
|
+
return false;
|
|
25
|
+
return a.every((element, index) => (0, exports.isDeepEqual)(element, b[index]));
|
|
26
|
+
}
|
|
27
|
+
// Check for object equality
|
|
28
|
+
if (typeof a === 'object' && typeof b === 'object') {
|
|
29
|
+
const aKeys = Object.keys(a);
|
|
30
|
+
const bKeys = Object.keys(b);
|
|
31
|
+
if (aKeys.length !== bKeys.length)
|
|
32
|
+
return false;
|
|
33
|
+
return aKeys.every((key) => (0, exports.isDeepEqual)(a[key], b[key]));
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
};
|
|
37
|
+
exports.isDeepEqual = isDeepEqual;
|
package/package.json
CHANGED