@oscarpalmer/atoms 0.75.0 → 0.76.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/js/array/chunk.cjs +2 -1
- package/dist/js/array/chunk.js +2 -1
- package/dist/js/array/count.cjs +10 -3
- package/dist/js/array/count.js +10 -3
- package/dist/js/array/exists.cjs +10 -3
- package/dist/js/array/exists.js +10 -3
- package/dist/js/array/filter.cjs +10 -3
- package/dist/js/array/filter.js +10 -3
- package/dist/js/array/find.cjs +10 -3
- package/dist/js/array/find.js +10 -3
- package/dist/js/array/group-by.cjs +18 -16
- package/dist/js/array/group-by.js +18 -16
- package/dist/js/array/index-of.cjs +10 -3
- package/dist/js/array/index-of.js +10 -3
- package/dist/js/array/index.cjs +2 -2
- package/dist/js/array/index.js +2 -2
- package/dist/js/array/insert.cjs +10 -4
- package/dist/js/array/insert.js +10 -4
- package/dist/js/array/splice.cjs +4 -4
- package/dist/js/array/splice.js +4 -4
- package/dist/js/array/to-map.cjs +11 -11
- package/dist/js/array/to-map.js +11 -11
- package/dist/js/array/to-record.cjs +3 -3
- package/dist/js/array/to-record.js +3 -3
- package/dist/js/array/unique.cjs +2 -2
- package/dist/js/array/unique.js +2 -2
- package/dist/js/colour/base.cjs +1 -1
- package/dist/js/colour/base.js +1 -1
- package/dist/js/colour/hex.cjs +2 -2
- package/dist/js/colour/hex.js +2 -2
- package/dist/js/colour/hsl.cjs +13 -7
- package/dist/js/colour/hsl.js +13 -7
- package/dist/js/colour/rgb.cjs +12 -6
- package/dist/js/colour/rgb.js +12 -6
- package/dist/js/emitter.cjs +8 -5
- package/dist/js/emitter.js +8 -5
- package/dist/js/function.cjs +6 -6
- package/dist/js/function.js +6 -6
- package/dist/js/index.cjs +0 -1
- package/dist/js/index.js +1 -2
- package/dist/js/internal/array/callbacks.cjs +23 -0
- package/dist/js/internal/array/callbacks.js +23 -0
- package/dist/js/internal/{array-find.cjs → array/find.cjs} +11 -12
- package/dist/js/internal/{array-find.js → array/find.js} +11 -12
- package/dist/js/logger.cjs +13 -13
- package/dist/js/logger.js +13 -13
- package/dist/js/query.cjs +0 -3
- package/dist/js/query.js +1 -4
- package/dist/js/random.cjs +0 -6
- package/dist/js/random.js +0 -6
- package/dist/js/sized.cjs +1 -1
- package/dist/js/sized.js +1 -1
- package/dist/js/string/index.cjs +7 -1
- package/dist/js/string/index.js +7 -1
- package/dist/js/value/compare.cjs +0 -1
- package/dist/js/value/compare.js +0 -1
- package/dist/js/value/get.cjs +3 -3
- package/dist/js/value/get.js +2 -2
- package/dist/js/value/set.cjs +4 -4
- package/dist/js/value/set.js +2 -2
- package/package.json +3 -2
- package/src/js/array/chunk.ts +6 -5
- package/src/js/array/compact.ts +8 -10
- package/src/js/array/count.ts +39 -19
- package/src/js/array/exists.ts +40 -16
- package/src/js/array/filter.ts +42 -20
- package/src/js/array/find.ts +45 -20
- package/src/js/array/group-by.ts +158 -27
- package/src/js/array/index-of.ts +40 -18
- package/src/js/array/index.ts +10 -9
- package/src/js/array/insert.ts +28 -14
- package/src/js/array/models.ts +18 -15
- package/src/js/array/shuffle.ts +3 -3
- package/src/js/array/sort.ts +28 -34
- package/src/js/array/splice.ts +22 -26
- package/src/js/array/to-map.ts +133 -45
- package/src/js/array/to-record.ts +137 -39
- package/src/js/array/unique.ts +20 -15
- package/src/js/colour/base.ts +3 -3
- package/src/js/colour/functions.ts +4 -6
- package/src/js/colour/hex.ts +5 -5
- package/src/js/colour/hsl.ts +18 -12
- package/src/js/colour/index.ts +1 -1
- package/src/js/colour/is.ts +3 -3
- package/src/js/colour/rgb.ts +17 -11
- package/src/js/emitter.ts +12 -9
- package/src/js/function.ts +12 -12
- package/src/js/index.ts +16 -16
- package/src/js/internal/array/callbacks.ts +33 -0
- package/src/js/internal/{array-find.ts → array/find.ts} +23 -24
- package/src/js/internal/{value-handle.ts → value/handle.ts} +1 -1
- package/src/js/is.ts +2 -2
- package/src/js/logger.ts +14 -14
- package/src/js/math.ts +1 -1
- package/src/js/models.ts +5 -5
- package/src/js/number.ts +2 -2
- package/src/js/query.ts +6 -10
- package/src/js/queue.ts +1 -1
- package/src/js/random.ts +9 -19
- package/src/js/sized.ts +145 -145
- package/src/js/string/case.ts +1 -1
- package/src/js/string/index.ts +13 -7
- package/src/js/string/template.ts +6 -3
- package/src/js/value/clone.ts +3 -7
- package/src/js/value/compare.ts +3 -5
- package/src/js/value/diff.ts +4 -4
- package/src/js/value/equal.ts +2 -2
- package/src/js/value/get.ts +3 -3
- package/src/js/value/index.ts +2 -2
- package/src/js/value/merge.ts +3 -3
- package/src/js/value/set.ts +3 -4
- package/src/js/value/smush.ts +4 -4
- package/src/js/value/unsmush.ts +4 -4
- package/types/array/chunk.d.cts +2 -2
- package/types/array/chunk.d.ts +2 -2
- package/types/array/compact.d.cts +4 -4
- package/types/array/compact.d.ts +4 -4
- package/types/array/count.d.cts +15 -8
- package/types/array/count.d.ts +13 -7
- package/types/array/exists.d.cts +14 -5
- package/types/array/exists.d.ts +12 -4
- package/types/array/filter.d.cts +16 -7
- package/types/array/filter.d.ts +14 -6
- package/types/array/find.d.cts +16 -7
- package/types/array/find.d.ts +14 -6
- package/types/array/group-by.d.cts +51 -5
- package/types/array/group-by.d.ts +49 -5
- package/types/array/index-of.d.cts +16 -7
- package/types/array/index-of.d.ts +14 -6
- package/types/array/index.d.cts +239 -95
- package/types/array/index.d.ts +7 -6
- package/types/array/insert.d.cts +9 -4
- package/types/array/insert.d.ts +10 -5
- package/types/array/models.d.cts +14 -11
- package/types/array/models.d.ts +14 -12
- package/types/array/shuffle.d.cts +2 -2
- package/types/array/shuffle.d.ts +2 -2
- package/types/array/sort.d.cts +11 -11
- package/types/array/sort.d.ts +10 -10
- package/types/array/splice.d.cts +4 -4
- package/types/array/splice.d.ts +4 -4
- package/types/array/to-map.d.cts +44 -20
- package/types/array/to-map.d.ts +42 -20
- package/types/array/to-record.d.cts +44 -20
- package/types/array/to-record.d.ts +42 -20
- package/types/array/unique.d.cts +11 -6
- package/types/array/unique.d.ts +10 -6
- package/types/colour/base.d.cts +28 -16
- package/types/colour/base.d.ts +2 -2
- package/types/colour/functions.d.cts +28 -16
- package/types/colour/functions.d.ts +3 -3
- package/types/colour/hex.d.cts +28 -16
- package/types/colour/hex.d.ts +4 -4
- package/types/colour/hsl.d.cts +28 -16
- package/types/colour/hsl.d.ts +16 -10
- package/types/colour/index.d.cts +28 -16
- package/types/colour/index.d.ts +1 -1
- package/types/colour/is.d.cts +28 -16
- package/types/colour/is.d.ts +3 -3
- package/types/colour/rgb.d.cts +28 -16
- package/types/colour/rgb.d.ts +15 -9
- package/types/emitter.d.cts +11 -8
- package/types/emitter.d.ts +11 -8
- package/types/function.d.cts +10 -10
- package/types/function.d.ts +11 -11
- package/types/index.d.cts +1061 -669
- package/types/index.d.ts +16 -16
- package/types/internal/array/callbacks.d.cts +11 -0
- package/types/internal/array/callbacks.d.ts +2 -0
- package/types/internal/array/find.d.cts +7 -0
- package/types/internal/array/find.d.ts +3 -0
- package/types/internal/{value-handle.d.ts → value/handle.d.ts} +1 -1
- package/types/is.d.ts +1 -1
- package/types/logger.d.cts +13 -13
- package/types/logger.d.ts +13 -13
- package/types/math.d.cts +1 -1
- package/types/math.d.ts +1 -1
- package/types/models.d.cts +4 -4
- package/types/models.d.ts +1 -4
- package/types/number.d.cts +2 -2
- package/types/number.d.ts +2 -2
- package/types/query.d.cts +2 -2
- package/types/query.d.ts +3 -3
- package/types/queue.d.cts +1 -1
- package/types/queue.d.ts +1 -1
- package/types/random.d.cts +8 -12
- package/types/random.d.ts +8 -12
- package/types/sized.d.cts +6 -6
- package/types/sized.d.ts +6 -6
- package/types/string/index.d.cts +6 -3
- package/types/string/index.d.ts +3 -3
- package/types/string/template.d.cts +3 -0
- package/types/string/template.d.ts +4 -1
- package/types/value/clone.d.cts +4 -2
- package/types/value/clone.d.ts +4 -2
- package/types/value/get.d.ts +1 -1
- package/types/value/index.d.cts +8 -6
- package/types/value/index.d.ts +2 -2
- package/types/value/merge.d.cts +1 -1
- package/types/value/merge.d.ts +2 -2
- package/types/value/set.d.ts +1 -1
- package/types/value/smush.d.cts +1 -1
- package/types/value/smush.d.ts +2 -2
- package/types/value/unsmush.d.cts +1 -1
- package/types/value/unsmush.d.ts +2 -2
- package/dist/js/internal/array-callbacks.cjs +0 -18
- package/dist/js/internal/array-callbacks.js +0 -18
- package/src/js/internal/array-callbacks.ts +0 -28
- package/types/internal/array-callbacks.d.cts +0 -13
- package/types/internal/array-callbacks.d.ts +0 -2
- package/types/internal/array-find.d.cts +0 -11
- package/types/internal/array-find.d.ts +0 -4
- /package/dist/js/internal/{value-handle.cjs → value/handle.cjs} +0 -0
- /package/dist/js/internal/{value-handle.js → value/handle.js} +0 -0
- /package/types/internal/{value-handle.d.cts → value/handle.d.cts} +0 -0
package/src/js/array/splice.ts
CHANGED
|
@@ -1,57 +1,53 @@
|
|
|
1
|
-
import {insertValues} from '
|
|
1
|
+
import {insertValues} from '~/array/insert';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Removes and returns all items from an array starting from a specific index
|
|
5
5
|
*/
|
|
6
|
-
export function splice<
|
|
6
|
+
export function splice<Item>(array: Item[], start: number): Item[];
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Removes and returns _(up to)_ a specific amount of items from an array, starting from a specific index
|
|
10
10
|
*/
|
|
11
|
-
export function splice<
|
|
12
|
-
array:
|
|
11
|
+
export function splice<Item>(
|
|
12
|
+
array: Item[],
|
|
13
13
|
start: number,
|
|
14
14
|
amount: number,
|
|
15
|
-
):
|
|
15
|
+
): Item[];
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* - Splices values into an array and returns any removed values
|
|
19
19
|
* - Uses chunking to avoid stack overflow
|
|
20
20
|
*/
|
|
21
|
-
export function splice<
|
|
22
|
-
array:
|
|
21
|
+
export function splice<Item>(
|
|
22
|
+
array: Item[],
|
|
23
23
|
start: number,
|
|
24
|
-
|
|
25
|
-
):
|
|
24
|
+
added: Item[],
|
|
25
|
+
): Item[];
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* - Splices values into an array and returns any removed values
|
|
29
29
|
* - Uses chunking to avoid stack overflow
|
|
30
30
|
*/
|
|
31
|
-
export function splice<
|
|
32
|
-
array:
|
|
31
|
+
export function splice<Item>(
|
|
32
|
+
array: Item[],
|
|
33
33
|
start: number,
|
|
34
34
|
amount: number,
|
|
35
|
-
|
|
36
|
-
):
|
|
35
|
+
added: Item[],
|
|
36
|
+
): Item[];
|
|
37
37
|
|
|
38
|
-
export function splice
|
|
39
|
-
array:
|
|
38
|
+
export function splice(
|
|
39
|
+
array: unknown[],
|
|
40
40
|
start: number,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
):
|
|
44
|
-
const
|
|
41
|
+
first?: number | unknown[],
|
|
42
|
+
second?: unknown[],
|
|
43
|
+
): unknown[] {
|
|
44
|
+
const isArray = Array.isArray(first);
|
|
45
45
|
|
|
46
46
|
return insertValues(
|
|
47
47
|
'splice',
|
|
48
48
|
array,
|
|
49
|
-
|
|
49
|
+
isArray ? first : (second ?? []),
|
|
50
50
|
start,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
: typeof amountOrValues === 'number' && amountOrValues > 0
|
|
54
|
-
? amountOrValues
|
|
55
|
-
: 0,
|
|
56
|
-
) as Value[];
|
|
51
|
+
isArray ? array.length : typeof first === 'number' && first > 0 ? first : 0,
|
|
52
|
+
) as unknown[];
|
|
57
53
|
}
|
package/src/js/array/to-map.ts
CHANGED
|
@@ -1,82 +1,170 @@
|
|
|
1
|
-
import type {KeyCallback} from '
|
|
2
|
-
import {getCallbacks} from '
|
|
3
|
-
import type {Key} from '
|
|
1
|
+
import type {KeyCallback, ValueCallback} from '~/array/models';
|
|
2
|
+
import {getCallbacks} from '~/internal/array/callbacks';
|
|
3
|
+
import type {Key, KeyedValue} from '~/models';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Create a map from an array of items _(using their indices as keys)_
|
|
7
7
|
*/
|
|
8
|
-
export function toMap<
|
|
8
|
+
export function toMap<Item>(array: Item[]): Map<number, Item>;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Create a map from an array of items using a specific key
|
|
12
12
|
*/
|
|
13
|
-
export function toMap<
|
|
14
|
-
array:
|
|
13
|
+
export function toMap<Item, Key extends keyof Item>(
|
|
14
|
+
array: Item[],
|
|
15
|
+
key: Key,
|
|
16
|
+
): Map<KeyedValue<Item, Key>, Item>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Create a map from an array of items using a specific key, and grouping them into arrays
|
|
20
|
+
*/
|
|
21
|
+
export function toMap<Item, Key extends keyof Item>(
|
|
22
|
+
array: Item[],
|
|
23
|
+
key: Key,
|
|
15
24
|
arrays: true,
|
|
16
|
-
): Map<
|
|
25
|
+
): Map<KeyedValue<Item, Key>, Item[]>;
|
|
17
26
|
|
|
18
27
|
/**
|
|
19
|
-
*
|
|
20
|
-
* - Uses `key` to find an identifcation value to use as keys
|
|
28
|
+
* Create a map from an array of items using a specific key
|
|
21
29
|
*/
|
|
22
|
-
export function toMap<
|
|
30
|
+
export function toMap<Item, Key extends KeyCallback<Item>>(
|
|
31
|
+
array: Item[],
|
|
32
|
+
key: Key,
|
|
33
|
+
): Map<ReturnType<Key>, Item>;
|
|
23
34
|
|
|
24
35
|
/**
|
|
25
|
-
*
|
|
26
|
-
* - Uses `key` to find an identifcation value to use as keys
|
|
27
|
-
* - Groups values into arrays
|
|
36
|
+
* Create a map from an array of items using a specific key, and grouping them into arrays
|
|
28
37
|
*/
|
|
29
|
-
export function toMap<
|
|
30
|
-
array:
|
|
38
|
+
export function toMap<Item, Key extends KeyCallback<Item>>(
|
|
39
|
+
array: Item[],
|
|
31
40
|
key: Key,
|
|
32
41
|
arrays: true,
|
|
33
|
-
): Map<Key
|
|
42
|
+
): Map<ReturnType<Key>, Item[]>;
|
|
34
43
|
|
|
35
44
|
/**
|
|
36
|
-
*
|
|
37
|
-
* - Uses `key` to find an identifcation value to use as keys
|
|
45
|
+
* Create a map from an array of items using a specific key and value
|
|
38
46
|
*/
|
|
39
|
-
export function toMap<Value>(
|
|
40
|
-
array:
|
|
41
|
-
key:
|
|
42
|
-
|
|
47
|
+
export function toMap<Item, Key extends keyof Item, Value extends keyof Item>(
|
|
48
|
+
array: Item[],
|
|
49
|
+
key: Key,
|
|
50
|
+
value: Value,
|
|
51
|
+
): Map<KeyedValue<Item, Key>, KeyedValue<Item, Value>>;
|
|
43
52
|
|
|
44
53
|
/**
|
|
45
|
-
*
|
|
46
|
-
* - Uses `key` to find an identifcation value to use as keys
|
|
47
|
-
* - Groups values into arrays
|
|
54
|
+
* Create a map from an array of items using a specific key and value, and grouping them into arrays
|
|
48
55
|
*/
|
|
49
|
-
export function toMap<Value>(
|
|
50
|
-
array:
|
|
51
|
-
key:
|
|
56
|
+
export function toMap<Item, Key extends keyof Item, Value extends keyof Item>(
|
|
57
|
+
array: Item[],
|
|
58
|
+
key: Key,
|
|
59
|
+
value: Value,
|
|
52
60
|
arrays: true,
|
|
53
|
-
): Map<Key, Value
|
|
61
|
+
): Map<KeyedValue<Item, Key>, Array<KeyedValue<Item, Value>>>;
|
|
54
62
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Create a map from an array of items using a specific key and value
|
|
65
|
+
*/
|
|
66
|
+
export function toMap<
|
|
67
|
+
Item,
|
|
68
|
+
Key extends keyof Item,
|
|
69
|
+
Value extends ValueCallback<Item>,
|
|
70
|
+
>(
|
|
71
|
+
array: Item[],
|
|
72
|
+
key: Key,
|
|
73
|
+
value: Value,
|
|
74
|
+
): Map<KeyedValue<Item, Key>, ReturnType<Value>>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Create a map from an array of items using a specific key and value, and grouping them into arrays
|
|
78
|
+
*/
|
|
79
|
+
export function toMap<
|
|
80
|
+
Item,
|
|
81
|
+
Key extends keyof Item,
|
|
82
|
+
Value extends ValueCallback<Item>,
|
|
83
|
+
>(
|
|
84
|
+
array: Item[],
|
|
85
|
+
key: Key,
|
|
86
|
+
value: Value,
|
|
87
|
+
arrays: true,
|
|
88
|
+
): Map<KeyedValue<Item, Key>, Array<ReturnType<Value>>>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Create a map from an array of items using a specific key and value
|
|
92
|
+
*/
|
|
93
|
+
export function toMap<
|
|
94
|
+
Item,
|
|
95
|
+
Key extends KeyCallback<Item>,
|
|
96
|
+
Value extends keyof Item,
|
|
97
|
+
>(
|
|
98
|
+
array: Item[],
|
|
99
|
+
key: Key,
|
|
100
|
+
value: Value,
|
|
101
|
+
): Map<ReturnType<Key>, KeyedValue<Item, Value>>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Create a map from an array of items using a specific key and value, and grouping them into arrays
|
|
105
|
+
*/
|
|
106
|
+
export function toMap<
|
|
107
|
+
Item,
|
|
108
|
+
Key extends KeyCallback<Item>,
|
|
109
|
+
Value extends keyof Item,
|
|
110
|
+
>(
|
|
111
|
+
array: Item[],
|
|
112
|
+
key: Key,
|
|
113
|
+
value: Value,
|
|
114
|
+
arrays: true,
|
|
115
|
+
): Map<ReturnType<Key>, Array<KeyedValue<Item, Value>>>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Create a map from an array of items using a specific key and value
|
|
119
|
+
*/
|
|
120
|
+
export function toMap<
|
|
121
|
+
Item,
|
|
122
|
+
Key extends KeyCallback<Item>,
|
|
123
|
+
Value extends ValueCallback<Item>,
|
|
124
|
+
>(
|
|
125
|
+
array: Item[],
|
|
126
|
+
key: Key,
|
|
127
|
+
value: Value,
|
|
128
|
+
): Map<ReturnType<Key>, ReturnType<Value>>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Create a map from an array of items using a specific key and value, and grouping them into arrays
|
|
132
|
+
*/
|
|
133
|
+
export function toMap<
|
|
134
|
+
Item,
|
|
135
|
+
Key extends KeyCallback<Item>,
|
|
136
|
+
Value extends ValueCallback<Item>,
|
|
137
|
+
>(
|
|
138
|
+
array: Item[],
|
|
139
|
+
key: Key,
|
|
140
|
+
value: Value,
|
|
141
|
+
arrays: true,
|
|
142
|
+
): Map<ReturnType<Key>, Array<ReturnType<Value>>>;
|
|
143
|
+
|
|
144
|
+
export function toMap(
|
|
145
|
+
array: unknown[],
|
|
146
|
+
first?: unknown,
|
|
147
|
+
second?: unknown,
|
|
148
|
+
third?: unknown,
|
|
59
149
|
): Map<Key, unknown> {
|
|
60
|
-
const asArrays = first === true || second === true;
|
|
61
|
-
const callbacks = getCallbacks(undefined, first);
|
|
62
|
-
const hasCallback = typeof callbacks?.key === 'function';
|
|
150
|
+
const asArrays = first === true || second === true || third === true;
|
|
151
|
+
const callbacks = getCallbacks(undefined, first, second);
|
|
63
152
|
const map = new Map<Key, unknown>();
|
|
64
153
|
const {length} = array;
|
|
65
154
|
|
|
66
155
|
for (let index = 0; index < length; index += 1) {
|
|
67
|
-
const
|
|
156
|
+
const item = array[index];
|
|
68
157
|
|
|
69
|
-
const key =
|
|
70
|
-
|
|
71
|
-
: index;
|
|
158
|
+
const key = callbacks?.key?.(item, index, array) ?? index;
|
|
159
|
+
const value = callbacks?.value?.(item, index, array) ?? item;
|
|
72
160
|
|
|
73
161
|
if (asArrays) {
|
|
74
162
|
const existing = map.get(key);
|
|
75
163
|
|
|
76
|
-
if (
|
|
77
|
-
existing.push(value);
|
|
78
|
-
} else {
|
|
164
|
+
if (existing == null) {
|
|
79
165
|
map.set(key, [value]);
|
|
166
|
+
} else {
|
|
167
|
+
(existing as unknown[]).push(value);
|
|
80
168
|
}
|
|
81
169
|
} else {
|
|
82
170
|
map.set(key, value);
|
|
@@ -1,66 +1,164 @@
|
|
|
1
|
-
import {groupValues} from '
|
|
2
|
-
import type {KeyCallback} from '
|
|
3
|
-
import type {
|
|
1
|
+
import {groupValues} from '~/array/group-by';
|
|
2
|
+
import type {KeyCallback, ValueCallback} from '~/array/models';
|
|
3
|
+
import type {KeyedValue, PlainObject} from '~/models';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Create a record from an array of items _(using their indices as keys)_
|
|
7
7
|
*/
|
|
8
|
-
export function toRecord<
|
|
8
|
+
export function toRecord<Item>(array: Item[]): Record<number, Item>;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Create a record from an array of items using a specific key
|
|
12
12
|
*/
|
|
13
|
-
export function toRecord<
|
|
14
|
-
array:
|
|
13
|
+
export function toRecord<Item, Key extends keyof Item>(
|
|
14
|
+
array: Item[],
|
|
15
|
+
key: Key,
|
|
16
|
+
): Record<KeyedValue<Item, Key>, Item>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Create a record from an array of items using a specific key, and grouping them into arrays
|
|
20
|
+
*/
|
|
21
|
+
export function toRecord<Item, Key extends keyof Item>(
|
|
22
|
+
array: Item[],
|
|
23
|
+
key: Key,
|
|
24
|
+
arrays: true,
|
|
25
|
+
): Record<KeyedValue<Item, Key>, Item[]>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a record from an array of items using a specific key
|
|
29
|
+
*/
|
|
30
|
+
export function toRecord<Item, Key extends KeyCallback<Item>>(
|
|
31
|
+
array: Item[],
|
|
32
|
+
key: Key,
|
|
33
|
+
): Record<ReturnType<Key>, Item>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Create a record from an array of items using a specific key, and grouping them into arrays
|
|
37
|
+
*/
|
|
38
|
+
export function toRecord<Item, Key extends KeyCallback<Item>>(
|
|
39
|
+
array: Item[],
|
|
40
|
+
key: Key,
|
|
15
41
|
arrays: true,
|
|
16
|
-
): Record<
|
|
42
|
+
): Record<ReturnType<Key>, Item[]>;
|
|
17
43
|
|
|
18
44
|
/**
|
|
19
|
-
*
|
|
20
|
-
* - Uses `key` to find an identifcation value to use as keys
|
|
45
|
+
* Create a record from an array of items using a specific key and value
|
|
21
46
|
*/
|
|
22
|
-
export function toRecord<
|
|
47
|
+
export function toRecord<
|
|
48
|
+
Item,
|
|
49
|
+
Key extends keyof Item,
|
|
50
|
+
Value extends keyof Item,
|
|
51
|
+
>(
|
|
52
|
+
array: Item[],
|
|
53
|
+
key: Key,
|
|
54
|
+
value: Value,
|
|
55
|
+
): Record<KeyedValue<Item, Key>, KeyedValue<Item, Value>>;
|
|
23
56
|
|
|
24
57
|
/**
|
|
25
|
-
*
|
|
26
|
-
* - Uses `key` to find an identifcation value to use as keys
|
|
27
|
-
* - Groups values into arrays
|
|
58
|
+
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
28
59
|
*/
|
|
29
|
-
export function toRecord<
|
|
30
|
-
|
|
60
|
+
export function toRecord<
|
|
61
|
+
Item,
|
|
62
|
+
Key extends keyof Item,
|
|
63
|
+
Value extends keyof Item,
|
|
64
|
+
>(
|
|
65
|
+
array: Item[],
|
|
31
66
|
key: Key,
|
|
67
|
+
value: Value,
|
|
32
68
|
arrays: true,
|
|
33
|
-
): Record<Key, Value
|
|
69
|
+
): Record<KeyedValue<Item, Key>, Array<KeyedValue<Item, Value>>>;
|
|
34
70
|
|
|
35
71
|
/**
|
|
36
|
-
*
|
|
37
|
-
* - Uses `key` to find an identifcation value to use as keys
|
|
72
|
+
* Create a record from an array of items using a specific key and value
|
|
38
73
|
*/
|
|
39
|
-
export function toRecord<
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
74
|
+
export function toRecord<
|
|
75
|
+
Item,
|
|
76
|
+
Key extends keyof Item,
|
|
77
|
+
Value extends ValueCallback<Item>,
|
|
78
|
+
>(
|
|
79
|
+
array: Item[],
|
|
80
|
+
key: Key,
|
|
81
|
+
value: Value,
|
|
82
|
+
): Record<KeyedValue<Item, Key>, ReturnType<Value>>;
|
|
43
83
|
|
|
44
84
|
/**
|
|
45
|
-
*
|
|
46
|
-
* - Uses `key` to find an identifcation value to use as keys
|
|
47
|
-
* - Groups values into arrays
|
|
85
|
+
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
48
86
|
*/
|
|
49
|
-
export function toRecord<
|
|
50
|
-
|
|
51
|
-
|
|
87
|
+
export function toRecord<
|
|
88
|
+
Item,
|
|
89
|
+
Key extends keyof Item,
|
|
90
|
+
Value extends ValueCallback<Item>,
|
|
91
|
+
>(
|
|
92
|
+
array: Item[],
|
|
93
|
+
key: Key,
|
|
94
|
+
value: Value,
|
|
95
|
+
arrays: true,
|
|
96
|
+
): Record<KeyedValue<Item, Key>, Array<ReturnType<Value>>>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Create a record from an array of items using a specific key and value
|
|
100
|
+
*/
|
|
101
|
+
export function toRecord<
|
|
102
|
+
Item,
|
|
103
|
+
Key extends KeyCallback<Item>,
|
|
104
|
+
Value extends keyof Item,
|
|
105
|
+
>(
|
|
106
|
+
array: Item[],
|
|
107
|
+
key: Key,
|
|
108
|
+
value: Value,
|
|
109
|
+
): Record<ReturnType<Key>, KeyedValue<Item, Value>>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
113
|
+
*/
|
|
114
|
+
export function toRecord<
|
|
115
|
+
Item,
|
|
116
|
+
Key extends KeyCallback<Item>,
|
|
117
|
+
Value extends keyof Item,
|
|
118
|
+
>(
|
|
119
|
+
array: Item[],
|
|
120
|
+
key: Key,
|
|
121
|
+
value: Value,
|
|
122
|
+
arrays: true,
|
|
123
|
+
): Record<ReturnType<Key>, Array<KeyedValue<Item, Value>>>;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Create a record from an array of items using a specific key and value
|
|
127
|
+
*/
|
|
128
|
+
export function toRecord<
|
|
129
|
+
Item,
|
|
130
|
+
Key extends KeyCallback<Item>,
|
|
131
|
+
Value extends ValueCallback<Item>,
|
|
132
|
+
>(
|
|
133
|
+
array: Item[],
|
|
134
|
+
key: Key,
|
|
135
|
+
value: Value,
|
|
136
|
+
): Record<ReturnType<Key>, ReturnType<Value>>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
140
|
+
*/
|
|
141
|
+
export function toRecord<
|
|
142
|
+
Item,
|
|
143
|
+
Key extends KeyCallback<Item>,
|
|
144
|
+
Value extends ValueCallback<Item>,
|
|
145
|
+
>(
|
|
146
|
+
array: Item[],
|
|
147
|
+
key: Key,
|
|
148
|
+
value: Value,
|
|
52
149
|
arrays: true,
|
|
53
|
-
): Record<Key
|
|
150
|
+
): Record<ReturnType<Key>, Array<ReturnType<Value>>>;
|
|
54
151
|
|
|
55
|
-
export function toRecord
|
|
56
|
-
array:
|
|
57
|
-
first?:
|
|
58
|
-
second?:
|
|
59
|
-
|
|
152
|
+
export function toRecord(
|
|
153
|
+
array: unknown[],
|
|
154
|
+
first?: unknown,
|
|
155
|
+
second?: unknown,
|
|
156
|
+
third?: unknown,
|
|
157
|
+
): PlainObject {
|
|
60
158
|
return groupValues(
|
|
61
159
|
array,
|
|
62
|
-
first as
|
|
63
|
-
|
|
64
|
-
true,
|
|
160
|
+
first as never,
|
|
161
|
+
second,
|
|
162
|
+
first === true || second === true || third === true,
|
|
65
163
|
);
|
|
66
164
|
}
|
package/src/js/array/unique.ts
CHANGED
|
@@ -1,24 +1,29 @@
|
|
|
1
|
-
import type {KeyCallback} from '
|
|
2
|
-
import {findValues} from '
|
|
3
|
-
import type {Key} from '@/models';
|
|
1
|
+
import type {KeyCallback} from '~/array/models';
|
|
2
|
+
import {findValues} from '~/internal/array/find';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
|
-
*
|
|
5
|
+
* Get an array of unique items
|
|
7
6
|
*/
|
|
8
|
-
export function unique<
|
|
7
|
+
export function unique<Item>(array: Item[]): Item[];
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
|
-
* -
|
|
10
|
+
* - Get an array of unique items
|
|
12
11
|
* - Use `key` to find a comparison value to match with `value`
|
|
13
12
|
*/
|
|
14
|
-
export function unique<
|
|
15
|
-
array:
|
|
16
|
-
key: Key
|
|
17
|
-
):
|
|
13
|
+
export function unique<Item, Key extends keyof Item>(
|
|
14
|
+
array: Item[],
|
|
15
|
+
key: Key,
|
|
16
|
+
): Item[];
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
/**
|
|
19
|
+
* - Get an array of unique items
|
|
20
|
+
* - Use `key` to find a comparison value to match with `value`
|
|
21
|
+
*/
|
|
22
|
+
export function unique<Item, Key extends KeyCallback<Item>>(
|
|
23
|
+
array: Item[],
|
|
24
|
+
key: Key,
|
|
25
|
+
): Item[];
|
|
26
|
+
|
|
27
|
+
export function unique(array: unknown[], key?: unknown): unknown[] {
|
|
28
|
+
return findValues('unique', array, undefined, key, undefined);
|
|
24
29
|
}
|
package/src/js/colour/base.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type {HexColour} from '
|
|
2
|
-
import {isColourValue} from '
|
|
1
|
+
import type {HexColour} from '~/colour/hex';
|
|
2
|
+
import {isColourValue} from '~/colour/is';
|
|
3
3
|
|
|
4
4
|
export abstract class Colour<Model> {
|
|
5
5
|
private declare readonly $colour: string;
|
|
6
6
|
protected declare readonly state: ColourState<Model>;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Get the current value of the colour
|
|
10
10
|
*/
|
|
11
11
|
get value(): Model {
|
|
12
12
|
return {...this.state.value};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {HexColour} from '
|
|
2
|
-
import {HSLColour, type HSLColourValue} from '
|
|
3
|
-
import {RGBColour, type RGBColourValue} from '
|
|
4
|
-
import {clamp} from '
|
|
1
|
+
import {HexColour} from '~/colour/hex';
|
|
2
|
+
import {HSLColour, type HSLColourValue} from '~/colour/hsl';
|
|
3
|
+
import {RGBColour, type RGBColourValue} from '~/colour/rgb';
|
|
4
|
+
import {clamp} from '~/number';
|
|
5
5
|
|
|
6
6
|
export const anyPattern = /^#*([a-f0-9]{3}){1,2}$/i;
|
|
7
7
|
const groupedPattern = /^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
@@ -115,8 +115,6 @@ export function rgbToHsl(rgb: RGBColourValue): HSLColour {
|
|
|
115
115
|
case red:
|
|
116
116
|
hue = (green - blue) / delta + (green < blue ? 6 : 0);
|
|
117
117
|
break;
|
|
118
|
-
default:
|
|
119
|
-
break;
|
|
120
118
|
}
|
|
121
119
|
|
|
122
120
|
hue *= 60;
|
package/src/js/colour/hex.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {anyPattern, getNormalisedHex, hexToRgb} from '
|
|
2
|
-
import type {HSLColour} from '
|
|
3
|
-
import type {RGBColour} from '
|
|
1
|
+
import {anyPattern, getNormalisedHex, hexToRgb} from '~/colour/functions';
|
|
2
|
+
import type {HSLColour} from '~/colour/hsl';
|
|
3
|
+
import type {RGBColour} from '~/colour/rgb';
|
|
4
4
|
|
|
5
5
|
type State = {
|
|
6
6
|
value: string;
|
|
@@ -11,14 +11,14 @@ export class HexColour {
|
|
|
11
11
|
private readonly state: State;
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* Get the value of the colour
|
|
15
15
|
*/
|
|
16
16
|
get value(): string {
|
|
17
17
|
return `#${this.state.value}`;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Set the value of the colour
|
|
22
22
|
*/
|
|
23
23
|
set value(value: string) {
|
|
24
24
|
this.state.value = anyPattern.test(value)
|