@oscarpalmer/atoms 0.189.0 → 0.190.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist/beacon.d.mts +51 -31
  2. package/dist/beacon.mjs +163 -141
  3. package/dist/color/index.d.mts +1 -2
  4. package/dist/color/index.mjs +2 -2
  5. package/dist/color/instance.d.mts +3 -167
  6. package/dist/color/instance.mjs +58 -217
  7. package/dist/color/misc/get.d.mts +1 -2
  8. package/dist/color/misc/get.mjs +3 -3
  9. package/dist/color/misc/index.d.mts +4 -4
  10. package/dist/color/misc/index.mjs +9 -9
  11. package/dist/color/misc/is.d.mts +1 -2
  12. package/dist/color/misc/is.mjs +5 -2
  13. package/dist/color/models.d.mts +158 -2
  14. package/dist/function/memoize.d.mts +3 -9
  15. package/dist/function/memoize.mjs +43 -90
  16. package/dist/function/once.mjs +16 -4
  17. package/dist/herald.d.mts +19 -15
  18. package/dist/herald.mjs +79 -89
  19. package/dist/index.d.mts +173 -162
  20. package/dist/index.mjs +754 -996
  21. package/dist/internal/result.d.mts +6 -6
  22. package/dist/internal/result.mjs +1 -1
  23. package/dist/internal/value/has.d.mts +2 -2
  24. package/dist/logger.d.mts +7 -7
  25. package/dist/logger.mjs +72 -51
  26. package/dist/promise/helpers.d.mts +2 -2
  27. package/dist/promise/helpers.mjs +2 -2
  28. package/dist/queue.d.mts +24 -12
  29. package/dist/queue.mjs +288 -346
  30. package/dist/string/fuzzy.d.mts +4 -13
  31. package/dist/string/fuzzy.mjs +15 -41
  32. package/package.json +2 -2
  33. package/src/beacon.ts +262 -160
  34. package/src/color/index.ts +8 -8
  35. package/src/color/instance.ts +82 -258
  36. package/src/color/misc/get.ts +4 -4
  37. package/src/color/misc/index.ts +10 -11
  38. package/src/color/misc/is.ts +4 -1
  39. package/src/color/models.ts +180 -2
  40. package/src/function/memoize.ts +74 -65
  41. package/src/function/once.ts +4 -0
  42. package/src/herald.ts +136 -94
  43. package/src/internal/result.ts +6 -6
  44. package/src/internal/value/has.ts +2 -2
  45. package/src/logger.ts +93 -42
  46. package/src/promise/helpers.ts +2 -2
  47. package/src/queue.ts +480 -375
  48. package/src/string/fuzzy.ts +36 -31
@@ -1,270 +1,94 @@
1
1
  import {formatHslColor, formatHwbColor, formatRgbColor} from './misc';
2
2
  import {getAlpha} from './misc/alpha';
3
3
  import {getColorState, setHexColor, setHSLColor, setHWBColor, setRGBColor} from './misc/state';
4
- import type {
5
- ColorState,
6
- HSLAColor,
7
- HSLColor,
8
- HWBAColor,
9
- HWBColor,
10
- RGBAColor,
11
- RGBColor,
12
- } from './models';
4
+ import type {Color, ColorState} from './models';
13
5
 
14
6
  // #region Types
15
7
 
16
- /**
17
- * A color that is represented in multiple color formats
18
- */
19
- export class Color {
20
- /**
21
- * A property to identify this as a Color instance, used for type checking
22
- *
23
- * @internal
24
- */
25
- declare private readonly $color: boolean;
26
-
27
- readonly #state: ColorState;
28
-
29
- /**
30
- * Get the alpha channel _(opacity)_ of the color as a percentage between `0` and `100`
31
- *
32
- * @returns Current alpha channel value
33
- */
34
- get alpha(): number {
35
- return this.#state.alpha.value;
36
- }
37
-
38
- /**
39
- * Set the alpha channel _(opacity)_ of the color as a percentage between `0` and `100`
40
- *
41
- * @param value New alpha channel value
42
- */
43
- set alpha(value: number) {
44
- if (typeof value === 'number' && !Number.isNaN(value)) {
45
- this.#state.alpha = getAlpha(value);
46
- }
47
- }
48
-
49
- /**
50
- * Get the color as a hex color string
51
- *
52
- * _Hex color string is returned with no `#`-prefix or alpha channel (opacity)_
53
- *
54
- * @returns Current color as a hex color string
55
- */
56
- get hex(): string {
57
- return this.#state.hex;
58
- }
59
-
60
- /**
61
- * Set the color from a hex color string
62
- *
63
- * - `#`-prefix is optional
64
- * - Alpha channel _(opacity)_ will be ignored
65
- *
66
- * @param value New hex color string
67
- */
68
- set hex(value: string) {
69
- setHexColor(this.#state, value, false);
70
- }
71
-
72
- /**
73
- * Get the color as a hex color with an alpha channel _(opacity)_
74
- *
75
- * _Hex color string is returned with alpha channel (opacity), but without `#`-prefix_
76
- *
77
- * @returns Current color as a hex color string
78
- */
79
- get hexa(): string {
80
- return `${this.#state.hex}${this.#state.alpha.hex}`;
81
- }
82
-
83
- /**
84
- * Set the color from a hex color string with an alpha channel _(opacity)_
85
- *
86
- * - `#`-prefix is optional
87
- * - Alpha channel _(opacity)_ will be respected
88
- *
89
- * @param value New hex color string
90
- */
91
- set hexa(value: string) {
92
- setHexColor(this.#state, value, true);
93
- }
94
-
95
- /**
96
- * Get the color as an _HSL_ color
97
- *
98
- * @returns Current color as an _HSL_ color
99
- */
100
- get hsl(): HSLColor {
101
- return {...this.#state.hsl};
102
- }
103
-
104
- /**
105
- * Set colors from an _HSL_ color
106
- *
107
- * @param value New _HSL_ color
108
- */
109
- set hsl(value: HSLColor) {
110
- setHSLColor(this.#state, value, false);
111
- }
112
-
113
- /**
114
- * Get the color as an _HSLA_ color
115
- *
116
- * @returns Current color as an _HSLA_ color
117
- */
118
- get hsla(): HSLAColor {
119
- return {
120
- ...this.#state.hsl,
121
- alpha: this.#state.alpha.value,
122
- };
123
- }
124
-
125
- /**
126
- * Set colors and alpha from an _HSLA_ color
127
- *
128
- * @param value New _HSLA_ color
129
- */
130
- set hsla(value: HSLAColor) {
131
- setHSLColor(this.#state, value, true);
132
- }
133
-
134
- /**
135
- * Get the color as an _HWB_ color
136
- *
137
- * @returns Current color as an _HWB_ color
138
- */
139
- get hwb(): HWBColor {
140
- return {...this.#state.hwb};
141
- }
142
-
143
- /**
144
- * Set colors from an _HWB_ color
145
- *
146
- * @param value New _HWB_ color
147
- */
148
- set hwb(value: HWBColor) {
149
- setHWBColor(this.#state, value, false);
150
- }
151
-
152
- /**
153
- * Get the color as an _HWBA_ color
154
- *
155
- * @returns Current color as an _HWBA_ color
156
- */
157
- get hwba(): HWBAColor {
158
- return {
159
- ...this.#state.hwb,
160
- alpha: this.#state.alpha.value,
161
- };
162
- }
163
-
164
- /**
165
- * Set colors and alpha from an _HWBA_ color
166
- *
167
- * @param value New _HWBA_ color
168
- */
169
- set hwba(value: HWBAColor) {
170
- setHWBColor(this.#state, value, true);
171
- }
172
-
173
- /**
174
- * Get the color as an _RGB_ color
175
- *
176
- * @returns Current color as an _RGB_ color
177
- */
178
- get rgb(): RGBColor {
179
- return {...this.#state.rgb};
180
- }
181
-
182
- /**
183
- * Set colors from an _RGB_ color
184
- *
185
- * @param value New _RGB_ color
186
- */
187
- set rgb(value: RGBColor) {
188
- setRGBColor(this.#state, value, false);
189
- }
190
-
191
- /**
192
- * Get the color as an _RGBA_ color
193
- *
194
- * @returns Current color as an _RGBA_ color
195
- */
196
- get rgba(): RGBAColor {
197
- return {
198
- ...this.#state.rgb,
199
- alpha: this.#state.alpha.value,
200
- };
201
- }
202
-
203
- /**
204
- * Set colors and alpha from an _RGBA_ color
205
- *
206
- * @param value New _RGBA_ color
207
- */
208
- set rgba(value: RGBAColor) {
209
- setRGBColor(this.#state, value, true);
210
- }
211
-
212
- constructor(value: unknown) {
213
- this.#state = getColorState(value);
214
-
215
- Object.defineProperty(this, '$color', {
216
- value: true,
217
- });
218
- }
219
-
220
- /**
221
- * Get the color as a hex string
222
- *
223
- * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
224
- * @returns Hex color string
225
- */
226
- toHexString(alpha?: boolean): string {
227
- return `#${alpha === true ? this.hexa : this.hex}`;
228
- }
8
+ export function color(value: unknown): Color {
9
+ const state = getColorState(value);
10
+
11
+ const instance = {
12
+ toHexString: (alpha?: boolean) =>
13
+ `#${alpha === true ? (instance as Color).hexa : (instance as Color).hex}`,
14
+ toHslString: (alpha?: boolean) => formatHslColor(state, alpha === true),
15
+ toHwbString: (alpha?: boolean) => formatHwbColor(state, alpha === true),
16
+ toRgbString: (alpha?: boolean) => formatRgbColor(state, alpha === true),
17
+ toString: () => (instance as Color).toHexString(),
18
+ };
19
+
20
+ Object.defineProperties(instance, {
21
+ $color: {
22
+ enumerable: false,
23
+ get: () => true,
24
+ },
25
+ alpha: {
26
+ enumerable: true,
27
+ get: () => state.alpha.value,
28
+ set: (value: number) => {
29
+ if (typeof value === 'number' && !Number.isNaN(value)) {
30
+ state.alpha = getAlpha(value);
31
+ }
32
+ },
33
+ },
34
+ hex: getProperty('hex', state, false),
35
+ hexa: getProperty('hex', state, true),
36
+ hsl: getProperty('hsl', state, false),
37
+ hsla: getProperty('hsl', state, true),
38
+ hwb: getProperty('hwb', state, false),
39
+ hwba: getProperty('hwb', state, true),
40
+ rgb: getProperty('rgb', state, false),
41
+ rgba: getProperty('rgb', state, true),
42
+ });
43
+
44
+ return Object.freeze(instance) as Color;
45
+ }
229
46
 
230
- /**
231
- * Get the color as an _HSL(A)_ string
232
- *
233
- * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
234
- * @returns _HSL(A)_ color string
235
- */
236
- toHslString(alpha?: boolean): string {
237
- return formatHslColor(this, alpha === true);
238
- }
47
+ function getHex(state: ColorState, alpha: boolean): string {
48
+ return alpha ? `${state.hex}${state.alpha.hex}` : state.hex;
49
+ }
239
50
 
240
- /**
241
- * Get the color as an _HWB(A)_ string
242
- *
243
- * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
244
- * @returns _HWB(A)_ color string
245
- */
246
- toHwbString(alpha?: boolean): string {
247
- return formatHwbColor(this, alpha === true);
248
- }
51
+ function getProperty(
52
+ space: keyof typeof setters,
53
+ state: ColorState,
54
+ alpha: boolean,
55
+ ): PropertyDescriptor {
56
+ const setter = setters[space];
57
+
58
+ return {
59
+ enumerable: true,
60
+ get: () => {
61
+ const value = state[space];
62
+
63
+ if (typeof value === 'string') {
64
+ return getHex(state, alpha);
65
+ }
66
+
67
+ return alpha
68
+ ? {
69
+ ...value,
70
+ alpha: state.alpha.value,
71
+ }
72
+ : {...value};
73
+ },
74
+ set: (value: unknown) => {
75
+ setter(state, value, alpha);
76
+ },
77
+ };
78
+ }
249
79
 
250
- /**
251
- * Get the color as an _RGB(A)_ string
252
- *
253
- * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
254
- * @returns _RGB(A)_ color string
255
- */
256
- toRgbString(alpha?: boolean): string {
257
- return formatRgbColor(this, alpha === true);
258
- }
80
+ // #endregion
259
81
 
260
- /**
261
- * Get the color as a hex color string
262
- *
263
- * @returns Hex color string
264
- */
265
- toString(): string {
266
- return this.toHexString();
267
- }
268
- }
82
+ // #region Variables
83
+
84
+ const setters: Record<
85
+ 'hex' | 'hsl' | 'hwb' | 'rgb',
86
+ (state: ColorState, value: any, alpha: boolean) => void
87
+ > = {
88
+ hex: setHexColor,
89
+ hsl: setHSLColor,
90
+ hwb: setHWBColor,
91
+ rgb: setRGBColor,
92
+ };
269
93
 
270
94
  // #endregion
@@ -16,8 +16,8 @@ import {
16
16
  SRGB_LUMINANCE_RED,
17
17
  SRGB_LUMINANCE_THRESHOLD,
18
18
  } from '../constants';
19
- import {Color} from '../instance';
20
- import type {HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor} from '../models';
19
+ import {color} from '../instance';
20
+ import type {Color, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor} from '../models';
21
21
  import {getColorState} from './state';
22
22
 
23
23
  // #region Functions
@@ -79,9 +79,9 @@ export function getForegroundColor(value: unknown, hex?: unknown): string | Colo
79
79
  SRGB_LUMINANCE_BLUE * values[0];
80
80
 
81
81
  // Rudimentary and ureliable?; implement APCA for more reliable results?
82
- const color = luminance > SRGB_LUMINANCE_THRESHOLD ? HEX_BLACK : HEX_WHITE;
82
+ const foreground = luminance > SRGB_LUMINANCE_THRESHOLD ? HEX_BLACK : HEX_WHITE;
83
83
 
84
- return hex === true ? color : new Color(color);
84
+ return hex === true ? foreground : color(foreground);
85
85
  }
86
86
 
87
87
  /**
@@ -1,28 +1,27 @@
1
- import {round} from '../../internal/math/misc';
2
- import type {Color} from '../instance';
1
+ import type {ColorState} from '../models';
3
2
 
4
3
  // #region Functions
5
4
 
6
- export function formatHslColor(color: Color, alpha: boolean): string {
7
- const {hue, lightness, saturation} = color.hsl;
5
+ export function formatHslColor(state: ColorState, alpha: boolean): string {
6
+ const {hue, lightness, saturation} = state.hsl;
8
7
 
9
- const suffix = alpha ? ` / ${color.alpha}%` : '';
8
+ const suffix = alpha ? ` / ${state.alpha.value}%` : '';
10
9
 
11
10
  return `hsl(${hue}deg ${saturation}% ${lightness}%${suffix})`;
12
11
  }
13
12
 
14
- export function formatHwbColor(color: Color, alpha: boolean): string {
15
- const {blackness, hue, whiteness} = color.hwb;
13
+ export function formatHwbColor(state: ColorState, alpha: boolean): string {
14
+ const {blackness, hue, whiteness} = state.hwb;
16
15
 
17
- const suffix = alpha ? ` / ${color.alpha}%` : '';
16
+ const suffix = alpha ? ` / ${state.alpha.value}%` : '';
18
17
 
19
18
  return `hwb(${hue}deg ${whiteness}% ${blackness}%${suffix})`;
20
19
  }
21
20
 
22
- export function formatRgbColor(color: Color, alpha: boolean): string {
23
- const {blue, green, red} = color.rgb;
21
+ export function formatRgbColor(state: ColorState, alpha: boolean): string {
22
+ const {blue, green, red} = state.rgb;
24
23
 
25
- const suffix = alpha ? ` / ${color.alpha}%` : '';
24
+ const suffix = alpha ? ` / ${state.alpha.value}%` : '';
26
25
 
27
26
  return `rgb(${red} ${green} ${blue}${suffix})`;
28
27
  }
@@ -4,6 +4,7 @@ import {
4
4
  ALPHA_NONE_VALUE,
5
5
  EXPRESSION_HEX_LONG,
6
6
  EXPRESSION_HEX_SHORT,
7
+ EXPRESSION_PREFIX,
7
8
  KEYS_HSL,
8
9
  KEYS_HSLA,
9
10
  KEYS_HWB,
@@ -109,7 +110,9 @@ export function isHexColor(value: unknown, alpha?: boolean): value is string {
109
110
  }
110
111
 
111
112
  if (alpha === false) {
112
- return value.length === LENGTH_SHORT || value.length === LENGTH_LONG;
113
+ const unprefixed = value.replace(EXPRESSION_PREFIX, '');
114
+
115
+ return unprefixed.length === LENGTH_SHORT || unprefixed.length === LENGTH_LONG;
113
116
  }
114
117
 
115
118
  return true;
@@ -5,6 +5,186 @@ export type Alpha = {
5
5
  value: number;
6
6
  };
7
7
 
8
+ /**
9
+ * A color that is represented in multiple color formats
10
+ */
11
+ export type Color = {
12
+ /**
13
+ * Get the alpha channel _(opacity)_ of the color as a percentage between `0` and `100`
14
+ *
15
+ * @returns Current alpha channel value
16
+ */
17
+ get alpha(): number;
18
+
19
+ /**
20
+ * Set the alpha channel _(opacity)_ of the color as a percentage between `0` and `100`
21
+ *
22
+ * @param value New alpha channel value
23
+ */
24
+ set alpha(value: number);
25
+
26
+ /**
27
+ * Get the color as a hex color string
28
+ *
29
+ * _Hex color string is returned with no `#`-prefix or alpha channel (opacity)_
30
+ *
31
+ * @returns Current color as a hex color string
32
+ */
33
+ get hex(): string;
34
+
35
+ /**
36
+ * Set the color from a hex color string
37
+ *
38
+ * - `#`-prefix is optional
39
+ * - Alpha channel _(opacity)_ will be ignored
40
+ *
41
+ * @param value New hex color string
42
+ */
43
+ set hex(value: string);
44
+
45
+ /**
46
+ * Get the color as a hex color with an alpha channel _(opacity)_
47
+ *
48
+ * _Hex color string is returned with alpha channel (opacity), but without `#`-prefix_
49
+ *
50
+ * @returns Current color as a hex color string
51
+ */
52
+ get hexa(): string;
53
+
54
+ /**
55
+ * Set the color from a hex color string with an alpha channel _(opacity)_
56
+ *
57
+ * - `#`-prefix is optional
58
+ * - Alpha channel _(opacity)_ will be respected
59
+ *
60
+ * @param value New hex color string
61
+ */
62
+ set hexa(value: string);
63
+
64
+ /**
65
+ * Get the color as an _HSL_ color
66
+ *
67
+ * @returns Current color as an _HSL_ color
68
+ */
69
+ get hsl(): HSLColor;
70
+
71
+ /**
72
+ * Set colors from an _HSL_ color
73
+ *
74
+ * @param value New _HSL_ color
75
+ */
76
+ set hsl(value: HSLColor);
77
+
78
+ /**
79
+ * Get the color as an _HSLA_ color
80
+ *
81
+ * @returns Current color as an _HSLA_ color
82
+ */
83
+ get hsla(): HSLAColor;
84
+
85
+ /**
86
+ * Set colors and alpha from an _HSLA_ color
87
+ *
88
+ * @param value New _HSLA_ color
89
+ */
90
+ set hsla(value: HSLAColor);
91
+
92
+ /**
93
+ * Get the color as an _HWB_ color
94
+ *
95
+ * @returns Current color as an _HWB_ color
96
+ */
97
+ get hwb(): HWBColor;
98
+
99
+ /**
100
+ * Set colors from an _HWB_ color
101
+ *
102
+ * @param value New _HWB_ color
103
+ */
104
+ set hwb(value: HWBColor);
105
+
106
+ /**
107
+ * Get the color as an _HWBA_ color
108
+ *
109
+ * @returns Current color as an _HWBA_ color
110
+ */
111
+ get hwba(): HWBAColor;
112
+
113
+ /**
114
+ * Set colors and alpha from an _HWBA_ color
115
+ *
116
+ * @param value New _HWBA_ color
117
+ */
118
+ set hwba(value: HWBAColor);
119
+
120
+ /**
121
+ * Get the color as an _RGB_ color
122
+ *
123
+ * @returns Current color as an _RGB_ color
124
+ */
125
+ get rgb(): RGBColor;
126
+
127
+ /**
128
+ * Set colors from an _RGB_ color
129
+ *
130
+ * @param value New _RGB_ color
131
+ */
132
+ set rgb(value: RGBColor);
133
+
134
+ /**
135
+ * Get the color as an _RGBA_ color
136
+ *
137
+ * @returns Current color as an _RGBA_ color
138
+ */
139
+ get rgba(): RGBAColor;
140
+
141
+ /**
142
+ * Set colors and alpha from an _RGBA_ color
143
+ *
144
+ * @param value New _RGBA_ color
145
+ */
146
+ set rgba(value: RGBAColor);
147
+
148
+ /**
149
+ * Get the color as a hex string
150
+ *
151
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
152
+ * @returns Hex color string
153
+ */
154
+ toHexString(alpha?: boolean): string;
155
+
156
+ /**
157
+ * Get the color as an _HSL(A)_ string
158
+ *
159
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
160
+ * @returns _HSL(A)_ color string
161
+ */
162
+ toHslString(alpha?: boolean): string;
163
+
164
+ /**
165
+ * Get the color as an _HWB(A)_ string
166
+ *
167
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
168
+ * @returns _HWB(A)_ color string
169
+ */
170
+ toHwbString(alpha?: boolean): string;
171
+
172
+ /**
173
+ * Get the color as an _RGB(A)_ string
174
+ *
175
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
176
+ * @returns _RGB(A)_ color string
177
+ */
178
+ toRgbString(alpha?: boolean): string;
179
+
180
+ /**
181
+ * Get the color as a hex color string
182
+ *
183
+ * @returns Hex color string
184
+ */
185
+ toString(): string;
186
+ };
187
+
8
188
  type ColorWithAlpha = {
9
189
  /**
10
190
  * Alpha channel _(opacity)_ of the color _(in percentage; 0-100)_
@@ -77,8 +257,6 @@ export type RGBColor = {
77
257
 
78
258
  export type ColorProperty = 'alpha' | keyof HSLColor | keyof HWBColor | keyof RGBColor;
79
259
 
80
- export type ColorSpace = 'hsl' | 'rgb';
81
-
82
260
  export type ColorState = {
83
261
  alpha: Alpha;
84
262
  hex: string;