nhb-toolbox 4.0.48 → 4.0.50

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.
@@ -156,10 +156,10 @@ class Color {
156
156
  * @returns A new `Color` instance with the modified darkness.
157
157
  */
158
158
  applyDarkness(percent) {
159
- const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
159
+ const [h, s, l, a] = (0, helpers_1._extractAlphaColorValues)(this.hsla);
160
160
  const newL = Math.max(0, l - percent);
161
161
  const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
162
- return new Color(newHSL);
162
+ return new Color(newHSL).applyOpacity((a * 100));
163
163
  }
164
164
  /**
165
165
  * @instance Lightens the color by increasing the lightness by the given percentage.
@@ -167,10 +167,10 @@ class Color {
167
167
  * @returns A new `Color` instance with the modified lightness.
168
168
  */
169
169
  applyBrightness(percent) {
170
- const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
170
+ const [h, s, l, a] = (0, helpers_1._extractAlphaColorValues)(this.hsla);
171
171
  const newL = Math.min(100, l + percent);
172
172
  const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
173
- return new Color(newHSL);
173
+ return new Color(newHSL).applyOpacity((a * 100));
174
174
  }
175
175
  /**
176
176
  * @instance Reduces the saturation of the color to make it appear duller.
@@ -178,10 +178,10 @@ class Color {
178
178
  * @returns A new `Color` instance with the modified saturation.
179
179
  */
180
180
  applyDullness(percent) {
181
- const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
181
+ const [h, s, l, a] = (0, helpers_1._extractAlphaColorValues)(this.hsla);
182
182
  const newS = Math.max(0, s - percent);
183
183
  const newHSL = `hsl(${h}, ${newS}%, ${l}%)`;
184
- return new Color(newHSL);
184
+ return new Color(newHSL).applyOpacity((a * 100));
185
185
  }
186
186
  /**
187
187
  * @instance Softens the color toward white by reducing saturation and increasing lightness based on a percentage.
@@ -190,15 +190,17 @@ class Color {
190
190
  * @returns A new `Color` instance shifted toward white.
191
191
  */
192
192
  applyWhiteShade(percent) {
193
- const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
193
+ const [h, s, l, a] = (0, helpers_1._extractAlphaColorValues)(this.hsla);
194
194
  // Cap values to avoid overshooting
195
195
  const newS = Math.max(0, s - (s * percent) / 100);
196
196
  const newL = Math.min(100, l + ((100 - l) * percent) / 100);
197
197
  const newHSL = `hsl(${h}, ${newS}%, ${newL}%)`;
198
- return new Color(newHSL);
198
+ return new Color(newHSL).applyOpacity((a * 100));
199
199
  }
200
200
  /**
201
201
  * @instance Blends the current color with another color based on the given weight.
202
+ *
203
+ * - **NOTE:** *If any of the input colors has opacity (alpha channel), it might be lost from the generated alpha variants of the respective color formats.*
202
204
  * @param other - The color in any 6 `(Hex, Hex8 RGB, RGBA, HSL or HSLA)` to blend with.
203
205
  * @param weight - A number from 0 to 1 indicating the weight of the other color. Defaults to `0.5`.
204
206
  * - `weight = 0` → only the original color (this)
@@ -209,11 +211,20 @@ class Color {
209
211
  blendWith(other, weight = 0.5) {
210
212
  const w = Math.max(0, Math.min(1, weight));
211
213
  const converted = new Color(other);
212
- const rgb1 = (0, helpers_1._extractSolidColorValues)(this.rgb);
213
- const rgb2 = (0, helpers_1._extractSolidColorValues)(converted.rgb);
214
- const blended = rgb1.map((c, i) => Math.round(c * (1 - w) + rgb2[i] * w));
215
- const blendedRGB = `rgb(${blended[0]}, ${blended[1]}, ${blended[2]})`;
216
- return new Color(blendedRGB);
214
+ const [r1, b1, g1, a1] = (0, helpers_1._extractAlphaColorValues)(this.rgba);
215
+ const [r2, b2, g2, a2] = (0, helpers_1._extractAlphaColorValues)(converted.rgba);
216
+ const alpha = a1 * (1 - w) + a2 * w;
217
+ // ! Original code for solid color
218
+ // const blended = rgb1.map((c, i) =>
219
+ // Math.round(c * (1 - w) + rgb2[i] * w),
220
+ // ) as ColorNumbers;
221
+ // const blendedRGB = `rgb(${blended[0]}, ${blended[1]}, ${blended[2]})`;
222
+ const blendChannel = (c1, c2) => Math.round((c1 * a1 * (1 - w) + c2 * a2 * w) / alpha);
223
+ const r = blendChannel(r1, r2);
224
+ const g = blendChannel(g1, g2);
225
+ const b = blendChannel(b1, b2);
226
+ const blended = `rgba(${r}, ${g}, ${b}, ${+alpha.toFixed(2)})`;
227
+ return new Color(blended);
217
228
  }
218
229
  /**
219
230
  * @instance Calculates the contrast ratio between this color and another color (WCAG).
@@ -243,10 +254,10 @@ class Color {
243
254
  * @returns A new Color that is the complement of the current color.
244
255
  */
245
256
  getComplementaryColor() {
246
- const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
257
+ const [h, s, l, a] = (0, helpers_1._extractAlphaColorValues)(this.hsla);
247
258
  const newHue = (h + 180) % 360;
248
259
  const newHSL = `hsl(${newHue}, ${s}%, ${l}%)`;
249
- return new Color(newHSL);
260
+ return new Color(newHSL).applyOpacity((a * 100));
250
261
  }
251
262
  /**
252
263
  * @instance Generates a color scheme of analogous colors, including the base color.
@@ -254,10 +265,11 @@ class Color {
254
265
  * @returns An array of three Color instances: [base, left, right].
255
266
  */
256
267
  getAnalogousColors() {
257
- const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
268
+ const [h, s, l, a] = (0, helpers_1._extractAlphaColorValues)(this.hsla);
258
269
  const left = `hsl(${(h + 330) % 360}, ${s}%, ${l}%)`;
259
270
  const right = `hsl(${(h + 30) % 360}, ${s}%, ${l}%)`;
260
- return [this, new Color(left), new Color(right)];
271
+ const analogous = [this, new Color(left), new Color(right)];
272
+ return analogous.map((c) => c.applyOpacity((a * 100)));
261
273
  }
262
274
  /**
263
275
  * @instance Generates a color triad scheme including the base color.
@@ -265,10 +277,11 @@ class Color {
265
277
  * @returns An array of three Color instances: [base, triad1, triad2].
266
278
  */
267
279
  getTriadColors() {
268
- const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
280
+ const [h, s, l, a] = (0, helpers_1._extractAlphaColorValues)(this.hsla);
269
281
  const c1 = `hsl(${(h + 120) % 360}, ${s}%, ${l}%)`;
270
282
  const c2 = `hsl(${(h + 240) % 360}, ${s}%, ${l}%)`;
271
- return [this, new Color(c1), new Color(c2)];
283
+ const triad = [this, new Color(c1), new Color(c2)];
284
+ return triad.map((c) => c.applyOpacity((a * 100)));
272
285
  }
273
286
  /**
274
287
  * @instance Generates a tetradic color scheme including the base color.
@@ -276,11 +289,12 @@ class Color {
276
289
  * @returns An array of four Color instances: [base, tetrad1, tetrad2, tetrad3].
277
290
  */
278
291
  getTetradColors() {
279
- const [h, s, l] = (0, helpers_1._extractSolidColorValues)(this.hsl);
292
+ const [h, s, l, a] = (0, helpers_1._extractAlphaColorValues)(this.hsla);
280
293
  const c1 = `hsl(${(h + 90) % 360}, ${s}%, ${l}%)`;
281
294
  const c2 = `hsl(${(h + 180) % 360}, ${s}%, ${l}%)`;
282
295
  const c3 = `hsl(${(h + 270) % 360}, ${s}%, ${l}%)`;
283
- return [this, new Color(c1), new Color(c2), new Color(c3)];
296
+ const tetrad = [this, new Color(c1), new Color(c2), new Color(c3)];
297
+ return tetrad.map((c) => c.applyOpacity((a * 100)));
284
298
  }
285
299
  /**
286
300
  * @instance Gets the `WCAG` accessibility rating between this and another color.
@@ -1,4 +1,4 @@
1
- import type { ColorType, Hex6, Hex8, HSL, HSLA, Percent, RGB, RGBA } from './types';
1
+ import type { Analogous, ColorType, Hex6, Hex8, HSL, HSLA, Percent, RGB, RGBA, Tetrad, Triad } from './types';
2
2
  /**
3
3
  * * Class representing a color and its conversions among `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` and `HSLA` formats.
4
4
  * * It has 13 instance methods to manipulate and play with the color values.
@@ -82,7 +82,7 @@ export declare class Color {
82
82
  */
83
83
  constructor(toConvert: ColorType);
84
84
  /** - Iterates over the color representations (Hex, RGB, HSL). */
85
- [Symbol.iterator](): Generator<Hex6 | RGB | HSL | Hex8 | RGBA | HSLA, void, unknown>;
85
+ [Symbol.iterator](): Generator<RGB | HSL | Hex6 | RGBA | Hex8 | HSLA, void, unknown>;
86
86
  /**
87
87
  * @instance Applies or modifies the opacity of a color. Mutate the original instance.
88
88
  * - For solid colors (Hex6/RGB/HSL): Adds an alpha channel with the specified opacity.
@@ -129,6 +129,8 @@ export declare class Color {
129
129
  applyWhiteShade(percent: Percent): Color;
130
130
  /**
131
131
  * @instance Blends the current color with another color based on the given weight.
132
+ *
133
+ * - **NOTE:** *If any of the input colors has opacity (alpha channel), it might be lost from the generated alpha variants of the respective color formats.*
132
134
  * @param other - The color in any 6 `(Hex, Hex8 RGB, RGBA, HSL or HSLA)` to blend with.
133
135
  * @param weight - A number from 0 to 1 indicating the weight of the other color. Defaults to `0.5`.
134
136
  * - `weight = 0` → only the original color (this)
@@ -153,19 +155,19 @@ export declare class Color {
153
155
  * Analogous colors are next to each other on the color wheel (±30°).
154
156
  * @returns An array of three Color instances: [base, left, right].
155
157
  */
156
- getAnalogousColors(): [Color, Color, Color];
158
+ getAnalogousColors(): Analogous;
157
159
  /**
158
160
  * @instance Generates a color triad scheme including the base color.
159
161
  * Triadic colors are evenly spaced (120° apart) on the color wheel.
160
162
  * @returns An array of three Color instances: [base, triad1, triad2].
161
163
  */
162
- getTriadColors(): [Color, Color, Color];
164
+ getTriadColors(): Triad;
163
165
  /**
164
166
  * @instance Generates a tetradic color scheme including the base color.
165
167
  * Tetradic colors form a rectangle on the color wheel (90° apart).
166
168
  * @returns An array of four Color instances: [base, tetrad1, tetrad2, tetrad3].
167
169
  */
168
- getTetradColors(): [Color, Color, Color, Color];
170
+ getTetradColors(): Tetrad;
169
171
  /**
170
172
  * @instance Gets the `WCAG` accessibility rating between this and another color.
171
173
  * @param other - The other color to test contrast against.
@@ -1 +1 @@
1
- {"version":3,"file":"Color.d.ts","sourceRoot":"","sources":["../../../src/colors/Color.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAIX,SAAS,EACT,IAAI,EACJ,IAAI,EACJ,GAAG,EACH,IAAI,EACJ,OAAO,EACP,GAAG,E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AAM,GAAG,KAAK,IAAI,GAAG;IAIzC;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI;CAkD3C"}
@@ -1,4 +1,5 @@
1
1
  import type { Branded } from '../types';
2
+ import type { Color } from './Color';
2
3
  /** - A string, number for generating color. */
3
4
  export type ColorInput = string | number;
4
5
  /** - An array of strings/numbers or nested arrays of strings/numbers for generating colors. */
@@ -115,4 +116,7 @@ export interface Colors {
115
116
  /** - `HSLA` color (e.g., `hsla(14, 100%, 60%, 1)`) */
116
117
  hsla: HSLA;
117
118
  }
119
+ export type Analogous = [Color, Color, Color];
120
+ export type Triad = [Color, Color, Color];
121
+ export type Tetrad = [Color, Color, Color, Color];
118
122
  //# sourceMappingURL=types.d.ts.map
@@ -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,qEAAqE;AACrE,MAAM,MAAM,OAAO,GAChB,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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CACrD;AAED,gDAAgD;AAChD,MAAM,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAEvD,sEAAsE;AACtE,MAAM,WAAW,MAAM;IACtB,sCAAsC;IACtC,GAAG,EAAE,IAAI,CAAC;IACV,2DAA2D;IAC3D,IAAI,EAAE,IAAI,CAAC;IACX,+CAA+C;IAC/C,GAAG,EAAE,GAAG,CAAC;IACT,oDAAoD;IACpD,IAAI,EAAE,IAAI,CAAC;IACX,iDAAiD;IACjD,GAAG,EAAE,GAAG,CAAC;IACT,sDAAsD;IACtD,IAAI,EAAE,IAAI,CAAC;CACX;AAED,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAE9C,MAAM,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAE1C,MAAM,MAAM,MAAM,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC"}
@@ -153,10 +153,10 @@ export class Color {
153
153
  * @returns A new `Color` instance with the modified darkness.
154
154
  */
155
155
  applyDarkness(percent) {
156
- const [h, s, l] = _extractSolidColorValues(this.hsl);
156
+ const [h, s, l, a] = _extractAlphaColorValues(this.hsla);
157
157
  const newL = Math.max(0, l - percent);
158
158
  const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
159
- return new Color(newHSL);
159
+ return new Color(newHSL).applyOpacity((a * 100));
160
160
  }
161
161
  /**
162
162
  * @instance Lightens the color by increasing the lightness by the given percentage.
@@ -164,10 +164,10 @@ export class Color {
164
164
  * @returns A new `Color` instance with the modified lightness.
165
165
  */
166
166
  applyBrightness(percent) {
167
- const [h, s, l] = _extractSolidColorValues(this.hsl);
167
+ const [h, s, l, a] = _extractAlphaColorValues(this.hsla);
168
168
  const newL = Math.min(100, l + percent);
169
169
  const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
170
- return new Color(newHSL);
170
+ return new Color(newHSL).applyOpacity((a * 100));
171
171
  }
172
172
  /**
173
173
  * @instance Reduces the saturation of the color to make it appear duller.
@@ -175,10 +175,10 @@ export class Color {
175
175
  * @returns A new `Color` instance with the modified saturation.
176
176
  */
177
177
  applyDullness(percent) {
178
- const [h, s, l] = _extractSolidColorValues(this.hsl);
178
+ const [h, s, l, a] = _extractAlphaColorValues(this.hsla);
179
179
  const newS = Math.max(0, s - percent);
180
180
  const newHSL = `hsl(${h}, ${newS}%, ${l}%)`;
181
- return new Color(newHSL);
181
+ return new Color(newHSL).applyOpacity((a * 100));
182
182
  }
183
183
  /**
184
184
  * @instance Softens the color toward white by reducing saturation and increasing lightness based on a percentage.
@@ -187,15 +187,17 @@ export class Color {
187
187
  * @returns A new `Color` instance shifted toward white.
188
188
  */
189
189
  applyWhiteShade(percent) {
190
- const [h, s, l] = _extractSolidColorValues(this.hsl);
190
+ const [h, s, l, a] = _extractAlphaColorValues(this.hsla);
191
191
  // Cap values to avoid overshooting
192
192
  const newS = Math.max(0, s - (s * percent) / 100);
193
193
  const newL = Math.min(100, l + ((100 - l) * percent) / 100);
194
194
  const newHSL = `hsl(${h}, ${newS}%, ${newL}%)`;
195
- return new Color(newHSL);
195
+ return new Color(newHSL).applyOpacity((a * 100));
196
196
  }
197
197
  /**
198
198
  * @instance Blends the current color with another color based on the given weight.
199
+ *
200
+ * - **NOTE:** *If any of the input colors has opacity (alpha channel), it might be lost from the generated alpha variants of the respective color formats.*
199
201
  * @param other - The color in any 6 `(Hex, Hex8 RGB, RGBA, HSL or HSLA)` to blend with.
200
202
  * @param weight - A number from 0 to 1 indicating the weight of the other color. Defaults to `0.5`.
201
203
  * - `weight = 0` → only the original color (this)
@@ -206,11 +208,20 @@ export class Color {
206
208
  blendWith(other, weight = 0.5) {
207
209
  const w = Math.max(0, Math.min(1, weight));
208
210
  const converted = new Color(other);
209
- const rgb1 = _extractSolidColorValues(this.rgb);
210
- const rgb2 = _extractSolidColorValues(converted.rgb);
211
- const blended = rgb1.map((c, i) => Math.round(c * (1 - w) + rgb2[i] * w));
212
- const blendedRGB = `rgb(${blended[0]}, ${blended[1]}, ${blended[2]})`;
213
- return new Color(blendedRGB);
211
+ const [r1, b1, g1, a1] = _extractAlphaColorValues(this.rgba);
212
+ const [r2, b2, g2, a2] = _extractAlphaColorValues(converted.rgba);
213
+ const alpha = a1 * (1 - w) + a2 * w;
214
+ // ! Original code for solid color
215
+ // const blended = rgb1.map((c, i) =>
216
+ // Math.round(c * (1 - w) + rgb2[i] * w),
217
+ // ) as ColorNumbers;
218
+ // const blendedRGB = `rgb(${blended[0]}, ${blended[1]}, ${blended[2]})`;
219
+ const blendChannel = (c1, c2) => Math.round((c1 * a1 * (1 - w) + c2 * a2 * w) / alpha);
220
+ const r = blendChannel(r1, r2);
221
+ const g = blendChannel(g1, g2);
222
+ const b = blendChannel(b1, b2);
223
+ const blended = `rgba(${r}, ${g}, ${b}, ${+alpha.toFixed(2)})`;
224
+ return new Color(blended);
214
225
  }
215
226
  /**
216
227
  * @instance Calculates the contrast ratio between this color and another color (WCAG).
@@ -240,10 +251,10 @@ export class Color {
240
251
  * @returns A new Color that is the complement of the current color.
241
252
  */
242
253
  getComplementaryColor() {
243
- const [h, s, l] = _extractSolidColorValues(this.hsl);
254
+ const [h, s, l, a] = _extractAlphaColorValues(this.hsla);
244
255
  const newHue = (h + 180) % 360;
245
256
  const newHSL = `hsl(${newHue}, ${s}%, ${l}%)`;
246
- return new Color(newHSL);
257
+ return new Color(newHSL).applyOpacity((a * 100));
247
258
  }
248
259
  /**
249
260
  * @instance Generates a color scheme of analogous colors, including the base color.
@@ -251,10 +262,11 @@ export class Color {
251
262
  * @returns An array of three Color instances: [base, left, right].
252
263
  */
253
264
  getAnalogousColors() {
254
- const [h, s, l] = _extractSolidColorValues(this.hsl);
265
+ const [h, s, l, a] = _extractAlphaColorValues(this.hsla);
255
266
  const left = `hsl(${(h + 330) % 360}, ${s}%, ${l}%)`;
256
267
  const right = `hsl(${(h + 30) % 360}, ${s}%, ${l}%)`;
257
- return [this, new Color(left), new Color(right)];
268
+ const analogous = [this, new Color(left), new Color(right)];
269
+ return analogous.map((c) => c.applyOpacity((a * 100)));
258
270
  }
259
271
  /**
260
272
  * @instance Generates a color triad scheme including the base color.
@@ -262,10 +274,11 @@ export class Color {
262
274
  * @returns An array of three Color instances: [base, triad1, triad2].
263
275
  */
264
276
  getTriadColors() {
265
- const [h, s, l] = _extractSolidColorValues(this.hsl);
277
+ const [h, s, l, a] = _extractAlphaColorValues(this.hsla);
266
278
  const c1 = `hsl(${(h + 120) % 360}, ${s}%, ${l}%)`;
267
279
  const c2 = `hsl(${(h + 240) % 360}, ${s}%, ${l}%)`;
268
- return [this, new Color(c1), new Color(c2)];
280
+ const triad = [this, new Color(c1), new Color(c2)];
281
+ return triad.map((c) => c.applyOpacity((a * 100)));
269
282
  }
270
283
  /**
271
284
  * @instance Generates a tetradic color scheme including the base color.
@@ -273,11 +286,12 @@ export class Color {
273
286
  * @returns An array of four Color instances: [base, tetrad1, tetrad2, tetrad3].
274
287
  */
275
288
  getTetradColors() {
276
- const [h, s, l] = _extractSolidColorValues(this.hsl);
289
+ const [h, s, l, a] = _extractAlphaColorValues(this.hsla);
277
290
  const c1 = `hsl(${(h + 90) % 360}, ${s}%, ${l}%)`;
278
291
  const c2 = `hsl(${(h + 180) % 360}, ${s}%, ${l}%)`;
279
292
  const c3 = `hsl(${(h + 270) % 360}, ${s}%, ${l}%)`;
280
- return [this, new Color(c1), new Color(c2), new Color(c3)];
293
+ const tetrad = [this, new Color(c1), new Color(c2), new Color(c3)];
294
+ return tetrad.map((c) => c.applyOpacity((a * 100)));
281
295
  }
282
296
  /**
283
297
  * @instance Gets the `WCAG` accessibility rating between this and another color.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "4.0.48",
3
+ "version": "4.0.50",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions for everyday development needs.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",