nhb-toolbox 3.4.1 → 3.4.5
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/Color.d.ts +25 -22
- package/dist/colors/Color.d.ts.map +1 -1
- package/dist/colors/Color.js +62 -41
- package/dist/colors/convert.js +2 -2
- package/dist/colors/types.d.ts +7 -1
- package/dist/colors/types.d.ts.map +1 -1
- package/dist/date/Chronos.d.ts +19 -4
- package/dist/date/Chronos.d.ts.map +1 -1
- package/dist/date/Chronos.js +42 -16
- package/package.json +2 -2
package/dist/colors/Color.d.ts
CHANGED
|
@@ -1,30 +1,40 @@
|
|
|
1
1
|
import type { AlphaColors, ColorType, Hex6, Hex8, HSL, HSLA, OpacityValue, RGB, RGBA, SolidColors } from './types';
|
|
2
2
|
/**
|
|
3
|
-
* * Class representing a color and its conversions
|
|
4
|
-
* * It has 1 instance method to apply opacity to `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` color.
|
|
3
|
+
* * Class representing a color and its conversions among `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` and `HSLA` formats.
|
|
4
|
+
* * It has 1 instance method `applyOpacity()` to apply opacity to `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` color.
|
|
5
5
|
* * It has 6 static methods that can be used to check if a color is in `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` format.
|
|
6
6
|
*
|
|
7
7
|
* @property {Hex} hex - The color in `Hex` format.
|
|
8
|
+
* @property {Hex8} hex8 - The color in `Hex8` format.
|
|
8
9
|
* @property {RGB} rgb - The color in `RGB` format.
|
|
10
|
+
* @property {RGBA} rgba - The color in `RGBA` format.
|
|
9
11
|
* @property {HSL} hsl - The color in `HSL` format.
|
|
12
|
+
* @property {HSLA} hsla - The color in `HSLA` format.
|
|
10
13
|
*
|
|
11
14
|
* @example
|
|
12
|
-
* const color = new Color("#ff5733"); // Accepts a color in `Hex`, `RGB` or `
|
|
15
|
+
* const color = new Color("#ff5733"); // Accepts a color in `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` format.
|
|
16
|
+
* console.log(color.hex); // Get Hex equivalent
|
|
17
|
+
* console.log(color.hex8); // Get Hex8 equivalent
|
|
13
18
|
* console.log(color.rgb); // Get RGB equivalent
|
|
19
|
+
* console.log(color.rgba); // Get RGBA equivalent
|
|
14
20
|
* console.log(color.hsl); // Get HSL equivalent
|
|
21
|
+
* console.log(color.hsla); // Get HSLA equivalent
|
|
15
22
|
*
|
|
16
23
|
* @example
|
|
17
24
|
* const randomColor = new Color(); // Generate a random color
|
|
18
|
-
* console.log(randomColor.hex, randomColor.rgb, randomColor.hsl);
|
|
25
|
+
* console.log(randomColor.hex, randomColor.rgb, randomColor.hsl, randomColor.hex8, randomColor.rgba, randomColor.hsla); // Get RGBA and HSLA equivalent
|
|
19
26
|
*/
|
|
20
27
|
export declare class Color {
|
|
21
|
-
hex: Hex6
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
hex: Hex6;
|
|
29
|
+
hex8: Hex8;
|
|
30
|
+
rgb: RGB;
|
|
31
|
+
rgba: RGBA;
|
|
32
|
+
hsl: HSL;
|
|
33
|
+
hsla: HSLA;
|
|
24
34
|
/** - Iterates over the color representations (Hex, RGB, HSL). */
|
|
25
35
|
[Symbol.iterator](): Generator<Hex6 | RGB | HSL | Hex8 | RGBA | HSLA, void, unknown>;
|
|
26
36
|
/**
|
|
27
|
-
* * Creates a new Color instance, optionally
|
|
37
|
+
* * Creates a new `Color` instance, optionally converts an input color to other 5 different color formats.
|
|
28
38
|
*
|
|
29
39
|
* @param toConvert - The color to convert. If not provided, a random color is generated.
|
|
30
40
|
*/
|
|
@@ -49,57 +59,50 @@ export declare class Color {
|
|
|
49
59
|
*/
|
|
50
60
|
applyOpacity(opacity: OpacityValue): SolidColors & AlphaColors;
|
|
51
61
|
/**
|
|
52
|
-
* @static
|
|
53
|
-
* Checks if a color is in `Hex6` format.
|
|
62
|
+
* @static Checks if a color is in `Hex6` format.
|
|
54
63
|
*
|
|
55
64
|
* @param color Color to check.
|
|
56
65
|
* @returns Boolean: `true` if it's a `Hex6` color, `false` if not.
|
|
57
66
|
*/
|
|
58
67
|
static isHex6(color: string): color is Hex6;
|
|
59
68
|
/**
|
|
60
|
-
* @static
|
|
61
|
-
* Checks if a color is in `Hex8` format.
|
|
69
|
+
* @static Checks if a color is in `Hex8` format.
|
|
62
70
|
*
|
|
63
71
|
* @param color Color to check.
|
|
64
72
|
* @returns Boolean: `true` if it's a `Hex8` color, `false` if not.
|
|
65
73
|
*/
|
|
66
74
|
static isHex8(color: string): color is Hex8;
|
|
67
75
|
/**
|
|
68
|
-
* @static
|
|
69
|
-
* Checks if a color is in `RGB` format.
|
|
76
|
+
* @static Checks if a color is in `RGB` format.
|
|
70
77
|
*
|
|
71
78
|
* @param color Color to check.
|
|
72
79
|
* @returns Boolean: `true` if it's an `RGB` color, `false` if not.
|
|
73
80
|
*/
|
|
74
81
|
static isRGB(color: string): color is RGB;
|
|
75
82
|
/**
|
|
76
|
-
* @static
|
|
77
|
-
* Checks if a color is in `RGBA` format.
|
|
83
|
+
* @static Checks if a color is in `RGBA` format.
|
|
78
84
|
*
|
|
79
85
|
* @param color Color to check.
|
|
80
86
|
* @returns Boolean: `true` if it's an `RGBA` color, `false` if not.
|
|
81
87
|
*/
|
|
82
88
|
static isRGBA(color: string): color is RGBA;
|
|
83
89
|
/**
|
|
84
|
-
* @static
|
|
85
|
-
* Checks if a color is in `HSL` format.
|
|
90
|
+
* @static Checks if a color is in `HSL` format.
|
|
86
91
|
*
|
|
87
92
|
* @param color Color to check.
|
|
88
93
|
* @returns Boolean: `true` if it's an `HSL` color, `false` if not.
|
|
89
94
|
*/
|
|
90
95
|
static isHSL(color: string): color is HSL;
|
|
91
96
|
/**
|
|
92
|
-
* @static
|
|
93
|
-
* Checks if a color is in `HSLA` format.
|
|
97
|
+
* @static Checks if a color is in `HSLA` format.
|
|
94
98
|
*
|
|
95
99
|
* @param color Color to check.
|
|
96
100
|
* @returns Boolean: `true` if it's an `HSLA` color, `false` if not.
|
|
97
101
|
*/
|
|
98
102
|
static isHSLA(color: string): color is HSLA;
|
|
99
103
|
/**
|
|
100
|
-
*
|
|
104
|
+
* @private Converts the given color to all other formats while preserving the original.
|
|
101
105
|
*
|
|
102
|
-
* @private
|
|
103
106
|
* @param color - The color to convert.
|
|
104
107
|
* @returns An object containing Hex, RGB, and HSL representations.
|
|
105
108
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Color.d.ts","sourceRoot":"","sources":["../../src/colors/Color.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACX,WAAW,EACX,SAAS,EACT,IAAI,EACJ,IAAI,EACJ,GAAG,EACH,IAAI,EACJ,YAAY,EACZ,GAAG,EACH,IAAI,EACJ,WAAW,EACX,MAAM,SAAS,CAAC;AAKjB
|
|
1
|
+
{"version":3,"file":"Color.d.ts","sourceRoot":"","sources":["../../src/colors/Color.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACX,WAAW,EACX,SAAS,EACT,IAAI,EACJ,IAAI,EACJ,GAAG,EACH,IAAI,EACJ,YAAY,EACZ,GAAG,EACH,IAAI,EACJ,WAAW,EACX,MAAM,SAAS,CAAC;AAKjB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,KAAK;IACV,GAAG,EAAE,IAAI,CAAC;IACV,IAAI,EAAE,IAAI,CAAC;IACX,GAAG,EAAE,GAAG,CAAC;IACT,IAAI,EAAE,IAAI,CAAC;IACX,GAAG,EAAE,GAAG,CAAC;IACT,IAAI,EAAE,IAAI,CAAC;IAElB,iEAAiE;IAChE,CAAC,MAAM,CAAC,QAAQ,CAAC;IASlB;;;;OAIG;gBACS,SAAS,CAAC,EAAE,SAAS;IAyCjC;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,WAAW,GAAG,WAAW;IAgC9D;;;;;OAKG;WACW,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;IAIlD;;;;;OAKG;WACW,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;IAIlD;;;;;OAKG;WACW,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,GAAG;IAIhD;;;;;OAKG;WACW,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;IAMlD;;;;;OAKG;WACW,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,GAAG;IAIhD;;;;;OAKG;WACW,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;IAMlD;;;;;OAKG;IACH,OAAO,CAAC,qBAAqB;CAuB7B"}
|
package/dist/colors/Color.js
CHANGED
|
@@ -4,59 +4,87 @@ import { generateRandomHSLColor } from './random';
|
|
|
4
4
|
const hsl = generateRandomHSLColor();
|
|
5
5
|
const hexRGB = convertColorCode(hsl);
|
|
6
6
|
/**
|
|
7
|
-
* * Class representing a color and its conversions
|
|
8
|
-
* * It has 1 instance method to apply opacity to `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` color.
|
|
7
|
+
* * Class representing a color and its conversions among `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` and `HSLA` formats.
|
|
8
|
+
* * It has 1 instance method `applyOpacity()` to apply opacity to `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` color.
|
|
9
9
|
* * It has 6 static methods that can be used to check if a color is in `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` format.
|
|
10
10
|
*
|
|
11
11
|
* @property {Hex} hex - The color in `Hex` format.
|
|
12
|
+
* @property {Hex8} hex8 - The color in `Hex8` format.
|
|
12
13
|
* @property {RGB} rgb - The color in `RGB` format.
|
|
14
|
+
* @property {RGBA} rgba - The color in `RGBA` format.
|
|
13
15
|
* @property {HSL} hsl - The color in `HSL` format.
|
|
16
|
+
* @property {HSLA} hsla - The color in `HSLA` format.
|
|
14
17
|
*
|
|
15
18
|
* @example
|
|
16
|
-
* const color = new Color("#ff5733"); // Accepts a color in `Hex`, `RGB` or `
|
|
19
|
+
* const color = new Color("#ff5733"); // Accepts a color in `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` format.
|
|
20
|
+
* console.log(color.hex); // Get Hex equivalent
|
|
21
|
+
* console.log(color.hex8); // Get Hex8 equivalent
|
|
17
22
|
* console.log(color.rgb); // Get RGB equivalent
|
|
23
|
+
* console.log(color.rgba); // Get RGBA equivalent
|
|
18
24
|
* console.log(color.hsl); // Get HSL equivalent
|
|
25
|
+
* console.log(color.hsla); // Get HSLA equivalent
|
|
19
26
|
*
|
|
20
27
|
* @example
|
|
21
28
|
* const randomColor = new Color(); // Generate a random color
|
|
22
|
-
* console.log(randomColor.hex, randomColor.rgb, randomColor.hsl);
|
|
29
|
+
* console.log(randomColor.hex, randomColor.rgb, randomColor.hsl, randomColor.hex8, randomColor.rgba, randomColor.hsla); // Get RGBA and HSLA equivalent
|
|
23
30
|
*/
|
|
24
31
|
export class Color {
|
|
25
32
|
hex;
|
|
33
|
+
hex8;
|
|
26
34
|
rgb;
|
|
35
|
+
rgba;
|
|
27
36
|
hsl;
|
|
37
|
+
hsla;
|
|
28
38
|
/** - Iterates over the color representations (Hex, RGB, HSL). */
|
|
29
39
|
*[Symbol.iterator]() {
|
|
30
40
|
yield this.hex;
|
|
41
|
+
yield this.hex8;
|
|
31
42
|
yield this.rgb;
|
|
43
|
+
yield this.rgba;
|
|
32
44
|
yield this.hsl;
|
|
45
|
+
yield this.hsla;
|
|
33
46
|
}
|
|
34
47
|
/**
|
|
35
|
-
* * Creates a new Color instance, optionally
|
|
48
|
+
* * Creates a new `Color` instance, optionally converts an input color to other 5 different color formats.
|
|
36
49
|
*
|
|
37
50
|
* @param toConvert - The color to convert. If not provided, a random color is generated.
|
|
38
51
|
*/
|
|
39
52
|
constructor(toConvert) {
|
|
40
53
|
if (toConvert) {
|
|
41
|
-
const
|
|
42
|
-
if ('hex8' in
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this.
|
|
54
|
+
const colors = this._convertColorToOthers(toConvert);
|
|
55
|
+
if ('hex8' in colors) {
|
|
56
|
+
// Extract alpha color values (Hex8, RGBA, HSLA)
|
|
57
|
+
const rgbaValues = _extractAlphaColorValues(colors.rgba);
|
|
58
|
+
const hslaValues = _extractAlphaColorValues(colors.hsla);
|
|
59
|
+
this.hex = colors.hex8.slice(0, 7);
|
|
60
|
+
this.hex8 = colors.hex8;
|
|
61
|
+
this.rgb = `rgb(${rgbaValues[0]}, ${rgbaValues[1]}, ${rgbaValues[2]})`;
|
|
62
|
+
this.rgba = colors.rgba;
|
|
63
|
+
this.hsl = `hsl(${hslaValues[0]}, ${hslaValues[1]}%, ${hslaValues[2]}%)`;
|
|
64
|
+
this.hsla = colors.hsla;
|
|
47
65
|
}
|
|
48
66
|
else {
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
this.
|
|
67
|
+
// Extract solid color values (Hex, RGB, HSL)
|
|
68
|
+
const rgbValues = _extractSolidColorValues(colors.rgb);
|
|
69
|
+
const hslValues = _extractSolidColorValues(colors.hsl);
|
|
70
|
+
this.hex = colors.hex;
|
|
71
|
+
this.hex8 = `${colors.hex}${_convertOpacityToHex(100)}`;
|
|
72
|
+
this.rgb = colors.rgb;
|
|
73
|
+
this.rgba = `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, 1)`;
|
|
74
|
+
this.hsl = colors.hsl;
|
|
75
|
+
this.hsla = `hsla(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%, 1)`;
|
|
53
76
|
}
|
|
54
77
|
}
|
|
55
78
|
else {
|
|
56
|
-
|
|
79
|
+
const rgbValues = _extractSolidColorValues(hexRGB.rgb);
|
|
80
|
+
const hslValues = _extractSolidColorValues(hsl);
|
|
81
|
+
// Generate random colors
|
|
57
82
|
this.hex = hexRGB.hex;
|
|
83
|
+
this.hex8 = `${hexRGB.hex}${_convertOpacityToHex(100)}`;
|
|
58
84
|
this.rgb = hexRGB.rgb;
|
|
85
|
+
this.rgba = `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, 1)`;
|
|
59
86
|
this.hsl = hsl;
|
|
87
|
+
this.hsla = `hsla(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%, 1)`;
|
|
60
88
|
}
|
|
61
89
|
}
|
|
62
90
|
/**
|
|
@@ -81,32 +109,31 @@ export class Color {
|
|
|
81
109
|
const validOpacity = Math.min(100, Math.max(0, opacity));
|
|
82
110
|
const alphaHex = _convertOpacityToHex(opacity);
|
|
83
111
|
const alphaDecimal = validOpacity / 100;
|
|
84
|
-
if (Color.isHex8(this.
|
|
85
|
-
const rgbaValues = _extractAlphaColorValues(this.
|
|
86
|
-
const hslaValues = _extractAlphaColorValues(this.
|
|
112
|
+
if (Color.isHex8(this.hex8)) {
|
|
113
|
+
const rgbaValues = _extractAlphaColorValues(this.rgba);
|
|
114
|
+
const hslaValues = _extractAlphaColorValues(this.hsla);
|
|
87
115
|
return {
|
|
88
|
-
hex: this.
|
|
89
|
-
hex8: `${this.
|
|
90
|
-
rgb: `
|
|
116
|
+
hex: this.hex8.slice(0, 7),
|
|
117
|
+
hex8: `${this.hex8.slice(0, 7)}${alphaHex}`,
|
|
118
|
+
rgb: `rgb(${rgbaValues[0]}, ${rgbaValues[1]}, ${rgbaValues[2]})`,
|
|
91
119
|
rgba: `rgba(${rgbaValues[0]}, ${rgbaValues[1]}, ${rgbaValues[2]}, ${alphaDecimal})`,
|
|
92
|
-
hsl: `
|
|
120
|
+
hsl: `hsl(${hslaValues[0]}, ${hslaValues[1]}%, ${hslaValues[2]}%)`,
|
|
93
121
|
hsla: `hsla(${hslaValues[0]}, ${hslaValues[1]}%, ${hslaValues[2]}%, ${alphaDecimal})`,
|
|
94
122
|
};
|
|
95
123
|
}
|
|
96
124
|
const rgbValues = _extractSolidColorValues(this.rgb);
|
|
97
125
|
const hslValues = _extractSolidColorValues(this.hsl);
|
|
98
126
|
return {
|
|
99
|
-
hex: this.hex,
|
|
100
|
-
hex8: `${this.hex}${alphaHex}`,
|
|
101
|
-
rgb:
|
|
127
|
+
hex: this.hex.slice(0, 7),
|
|
128
|
+
hex8: `${this.hex.slice(0, 7)}${alphaHex}`,
|
|
129
|
+
rgb: `rgb(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]})`,
|
|
102
130
|
rgba: `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, ${alphaDecimal})`,
|
|
103
|
-
hsl:
|
|
131
|
+
hsl: `hsl(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%)`,
|
|
104
132
|
hsla: `hsla(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%, ${alphaDecimal})`,
|
|
105
133
|
};
|
|
106
134
|
}
|
|
107
135
|
/**
|
|
108
|
-
* @static
|
|
109
|
-
* Checks if a color is in `Hex6` format.
|
|
136
|
+
* @static Checks if a color is in `Hex6` format.
|
|
110
137
|
*
|
|
111
138
|
* @param color Color to check.
|
|
112
139
|
* @returns Boolean: `true` if it's a `Hex6` color, `false` if not.
|
|
@@ -115,8 +142,7 @@ export class Color {
|
|
|
115
142
|
return /^#[0-9A-Fa-f]{6}$/.test(color);
|
|
116
143
|
}
|
|
117
144
|
/**
|
|
118
|
-
* @static
|
|
119
|
-
* Checks if a color is in `Hex8` format.
|
|
145
|
+
* @static Checks if a color is in `Hex8` format.
|
|
120
146
|
*
|
|
121
147
|
* @param color Color to check.
|
|
122
148
|
* @returns Boolean: `true` if it's a `Hex8` color, `false` if not.
|
|
@@ -125,8 +151,7 @@ export class Color {
|
|
|
125
151
|
return /^#[0-9A-Fa-f]{8}$/.test(color);
|
|
126
152
|
}
|
|
127
153
|
/**
|
|
128
|
-
* @static
|
|
129
|
-
* Checks if a color is in `RGB` format.
|
|
154
|
+
* @static Checks if a color is in `RGB` format.
|
|
130
155
|
*
|
|
131
156
|
* @param color Color to check.
|
|
132
157
|
* @returns Boolean: `true` if it's an `RGB` color, `false` if not.
|
|
@@ -135,8 +160,7 @@ export class Color {
|
|
|
135
160
|
return /^rgb\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\)$/.test(color);
|
|
136
161
|
}
|
|
137
162
|
/**
|
|
138
|
-
* @static
|
|
139
|
-
* Checks if a color is in `RGBA` format.
|
|
163
|
+
* @static Checks if a color is in `RGBA` format.
|
|
140
164
|
*
|
|
141
165
|
* @param color Color to check.
|
|
142
166
|
* @returns Boolean: `true` if it's an `RGBA` color, `false` if not.
|
|
@@ -145,8 +169,7 @@ export class Color {
|
|
|
145
169
|
return /^rgba\(\d{1,3},\s*\d{1,3},\s*\d{1,3},\s*(0|1|0?\.\d+)\)$/.test(color);
|
|
146
170
|
}
|
|
147
171
|
/**
|
|
148
|
-
* @static
|
|
149
|
-
* Checks if a color is in `HSL` format.
|
|
172
|
+
* @static Checks if a color is in `HSL` format.
|
|
150
173
|
*
|
|
151
174
|
* @param color Color to check.
|
|
152
175
|
* @returns Boolean: `true` if it's an `HSL` color, `false` if not.
|
|
@@ -155,8 +178,7 @@ export class Color {
|
|
|
155
178
|
return /^hsl\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%\)$/.test(color);
|
|
156
179
|
}
|
|
157
180
|
/**
|
|
158
|
-
* @static
|
|
159
|
-
* Checks if a color is in `HSLA` format.
|
|
181
|
+
* @static Checks if a color is in `HSLA` format.
|
|
160
182
|
*
|
|
161
183
|
* @param color Color to check.
|
|
162
184
|
* @returns Boolean: `true` if it's an `HSLA` color, `false` if not.
|
|
@@ -165,9 +187,8 @@ export class Color {
|
|
|
165
187
|
return /^hsla\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%,\s*(0|1|0?\.\d+)\)$/.test(color);
|
|
166
188
|
}
|
|
167
189
|
/**
|
|
168
|
-
*
|
|
190
|
+
* @private Converts the given color to all other formats while preserving the original.
|
|
169
191
|
*
|
|
170
|
-
* @private
|
|
171
192
|
* @param color - The color to convert.
|
|
172
193
|
* @returns An object containing Hex, RGB, and HSL representations.
|
|
173
194
|
*/
|
package/dist/colors/convert.js
CHANGED
|
@@ -166,7 +166,7 @@ export const convertRgbaToHex8 = (r, g, b, a = 1) => {
|
|
|
166
166
|
}
|
|
167
167
|
const hex = convertRgbToHex(r, g, b);
|
|
168
168
|
const alphaHex = _convertOpacityToHex(Math.round(newAlpha * 100));
|
|
169
|
-
return
|
|
169
|
+
return `${hex}${alphaHex}`;
|
|
170
170
|
};
|
|
171
171
|
/**
|
|
172
172
|
* * Converts HSLA to RGBA color format, including alpha channel.
|
|
@@ -239,7 +239,7 @@ export const convertHslaToHex8 = (h, s, l, a = 1) => {
|
|
|
239
239
|
}
|
|
240
240
|
const hex = convertHslToHex(h, s, l);
|
|
241
241
|
const alphaHex = _convertOpacityToHex(Math.round(newAlpha * 100));
|
|
242
|
-
return
|
|
242
|
+
return `${hex}${alphaHex}`;
|
|
243
243
|
};
|
|
244
244
|
/**
|
|
245
245
|
* * Converts Hex8 to HSLA color format.
|
package/dist/colors/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type ColorInput = string | number;
|
|
|
4
4
|
/** - An array of strings/numbers or nested arrays of strings/numbers for generating colors. */
|
|
5
5
|
export interface ColorInputArray extends Array<ColorInput | ColorInputArray> {
|
|
6
6
|
}
|
|
7
|
-
/** - Opacity
|
|
7
|
+
/** - Opacity value in percentage `(0% - 100%)` without `%` symbol. */
|
|
8
8
|
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;
|
|
9
9
|
/**
|
|
10
10
|
* * Represents a hexadecimal color code.
|
|
@@ -57,14 +57,20 @@ export type ColorTypeAlpha = Hex8 | RGBA | HSLA;
|
|
|
57
57
|
export type ColorType = Hex | Hex6 | RGB | HSL | Hex8 | RGBA | HSLA;
|
|
58
58
|
/** - Colors Object that includes `Hex8`, `RGBA` and `HSLA` formats of the same color. */
|
|
59
59
|
export interface SolidColors {
|
|
60
|
+
/** Represents a normal Hex color */
|
|
60
61
|
hex: Hex6;
|
|
62
|
+
/** Represents a normal RGB color */
|
|
61
63
|
rgb: RGB;
|
|
64
|
+
/** Represents a normal HSL color */
|
|
62
65
|
hsl: HSL;
|
|
63
66
|
}
|
|
64
67
|
/** - Colors Object that includes `Hex`, `RGB` and `HSL` formats of the same color. */
|
|
65
68
|
export interface AlphaColors {
|
|
69
|
+
/** Represents a Hex color with alpha channel */
|
|
66
70
|
hex8: Hex8;
|
|
71
|
+
/** Represents a RGBA color with alpha channel */
|
|
67
72
|
rgba: RGBA;
|
|
73
|
+
/** Represents a HSLA color with alpha channel */
|
|
68
74
|
hsla: HSLA;
|
|
69
75
|
}
|
|
70
76
|
/** * Represents a tuple of three numerical values corresponding to RGB or HSL color components. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/colors/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC,+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,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/colors/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC,+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,sEAAsE;AACtE,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,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IAAI,EAAE,CAAC,SAAS,IAAI,GAAG,cAAc,GAAG,KAAK,GAAG,IAAI,CAAC;IACrD,yEAAyE;IACzE,IAAI,EAAE,CAAC,SAAS,IAAI,GAAG,cAAc,GAAG,KAAK,GAAG,IAAI,CAAC;IACrD,yEAAyE;IACzE,IAAI,EAAE,CAAC,SAAS,IAAI,GAAG,cAAc,GAAG,KAAK,GAAG,IAAI,CAAC;CACrD;AAED,gDAAgD;AAChD,MAAM,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC"}
|
package/dist/date/Chronos.d.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
export declare class Chronos {
|
|
2
|
-
private readonly
|
|
2
|
+
private readonly date;
|
|
3
3
|
/**
|
|
4
4
|
* * Creates a new immutable Chronos instance.
|
|
5
|
-
* @param value - A date value (`timestamp`, `string`,
|
|
5
|
+
* @param value - A date value (`timestamp`, `string`, `Date`, `Chronos`).
|
|
6
6
|
*/
|
|
7
7
|
constructor(value?: number | string | Date | Chronos);
|
|
8
|
+
/** * Gets the native `Date` instance (read-only). */
|
|
9
|
+
toDate(): Date;
|
|
10
|
+
/** * Returns a string representation of a date. The format of the string depends on the locale. */
|
|
11
|
+
toString(): string;
|
|
12
|
+
/** * Returns a date as a string value in ISO format. */
|
|
13
|
+
toISOString(): string;
|
|
14
|
+
/** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
|
|
15
|
+
getTimeStamp(): number;
|
|
16
|
+
/** * Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC). */
|
|
17
|
+
static now(): number;
|
|
8
18
|
/**
|
|
9
19
|
* * Formats the date into a custom string format.
|
|
10
20
|
* @param format - The desired format (Default format is `DD-MM-YYYY` = `30-06-1995`).
|
|
@@ -33,7 +43,12 @@ export declare class Chronos {
|
|
|
33
43
|
* - Other positive or negative numbers for other relative days (e.g., `-2` for two days ago, `2` for two days ahead).
|
|
34
44
|
*/
|
|
35
45
|
getRelativeDay(): number;
|
|
36
|
-
/**
|
|
37
|
-
|
|
46
|
+
/**
|
|
47
|
+
* * Checks if the year is a leap year.
|
|
48
|
+
* - A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.
|
|
49
|
+
* - For example, 2000 and 2400 are leap years, but 1900 and 2100 are not.
|
|
50
|
+
* @returns `true` if the year is a leap year, `false` otherwise.
|
|
51
|
+
*/
|
|
52
|
+
isLeapYear(): boolean;
|
|
38
53
|
}
|
|
39
54
|
//# sourceMappingURL=Chronos.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Chronos.d.ts","sourceRoot":"","sources":["../../src/date/Chronos.ts"],"names":[],"mappings":"AAGA,qBAAa,OAAO;IACnB,OAAO,CAAC,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"Chronos.d.ts","sourceRoot":"","sources":["../../src/date/Chronos.ts"],"names":[],"mappings":"AAGA,qBAAa,OAAO;IACnB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAO;IAE5B;;;OAGG;gBACS,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAcpD,qDAAqD;IACrD,MAAM,IAAI,IAAI;IAId,mGAAmG;IACnG,QAAQ,IAAI,MAAM;IAIlB,wDAAwD;IACxD,WAAW,IAAI,MAAM;IAIrB,oFAAoF;IACpF,YAAY,IAAI,MAAM;IAItB,qHAAqH;IACrH,MAAM,CAAC,GAAG,IAAI,MAAM;IAIpB;;;;OAIG;IACH,MAAM,CAAC,MAAM,GAAE,MAAqB,GAAG,MAAM;IA2E7C;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAM9B;;;;OAIG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAInC;;;;;;;;OAQG;IACH,cAAc,IAAI,MAAM;IAexB;;;;;OAKG;IACH,UAAU,IAAI,OAAO;CAKrB"}
|
package/dist/date/Chronos.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { DAYS, MONTHS, sortedFormats } from './constants';
|
|
2
2
|
export class Chronos {
|
|
3
|
-
|
|
3
|
+
date;
|
|
4
4
|
/**
|
|
5
5
|
* * Creates a new immutable Chronos instance.
|
|
6
|
-
* @param value - A date value (`timestamp`, `string`,
|
|
6
|
+
* @param value - A date value (`timestamp`, `string`, `Date`, `Chronos`).
|
|
7
7
|
*/
|
|
8
8
|
constructor(value) {
|
|
9
9
|
const date = value instanceof Chronos ?
|
|
@@ -13,7 +13,27 @@ export class Chronos {
|
|
|
13
13
|
if (isNaN(date.getTime())) {
|
|
14
14
|
throw new Error('Invalid date provided!');
|
|
15
15
|
}
|
|
16
|
-
this.
|
|
16
|
+
this.date = date;
|
|
17
|
+
}
|
|
18
|
+
/** * Gets the native `Date` instance (read-only). */
|
|
19
|
+
toDate() {
|
|
20
|
+
return new Date(this.date);
|
|
21
|
+
}
|
|
22
|
+
/** * Returns a string representation of a date. The format of the string depends on the locale. */
|
|
23
|
+
toString() {
|
|
24
|
+
return this.date.toString();
|
|
25
|
+
}
|
|
26
|
+
/** * Returns a date as a string value in ISO format. */
|
|
27
|
+
toISOString() {
|
|
28
|
+
return this.date.toISOString();
|
|
29
|
+
}
|
|
30
|
+
/** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
|
|
31
|
+
getTimeStamp() {
|
|
32
|
+
return this.date.getTime();
|
|
33
|
+
}
|
|
34
|
+
/** * Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC). */
|
|
35
|
+
static now() {
|
|
36
|
+
return Date.now();
|
|
17
37
|
}
|
|
18
38
|
/**
|
|
19
39
|
* * Formats the date into a custom string format.
|
|
@@ -21,14 +41,14 @@ export class Chronos {
|
|
|
21
41
|
* @returns Formatted date string in desired format.
|
|
22
42
|
*/
|
|
23
43
|
format(format = 'DD-MM-YYYY') {
|
|
24
|
-
const year = this.
|
|
25
|
-
const month = this.
|
|
26
|
-
const day = this.
|
|
27
|
-
const date = this.
|
|
28
|
-
const hours = this.
|
|
29
|
-
const minutes = this.
|
|
30
|
-
const seconds = this.
|
|
31
|
-
const milliseconds = this.
|
|
44
|
+
const year = this.date.getFullYear();
|
|
45
|
+
const month = this.date.getMonth();
|
|
46
|
+
const day = this.date.getDay();
|
|
47
|
+
const date = this.date.getDate();
|
|
48
|
+
const hours = this.date.getHours();
|
|
49
|
+
const minutes = this.date.getMinutes();
|
|
50
|
+
const seconds = this.date.getSeconds();
|
|
51
|
+
const milliseconds = this.date.getMilliseconds();
|
|
32
52
|
const dateComponents = {
|
|
33
53
|
YYYY: String(year),
|
|
34
54
|
YY: String(year).slice(-2),
|
|
@@ -91,7 +111,7 @@ export class Chronos {
|
|
|
91
111
|
* @returns A new `Chronos` instance with the updated date.
|
|
92
112
|
*/
|
|
93
113
|
addDays(days) {
|
|
94
|
-
const newDate = new Date(this.
|
|
114
|
+
const newDate = new Date(this.date);
|
|
95
115
|
newDate.setDate(newDate.getDate() + days);
|
|
96
116
|
return new Chronos(newDate);
|
|
97
117
|
}
|
|
@@ -117,14 +137,20 @@ export class Chronos {
|
|
|
117
137
|
// Set the time of today to 00:00:00 for comparison purposes
|
|
118
138
|
today.setHours(0, 0, 0, 0);
|
|
119
139
|
// Normalize the input date to 00:00:00
|
|
120
|
-
const inputDate = new Date(this.
|
|
140
|
+
const inputDate = new Date(this.date);
|
|
121
141
|
inputDate.setHours(0, 0, 0, 0);
|
|
122
142
|
const diffTime = inputDate.getTime() - today.getTime();
|
|
123
143
|
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
|
|
124
144
|
return diffDays;
|
|
125
145
|
}
|
|
126
|
-
/**
|
|
127
|
-
|
|
128
|
-
|
|
146
|
+
/**
|
|
147
|
+
* * Checks if the year is a leap year.
|
|
148
|
+
* - A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.
|
|
149
|
+
* - For example, 2000 and 2400 are leap years, but 1900 and 2100 are not.
|
|
150
|
+
* @returns `true` if the year is a leap year, `false` otherwise.
|
|
151
|
+
*/
|
|
152
|
+
isLeapYear() {
|
|
153
|
+
const year = this.date.getFullYear();
|
|
154
|
+
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
129
155
|
}
|
|
130
156
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nhb-toolbox",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.5",
|
|
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",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"build": "node scripts/build.mjs",
|
|
58
58
|
"test": "jest --coverage --verbose",
|
|
59
59
|
"format": "prettier --write .",
|
|
60
|
-
"lint": "node scripts
|
|
60
|
+
"lint": "node scripts/lint.mjs",
|
|
61
61
|
"fix": "node scripts/fix.mjs",
|
|
62
62
|
"commit": "node scripts/commit.mjs"
|
|
63
63
|
}
|