@oscarpalmer/atoms 0.203.0 → 0.204.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 (75) hide show
  1. package/dist/array/filter.mjs +0 -1
  2. package/dist/array/find.mjs +0 -1
  3. package/dist/array/first.mjs +0 -1
  4. package/dist/array/group-by.mjs +0 -1
  5. package/dist/array/last.mjs +0 -1
  6. package/dist/array/move.mjs +0 -4
  7. package/dist/array/select.mjs +0 -1
  8. package/dist/array/sort.mjs +6 -15
  9. package/dist/array/swap.mjs +0 -1
  10. package/dist/array/to-map.mjs +0 -1
  11. package/dist/array/to-record.mjs +0 -1
  12. package/dist/beacon.mjs +36 -36
  13. package/dist/color/instance.mjs +42 -84
  14. package/dist/color/misc/alpha.d.mts +3 -1
  15. package/dist/color/misc/alpha.mjs +8 -2
  16. package/dist/color/misc/is.mjs +5 -4
  17. package/dist/color/misc/state.d.mts +29 -4
  18. package/dist/color/misc/state.mjs +18 -40
  19. package/dist/color/space/hex.d.mts +5 -1
  20. package/dist/color/space/hex.mjs +21 -2
  21. package/dist/color/space/hsl.d.mts +6 -2
  22. package/dist/color/space/hsl.mjs +25 -4
  23. package/dist/color/space/hwb.d.mts +6 -2
  24. package/dist/color/space/hwb.mjs +24 -3
  25. package/dist/color/space/rgb.d.mts +6 -2
  26. package/dist/color/space/rgb.mjs +41 -20
  27. package/dist/function/limit.mjs +0 -2
  28. package/dist/function/once.mjs +30 -36
  29. package/dist/function/retry.mjs +0 -1
  30. package/dist/index.d.mts +5 -5
  31. package/dist/index.mjs +488 -547
  32. package/dist/internal/{abort.d.mts → aborter.d.mts} +1 -1
  33. package/dist/internal/{abort.mjs → aborter.mjs} +1 -1
  34. package/dist/internal/array/get.mjs +3 -2
  35. package/dist/internal/array/index-of.mjs +0 -1
  36. package/dist/internal/function/assert.d.mts +1 -1
  37. package/dist/internal/function/assert.mjs +0 -7
  38. package/dist/internal/function/limit.mjs +10 -14
  39. package/dist/internal/function/memoize.mjs +22 -20
  40. package/dist/internal/function/work.mjs +0 -2
  41. package/dist/internal/herald.mjs +14 -15
  42. package/dist/internal/result/misc.mjs +2 -5
  43. package/dist/internal/subscription.mjs +24 -22
  44. package/dist/internal/value/compare.d.mts +1 -1
  45. package/dist/internal/value/compare.mjs +0 -5
  46. package/dist/internal/value/equal.d.mts +1 -1
  47. package/dist/internal/value/equal.mjs +4 -12
  48. package/dist/internal/value/handlers.d.mts +1 -1
  49. package/dist/internal/value/handlers.mjs +5 -7
  50. package/dist/internal/value/has.mjs +0 -1
  51. package/dist/internal/value/misc.d.mts +1 -1
  52. package/dist/internal/value/misc.mjs +4 -3
  53. package/dist/internal/value/partial.mjs +2 -1
  54. package/dist/internal/value/set.mjs +2 -1
  55. package/dist/logger.mjs +46 -45
  56. package/dist/promise/delay.mjs +1 -1
  57. package/dist/promise/helpers.mjs +4 -3
  58. package/dist/promise/index.mjs +1 -2
  59. package/dist/promise/misc.d.mts +1 -1
  60. package/dist/promise/models.d.mts +1 -1
  61. package/dist/promise/timed.mjs +1 -1
  62. package/dist/queue.mjs +86 -80
  63. package/dist/result/index.mjs +0 -1
  64. package/dist/result/match.mjs +0 -1
  65. package/dist/result/work/flow.mjs +0 -1
  66. package/dist/result/work/pipe.mjs +0 -1
  67. package/dist/string/fuzzy.mjs +18 -21
  68. package/dist/string/normalize.mjs +2 -3
  69. package/dist/string/template.mjs +2 -3
  70. package/dist/value/clone.d.mts +1 -1
  71. package/dist/value/clone.mjs +4 -12
  72. package/dist/value/freeze.mjs +0 -4
  73. package/dist/value/merge.mjs +5 -7
  74. package/dist/value/transform.mjs +1 -2
  75. package/package.json +2 -2
@@ -7,6 +7,5 @@ function filter(array, ...parameters) {
7
7
  return findValues("all", array, parameters).matched;
8
8
  }
9
9
  filter.remove = exclude;
10
- Object.defineProperty(filter, "remove", { value: exclude });
11
10
  //#endregion
12
11
  export { exclude, filter };
@@ -7,6 +7,5 @@ function findLast(array, ...parameters) {
7
7
  return findValue(FIND_VALUE_ITEM, array, parameters, true);
8
8
  }
9
9
  find.last = findLast;
10
- Object.defineProperty(find, "last", { value: findLast });
11
10
  //#endregion
12
11
  export { find, findLast };
@@ -7,6 +7,5 @@ function firstOrDefault(array, defaultValue, ...parameters) {
7
7
  return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, false);
8
8
  }
9
9
  first.default = firstOrDefault;
10
- Object.defineProperty(first, "default", { value: firstOrDefault });
11
10
  //#endregion
12
11
  export { first, firstOrDefault };
@@ -7,6 +7,5 @@ function groupArraysBy(array, first, second) {
7
7
  return groupValues(array, first, second, true);
8
8
  }
9
9
  groupBy.arrays = groupArraysBy;
10
- Object.defineProperty(groupBy, "arrays", { value: groupArraysBy });
11
10
  //#endregion
12
11
  export { groupArraysBy, groupBy };
@@ -7,6 +7,5 @@ function lastOrDefault(array, defaultValue, ...parameters) {
7
7
  return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, true);
8
8
  }
9
9
  last.default = lastOrDefault;
10
- Object.defineProperty(last, "default", { value: lastOrDefault });
11
10
  //#endregion
12
11
  export { last, lastOrDefault };
@@ -72,9 +72,5 @@ function moveToIndex(array, value, index, key) {
72
72
  }
73
73
  move.indices = moveIndices;
74
74
  move.toIndex = moveToIndex;
75
- Object.defineProperties(move, {
76
- indices: { value: moveIndices },
77
- toIndex: { value: moveToIndex }
78
- });
79
75
  //#endregion
80
76
  export { move, moveIndices, moveToIndex };
@@ -18,6 +18,5 @@ function selectValues(array, parameters) {
18
18
  }).matched;
19
19
  }
20
20
  select.reverse = reverseSelect;
21
- Object.defineProperty(select, "reverse", { value: reverseSelect });
22
21
  //#endregion
23
22
  export { reverseSelect, select };
@@ -1,16 +1,12 @@
1
1
  import { isPlainObject } from "../internal/is.mjs";
2
2
  import { compare } from "../internal/value/compare.mjs";
3
3
  //#region src/array/sort.ts
4
- const SORTER_PROPERTY = "$sorter";
5
4
  function Sorter(sorters) {
6
- Object.defineProperty(this, SORTER_SYMBOL, { value: sorters });
5
+ this[SORTER_SYMBOL] = sorters;
7
6
  }
8
- Object.defineProperties(Sorter.prototype, {
9
- [SORTER_PROPERTY]: { value: true },
10
- index: { value: getSortedArrayIndex },
11
- is: { value: isSortedArray },
12
- sort: { value: sortArray }
13
- });
7
+ Sorter.prototype.index = getSortedArrayIndex;
8
+ Sorter.prototype.is = isSortedArray;
9
+ Sorter.prototype.sort = sortArray;
14
10
  function getComparisonSorter(callback, modifier) {
15
11
  return {
16
12
  modifier,
@@ -22,7 +18,7 @@ function getComparisonSorter(callback, modifier) {
22
18
  function getComparisonValue(first, second, sorters, length) {
23
19
  for (let index = 0; index < length; index += 1) {
24
20
  const sorter = sorters[index];
25
- const values = [sorter.get ? sorter.value(first) : first, sorter.get ? sorter.value(second) : second];
21
+ const values = [sorter.get ? sorter.value?.(first) : first, sorter.get ? sorter.value?.(second) : second];
26
22
  const comparison = (sorter.comparison?.complex?.(first, values[0], second, values[1]) ?? sorter.comparison?.simple?.(values[0], values[1]) ?? compare(values[0], values[1])) * sorter.modifier;
27
23
  if (comparison !== 0) return comparison;
28
24
  }
@@ -137,7 +133,7 @@ const SORT_PEEK_PERCENTAGE = 10;
137
133
  const SORT_THRESHOLD = 100;
138
134
  const SORT_DIRECTION_ASCENDING = "ascending";
139
135
  const SORT_DIRECTION_DESCENDING = "descending";
140
- const SORTER_SYMBOL = Symbol(SORTER_PROPERTY);
136
+ const SORTER_SYMBOL = Symbol("sorter");
141
137
  const modifiers = {
142
138
  [SORT_DIRECTION_ASCENDING]: 1,
143
139
  [SORT_DIRECTION_DESCENDING]: -1
@@ -145,10 +141,5 @@ const modifiers = {
145
141
  sort.getIndex = getSortedIndex;
146
142
  sort.initialize = initializeSorter;
147
143
  sort.is = isSorted;
148
- Object.defineProperties(sort, {
149
- getIndex: { value: getSortedIndex },
150
- initialize: { value: initializeSorter },
151
- is: { value: isSorted }
152
- });
153
144
  //#endregion
154
145
  export { SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, getSortedIndex, initializeSorter, isSorted, sort };
@@ -58,6 +58,5 @@ function swapValues(array, from, to, key) {
58
58
  return swapIndices(array, first, second);
59
59
  }
60
60
  swap.indices = swapIndices;
61
- Object.defineProperty(swap, "indices", { value: swapIndices });
62
61
  //#endregion
63
62
  export { swap };
@@ -24,6 +24,5 @@ function toMapArrays(array, first, second) {
24
24
  return getMapValues(array, first, second, true);
25
25
  }
26
26
  toMap.arrays = toMapArrays;
27
- Object.defineProperty(toMap, "arrays", { value: toMapArrays });
28
27
  //#endregion
29
28
  export { toMap, toMapArrays };
@@ -7,6 +7,5 @@ function toRecordArrays(array, first, second) {
7
7
  return groupValues(array, first, second, true);
8
8
  }
9
9
  toRecord.arrays = toRecordArrays;
10
- Object.defineProperty(toRecord, "arrays", { value: toRecordArrays });
11
10
  //#endregion
12
11
  export { toRecord, toRecordArrays };
package/dist/beacon.mjs CHANGED
@@ -6,54 +6,43 @@ const BEACON_PROPERTY = "$beacon";
6
6
  const BEACON_NAME = "beacon";
7
7
  const BEACON_OBSERVABLE = "observable";
8
8
  function Beacon(value, options) {
9
- Object.defineProperty(this, BEACON_SYMBOL, { value: {
9
+ this[BEACON_SYMBOL] = {
10
10
  value,
11
11
  active: true,
12
12
  options: createBeaconOptions(options)
13
- } });
13
+ };
14
14
  }
15
+ Beacon.prototype[BEACON_PROPERTY] = BEACON_NAME;
16
+ Beacon.prototype.deactivate = deactivateBeacon;
17
+ Beacon.prototype.emit = emitBeaconValue;
18
+ Beacon.prototype.error = emitBeaconError;
19
+ Beacon.prototype.finish = finishBeacon;
15
20
  Object.defineProperties(Beacon.prototype, {
16
- [BEACON_PROPERTY]: { value: BEACON_NAME },
17
21
  active: {
18
22
  enumerable: true,
19
- get() {
20
- return this[BEACON_SYMBOL].active;
21
- }
23
+ get: getBeaconActive
22
24
  },
23
- deactivate: { value: deactivateBeacon },
24
- emit: { value: emitBeaconValue },
25
- error: { value: emitBeaconError },
26
- finish: { value: finishBeacon },
27
25
  observable: {
28
26
  enumerable: true,
29
- get() {
30
- return getObservable(this[BEACON_SYMBOL]);
31
- }
27
+ get: getBeaconObservable
32
28
  },
33
29
  value: {
34
30
  enumerable: true,
35
- get() {
36
- return this[BEACON_SYMBOL].value;
37
- }
31
+ get: getBeaconValue
38
32
  }
39
33
  });
40
34
  function Observable(beacon) {
41
- Object.defineProperty(this, BEACON_SYMBOL, { value: {
35
+ this[BEACON_SYMBOL] = {
42
36
  beacon,
43
37
  active: beacon.active
44
- } });
38
+ };
45
39
  }
46
- Object.defineProperties(Observable.prototype, {
47
- [BEACON_PROPERTY]: { value: BEACON_OBSERVABLE },
48
- active: {
49
- enumerable: true,
50
- get() {
51
- const state = this[BEACON_SYMBOL];
52
- return state.beacon.active && state.active;
53
- }
54
- },
55
- deactivate: { value: deactiveateObservable },
56
- subscribe: { value: subscribeToObservable }
40
+ Observable.prototype[BEACON_PROPERTY] = BEACON_OBSERVABLE;
41
+ Observable.prototype.deactivate = deactiveateObservable;
42
+ Observable.prototype.subscribe = subscribeToObservable;
43
+ Object.defineProperty(Observable.prototype, "active", {
44
+ enumerable: true,
45
+ get: getObservableActive
57
46
  });
58
47
  /**
59
48
  * Create a new beacon
@@ -78,8 +67,7 @@ function closeBeacon(state, emit) {
78
67
  }
79
68
  function createBeaconOptions(input) {
80
69
  const options = isPlainObject(input) ? input : {};
81
- options.equal = typeof options.equal === "function" ? options.equal : Object.is;
82
- return options;
70
+ return { equal: typeof options.equal === "function" ? options.equal : Object.is };
83
71
  }
84
72
  function createObserver(first, second, third) {
85
73
  let observer = { next: noop };
@@ -89,9 +77,10 @@ function createObserver(first, second, third) {
89
77
  complete: getObservableCallback(third)
90
78
  };
91
79
  else if (typeof first === "object") {
92
- observer.complete = getObservableCallback(first?.complete);
93
- observer.error = getObservableCallback(first?.error);
94
- observer.next = getObservableCallback(first?.next);
80
+ const object = first;
81
+ observer.complete = getObservableCallback(object?.complete);
82
+ observer.error = getObservableCallback(object?.error);
83
+ observer.next = getObservableCallback(object?.next);
95
84
  }
96
85
  return observer;
97
86
  }
@@ -110,11 +99,22 @@ function emitBeaconValue(value, finish) {
110
99
  function finishBeacon() {
111
100
  closeBeacon(this[BEACON_SYMBOL], true);
112
101
  }
113
- function getObservable(state) {
102
+ function getBeaconActive() {
103
+ return this[BEACON_SYMBOL].active;
104
+ }
105
+ function getBeaconObservable() {
106
+ const state = this[BEACON_SYMBOL];
114
107
  if (!state.active) throw new Error(BEACON_MESSAGE_RETRIEVE);
115
108
  state.observable ??= new Observable(state);
116
109
  return state.observable;
117
110
  }
111
+ function getBeaconValue() {
112
+ return this[BEACON_SYMBOL].value;
113
+ }
114
+ function getObservableActive() {
115
+ const state = this[BEACON_SYMBOL];
116
+ return state.beacon.active && state.active;
117
+ }
118
118
  function getObservableCallback(value) {
119
119
  return typeof value === "function" ? value : noop;
120
120
  }
@@ -128,7 +128,7 @@ function isBeacon(value) {
128
128
  return isBeaconInstance(BEACON_NAME, value);
129
129
  }
130
130
  function isBeaconInstance(name, value) {
131
- return typeof value === "object" && value !== null && value[BEACON_PROPERTY] === name;
131
+ return typeof value === "object" && value !== null && BEACON_PROPERTY in value && value[BEACON_PROPERTY] === name;
132
132
  }
133
133
  /**
134
134
  * Is the value a beacon subscription?
@@ -1,134 +1,95 @@
1
1
  import "../internal/subscription.mjs";
2
2
  import { herald } from "../internal/herald.mjs";
3
3
  import { COLOR_NAME, COLOR_PROPERTY, COLOR_SYMBOL, COLOR_TYPE, colorSubscription } from "./constants.mjs";
4
- import { getAlpha } from "./misc/alpha.mjs";
5
- import { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor } from "./misc/state.mjs";
4
+ import { getColorAlpha, setColorAlpha } from "./misc/alpha.mjs";
5
+ import { getRgbColor, getRgbaColor, setRgbValue, setRgbaValue } from "./space/rgb.mjs";
6
+ import { getHexColor, getHexaColor, setHexValue, setHexaValue } from "./space/hex.mjs";
7
+ import { getHslValue, getHslaValue, setHslValue, setHslaValue } from "./space/hsl.mjs";
8
+ import { getHwbValue, getHwbaValue, setHwbValue, setHwbaValue } from "./space/hwb.mjs";
9
+ import { getColorState } from "./misc/state.mjs";
6
10
  import { formatHexColor, formatHslColor, formatHwbColor, formatRgbColor } from "./misc/format.mjs";
7
11
  //#region src/color/instance.ts
8
12
  function Color(value) {
9
- Object.defineProperty(this, COLOR_SYMBOL, { value: {
13
+ this[COLOR_SYMBOL] = {
10
14
  changes: herald({
11
15
  names: [COLOR_TYPE.wildcard, ...COLOR_TYPE.all],
12
16
  property: colorSubscription,
13
17
  onCreate: (event, callback) => onCreateSubscription(this, event, callback)
14
18
  }),
15
19
  values: getColorState(value)
16
- } });
20
+ };
17
21
  }
22
+ Color.prototype[COLOR_PROPERTY] = COLOR_NAME;
23
+ Color.prototype.subscribe = subscribeToColor;
24
+ Color.prototype.toHexString = formatHexColor;
25
+ Color.prototype.toHslString = formatHslColor;
26
+ Color.prototype.toHwbString = formatHwbColor;
27
+ Color.prototype.toRgbString = formatRgbColor;
28
+ Color.prototype.toString = formatHexColor;
29
+ Color.prototype.unsubscribe = unsubscribeFromColor;
18
30
  Object.defineProperties(Color.prototype, {
19
- [COLOR_PROPERTY]: { value: COLOR_NAME },
20
31
  alpha: {
21
32
  enumerable: true,
22
- get() {
23
- return this[COLOR_SYMBOL].values.alpha.value;
24
- },
25
- set(value) {
26
- setAlphaValue(this[COLOR_SYMBOL].values, value);
27
- }
33
+ get: getColorAlpha,
34
+ set: setColorAlpha
28
35
  },
29
36
  changes: {
30
37
  enumerable: true,
31
- get() {
32
- return this[COLOR_SYMBOL].changes.events;
33
- }
38
+ get: getColorChanges
34
39
  },
35
40
  hex: {
36
41
  enumerable: true,
37
- get() {
38
- return getColorValue(this, COLOR_TYPE.hex, false);
39
- },
40
- set(value) {
41
- setHexColor(this, value, false);
42
- }
42
+ get: getHexColor,
43
+ set: setHexValue
43
44
  },
44
45
  hexa: {
45
46
  enumerable: true,
46
- get() {
47
- return getColorValue(this, COLOR_TYPE.hex, true);
48
- },
49
- set(value) {
50
- setHexColor(this, value, true);
51
- }
47
+ get: getHexaColor,
48
+ set: setHexaValue
52
49
  },
53
50
  hsl: {
54
51
  enumerable: true,
55
- get() {
56
- return getColorValue(this, COLOR_TYPE.hsl, false);
57
- },
58
- set(value) {
59
- setHSLColor(this, value, false);
60
- }
52
+ get: getHslValue,
53
+ set: setHslValue
61
54
  },
62
55
  hsla: {
63
56
  enumerable: true,
64
- get() {
65
- return getColorValue(this, COLOR_TYPE.hsl, true);
66
- },
67
- set(value) {
68
- setHSLColor(this, value, true);
69
- }
57
+ get: getHslaValue,
58
+ set: setHslaValue
70
59
  },
71
60
  hwb: {
72
61
  enumerable: true,
73
- get() {
74
- return getColorValue(this, COLOR_TYPE.hwb, false);
75
- },
76
- set(value) {
77
- setHWBColor(this, value, false);
78
- }
62
+ get: getHwbValue,
63
+ set: setHwbValue
79
64
  },
80
65
  hwba: {
81
66
  enumerable: true,
82
- get() {
83
- return getColorValue(this, COLOR_TYPE.hwb, true);
84
- },
85
- set(value) {
86
- setHWBColor(this, value, true);
87
- }
67
+ get: getHwbaValue,
68
+ set: setHwbaValue
88
69
  },
89
70
  origin: {
90
71
  enumerable: true,
91
- get() {
92
- return this[COLOR_SYMBOL].values.origin;
93
- }
72
+ get: getOrigin
94
73
  },
95
74
  rgb: {
96
75
  enumerable: true,
97
- get() {
98
- return getColorValue(this, COLOR_TYPE.rgb, false);
99
- },
100
- set(value) {
101
- setRGBColor(this, value, false);
102
- }
76
+ get: getRgbColor,
77
+ set: setRgbValue
103
78
  },
104
79
  rgba: {
105
80
  enumerable: true,
106
- get() {
107
- return getColorValue(this, COLOR_TYPE.rgb, true);
108
- },
109
- set(value) {
110
- setRGBColor(this, value, true);
111
- }
112
- },
113
- subscribe: { value: subscribeToColor },
114
- toHexString: { value: formatHexColor },
115
- toHslString: { value: formatHslColor },
116
- toHwbString: { value: formatHwbColor },
117
- toRgbString: { value: formatRgbColor },
118
- toString: { value: formatHexColor },
119
- unsubscribe: { value: unsubscribeFromColor }
81
+ get: getRgbaColor,
82
+ set: setRgbaValue
83
+ }
120
84
  });
121
85
  function color(value) {
122
86
  return new Color(value);
123
87
  }
124
- function getColorValue(instance, type, alpha) {
125
- const { values } = instance[COLOR_SYMBOL];
126
- const value = getColorFromState(values, type);
127
- if (typeof value === "string") return alpha ? `${value}${values.alpha.hex}` : value;
128
- return alpha ? {
129
- ...value,
130
- alpha: values.alpha.value
131
- } : { ...value };
88
+ function getColorChanges() {
89
+ return this[COLOR_SYMBOL].changes.events;
90
+ }
91
+ function getOrigin() {
92
+ return this[COLOR_SYMBOL].values.origin;
132
93
  }
133
94
  function onCreateSubscription(instance, event, callback) {
134
95
  let value;
@@ -136,9 +97,6 @@ function onCreateSubscription(instance, event, callback) {
136
97
  else value = instance[event];
137
98
  callback(value);
138
99
  }
139
- function setAlphaValue(state, value) {
140
- if (typeof value === "number" && !Number.isNaN(value)) state.alpha = getAlpha(value, false);
141
- }
142
100
  function subscribeToColor(callback, signal) {
143
101
  const { changes } = this[COLOR_SYMBOL];
144
102
  return changes.subscribe(COLOR_TYPE.wildcard, callback, signal);
@@ -1,6 +1,8 @@
1
- import { Alpha } from "../models.mjs";
1
+ import { Alpha, InternalColor } from "../models.mjs";
2
2
  //#region src/color/misc/alpha.d.ts
3
3
  export declare function getAlpha(value: unknown, hex: boolean): Alpha;
4
+ export declare function getColorAlpha(this: InternalColor): number;
4
5
  export declare function getAlphaHexadecimal(value: number): string;
5
6
  export declare function getAlphaValue(original: number): number;
7
+ export declare function setColorAlpha(this: InternalColor, value: unknown): void;
6
8
  //#endregion
@@ -1,5 +1,5 @@
1
1
  import { clamp } from "../../internal/number.mjs";
2
- import { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_MAX } from "../constants.mjs";
2
+ import { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_MAX, COLOR_SYMBOL } from "../constants.mjs";
3
3
  import { round } from "../../internal/math/misc.mjs";
4
4
  //#region src/color/misc/alpha.ts
5
5
  function getAlpha(value, hex) {
@@ -11,6 +11,9 @@ function getAlpha(value, hex) {
11
11
  };
12
12
  return getAlphaFromValue(Number.parseFloat(value));
13
13
  }
14
+ function getColorAlpha() {
15
+ return this[COLOR_SYMBOL].values.alpha.value;
16
+ }
14
17
  function getAlphaHexadecimal(value) {
15
18
  if (value === COLOR_ALPHA.noneValue) return COLOR_ALPHA.noneHex;
16
19
  if (value === COLOR_MAX.percent) return COLOR_ALPHA.fullHexLong;
@@ -27,5 +30,8 @@ function getAlphaValue(original) {
27
30
  if (Number.isNaN(original)) return COLOR_MAX.percent;
28
31
  return clamp(original, COLOR_ALPHA.noneValue, COLOR_MAX.percent);
29
32
  }
33
+ function setColorAlpha(value) {
34
+ if (typeof value === "number" && !Number.isNaN(value)) this[COLOR_SYMBOL].values.alpha = getAlpha(value, false);
35
+ }
30
36
  //#endregion
31
- export { getAlpha, getAlphaHexadecimal, getAlphaValue };
37
+ export { getAlpha, getAlphaHexadecimal, getAlphaValue, getColorAlpha, setColorAlpha };
@@ -27,13 +27,14 @@ function isColor(value) {
27
27
  * @returns `true` if the value is a color subscription, otherwise `false`
28
28
  */
29
29
  function isColorSubscription(value) {
30
- return isSubscription(value) && value["$color"] === "subscription";
30
+ return isSubscription(value) && "$color" in value && value["$color"] === "subscription";
31
31
  }
32
- function isColorValue(obj, properties) {
33
- if (typeof obj !== "object" || obj === null) return false;
34
- const keys = Object.keys(obj);
32
+ function isColorValue(value, properties) {
33
+ if (typeof value !== "object" || value === null) return false;
34
+ const keys = Object.keys(value);
35
35
  const { length } = keys;
36
36
  if (length !== properties.length) return false;
37
+ const obj = value;
37
38
  for (let index = 0; index < length; index += 1) {
38
39
  const key = keys[index];
39
40
  if (!(properties.includes(key) && validators[key](obj[key]))) return false;
@@ -2,8 +2,33 @@ import { ColorState, ColorType, InternalColor } from "../models.mjs";
2
2
  //#region src/color/misc/state.d.ts
3
3
  export declare function getColorFromState<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
4
4
  export declare function getColorState(value: unknown): ColorState;
5
- export declare function setHexColor(instance: InternalColor, value: string, alpha: boolean): void;
6
- export declare function setHSLColor(instance: InternalColor, value: unknown, alpha: boolean): void;
7
- export declare function setHWBColor(instance: InternalColor, value: unknown, alpha: boolean): void;
8
- export declare function setRGBColor(instance: InternalColor, value: unknown, alpha: boolean): void;
5
+ export declare function getStateValue(instance: InternalColor, type: ColorType, alpha: boolean): string | {
6
+ alpha: number;
7
+ hue: number;
8
+ lightness: number;
9
+ saturation: number;
10
+ } | {
11
+ alpha: number;
12
+ blackness: number;
13
+ hue: number;
14
+ whiteness: number;
15
+ } | {
16
+ alpha: number;
17
+ blue: number;
18
+ green: number;
19
+ red: number;
20
+ } | {
21
+ hue: number;
22
+ lightness: number;
23
+ saturation: number;
24
+ } | {
25
+ blackness: number;
26
+ hue: number;
27
+ whiteness: number;
28
+ } | {
29
+ blue: number;
30
+ green: number;
31
+ red: number;
32
+ };
33
+ export declare function setStateValue<Type extends ColorType>(instance: InternalColor, type: Type, value: ColorState[Type], alpha?: number | string): void;
9
34
  //#endregion
@@ -1,11 +1,10 @@
1
1
  import { COLOR_DEFAULTS, COLOR_KEYS, COLOR_LENGTHS, COLOR_MAX, COLOR_SYMBOL, COLOR_TYPE } from "../constants.mjs";
2
2
  import { getAlpha } from "./alpha.mjs";
3
- import { getDegrees, getHexValue, getPercentage } from "./get.mjs";
4
- import { isColor, isHexColor, isHslLike, isHwbLike, isRgbLike } from "./is.mjs";
5
- import { getColorFromRgb, getRgbValue } from "../space/rgb.mjs";
3
+ import { isColor } from "./is.mjs";
4
+ import { getColorFromRgb, getRgbValues } from "../space/rgb.mjs";
6
5
  import { getColorFromHex, getNormalizedHex } from "../space/hex.mjs";
7
- import { getColorFromHsl, getHslValue } from "../space/hsl.mjs";
8
- import { getColorFromHwb, getHwbValue } from "../space/hwb.mjs";
6
+ import { getColorFromHsl, getHslValues } from "../space/hsl.mjs";
7
+ import { getColorFromHwb, getHwbValues } from "../space/hwb.mjs";
9
8
  //#region src/color/misc/state.ts
10
9
  function getColorFromState(state, type) {
11
10
  const { origin } = state;
@@ -28,13 +27,13 @@ function getColorState(value) {
28
27
  origin: void 0
29
28
  };
30
29
  if (COLOR_KEYS.hsl.every((key) => key in value)) {
31
- state.hsl = getHslValue(value);
30
+ state.hsl = getHslValues(value);
32
31
  state.origin = COLOR_TYPE.hsl;
33
32
  } else if (COLOR_KEYS.hwb.every((key) => key in value)) {
34
- state.hwb = getHwbValue(value);
33
+ state.hwb = getHwbValues(value);
35
34
  state.origin = COLOR_TYPE.hwb;
36
35
  } else if (COLOR_KEYS.rgb.every((key) => key in value)) {
37
- state.rgb = getRgbValue(value);
36
+ state.rgb = getRgbValues(value);
38
37
  state.origin = COLOR_TYPE.rgb;
39
38
  }
40
39
  if (state.origin != null) return state;
@@ -59,7 +58,16 @@ function getDefaultColorState() {
59
58
  origin: COLOR_TYPE.hex
60
59
  };
61
60
  }
62
- function setColorValue(instance, type, value, alpha) {
61
+ function getStateValue(instance, type, alpha) {
62
+ const { values } = instance[COLOR_SYMBOL];
63
+ const value = getColorFromState(values, type);
64
+ if (typeof value === "string") return alpha ? `${value}${values.alpha.hex}` : value;
65
+ return alpha ? {
66
+ ...value,
67
+ alpha: values.alpha.value
68
+ } : { ...value };
69
+ }
70
+ function setStateValue(instance, type, value, alpha) {
63
71
  const { changes, values } = instance[COLOR_SYMBOL];
64
72
  values.hex = void 0;
65
73
  values.hsl = void 0;
@@ -71,36 +79,6 @@ function setColorValue(instance, type, value, alpha) {
71
79
  for (const type of COLOR_TYPE.all) if (changes.observed(type)) changes.emit(type, instance[type]);
72
80
  changes.emit(COLOR_TYPE.wildcard, instance);
73
81
  }
74
- function setHexColor(instance, value, alpha) {
75
- const { values } = instance[COLOR_SYMBOL];
76
- if (!isHexColor(value) || !alpha && value === values.hex) return;
77
- const normalized = getNormalizedHex(value, true);
78
- setColorValue(instance, COLOR_TYPE.hex, normalized.slice(0, COLOR_LENGTHS.hexLong), alpha ? normalized.slice(COLOR_LENGTHS.hexLong) : void 0);
79
- }
80
- function setHSLColor(instance, value, alpha) {
81
- if (!isHslLike(value)) return;
82
- setColorValue(instance, COLOR_TYPE.hsl, {
83
- hue: getDegrees(value.hue),
84
- saturation: getPercentage(value.saturation),
85
- lightness: getPercentage(value.lightness)
86
- }, alpha ? value.alpha : void 0);
87
- }
88
- function setHWBColor(instance, value, alpha) {
89
- if (!isHwbLike(value)) return;
90
- setColorValue(instance, COLOR_TYPE.hwb, {
91
- hue: getDegrees(value.hue),
92
- whiteness: getPercentage(value.whiteness),
93
- blackness: getPercentage(value.blackness)
94
- }, alpha ? value.alpha : void 0);
95
- }
96
- function setRGBColor(instance, value, alpha) {
97
- if (!isRgbLike(value)) return;
98
- setColorValue(instance, COLOR_TYPE.rgb, {
99
- red: getHexValue(value.red),
100
- green: getHexValue(value.green),
101
- blue: getHexValue(value.blue)
102
- }, alpha ? value.alpha : void 0);
103
- }
104
82
  const getters = {
105
83
  hex: getColorFromHex,
106
84
  hsl: getColorFromHsl,
@@ -108,4 +86,4 @@ const getters = {
108
86
  rgb: getColorFromRgb
109
87
  };
110
88
  //#endregion
111
- export { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
89
+ export { getColorFromState, getColorState, getStateValue, setStateValue };
@@ -1,6 +1,8 @@
1
- import { ColorState, ColorType, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
1
+ import { ColorState, ColorType, HSLAColor, HSLColor, HWBAColor, HWBColor, InternalColor, RGBAColor, RGBColor } from "../models.mjs";
2
2
  //#region src/color/space/hex.d.ts
3
3
  export declare function getColorFromHex<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
4
+ export declare function getHexColor(this: InternalColor): string;
5
+ export declare function getHexaColor(this: InternalColor): string;
4
6
  /**
5
7
  * Get the normalized hex color from a value
6
8
  *
@@ -65,4 +67,6 @@ export declare function hexToRgb(value: string): RGBColor;
65
67
  * @returns _RGBA_ color
66
68
  */
67
69
  export declare function hexToRgba(value: string): RGBAColor;
70
+ export declare function setHexValue(this: InternalColor, value: string): void;
71
+ export declare function setHexaValue(this: InternalColor, value: string): void;
68
72
  //#endregion