@oscarpalmer/atoms 0.184.1 → 0.185.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/index.d.mts +2 -2
- package/dist/array/index.mjs +2 -2
- package/dist/array/{position.d.mts → match.d.mts} +9 -6
- package/dist/array/{position.mjs → match.mjs} +16 -16
- package/dist/array/move.mjs +1 -1
- package/dist/array/single.mjs +2 -2
- package/dist/array/sort.d.mts +9 -4
- package/dist/array/sort.mjs +6 -6
- package/dist/array/swap.mjs +1 -1
- package/dist/beacon.d.mts +12 -0
- package/dist/beacon.mjs +9 -0
- package/dist/color/instance.d.mts +8 -0
- package/dist/color/instance.mjs +5 -2
- package/dist/color/misc/get.mjs +8 -8
- package/dist/color/misc/state.d.mts +2 -2
- package/dist/color/misc/state.mjs +2 -2
- package/dist/color/models.d.mts +30 -0
- package/dist/function/assert.d.mts +29 -8
- package/dist/function/assert.mjs +29 -8
- package/dist/function/memoize.d.mts +3 -0
- package/dist/function/memoize.mjs +3 -0
- package/dist/function/once.mjs +9 -9
- package/dist/function/retry.d.mts +3 -0
- package/dist/function/retry.mjs +11 -8
- package/dist/function/work.mjs +1 -1
- package/dist/index.d.mts +272 -160
- package/dist/index.mjs +279 -216
- package/dist/internal/number.d.mts +2 -1
- package/dist/internal/number.mjs +4 -1
- package/dist/internal/value/compare.d.mts +2 -1
- package/dist/internal/value/equal.d.mts +5 -0
- package/dist/internal/value/equal.mjs +5 -5
- package/dist/internal/value/get.d.mts +2 -2
- package/dist/internal/value/has.d.mts +3 -3
- package/dist/internal/value/has.mjs +1 -1
- package/dist/internal/value/misc.d.mts +2 -2
- package/dist/internal/value/misc.mjs +10 -4
- package/dist/logger.d.mts +11 -0
- package/dist/logger.mjs +11 -0
- package/dist/models.d.mts +1 -1
- package/dist/promise/helpers.mjs +3 -5
- package/dist/promise/index.d.mts +0 -6
- package/dist/promise/models.d.mts +36 -0
- package/dist/promise/models.mjs +6 -0
- package/dist/queue.d.mts +13 -1
- package/dist/queue.mjs +14 -7
- package/dist/result/index.d.mts +0 -8
- package/dist/result/index.mjs +0 -8
- package/dist/result/match.d.mts +4 -4
- package/dist/result/work/flow.d.mts +12 -36
- package/dist/result/work/pipe.d.mts +11 -33
- package/dist/sized/set.d.mts +3 -2
- package/dist/sized/set.mjs +3 -2
- package/dist/string/fuzzy.mjs +2 -2
- package/dist/value/handle.mjs +1 -1
- package/dist/value/merge.d.mts +0 -1
- package/dist/value/merge.mjs +0 -1
- package/dist/value/shake.d.mts +3 -0
- package/dist/value/smush.d.mts +3 -0
- package/dist/value/transform.d.mts +9 -0
- package/dist/value/unsmush.d.mts +3 -0
- package/package.json +3 -3
- package/src/array/difference.ts +4 -0
- package/src/array/from.ts +4 -0
- package/src/array/index.ts +1 -1
- package/src/array/intersection.ts +4 -0
- package/src/array/{position.ts → match.ts} +28 -25
- package/src/array/move.ts +5 -1
- package/src/array/reverse.ts +4 -0
- package/src/array/select.ts +2 -0
- package/src/array/single.ts +2 -2
- package/src/array/sort.ts +14 -9
- package/src/array/swap.ts +5 -1
- package/src/array/toggle.ts +4 -0
- package/src/array/union.ts +4 -0
- package/src/beacon.ts +12 -0
- package/src/color/index.ts +0 -3
- package/src/color/instance.ts +11 -3
- package/src/color/misc/get.ts +8 -8
- package/src/color/misc/state.ts +1 -1
- package/src/color/models.ts +30 -0
- package/src/function/assert.ts +66 -7
- package/src/function/memoize.ts +3 -0
- package/src/function/once.ts +13 -9
- package/src/function/retry.ts +11 -8
- package/src/internal/number.ts +6 -0
- package/src/internal/value/compare.ts +2 -1
- package/src/internal/value/equal.ts +10 -5
- package/src/internal/value/get.ts +2 -2
- package/src/internal/value/has.ts +6 -6
- package/src/internal/value/misc.ts +24 -13
- package/src/logger.ts +11 -0
- package/src/models.ts +1 -1
- package/src/promise/helpers.ts +3 -6
- package/src/promise/index.ts +0 -6
- package/src/promise/models.ts +36 -0
- package/src/queue.ts +23 -11
- package/src/result/index.ts +0 -8
- package/src/result/match.ts +4 -4
- package/src/result/work/flow.ts +12 -36
- package/src/result/work/pipe.ts +11 -33
- package/src/sized/set.ts +4 -3
- package/src/string/fuzzy.ts +2 -2
- package/src/value/merge.ts +0 -1
- package/src/value/shake.ts +3 -0
- package/src/value/smush.ts +3 -0
- package/src/value/transform.ts +9 -0
- package/src/value/unsmush.ts +3 -0
package/dist/array/index.d.mts
CHANGED
|
@@ -11,7 +11,7 @@ import { shuffle } from "../internal/array/shuffle.mjs";
|
|
|
11
11
|
import { insert } from "./insert.mjs";
|
|
12
12
|
import { intersection } from "./intersection.mjs";
|
|
13
13
|
import { partition } from "./partition.mjs";
|
|
14
|
-
import {
|
|
14
|
+
import { ArrayComparison, endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray } from "./match.mjs";
|
|
15
15
|
import { push } from "./push.mjs";
|
|
16
16
|
import { reverse } from "./reverse.mjs";
|
|
17
17
|
import { select } from "./select.mjs";
|
|
@@ -23,4 +23,4 @@ import { toggle } from "./toggle.mjs";
|
|
|
23
23
|
import { union } from "./union.mjs";
|
|
24
24
|
import { unique } from "./unique.mjs";
|
|
25
25
|
import { update } from "./update.mjs";
|
|
26
|
-
export {
|
|
26
|
+
export { ArrayComparison, chunk, compact, difference, drop, endsWithArray, exists, find, findLast, flatten, getArray, getArrayComparison, includesArray, indexOf, indexOfArray, insert, intersection, lastIndexOf, partition, push, range, reverse, select, shuffle, single, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
|
package/dist/array/index.mjs
CHANGED
|
@@ -11,7 +11,7 @@ import { getArray } from "./get.mjs";
|
|
|
11
11
|
import { insert } from "./insert.mjs";
|
|
12
12
|
import { intersection } from "./intersection.mjs";
|
|
13
13
|
import { partition } from "./partition.mjs";
|
|
14
|
-
import { endsWithArray,
|
|
14
|
+
import { endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray } from "./match.mjs";
|
|
15
15
|
import { push } from "./push.mjs";
|
|
16
16
|
import { reverse } from "./reverse.mjs";
|
|
17
17
|
import { select } from "./select.mjs";
|
|
@@ -23,4 +23,4 @@ import { toggle } from "./toggle.mjs";
|
|
|
23
23
|
import { union } from "./union.mjs";
|
|
24
24
|
import { unique } from "./unique.mjs";
|
|
25
25
|
import { update } from "./update.mjs";
|
|
26
|
-
export { chunk, compact, difference, drop, endsWithArray, exists, find, findLast, flatten, getArray,
|
|
26
|
+
export { chunk, compact, difference, drop, endsWithArray, exists, find, findLast, flatten, getArray, getArrayComparison, includesArray, indexOf, indexOfArray, insert, intersection, lastIndexOf, partition, push, range, reverse, select, shuffle, single, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { PlainObject } from "../models.mjs";
|
|
2
2
|
|
|
3
|
-
//#region src/array/
|
|
4
|
-
|
|
3
|
+
//#region src/array/match.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Comparison of an array within another array
|
|
6
|
+
*/
|
|
7
|
+
type ArrayComparison = 'end' | 'inside' | 'invalid' | 'outside' | 'same' | 'start';
|
|
5
8
|
/**
|
|
6
9
|
* Does the needle array end the haystack array?
|
|
7
10
|
* @param haystack Haystack array
|
|
@@ -32,7 +35,7 @@ declare function endsWithArray<Item>(haystack: Item[], needle: Item[]): boolean;
|
|
|
32
35
|
* @param key Key to get an item's value for matching
|
|
33
36
|
* @returns Position of the needle within the haystack
|
|
34
37
|
*/
|
|
35
|
-
declare function
|
|
38
|
+
declare function getArrayComparison<Item extends PlainObject>(haystack: Item[], needle: Item[], key: keyof Item): ArrayComparison;
|
|
36
39
|
/**
|
|
37
40
|
* Get the position of an array within another array
|
|
38
41
|
* @param haystack Haystack array
|
|
@@ -40,14 +43,14 @@ declare function getArrayPosition<Item extends PlainObject>(haystack: Item[], ne
|
|
|
40
43
|
* @param callback Callback to get an item's value for matching
|
|
41
44
|
* @returns Position of the needle within the haystack
|
|
42
45
|
*/
|
|
43
|
-
declare function
|
|
46
|
+
declare function getArrayComparison<Item>(haystack: Item[], needle: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): ArrayComparison;
|
|
44
47
|
/**
|
|
45
48
|
* Get the position of an array within another array
|
|
46
49
|
* @param haystack Haystack array
|
|
47
50
|
* @param needle Needle array
|
|
48
51
|
* @returns Position of the needle within the haystack
|
|
49
52
|
*/
|
|
50
|
-
declare function
|
|
53
|
+
declare function getArrayComparison<Item>(haystack: Item[], needle: Item[]): ArrayComparison;
|
|
51
54
|
/**
|
|
52
55
|
* Does the needle array exist within the haystack array?
|
|
53
56
|
* @param haystack Haystack array
|
|
@@ -118,4 +121,4 @@ declare function startsWithArray<Item>(haystack: Item[], needle: Item[], callbac
|
|
|
118
121
|
*/
|
|
119
122
|
declare function startsWithArray<Item>(haystack: Item[], needle: Item[]): boolean;
|
|
120
123
|
//#endregion
|
|
121
|
-
export {
|
|
124
|
+
export { ArrayComparison, endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { getArrayCallback } from "../internal/array/callbacks.mjs";
|
|
2
|
-
//#region src/array/
|
|
2
|
+
//#region src/array/match.ts
|
|
3
3
|
function endsWithArray(haystack, needle, key) {
|
|
4
4
|
return endings.has(getPosition(haystack, needle, key)[1]);
|
|
5
5
|
}
|
|
6
|
-
function
|
|
6
|
+
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 ? COMPARISON_SAME : COMPARISON_START;
|
|
11
|
+
return start + needle === haystack ? COMPARISON_END : 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 = new Set([
|
|
53
|
-
const invalid = [-1,
|
|
54
|
-
const outside = [-1,
|
|
55
|
-
const outsides = new Set([
|
|
56
|
-
const starts = new Set([
|
|
46
|
+
const COMPARISON_END = "end";
|
|
47
|
+
const COMPARISON_INSIDE = "inside";
|
|
48
|
+
const COMPARISON_INVALID = "invalid";
|
|
49
|
+
const COMPARISON_OUTSIDE = "outside";
|
|
50
|
+
const COMPARISON_SAME = "same";
|
|
51
|
+
const COMPARISON_START = "start";
|
|
52
|
+
const endings = new Set([COMPARISON_END, COMPARISON_SAME]);
|
|
53
|
+
const invalid = [-1, COMPARISON_INVALID];
|
|
54
|
+
const outside = [-1, COMPARISON_OUTSIDE];
|
|
55
|
+
const outsides = new Set([COMPARISON_INVALID, COMPARISON_OUTSIDE]);
|
|
56
|
+
const starts = new Set([COMPARISON_START, COMPARISON_SAME]);
|
|
57
57
|
//#endregion
|
|
58
|
-
export { endsWithArray,
|
|
58
|
+
export { endsWithArray, getArrayComparison, includesArray, indexOfArray, startsWithArray };
|
package/dist/array/move.mjs
CHANGED
package/dist/array/single.mjs
CHANGED
|
@@ -2,9 +2,9 @@ import { findValues } from "../internal/array/find.mjs";
|
|
|
2
2
|
//#region src/array/single.ts
|
|
3
3
|
function single(array, ...parameters) {
|
|
4
4
|
const { matched } = findValues("all", array, parameters);
|
|
5
|
-
if (matched.length > 1) throw new Error(
|
|
5
|
+
if (matched.length > 1) throw new Error(SINGLE_MESSAGE);
|
|
6
6
|
return matched[0];
|
|
7
7
|
}
|
|
8
|
-
const
|
|
8
|
+
const SINGLE_MESSAGE = "Multiple items were found";
|
|
9
9
|
//#endregion
|
|
10
10
|
export { single };
|
package/dist/array/sort.d.mts
CHANGED
|
@@ -65,6 +65,11 @@ type ComparisonSorter<Item> = (first: Item, second: Item) => number;
|
|
|
65
65
|
* Direction to sort by
|
|
66
66
|
*/
|
|
67
67
|
type SortDirection = 'ascending' | 'descending';
|
|
68
|
+
/**
|
|
69
|
+
* Sorter for an array with predefined sorters
|
|
70
|
+
*
|
|
71
|
+
* Can be used to sort an array, get the predicted index for an item, and check if an array is sorted
|
|
72
|
+
*/
|
|
68
73
|
type Sorter<Item> = {
|
|
69
74
|
/**
|
|
70
75
|
* Sort an array of items
|
|
@@ -93,7 +98,7 @@ type Sorter<Item> = {
|
|
|
93
98
|
*
|
|
94
99
|
* _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
|
|
95
100
|
*
|
|
96
|
-
* Available as `getSortedIndex` and `sort.
|
|
101
|
+
* Available as `getSortedIndex` and `sort.getIndex`
|
|
97
102
|
* @param array Array to get the index from
|
|
98
103
|
* @param item Item to get the index for
|
|
99
104
|
* @param sorters Sorters to use to determine sorting
|
|
@@ -106,7 +111,7 @@ declare function getSortedIndex<Item>(array: Item[], item: Item, sorters: Array<
|
|
|
106
111
|
*
|
|
107
112
|
* _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
|
|
108
113
|
*
|
|
109
|
-
* Available as `getSortedIndex` and `sort.
|
|
114
|
+
* Available as `getSortedIndex` and `sort.getIndex`
|
|
110
115
|
* @param array Array to get the index from
|
|
111
116
|
* @param item Item to get the index for
|
|
112
117
|
* @param sorter Sorter to use to determine sorting
|
|
@@ -119,7 +124,7 @@ declare function getSortedIndex<Item>(array: Item[], item: Item, sorter: ArraySo
|
|
|
119
124
|
*
|
|
120
125
|
* _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
|
|
121
126
|
*
|
|
122
|
-
* Available as `getSortedIndex` and `sort.
|
|
127
|
+
* Available as `getSortedIndex` and `sort.getIndex`
|
|
123
128
|
* @param array Array to get the index from
|
|
124
129
|
* @param item Item to get the index for
|
|
125
130
|
* @param descending Sorted in descending order? _(defaults to `false`)_
|
|
@@ -209,7 +214,7 @@ declare function sort<Item>(array: Item[], sorter: ArraySorter<Item>, descending
|
|
|
209
214
|
*/
|
|
210
215
|
declare function sort<Item>(array: Item[], descending?: boolean): Item[];
|
|
211
216
|
declare namespace sort {
|
|
212
|
-
var
|
|
217
|
+
var getIndex: typeof getSortedIndex;
|
|
213
218
|
var initialize: typeof initializeSorter;
|
|
214
219
|
var is: typeof isSorted;
|
|
215
220
|
}
|
package/dist/array/sort.mjs
CHANGED
|
@@ -98,9 +98,9 @@ function isSortedArray(array, sorters) {
|
|
|
98
98
|
if (length < 2) return true;
|
|
99
99
|
const sortersLength = sorters.length;
|
|
100
100
|
let offset = 0;
|
|
101
|
-
if (length >=
|
|
102
|
-
offset = Math.round(length /
|
|
103
|
-
offset = offset >
|
|
101
|
+
if (length >= SORT_THRESHOLD) {
|
|
102
|
+
offset = Math.round(length / SORT_PEEK_PERCENTAGE);
|
|
103
|
+
offset = offset > SORT_THRESHOLD ? SORT_THRESHOLD : offset;
|
|
104
104
|
for (let index = 0; index < offset; index += 1) {
|
|
105
105
|
const [firstItem, firstOffset] = [array[index], array[index + 1]];
|
|
106
106
|
const [secondItem, secondOffset] = [array[length - index - 2], array[length - index - 1]];
|
|
@@ -124,11 +124,11 @@ function sortArray(array, sorters) {
|
|
|
124
124
|
const { length } = sorters;
|
|
125
125
|
return array.length > 1 ? array.sort((first, second) => getComparisonValue(first, second, sorters, length)) : array;
|
|
126
126
|
}
|
|
127
|
-
sort.
|
|
127
|
+
sort.getIndex = getSortedIndex;
|
|
128
128
|
sort.initialize = initializeSorter;
|
|
129
129
|
sort.is = isSorted;
|
|
130
|
-
const
|
|
131
|
-
const
|
|
130
|
+
const SORT_PEEK_PERCENTAGE = 10;
|
|
131
|
+
const SORT_THRESHOLD = 100;
|
|
132
132
|
const SORT_DIRECTION_ASCENDING = "ascending";
|
|
133
133
|
const SORT_DIRECTION_DESCENDING = "descending";
|
|
134
134
|
const modifiers = {
|
package/dist/array/swap.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getArrayCallback } from "../internal/array/callbacks.mjs";
|
|
2
2
|
import { indexOf } from "../internal/array/index-of.mjs";
|
|
3
|
-
import { indexOfArray } from "./
|
|
3
|
+
import { indexOfArray } from "./match.mjs";
|
|
4
4
|
import { arraysOverlap } from "../internal/array/overlap.mjs";
|
|
5
5
|
//#region src/array/swap.ts
|
|
6
6
|
function swap(array, first, second, third) {
|
package/dist/beacon.d.mts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
//#region src/beacon.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A beacon is a lighthouse, holding an observable value that can be subscribed to and emitted from
|
|
4
|
+
*/
|
|
2
5
|
declare class Beacon<Value> {
|
|
3
6
|
#private;
|
|
4
7
|
/**
|
|
@@ -48,6 +51,9 @@ type BeaconOptions<Value> = {
|
|
|
48
51
|
*/
|
|
49
52
|
equal?: (first: Value, second: Value) => boolean;
|
|
50
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* An observable holds a value and allows observers to subscribe to changes in that value
|
|
56
|
+
*/
|
|
51
57
|
declare class Observable<Value> {
|
|
52
58
|
#private;
|
|
53
59
|
constructor(instance: Beacon<Value>, observers: Map<Subscription<Value>, Observer<Value>>);
|
|
@@ -75,6 +81,9 @@ type ObservableState<Value> = {
|
|
|
75
81
|
closed: boolean;
|
|
76
82
|
observers: Map<Subscription<Value>, Observer<Value>>;
|
|
77
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* An observer receives notifications from an observable
|
|
86
|
+
*/
|
|
78
87
|
type Observer<Value> = {
|
|
79
88
|
/**
|
|
80
89
|
* Callback for when the observable is completed
|
|
@@ -89,6 +98,9 @@ type Observer<Value> = {
|
|
|
89
98
|
*/
|
|
90
99
|
next?: (value: Value) => void;
|
|
91
100
|
};
|
|
101
|
+
/**
|
|
102
|
+
* A subscription represents an active subscription to an observable, holding its state and allowing it to be destroyed or unsubscribed from
|
|
103
|
+
*/
|
|
92
104
|
declare class Subscription<Value> {
|
|
93
105
|
#private;
|
|
94
106
|
constructor(state: ObservableState<Value>);
|
package/dist/beacon.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { noop } from "./internal/function/misc.mjs";
|
|
2
2
|
import { isPlainObject } from "./internal/is.mjs";
|
|
3
3
|
//#region src/beacon.ts
|
|
4
|
+
/**
|
|
5
|
+
* A beacon is a lighthouse, holding an observable value that can be subscribed to and emitted from
|
|
6
|
+
*/
|
|
4
7
|
var Beacon = class {
|
|
5
8
|
#options;
|
|
6
9
|
#state;
|
|
@@ -71,6 +74,9 @@ var Beacon = class {
|
|
|
71
74
|
if (finish === true) finishBeacon(this.#state, true);
|
|
72
75
|
}
|
|
73
76
|
};
|
|
77
|
+
/**
|
|
78
|
+
* An observable holds a value and allows observers to subscribe to changes in that value
|
|
79
|
+
*/
|
|
74
80
|
var Observable = class {
|
|
75
81
|
#state;
|
|
76
82
|
constructor(instance, observers) {
|
|
@@ -95,6 +101,9 @@ var Observable = class {
|
|
|
95
101
|
return instance;
|
|
96
102
|
}
|
|
97
103
|
};
|
|
104
|
+
/**
|
|
105
|
+
* A subscription represents an active subscription to an observable, holding its state and allowing it to be destroyed or unsubscribed from
|
|
106
|
+
*/
|
|
98
107
|
var Subscription = class {
|
|
99
108
|
#state;
|
|
100
109
|
constructor(state) {
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { HSLAColor, HSLColor, RGBAColor, RGBColor } from "./models.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/color/instance.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A color that is represented in multiple color formats
|
|
6
|
+
*/
|
|
4
7
|
declare class Color {
|
|
5
8
|
#private;
|
|
9
|
+
/**
|
|
10
|
+
* A property to identify this as a Color instance, used for type checking
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
6
14
|
private readonly $color;
|
|
7
15
|
/**
|
|
8
16
|
* Get the alpha channel
|
package/dist/color/instance.mjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import "./constants.mjs";
|
|
2
2
|
import { formatColor } from "./misc/index.mjs";
|
|
3
3
|
import { getAlpha } from "./misc/alpha.mjs";
|
|
4
|
-
import {
|
|
4
|
+
import { getColorState, setHSLColor, setHexColor, setRGBColor } from "./misc/state.mjs";
|
|
5
5
|
//#region src/color/instance.ts
|
|
6
|
+
/**
|
|
7
|
+
* A color that is represented in multiple color formats
|
|
8
|
+
*/
|
|
6
9
|
var Color = class {
|
|
7
10
|
#state;
|
|
8
11
|
/**
|
|
@@ -96,7 +99,7 @@ var Color = class {
|
|
|
96
99
|
setRGBColor(this.#state, value, true);
|
|
97
100
|
}
|
|
98
101
|
constructor(value) {
|
|
99
|
-
this.#state =
|
|
102
|
+
this.#state = getColorState(value);
|
|
100
103
|
Object.defineProperty(this, "$color", { value: true });
|
|
101
104
|
}
|
|
102
105
|
toHexString(alpha) {
|
package/dist/color/misc/get.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { clamp } from "../../internal/number.mjs";
|
|
2
2
|
import { HEX_BLACK, HEX_WHITE, SRGB_LUMINANCE_EXPONENT, SRGB_LUMINANCE_MODIFIER, SRGB_LUMINANCE_MULTIPLIER, SRGB_LUMINANCE_OFFSET } from "../constants.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { getColorState } from "./state.mjs";
|
|
4
4
|
import { Color } from "../instance.mjs";
|
|
5
5
|
//#region src/color/misc/get.ts
|
|
6
6
|
function getClampedValue(value, minimum, maximum) {
|
|
@@ -12,7 +12,7 @@ function getClampedValue(value, minimum, maximum) {
|
|
|
12
12
|
* @returns Foreground color
|
|
13
13
|
*/
|
|
14
14
|
function getForegroundColor(value) {
|
|
15
|
-
const { blue, green, red } =
|
|
15
|
+
const { blue, green, red } = getColorState(value).rgb;
|
|
16
16
|
const values = [
|
|
17
17
|
blue / 255,
|
|
18
18
|
green / 255,
|
|
@@ -32,7 +32,7 @@ function getForegroundColor(value) {
|
|
|
32
32
|
* @returns Hex color
|
|
33
33
|
*/
|
|
34
34
|
function getHexaColor(value) {
|
|
35
|
-
const { alpha, hex } =
|
|
35
|
+
const { alpha, hex } = getColorState(value);
|
|
36
36
|
return `${hex}${alpha.hex}`;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
@@ -41,7 +41,7 @@ function getHexaColor(value) {
|
|
|
41
41
|
* @returns Hex color
|
|
42
42
|
*/
|
|
43
43
|
function getHexColor(value) {
|
|
44
|
-
return
|
|
44
|
+
return getColorState(value).hex;
|
|
45
45
|
}
|
|
46
46
|
function getHexValue(value) {
|
|
47
47
|
return getClampedValue(value, 0, 255);
|
|
@@ -55,7 +55,7 @@ function getDegrees(value) {
|
|
|
55
55
|
* @returns HSLA color
|
|
56
56
|
*/
|
|
57
57
|
function getHslaColor(value) {
|
|
58
|
-
const { alpha, hsl } =
|
|
58
|
+
const { alpha, hsl } = getColorState(value);
|
|
59
59
|
return {
|
|
60
60
|
...hsl,
|
|
61
61
|
alpha: alpha.value
|
|
@@ -67,7 +67,7 @@ function getHslaColor(value) {
|
|
|
67
67
|
* @returns HSL color
|
|
68
68
|
*/
|
|
69
69
|
function getHslColor(value) {
|
|
70
|
-
return
|
|
70
|
+
return getColorState(value).hsl;
|
|
71
71
|
}
|
|
72
72
|
function getPercentage(value) {
|
|
73
73
|
return getClampedValue(value, 0, 100);
|
|
@@ -78,7 +78,7 @@ function getPercentage(value) {
|
|
|
78
78
|
* @returns RGBA color
|
|
79
79
|
*/
|
|
80
80
|
function getRgbaColor(value) {
|
|
81
|
-
const { alpha, rgb } =
|
|
81
|
+
const { alpha, rgb } = getColorState(value);
|
|
82
82
|
return {
|
|
83
83
|
...rgb,
|
|
84
84
|
alpha: alpha.value
|
|
@@ -90,7 +90,7 @@ function getRgbaColor(value) {
|
|
|
90
90
|
* @returns RGB color
|
|
91
91
|
*/
|
|
92
92
|
function getRgbColor(value) {
|
|
93
|
-
return
|
|
93
|
+
return getColorState(value).rgb;
|
|
94
94
|
}
|
|
95
95
|
//#endregion
|
|
96
96
|
export { getDegrees, getForegroundColor, getHexColor, getHexValue, getHexaColor, getHslColor, getHslaColor, getPercentage, getRgbColor, getRgbaColor };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ColorState } from "../models.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/color/misc/state.d.ts
|
|
4
|
-
declare function
|
|
4
|
+
declare function getColorState(value: unknown): ColorState;
|
|
5
5
|
declare function setHexColor(state: ColorState, value: string, alpha: boolean): void;
|
|
6
6
|
declare function setHSLColor(state: ColorState, value: unknown, alpha: boolean): void;
|
|
7
7
|
declare function setRGBColor(state: ColorState, value: unknown, alpha: boolean): void;
|
|
8
8
|
//#endregion
|
|
9
|
-
export {
|
|
9
|
+
export { getColorState, setHSLColor, setHexColor, setRGBColor };
|
|
@@ -6,7 +6,7 @@ import { getRgbValue, rgbToHex, rgbToHsl } from "../space/rgb.mjs";
|
|
|
6
6
|
import { getNormalizedHex, hexToRgb } from "../space/hex.mjs";
|
|
7
7
|
import { getHslValue, hslToRgb } from "../space/hsl.mjs";
|
|
8
8
|
//#region src/color/misc/state.ts
|
|
9
|
-
function
|
|
9
|
+
function getColorState(value) {
|
|
10
10
|
if (typeof value === "string") {
|
|
11
11
|
const normalized = getNormalizedHex(value, true);
|
|
12
12
|
const hex = normalized.slice(0, 6);
|
|
@@ -82,4 +82,4 @@ function setRGBColor(state, value, alpha) {
|
|
|
82
82
|
if (alpha) state.alpha = getAlpha(value.alpha);
|
|
83
83
|
}
|
|
84
84
|
//#endregion
|
|
85
|
-
export {
|
|
85
|
+
export { getColorState, setHSLColor, setHexColor, setRGBColor };
|
package/dist/color/models.d.mts
CHANGED
|
@@ -6,16 +6,46 @@ type Alpha = {
|
|
|
6
6
|
type ColorWithAlpha = {
|
|
7
7
|
alpha: number;
|
|
8
8
|
};
|
|
9
|
+
/**
|
|
10
|
+
* An _HSL_-color with an alpha channel
|
|
11
|
+
*/
|
|
9
12
|
type HSLAColor = HSLColor & ColorWithAlpha;
|
|
13
|
+
/**
|
|
14
|
+
* An _HSL_-color
|
|
15
|
+
*/
|
|
10
16
|
type HSLColor = {
|
|
17
|
+
/**
|
|
18
|
+
* Hue of the color _(in degrees; 0-360)_
|
|
19
|
+
*/
|
|
11
20
|
hue: number;
|
|
21
|
+
/**
|
|
22
|
+
* Lightness of the color _(in percentage; 0-100)_
|
|
23
|
+
*/
|
|
12
24
|
lightness: number;
|
|
25
|
+
/**
|
|
26
|
+
* Saturation of the color _(in percentage; 0-100)_
|
|
27
|
+
*/
|
|
13
28
|
saturation: number;
|
|
14
29
|
};
|
|
30
|
+
/**
|
|
31
|
+
* An _RGB_-color with an alpha channel
|
|
32
|
+
*/
|
|
15
33
|
type RGBAColor = RGBColor & ColorWithAlpha;
|
|
34
|
+
/**
|
|
35
|
+
* An _RGB_-color
|
|
36
|
+
*/
|
|
16
37
|
type RGBColor = {
|
|
38
|
+
/**
|
|
39
|
+
* Blue channel of the color _(in hexadecimal; 0-255)_
|
|
40
|
+
*/
|
|
17
41
|
blue: number;
|
|
42
|
+
/**
|
|
43
|
+
* Green channel of the color _(in hexadecimal; 0-255)_
|
|
44
|
+
*/
|
|
18
45
|
green: number;
|
|
46
|
+
/**
|
|
47
|
+
* Red channel of the color _(in hexadecimal; 0-255)_
|
|
48
|
+
*/
|
|
19
49
|
red: number;
|
|
20
50
|
};
|
|
21
51
|
type ColorProperty = 'alpha' | 'blue' | 'green' | 'hue' | 'lightness' | 'red' | 'saturation';
|
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import { Constructor } from "../models.mjs";
|
|
1
|
+
import { Constructor, NestedKeys, NestedValue, PlainObject } from "../models.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/function/assert.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Asserter for a nested property of a value
|
|
6
|
+
*/
|
|
7
|
+
type AssertProperty<Value extends PlainObject, Path extends NestedKeys<Value>, Asserted extends NestedPick<Value, Path> = NestedPick<Value, Path>> = Asserter<Asserted>;
|
|
8
|
+
/**
|
|
9
|
+
* A function that asserts a value is of a specific type, throwing an error if it is not
|
|
10
|
+
*/
|
|
4
11
|
type Asserter<Value> = (value: unknown) => asserts value is Value;
|
|
12
|
+
type NestedPick<Value, Path extends string> = Value extends PlainObject ? Path extends `${infer Head}.${infer Rest}` ? Head extends keyof Value ? { [Key in Head]: NestedPick<Value[Key], Rest> } : never : Path extends keyof Value ? { [Key in Path]: Value[Key] } : never : never;
|
|
5
13
|
/**
|
|
6
14
|
* Asserts that a condition is true, throwing an error if it is not
|
|
7
15
|
* @param condition Condition to assert
|
|
8
16
|
* @param message Error message
|
|
9
|
-
* @param error Error constructor
|
|
17
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
10
18
|
*/
|
|
11
19
|
declare function assert<Condition extends () => boolean>(condition: Condition, message: string, error?: ErrorConstructor): asserts condition;
|
|
12
20
|
declare namespace assert {
|
|
@@ -14,6 +22,7 @@ declare namespace assert {
|
|
|
14
22
|
var defined: typeof assertDefined;
|
|
15
23
|
var instanceOf: typeof assertInstanceOf;
|
|
16
24
|
var is: typeof assertIs;
|
|
25
|
+
var property: typeof assertProperty;
|
|
17
26
|
}
|
|
18
27
|
/**
|
|
19
28
|
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
@@ -21,25 +30,26 @@ declare namespace assert {
|
|
|
21
30
|
* Available as `assertCondition` and `assert.condition`
|
|
22
31
|
* @param condition Condition to assert
|
|
23
32
|
* @param message Error message
|
|
24
|
-
* @param error Error constructor
|
|
33
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
25
34
|
* @returns Asserter
|
|
26
35
|
*/
|
|
27
36
|
declare function assertCondition<Value>(condition: (value: unknown) => boolean, message: string, error?: ErrorConstructor): Asserter<Value>;
|
|
28
37
|
/**
|
|
29
|
-
* Asserts that a value is defined throwing an error if it is not
|
|
38
|
+
* Asserts that a value is defined, throwing an error if it is not
|
|
30
39
|
*
|
|
31
40
|
* Available as `assertDefined` and `assert.defined`
|
|
32
41
|
* @param value Value to assert
|
|
33
42
|
* @param message Error message
|
|
43
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
34
44
|
*/
|
|
35
|
-
declare function assertDefined<Value>(value: unknown, message?: string): asserts value is Exclude<Value, null | undefined>;
|
|
45
|
+
declare function assertDefined<Value>(value: unknown, message?: string, error?: ErrorConstructor): asserts value is Exclude<Value, null | undefined>;
|
|
36
46
|
/**
|
|
37
47
|
* Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
38
48
|
*
|
|
39
49
|
* Available as `assertInstanceOf` and `assert.instanceOf`
|
|
40
50
|
* @param constructor Constructor to check against
|
|
41
51
|
* @param message Error message
|
|
42
|
-
* @param error Error constructor
|
|
52
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
43
53
|
* @returns Asserter
|
|
44
54
|
*/
|
|
45
55
|
declare function assertInstanceOf<Value>(constructor: Constructor<Value>, message: string, error?: ErrorConstructor): Asserter<Value>;
|
|
@@ -49,9 +59,20 @@ declare function assertInstanceOf<Value>(constructor: Constructor<Value>, messag
|
|
|
49
59
|
* Available as `assertIs` and `assert.is`
|
|
50
60
|
* @param condition Type guard function to check the value
|
|
51
61
|
* @param message Error message
|
|
52
|
-
* @param error Error constructor
|
|
62
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
53
63
|
* @returns Asserter
|
|
54
64
|
*/
|
|
55
65
|
declare function assertIs<Value>(condition: (value: unknown) => value is Value, message: string, error?: ErrorConstructor): Asserter<Value>;
|
|
66
|
+
/**
|
|
67
|
+
* Creates an asserter that asserts a property of a value exists and satisfies a condition, throwing an error if it does not
|
|
68
|
+
*
|
|
69
|
+
* Available as `assertProperty` and `assert.property`
|
|
70
|
+
* @param path Path to the property to check, e.g., `foo.bar.baz` for a nested property
|
|
71
|
+
* @param condition Condition to assert for the property
|
|
72
|
+
* @param message Error message
|
|
73
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
74
|
+
* @returns Asserter
|
|
75
|
+
*/
|
|
76
|
+
declare function assertProperty<Value extends PlainObject, Path extends NestedKeys<Value>, Asserted = NestedPick<Value, Path>>(path: Path, condition: (value: NestedValue<Value, Path>) => boolean, message: string, error?: ErrorConstructor): Asserter<Asserted>;
|
|
56
77
|
//#endregion
|
|
57
|
-
export { Asserter, assert, assertCondition, assertDefined, assertInstanceOf, assertIs };
|
|
78
|
+
export { AssertProperty, Asserter, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty };
|
package/dist/function/assert.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { hasValueResult } from "../internal/value/has.mjs";
|
|
1
2
|
//#region src/function/assert.ts
|
|
2
3
|
/**
|
|
3
4
|
* Asserts that a condition is true, throwing an error if it is not
|
|
4
5
|
* @param condition Condition to assert
|
|
5
6
|
* @param message Error message
|
|
6
|
-
* @param error Error constructor
|
|
7
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
7
8
|
*/
|
|
8
9
|
function assert(condition, message, error) {
|
|
9
10
|
if (!condition()) throw new (error ?? Error)(message);
|
|
@@ -12,13 +13,14 @@ assert.condition = assertCondition;
|
|
|
12
13
|
assert.defined = assertDefined;
|
|
13
14
|
assert.instanceOf = assertInstanceOf;
|
|
14
15
|
assert.is = assertIs;
|
|
16
|
+
assert.property = assertProperty;
|
|
15
17
|
/**
|
|
16
18
|
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
17
19
|
*
|
|
18
20
|
* Available as `assertCondition` and `assert.condition`
|
|
19
21
|
* @param condition Condition to assert
|
|
20
22
|
* @param message Error message
|
|
21
|
-
* @param error Error constructor
|
|
23
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
22
24
|
* @returns Asserter
|
|
23
25
|
*/
|
|
24
26
|
function assertCondition(condition, message, error) {
|
|
@@ -27,14 +29,15 @@ function assertCondition(condition, message, error) {
|
|
|
27
29
|
};
|
|
28
30
|
}
|
|
29
31
|
/**
|
|
30
|
-
* Asserts that a value is defined throwing an error if it is not
|
|
32
|
+
* Asserts that a value is defined, throwing an error if it is not
|
|
31
33
|
*
|
|
32
34
|
* Available as `assertDefined` and `assert.defined`
|
|
33
35
|
* @param value Value to assert
|
|
34
36
|
* @param message Error message
|
|
37
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
35
38
|
*/
|
|
36
|
-
function assertDefined(value, message) {
|
|
37
|
-
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
|
|
39
|
+
function assertDefined(value, message, error) {
|
|
40
|
+
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED, error);
|
|
38
41
|
}
|
|
39
42
|
/**
|
|
40
43
|
* Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
@@ -42,7 +45,7 @@ function assertDefined(value, message) {
|
|
|
42
45
|
* Available as `assertInstanceOf` and `assert.instanceOf`
|
|
43
46
|
* @param constructor Constructor to check against
|
|
44
47
|
* @param message Error message
|
|
45
|
-
* @param error Error constructor
|
|
48
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
46
49
|
* @returns Asserter
|
|
47
50
|
*/
|
|
48
51
|
function assertInstanceOf(constructor, message, error) {
|
|
@@ -56,7 +59,7 @@ function assertInstanceOf(constructor, message, error) {
|
|
|
56
59
|
* Available as `assertIs` and `assert.is`
|
|
57
60
|
* @param condition Type guard function to check the value
|
|
58
61
|
* @param message Error message
|
|
59
|
-
* @param error Error constructor
|
|
62
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
60
63
|
* @returns Asserter
|
|
61
64
|
*/
|
|
62
65
|
function assertIs(condition, message, error) {
|
|
@@ -64,6 +67,24 @@ function assertIs(condition, message, error) {
|
|
|
64
67
|
assert(() => condition(value), message, error);
|
|
65
68
|
};
|
|
66
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates an asserter that asserts a property of a value exists and satisfies a condition, throwing an error if it does not
|
|
72
|
+
*
|
|
73
|
+
* Available as `assertProperty` and `assert.property`
|
|
74
|
+
* @param path Path to the property to check, e.g., `foo.bar.baz` for a nested property
|
|
75
|
+
* @param condition Condition to assert for the property
|
|
76
|
+
* @param message Error message
|
|
77
|
+
* @param error Error constructor _(defaults to `Error`)_
|
|
78
|
+
* @returns Asserter
|
|
79
|
+
*/
|
|
80
|
+
function assertProperty(path, condition, message, error) {
|
|
81
|
+
return (value) => {
|
|
82
|
+
assert(() => {
|
|
83
|
+
const result = hasValueResult(value, path, false);
|
|
84
|
+
return result.ok && condition(result.value);
|
|
85
|
+
}, message, error);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
67
88
|
const MESSAGE_VALUE_DEFINED = "Expected value to be defined";
|
|
68
89
|
//#endregion
|
|
69
|
-
export { assert, assertCondition, assertDefined, assertInstanceOf, assertIs };
|
|
90
|
+
export { assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty };
|