@oscarpalmer/atoms 0.193.1 → 0.194.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.
- package/dist/array/difference.mjs +2 -2
- package/dist/array/intersection.mjs +2 -2
- package/dist/array/match.mjs +13 -13
- package/dist/array/slice.mjs +5 -5
- package/dist/array/sort.d.mts +5 -1
- package/dist/array/sort.mjs +4 -2
- package/dist/array/union.mjs +2 -2
- package/dist/beacon.d.mts +8 -1
- package/dist/beacon.mjs +52 -60
- package/dist/color/constants.d.mts +1 -1
- package/dist/color/constants.mjs +3 -5
- package/dist/color/instance.mjs +34 -41
- package/dist/color/misc/format.d.mts +3 -2
- package/dist/color/misc/format.mjs +4 -1
- package/dist/color/misc/is.d.mts +9 -1
- package/dist/color/misc/is.mjs +11 -1
- package/dist/color/misc/state.d.mts +6 -6
- package/dist/color/misc/state.mjs +11 -14
- package/dist/color/models.d.mts +23 -68
- package/dist/function/assert.mjs +2 -2
- package/dist/function/limit.d.mts +0 -6
- package/dist/function/limit.mjs +2 -2
- package/dist/function/memoize.mjs +19 -32
- package/dist/function/once.d.mts +0 -3
- package/dist/function/once.mjs +16 -13
- package/dist/function/retry.d.mts +1 -24
- package/dist/function/retry.mjs +1 -1
- package/dist/function/work.mjs +13 -11
- package/dist/herald.d.mts +30 -31
- package/dist/herald.mjs +74 -56
- package/dist/index.d.mts +100 -185
- package/dist/index.mjs +680 -702
- package/dist/internal/abort.d.mts +10 -0
- package/dist/internal/abort.mjs +14 -0
- package/dist/internal/array/chunk.mjs +2 -2
- package/dist/internal/array/find.mjs +2 -2
- package/dist/internal/array/sets.d.mts +4 -4
- package/dist/internal/array/sets.mjs +7 -7
- package/dist/internal/function/timer.mjs +19 -15
- package/dist/internal/number.mjs +7 -7
- package/dist/internal/result.mjs +3 -3
- package/dist/internal/sized.mjs +4 -4
- package/dist/internal/string.mjs +4 -4
- package/dist/internal/subscription.d.mts +24 -8
- package/dist/internal/subscription.mjs +49 -36
- package/dist/internal/value/equal.mjs +6 -6
- package/dist/internal/value/misc.mjs +14 -14
- package/dist/internal/value/set.mjs +2 -2
- package/dist/logger.mjs +15 -39
- package/dist/promise/constants.d.mts +17 -0
- package/dist/promise/constants.mjs +14 -0
- package/dist/promise/delay.mjs +7 -10
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.mjs +14 -17
- package/dist/promise/misc.d.mts +2 -1
- package/dist/promise/misc.mjs +8 -7
- package/dist/promise/models.d.mts +4 -16
- package/dist/promise/models.mjs +4 -13
- package/dist/promise/timed.mjs +9 -10
- package/dist/query.mjs +15 -15
- package/dist/queue.mjs +103 -116
- package/dist/random.mjs +7 -7
- package/dist/result/index.d.mts +0 -3
- package/dist/result/index.mjs +1 -1
- package/dist/result/match.d.mts +0 -3
- package/dist/result/match.mjs +4 -4
- package/dist/result/misc.mjs +2 -2
- package/dist/result/work/flow.d.mts +0 -3
- package/dist/result/work/flow.mjs +5 -5
- package/dist/result/work/pipe.d.mts +0 -3
- package/dist/result/work/pipe.mjs +3 -3
- package/dist/string/case.mjs +20 -20
- package/dist/string/fuzzy.mjs +23 -18
- package/dist/string/index.mjs +7 -7
- package/dist/string/normalize.mjs +14 -14
- package/dist/string/template.mjs +2 -2
- package/dist/value/clone.mjs +10 -10
- package/dist/value/smush.mjs +2 -2
- package/package.json +13 -4
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SETS_COMPARE_DIFFERENCE, compareSets } from "../internal/array/sets.mjs";
|
|
2
2
|
//#region src/array/difference.ts
|
|
3
3
|
function difference(first, second, key) {
|
|
4
|
-
return compareSets(
|
|
4
|
+
return compareSets(SETS_COMPARE_DIFFERENCE, first, second, key);
|
|
5
5
|
}
|
|
6
6
|
//#endregion
|
|
7
7
|
export { difference };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SETS_COMPARE_INTERSECTION, compareSets } from "../internal/array/sets.mjs";
|
|
2
2
|
//#region src/array/intersection.ts
|
|
3
3
|
function intersection(first, second, key) {
|
|
4
|
-
return compareSets(
|
|
4
|
+
return compareSets(SETS_COMPARE_INTERSECTION, first, second, key);
|
|
5
5
|
}
|
|
6
6
|
//#endregion
|
|
7
7
|
export { intersection };
|
package/dist/array/match.mjs
CHANGED
|
@@ -7,8 +7,8 @@ function getArrayComparison(haystack, needle, key) {
|
|
|
7
7
|
return getPosition(haystack, needle, key)[1];
|
|
8
8
|
}
|
|
9
9
|
function getName(start, haystack, needle) {
|
|
10
|
-
if (start === 0) return haystack === needle ?
|
|
11
|
-
return start + needle === haystack ?
|
|
10
|
+
if (start === 0) return haystack === needle ? MATCH_COMPARISON_SAME : MATCH_COMPARISON_START;
|
|
11
|
+
return start + needle === haystack ? MATCH_COMPARISON_END : MATCH_COMPARISON_INSIDE;
|
|
12
12
|
}
|
|
13
13
|
function getPosition(haystack, needle, key) {
|
|
14
14
|
if (!Array.isArray(haystack) || !Array.isArray(needle)) return invalid;
|
|
@@ -43,16 +43,16 @@ function indexOfArray(haystack, needle, key) {
|
|
|
43
43
|
function startsWithArray(haystack, needle, key) {
|
|
44
44
|
return starts.has(getPosition(haystack, needle, key)[1]);
|
|
45
45
|
}
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
const endings = /* @__PURE__ */ new Set([
|
|
53
|
-
const invalid = [-1,
|
|
54
|
-
const outside = [-1,
|
|
55
|
-
const outsides = /* @__PURE__ */ new Set([
|
|
56
|
-
const starts = /* @__PURE__ */ new Set([
|
|
46
|
+
const MATCH_COMPARISON_END = "end";
|
|
47
|
+
const MATCH_COMPARISON_INSIDE = "inside";
|
|
48
|
+
const MATCH_COMPARISON_INVALID = "invalid";
|
|
49
|
+
const MATCH_COMPARISON_OUTSIDE = "outside";
|
|
50
|
+
const MATCH_COMPARISON_SAME = "same";
|
|
51
|
+
const MATCH_COMPARISON_START = "start";
|
|
52
|
+
const endings = /* @__PURE__ */ new Set([MATCH_COMPARISON_END, MATCH_COMPARISON_SAME]);
|
|
53
|
+
const invalid = [-1, MATCH_COMPARISON_INVALID];
|
|
54
|
+
const outside = [-1, MATCH_COMPARISON_OUTSIDE];
|
|
55
|
+
const outsides = /* @__PURE__ */ new Set([MATCH_COMPARISON_INVALID, MATCH_COMPARISON_OUTSIDE]);
|
|
56
|
+
const starts = /* @__PURE__ */ new Set([MATCH_COMPARISON_START, MATCH_COMPARISON_SAME]);
|
|
57
57
|
//#endregion
|
|
58
58
|
export { endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray };
|
package/dist/array/slice.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { getArrayCallbacks } from "../internal/array/callbacks.mjs";
|
|
2
2
|
//#region src/array/slice.ts
|
|
3
3
|
function drop(array, first, second) {
|
|
4
|
-
return extract(
|
|
4
|
+
return extract(SLICE_DROP, array, first, second);
|
|
5
5
|
}
|
|
6
6
|
function extract(type, array, first, second) {
|
|
7
7
|
if (!Array.isArray(array)) return [];
|
|
8
8
|
const { length } = array;
|
|
9
9
|
if (length === 0) return [];
|
|
10
|
-
const isTake = type ===
|
|
10
|
+
const isTake = type === SLICE_TAKE;
|
|
11
11
|
if (typeof first === "number") {
|
|
12
12
|
if (Math.abs(first) >= length) return isTake ? array.slice() : [];
|
|
13
13
|
if (first === 0) return isTake ? [] : array.slice();
|
|
@@ -45,9 +45,9 @@ function slice(array, first, second) {
|
|
|
45
45
|
return first >= 0 ? array.slice(first, second) : array.slice(array.length + first, array.length + second);
|
|
46
46
|
}
|
|
47
47
|
function take(array, first, second) {
|
|
48
|
-
return extract(
|
|
48
|
+
return extract(SLICE_TAKE, array, first, second);
|
|
49
49
|
}
|
|
50
|
-
const
|
|
51
|
-
const
|
|
50
|
+
const SLICE_DROP = "drop";
|
|
51
|
+
const SLICE_TAKE = "take";
|
|
52
52
|
//#endregion
|
|
53
53
|
export { drop, slice, take };
|
package/dist/array/sort.d.mts
CHANGED
|
@@ -38,6 +38,10 @@ type ArrayKeySorters<Item extends PlainObject> = { [ItemKey in keyof Item]: Arra
|
|
|
38
38
|
* Sorter to use for sorting
|
|
39
39
|
*/
|
|
40
40
|
type ArraySorter<Item> = Item extends PlainObject ? keyof Item | ArrayComparisonSorter<Item> | ArrayKeySorters<Item> | ArrayValueSorter<Item> | ComparisonSorter<Item> : ArrayComparisonSorter<Item> | ArrayValueSorter<Item> | ComparisonSorter<Item>;
|
|
41
|
+
/**
|
|
42
|
+
* Sorters to use for sorting
|
|
43
|
+
*/
|
|
44
|
+
type ArraySorters<Item> = Array<ArraySorter<Item>>;
|
|
41
45
|
/**
|
|
42
46
|
* Sorting information for arrays _(using a value callback and built-in comparison)_
|
|
43
47
|
*/
|
|
@@ -244,4 +248,4 @@ declare namespace sort {
|
|
|
244
248
|
declare const SORT_DIRECTION_ASCENDING: SortDirection;
|
|
245
249
|
declare const SORT_DIRECTION_DESCENDING: SortDirection;
|
|
246
250
|
//#endregion
|
|
247
|
-
export { ArrayComparisonSorter, ArrayKeySorter, ArrayValueSorter, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SortDirection, Sorter, getSortedIndex, initializeSorter, isSorted, sort };
|
|
251
|
+
export { ArrayComparisonSorter, ArrayKeySorter, ArraySorter, ArraySorters, ArrayValueSorter, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SortDirection, Sorter, getSortedIndex, initializeSorter, isSorted, sort };
|
package/dist/array/sort.mjs
CHANGED
|
@@ -85,8 +85,10 @@ function getValueSorter(value, modifier) {
|
|
|
85
85
|
function initializeSorter(first, second) {
|
|
86
86
|
const sorters = getSorters(first, getModifier(first, second));
|
|
87
87
|
const sorter = (array) => sortArray(array, sorters);
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
Object.defineProperties(sorter, {
|
|
89
|
+
index: { value: (array, item) => getIndex(array, item, sorters) },
|
|
90
|
+
is: { value: (array) => isSortedArray(array, sorters) }
|
|
91
|
+
});
|
|
90
92
|
return sorter;
|
|
91
93
|
}
|
|
92
94
|
function isSorted(array, first, second) {
|
package/dist/array/union.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SETS_COMPARE_UNION, compareSets } from "../internal/array/sets.mjs";
|
|
2
2
|
//#region src/array/union.ts
|
|
3
3
|
function union(first, second, key) {
|
|
4
|
-
return compareSets(
|
|
4
|
+
return compareSets(SETS_COMPARE_UNION, first, second, key);
|
|
5
5
|
}
|
|
6
6
|
//#endregion
|
|
7
7
|
export { union };
|
package/dist/beacon.d.mts
CHANGED
|
@@ -105,6 +105,13 @@ declare function beacon<Value>(value: Value, options?: BeaconOptions<Value>): Be
|
|
|
105
105
|
* @returns `true` if the value is a beacon, otherwise `false`
|
|
106
106
|
*/
|
|
107
107
|
declare function isBeacon<Value = unknown>(value: unknown): value is Beacon<Value>;
|
|
108
|
+
/**
|
|
109
|
+
* Is the value a beacon subscription?
|
|
110
|
+
*
|
|
111
|
+
* @param value Value to check
|
|
112
|
+
* @returns `true` if the value is a beacon subscription, otherwise `false`
|
|
113
|
+
*/
|
|
114
|
+
declare function isBeaconSubscription(value: unknown): value is Subscription;
|
|
108
115
|
/**
|
|
109
116
|
* Is the value an observable?
|
|
110
117
|
*
|
|
@@ -113,4 +120,4 @@ declare function isBeacon<Value = unknown>(value: unknown): value is Beacon<Valu
|
|
|
113
120
|
*/
|
|
114
121
|
declare function isObservable<Value = unknown>(value: unknown): value is Observable<Value>;
|
|
115
122
|
//#endregion
|
|
116
|
-
export { Beacon, BeaconOptions, Observable, beacon, isBeacon, isObservable };
|
|
123
|
+
export { Beacon, BeaconOptions, Observable, beacon, isBeacon, isBeaconSubscription, isObservable };
|
package/dist/beacon.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { noop } from "./internal/function/misc.mjs";
|
|
2
2
|
import { isPlainObject } from "./internal/is.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { createSubscriptions, isSubscription } from "./internal/subscription.mjs";
|
|
4
4
|
//#region src/beacon.ts
|
|
5
5
|
/**
|
|
6
6
|
* Create a new beacon
|
|
@@ -15,38 +15,23 @@ function beacon(value, options) {
|
|
|
15
15
|
active: true,
|
|
16
16
|
observable: void 0,
|
|
17
17
|
options: getBeaconOptions(options),
|
|
18
|
-
subscriptions:
|
|
18
|
+
subscriptions: createSubscriptions({ property: beaconSubscription })
|
|
19
19
|
};
|
|
20
20
|
const instance = {
|
|
21
|
-
deactivate: () =>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
update(TYPE_NEXT, state, value, finish);
|
|
26
|
-
},
|
|
27
|
-
error: (value, finish) => {
|
|
28
|
-
update(TYPE_ERROR, state, value, finish);
|
|
29
|
-
},
|
|
30
|
-
finish: () => {
|
|
31
|
-
finishBeacon(state, true);
|
|
32
|
-
}
|
|
21
|
+
deactivate: () => finishBeacon(state, false),
|
|
22
|
+
emit: (value, finish) => updateBeacon(BEACON_TYPE_NEXT, state, value, finish),
|
|
23
|
+
error: (value, finish) => updateBeacon(BEACON_TYPE_ERROR, state, value, finish),
|
|
24
|
+
finish: () => finishBeacon(state, true)
|
|
33
25
|
};
|
|
34
26
|
Object.defineProperties(instance, {
|
|
35
|
-
[
|
|
36
|
-
enumerable: false,
|
|
37
|
-
value: NAME_BEACON
|
|
38
|
-
},
|
|
27
|
+
[BEACON_PROPERTY]: { value: BEACON_NAME },
|
|
39
28
|
active: {
|
|
40
29
|
enumerable: true,
|
|
41
30
|
get: () => state.active
|
|
42
31
|
},
|
|
43
32
|
observable: {
|
|
44
33
|
enumerable: true,
|
|
45
|
-
get: () =>
|
|
46
|
-
if (!state.active) throw new Error(MESSAGE_BEACON);
|
|
47
|
-
state.observable ??= observe(state);
|
|
48
|
-
return state.observable;
|
|
49
|
-
}
|
|
34
|
+
get: () => getObservable(state)
|
|
50
35
|
},
|
|
51
36
|
value: {
|
|
52
37
|
enumerable: true,
|
|
@@ -58,8 +43,8 @@ function beacon(value, options) {
|
|
|
58
43
|
function finishBeacon(state, emit) {
|
|
59
44
|
if (!state.active) return;
|
|
60
45
|
state.active = false;
|
|
61
|
-
for (const [, observer] of state.subscriptions.values.to.any) if (emit) observer.complete?.();
|
|
62
|
-
|
|
46
|
+
for (const [, observer] of state.subscriptions.state.values.to.any) if (emit) observer.complete?.();
|
|
47
|
+
state.subscriptions.clear();
|
|
63
48
|
state.observable?.deactivate();
|
|
64
49
|
state.observable = void 0;
|
|
65
50
|
}
|
|
@@ -68,6 +53,11 @@ function getBeaconOptions(input) {
|
|
|
68
53
|
options.equal = typeof options.equal === "function" ? options.equal : Object.is;
|
|
69
54
|
return options;
|
|
70
55
|
}
|
|
56
|
+
function getObservable(state) {
|
|
57
|
+
if (!state.active) throw new Error(BEACON_MESSAGE_RETRIEVE);
|
|
58
|
+
state.observable ??= observeBeacon(state);
|
|
59
|
+
return state.observable;
|
|
60
|
+
}
|
|
71
61
|
function getObservableCallback(value) {
|
|
72
62
|
return typeof value === "function" ? value : noop;
|
|
73
63
|
}
|
|
@@ -92,10 +82,19 @@ function getObserver(first, second, third) {
|
|
|
92
82
|
* @returns `true` if the value is a beacon, otherwise `false`
|
|
93
83
|
*/
|
|
94
84
|
function isBeacon(value) {
|
|
95
|
-
return isBeaconInstance(
|
|
85
|
+
return isBeaconInstance(BEACON_NAME, value);
|
|
96
86
|
}
|
|
97
87
|
function isBeaconInstance(name, value) {
|
|
98
|
-
return isPlainObject(value) && value[
|
|
88
|
+
return isPlainObject(value) && value[BEACON_PROPERTY] === name;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Is the value a beacon subscription?
|
|
92
|
+
*
|
|
93
|
+
* @param value Value to check
|
|
94
|
+
* @returns `true` if the value is a beacon subscription, otherwise `false`
|
|
95
|
+
*/
|
|
96
|
+
function isBeaconSubscription(value) {
|
|
97
|
+
return isSubscription(value) && value[BEACON_PROPERTY] === "subscription";
|
|
99
98
|
}
|
|
100
99
|
/**
|
|
101
100
|
* Is the value an observable?
|
|
@@ -104,9 +103,9 @@ function isBeaconInstance(name, value) {
|
|
|
104
103
|
* @returns `true` if the value is an observable, otherwise `false`
|
|
105
104
|
*/
|
|
106
105
|
function isObservable(value) {
|
|
107
|
-
return isBeaconInstance(
|
|
106
|
+
return isBeaconInstance(BEACON_OBSERVABLE, value);
|
|
108
107
|
}
|
|
109
|
-
function
|
|
108
|
+
function observeBeacon(beacon) {
|
|
110
109
|
const state = {
|
|
111
110
|
beacon,
|
|
112
111
|
active: beacon.active
|
|
@@ -115,24 +114,10 @@ function observe(beacon) {
|
|
|
115
114
|
deactivate: () => {
|
|
116
115
|
state.active = false;
|
|
117
116
|
},
|
|
118
|
-
subscribe(first, second, third)
|
|
119
|
-
if (!beacon.active || !state.active) throw new Error(MESSAGE_OBSERVABLE);
|
|
120
|
-
const observer = getObserver(first, second, third);
|
|
121
|
-
const subscription = getSubscription({
|
|
122
|
-
isActive: () => beacon.active && state.active,
|
|
123
|
-
property: beaconSubscription,
|
|
124
|
-
subscriptions: beacon.subscriptions,
|
|
125
|
-
value: observer
|
|
126
|
-
});
|
|
127
|
-
observer.next?.(beacon.value);
|
|
128
|
-
return subscription;
|
|
129
|
-
}
|
|
117
|
+
subscribe: (first, second, third) => subscribeToObservable(state, first, second, third)
|
|
130
118
|
};
|
|
131
119
|
Object.defineProperties(instance, {
|
|
132
|
-
[
|
|
133
|
-
enumerable: false,
|
|
134
|
-
value: NAME_OBSERVABLE
|
|
135
|
-
},
|
|
120
|
+
[BEACON_PROPERTY]: { value: BEACON_OBSERVABLE },
|
|
136
121
|
active: {
|
|
137
122
|
enumerable: true,
|
|
138
123
|
get: () => beacon.active && state.active
|
|
@@ -140,25 +125,32 @@ function observe(beacon) {
|
|
|
140
125
|
});
|
|
141
126
|
return Object.freeze(instance);
|
|
142
127
|
}
|
|
143
|
-
function
|
|
128
|
+
function subscribeToObservable(state, first, second, third) {
|
|
129
|
+
if (!state.beacon.active || !state.active) throw new Error(BEACON_MESSAGE_SUBSCRIBE);
|
|
130
|
+
const observer = getObserver(first, second, third);
|
|
131
|
+
const [subscription] = state.beacon.subscriptions.create({
|
|
132
|
+
isActive: () => state.beacon.active && state.active,
|
|
133
|
+
value: observer
|
|
134
|
+
});
|
|
135
|
+
observer.next?.(state.beacon.value);
|
|
136
|
+
return subscription;
|
|
137
|
+
}
|
|
138
|
+
function updateBeacon(type, state, value, finish) {
|
|
144
139
|
if (!state.active) return;
|
|
145
|
-
if (type ===
|
|
140
|
+
if (type === BEACON_TYPE_NEXT) {
|
|
146
141
|
if (state.options.equal(state.value, value)) return;
|
|
147
142
|
state.value = value;
|
|
148
143
|
}
|
|
149
|
-
for (const [, observer] of state.subscriptions.values.to.any) observer[type]?.(value);
|
|
144
|
+
for (const [, observer] of state.subscriptions.state.values.to.any) observer[type]?.(value);
|
|
150
145
|
if (finish === true) finishBeacon(state, true);
|
|
151
146
|
}
|
|
152
|
-
const
|
|
153
|
-
const
|
|
154
|
-
const
|
|
155
|
-
const
|
|
156
|
-
const
|
|
157
|
-
const
|
|
158
|
-
const
|
|
159
|
-
const beaconSubscription = {
|
|
160
|
-
key: KEY_BEACON,
|
|
161
|
-
value: "subscription"
|
|
162
|
-
};
|
|
147
|
+
const BEACON_MESSAGE_RETRIEVE = "Cannot retrieve observable from a closed beacon";
|
|
148
|
+
const BEACON_MESSAGE_SUBSCRIBE = "Cannot subscribe to a closed observable";
|
|
149
|
+
const BEACON_PROPERTY = "$beacon";
|
|
150
|
+
const BEACON_NAME = "beacon";
|
|
151
|
+
const BEACON_OBSERVABLE = "observable";
|
|
152
|
+
const BEACON_TYPE_ERROR = "error";
|
|
153
|
+
const BEACON_TYPE_NEXT = "next";
|
|
154
|
+
const beaconSubscription = { key: BEACON_PROPERTY };
|
|
163
155
|
//#endregion
|
|
164
|
-
export { beacon, isBeacon, isObservable };
|
|
156
|
+
export { beacon, isBeacon, isBeaconSubscription, isObservable };
|
|
@@ -10,6 +10,7 @@ type ColorTypes = {
|
|
|
10
10
|
hwba: 'hwba';
|
|
11
11
|
rgb: 'rgb';
|
|
12
12
|
rgba: 'rgba';
|
|
13
|
+
wildcard: '*';
|
|
13
14
|
};
|
|
14
15
|
declare const COLOR_ALPHA: {
|
|
15
16
|
fullHexShort: string;
|
|
@@ -66,7 +67,6 @@ declare const COLOR_MAX: {
|
|
|
66
67
|
};
|
|
67
68
|
declare const COLOR_PROPERTY: {
|
|
68
69
|
name: string;
|
|
69
|
-
subscription: string;
|
|
70
70
|
};
|
|
71
71
|
declare const COLOR_TYPE: ColorTypes;
|
|
72
72
|
declare const COLOR_SRGB: {
|
package/dist/color/constants.mjs
CHANGED
|
@@ -67,10 +67,7 @@ const COLOR_MAX = {
|
|
|
67
67
|
hex: 255,
|
|
68
68
|
percent: 100
|
|
69
69
|
};
|
|
70
|
-
const COLOR_PROPERTY = {
|
|
71
|
-
name: "$color",
|
|
72
|
-
subscription: "subscription"
|
|
73
|
-
};
|
|
70
|
+
const COLOR_PROPERTY = { name: "$color" };
|
|
74
71
|
const COLOR_TYPE = {
|
|
75
72
|
all: void 0,
|
|
76
73
|
hex: "hex",
|
|
@@ -80,7 +77,8 @@ const COLOR_TYPE = {
|
|
|
80
77
|
hwb: "hwb",
|
|
81
78
|
hwba: "hwba",
|
|
82
79
|
rgb: "rgb",
|
|
83
|
-
rgba: "rgba"
|
|
80
|
+
rgba: "rgba",
|
|
81
|
+
wildcard: "*"
|
|
84
82
|
};
|
|
85
83
|
COLOR_TYPE.all = /* @__PURE__ */ new Set([
|
|
86
84
|
COLOR_ALPHA.name,
|
package/dist/color/instance.mjs
CHANGED
|
@@ -1,55 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { herald } from "../herald.mjs";
|
|
2
2
|
import { COLOR_PROPERTY, COLOR_TYPE } from "./constants.mjs";
|
|
3
3
|
import { getAlpha } from "./misc/alpha.mjs";
|
|
4
4
|
import { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor } from "./misc/state.mjs";
|
|
5
|
-
import { formatHslColor, formatHwbColor, formatRgbColor } from "./misc/format.mjs";
|
|
5
|
+
import { formatHexColor, formatHslColor, formatHwbColor, formatRgbColor } from "./misc/format.mjs";
|
|
6
6
|
//#region src/color/instance.ts
|
|
7
7
|
function color(value) {
|
|
8
|
+
const changes = herald({
|
|
9
|
+
names: [COLOR_TYPE.wildcard, ...COLOR_TYPE.all],
|
|
10
|
+
property: colorSubscription,
|
|
11
|
+
onCreate: (event, callback) => onCreateSubscription(instance, event, callback)
|
|
12
|
+
});
|
|
8
13
|
const state = getColorState(value);
|
|
9
|
-
const subscriptions = getSubscriptions(COLOR_TYPE.all);
|
|
10
14
|
const instance = {
|
|
11
|
-
subscribe: (
|
|
12
|
-
toHexString: (alpha) =>
|
|
15
|
+
subscribe: (callback, signal) => changes.subscribe(COLOR_TYPE.wildcard, callback, signal),
|
|
16
|
+
toHexString: (alpha) => formatHexColor(instance, alpha),
|
|
13
17
|
toHslString: (alpha) => formatHslColor(state, alpha),
|
|
14
18
|
toHwbString: (alpha) => formatHwbColor(state, alpha),
|
|
15
19
|
toRgbString: (alpha) => formatRgbColor(state, alpha),
|
|
16
20
|
toString: () => instance.toHexString(),
|
|
17
|
-
unsubscribe: () =>
|
|
21
|
+
unsubscribe: () => changes.clear()
|
|
18
22
|
};
|
|
19
23
|
Object.defineProperties(instance, {
|
|
20
|
-
[COLOR_PROPERTY.name]: {
|
|
21
|
-
enumerable: false,
|
|
22
|
-
value: true
|
|
23
|
-
},
|
|
24
|
+
[COLOR_PROPERTY.name]: { value: true },
|
|
24
25
|
alpha: {
|
|
25
26
|
enumerable: true,
|
|
26
27
|
get: () => state.alpha.value,
|
|
27
28
|
set: (value) => setAlphaValue(state, value)
|
|
28
29
|
},
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
changes: {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
value: changes.events
|
|
33
|
+
},
|
|
34
|
+
hex: getColorProperty(COLOR_TYPE.hex, instance, state, changes, false),
|
|
35
|
+
hexa: getColorProperty(COLOR_TYPE.hex, instance, state, changes, true),
|
|
36
|
+
hsl: getColorProperty(COLOR_TYPE.hsl, instance, state, changes, false),
|
|
37
|
+
hsla: getColorProperty(COLOR_TYPE.hsl, instance, state, changes, true),
|
|
38
|
+
hwb: getColorProperty(COLOR_TYPE.hwb, instance, state, changes, false),
|
|
39
|
+
hwba: getColorProperty(COLOR_TYPE.hwb, instance, state, changes, true),
|
|
35
40
|
origin: {
|
|
36
41
|
enumerable: true,
|
|
37
42
|
get: () => state.origin
|
|
38
43
|
},
|
|
39
|
-
rgb:
|
|
40
|
-
rgba:
|
|
44
|
+
rgb: getColorProperty(COLOR_TYPE.rgb, instance, state, changes, false),
|
|
45
|
+
rgba: getColorProperty(COLOR_TYPE.rgb, instance, state, changes, true)
|
|
41
46
|
});
|
|
42
47
|
return Object.freeze(instance);
|
|
43
48
|
}
|
|
44
|
-
function
|
|
49
|
+
function getColorProperty(space, color, state, changes, alpha) {
|
|
45
50
|
const setValue = setters[space];
|
|
46
51
|
return {
|
|
47
52
|
enumerable: true,
|
|
48
|
-
get: () =>
|
|
49
|
-
set: (value) => setValue(color, state,
|
|
53
|
+
get: () => getColorValue(space, state, alpha),
|
|
54
|
+
set: (value) => setValue(color, state, changes, value, alpha)
|
|
50
55
|
};
|
|
51
56
|
}
|
|
52
|
-
function
|
|
57
|
+
function getColorValue(space, state, alpha) {
|
|
53
58
|
const value = getColorFromState(state, space);
|
|
54
59
|
if (typeof value === "string") return alpha ? `${value}${state.alpha.hex}` : value;
|
|
55
60
|
return alpha ? {
|
|
@@ -57,28 +62,16 @@ function getValue(space, state, alpha) {
|
|
|
57
62
|
alpha: state.alpha.value
|
|
58
63
|
} : { ...value };
|
|
59
64
|
}
|
|
65
|
+
function onCreateSubscription(instance, event, callback) {
|
|
66
|
+
let value;
|
|
67
|
+
if (event === COLOR_TYPE.wildcard) value = instance;
|
|
68
|
+
else value = instance[event];
|
|
69
|
+
callback(value);
|
|
70
|
+
}
|
|
60
71
|
function setAlphaValue(state, value) {
|
|
61
72
|
if (typeof value === "number" && !Number.isNaN(value)) state.alpha = getAlpha(value, false);
|
|
62
73
|
}
|
|
63
|
-
|
|
64
|
-
let key;
|
|
65
|
-
let value;
|
|
66
|
-
if (typeof first === "string") {
|
|
67
|
-
key = first;
|
|
68
|
-
value = second;
|
|
69
|
-
} else if (typeof first === "function") value = first;
|
|
70
|
-
if (typeof value !== "function") throw new Error();
|
|
71
|
-
return getSubscription({
|
|
72
|
-
key,
|
|
73
|
-
subscriptions,
|
|
74
|
-
value,
|
|
75
|
-
property: colorSubscription
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
const colorSubscription = {
|
|
79
|
-
key: COLOR_PROPERTY.name,
|
|
80
|
-
value: COLOR_PROPERTY.subscription
|
|
81
|
-
};
|
|
74
|
+
const colorSubscription = { key: COLOR_PROPERTY.name };
|
|
82
75
|
const setters = {
|
|
83
76
|
hex: setHexColor,
|
|
84
77
|
hsl: setHSLColor,
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { ColorState } from "../models.mjs";
|
|
1
|
+
import { Color, ColorState } from "../models.mjs";
|
|
2
2
|
//#region src/color/misc/format.d.ts
|
|
3
|
+
declare function formatHexColor(color: Color, alpha?: boolean): string;
|
|
3
4
|
declare function formatHslColor(state: ColorState, alpha?: boolean): string;
|
|
4
5
|
declare function formatHwbColor(state: ColorState, alpha?: boolean): string;
|
|
5
6
|
declare function formatRgbColor(state: ColorState, alpha?: boolean): string;
|
|
6
7
|
//#endregion
|
|
7
|
-
export { formatHslColor, formatHwbColor, formatRgbColor };
|
|
8
|
+
export { formatHexColor, formatHslColor, formatHwbColor, formatRgbColor };
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { COLOR_TYPE } from "../constants.mjs";
|
|
2
2
|
import { getColorFromState } from "./state.mjs";
|
|
3
3
|
//#region src/color/misc/format.ts
|
|
4
|
+
function formatHexColor(color, alpha) {
|
|
5
|
+
return `#${alpha === true ? color.hexa : color.hex}`;
|
|
6
|
+
}
|
|
4
7
|
function formatHslColor(state, alpha) {
|
|
5
8
|
const { hue, lightness, saturation } = getColorFromState(state, COLOR_TYPE.hsl);
|
|
6
9
|
return `hsl(${hue}deg ${saturation}% ${lightness}%${getSuffix(state, alpha)})`;
|
|
@@ -17,4 +20,4 @@ function getSuffix(state, alpha) {
|
|
|
17
20
|
return alpha === true ? ` / ${state.alpha.value}%` : "";
|
|
18
21
|
}
|
|
19
22
|
//#endregion
|
|
20
|
-
export { formatHslColor, formatHwbColor, formatRgbColor };
|
|
23
|
+
export { formatHexColor, formatHslColor, formatHwbColor, formatRgbColor };
|
package/dist/color/misc/is.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Subscription } from "../../internal/subscription.mjs";
|
|
1
2
|
import { Color, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
2
3
|
//#region src/color/misc/is.d.ts
|
|
3
4
|
/**
|
|
@@ -7,6 +8,13 @@ import { Color, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor }
|
|
|
7
8
|
* @returns `true` if the value is a _Color_, otherwise `false`
|
|
8
9
|
*/
|
|
9
10
|
declare function isColor(value: unknown): value is Color;
|
|
11
|
+
/**
|
|
12
|
+
* Is the value a color subscription?
|
|
13
|
+
*
|
|
14
|
+
* @param value Value to check
|
|
15
|
+
* @returns `true` if the value is a color subscription, otherwise `false`
|
|
16
|
+
*/
|
|
17
|
+
declare function isColorSubscription(value: unknown): value is Subscription;
|
|
10
18
|
/**
|
|
11
19
|
* Is the value a hex color?
|
|
12
20
|
*
|
|
@@ -73,4 +81,4 @@ declare function isRgbaLike(value: unknown): value is Record<keyof RGBAColor, un
|
|
|
73
81
|
declare function isRgbColor(value: unknown): value is RGBColor;
|
|
74
82
|
declare function isRgbLike(value: unknown): value is Record<keyof RGBColor, unknown>;
|
|
75
83
|
//#endregion
|
|
76
|
-
export { isColor, isHexColor, isHslColor, isHslLike, isHslaColor, isHslaLike, isHwbColor, isHwbLike, isHwbaColor, isHwbaLike, isRgbColor, isRgbLike, isRgbaColor, isRgbaLike };
|
|
84
|
+
export { isColor, isColorSubscription, isHexColor, isHslColor, isHslLike, isHslaColor, isHslaLike, isHwbColor, isHwbLike, isHwbaColor, isHwbaLike, isRgbColor, isRgbLike, isRgbaColor, isRgbaLike };
|
package/dist/color/misc/is.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isPlainObject } from "../../internal/is.mjs";
|
|
2
|
+
import { isSubscription } from "../../internal/subscription.mjs";
|
|
2
3
|
import { between } from "../../internal/number.mjs";
|
|
3
4
|
import { COLOR_ALPHA, COLOR_EXPRESSION, COLOR_KEYS, COLOR_LENGTHS, COLOR_MAX, COLOR_PROPERTY } from "../constants.mjs";
|
|
4
5
|
//#region src/color/misc/is.ts
|
|
@@ -20,6 +21,15 @@ function isBytey(value) {
|
|
|
20
21
|
function isColor(value) {
|
|
21
22
|
return isPlainObject(value) && COLOR_PROPERTY.name in value && value[COLOR_PROPERTY.name] === true;
|
|
22
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Is the value a color subscription?
|
|
26
|
+
*
|
|
27
|
+
* @param value Value to check
|
|
28
|
+
* @returns `true` if the value is a color subscription, otherwise `false`
|
|
29
|
+
*/
|
|
30
|
+
function isColorSubscription(value) {
|
|
31
|
+
return isSubscription(value) && value[COLOR_PROPERTY.name] === "subscription";
|
|
32
|
+
}
|
|
23
33
|
function isColorValue(obj, properties) {
|
|
24
34
|
if (typeof obj !== "object" || obj === null) return false;
|
|
25
35
|
const keys = Object.keys(obj);
|
|
@@ -146,4 +156,4 @@ const validators = {
|
|
|
146
156
|
whiteness: isPercentage
|
|
147
157
|
};
|
|
148
158
|
//#endregion
|
|
149
|
-
export { isColor, isHexColor, isHslColor, isHslLike, isHslaColor, isHslaLike, isHwbColor, isHwbLike, isHwbaColor, isHwbaLike, isRgbColor, isRgbLike, isRgbaColor, isRgbaLike };
|
|
159
|
+
export { isColor, isColorSubscription, isHexColor, isHslColor, isHslLike, isHslaColor, isHslaLike, isHwbColor, isHwbLike, isHwbaColor, isHwbaLike, isRgbColor, isRgbLike, isRgbaColor, isRgbaLike };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Color, ColorState, ColorType } from "../models.mjs";
|
|
1
|
+
import { Herald } from "../../herald.mjs";
|
|
2
|
+
import { Color, ColorChanges, ColorState, ColorType } from "../models.mjs";
|
|
3
3
|
//#region src/color/misc/state.d.ts
|
|
4
4
|
declare function getColorFromState<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
|
|
5
5
|
declare function getColorState(value: unknown): ColorState;
|
|
6
|
-
declare function setHexColor(color: Color, state: ColorState,
|
|
7
|
-
declare function setHSLColor(color: Color, state: ColorState,
|
|
8
|
-
declare function setHWBColor(color: Color, state: ColorState,
|
|
9
|
-
declare function setRGBColor(color: Color, state: ColorState,
|
|
6
|
+
declare function setHexColor(color: Color, state: ColorState, changes: Herald<ColorChanges>, value: string, alpha: boolean): void;
|
|
7
|
+
declare function setHSLColor(color: Color, state: ColorState, changes: Herald<ColorChanges>, value: unknown, alpha: boolean): void;
|
|
8
|
+
declare function setHWBColor(color: Color, state: ColorState, changes: Herald<ColorChanges>, value: unknown, alpha: boolean): void;
|
|
9
|
+
declare function setRGBColor(color: Color, state: ColorState, changes: Herald<ColorChanges>, value: unknown, alpha: boolean): void;
|
|
10
10
|
//#endregion
|
|
11
11
|
export { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
|
|
@@ -59,7 +59,7 @@ function getDefaultColorState() {
|
|
|
59
59
|
origin: COLOR_TYPE.hex
|
|
60
60
|
};
|
|
61
61
|
}
|
|
62
|
-
function setColorValue(color, state,
|
|
62
|
+
function setColorValue(color, state, changes, type, value, alpha) {
|
|
63
63
|
state.hex = void 0;
|
|
64
64
|
state.hsl = void 0;
|
|
65
65
|
state.hwb = void 0;
|
|
@@ -67,36 +67,33 @@ function setColorValue(color, state, subscriptions, type, value, alpha) {
|
|
|
67
67
|
state.origin = type;
|
|
68
68
|
state[type] = value;
|
|
69
69
|
if (alpha != null) state.alpha = getAlpha(alpha, type === COLOR_TYPE.hex);
|
|
70
|
-
for (const type of COLOR_TYPE.all)
|
|
71
|
-
|
|
72
|
-
if (callbacks != null) for (const callback of callbacks.values()) callback(color[type]);
|
|
73
|
-
}
|
|
74
|
-
for (const callback of subscriptions.values.to.any.values()) callback(color);
|
|
70
|
+
for (const type of COLOR_TYPE.all) if (changes.observed(type)) changes.emit(type, color[type]);
|
|
71
|
+
changes.emit(COLOR_TYPE.wildcard, color);
|
|
75
72
|
}
|
|
76
|
-
function setHexColor(color, state,
|
|
73
|
+
function setHexColor(color, state, changes, value, alpha) {
|
|
77
74
|
if (!isHexColor(value) || !alpha && value === state.hex) return;
|
|
78
75
|
const normalized = getNormalizedHex(value, true);
|
|
79
|
-
setColorValue(color, state,
|
|
76
|
+
setColorValue(color, state, changes, COLOR_TYPE.hex, normalized.slice(0, COLOR_LENGTHS.hexLong), alpha ? normalized.slice(COLOR_LENGTHS.hexLong) : void 0);
|
|
80
77
|
}
|
|
81
|
-
function setHSLColor(color, state,
|
|
78
|
+
function setHSLColor(color, state, changes, value, alpha) {
|
|
82
79
|
if (!isHslLike(value)) return;
|
|
83
|
-
setColorValue(color, state,
|
|
80
|
+
setColorValue(color, state, changes, COLOR_TYPE.hsl, {
|
|
84
81
|
hue: getDegrees(value.hue),
|
|
85
82
|
saturation: getPercentage(value.saturation),
|
|
86
83
|
lightness: getPercentage(value.lightness)
|
|
87
84
|
}, alpha ? value.alpha : void 0);
|
|
88
85
|
}
|
|
89
|
-
function setHWBColor(color, state,
|
|
86
|
+
function setHWBColor(color, state, changes, value, alpha) {
|
|
90
87
|
if (!isHwbLike(value)) return;
|
|
91
|
-
setColorValue(color, state,
|
|
88
|
+
setColorValue(color, state, changes, COLOR_TYPE.hwb, {
|
|
92
89
|
hue: getDegrees(value.hue),
|
|
93
90
|
whiteness: getPercentage(value.whiteness),
|
|
94
91
|
blackness: getPercentage(value.blackness)
|
|
95
92
|
}, alpha ? value.alpha : void 0);
|
|
96
93
|
}
|
|
97
|
-
function setRGBColor(color, state,
|
|
94
|
+
function setRGBColor(color, state, changes, value, alpha) {
|
|
98
95
|
if (!isRgbLike(value)) return;
|
|
99
|
-
setColorValue(color, state,
|
|
96
|
+
setColorValue(color, state, changes, COLOR_TYPE.rgb, {
|
|
100
97
|
red: getHexValue(value.red),
|
|
101
98
|
green: getHexValue(value.green),
|
|
102
99
|
blue: getHexValue(value.blue)
|