@oscarpalmer/atoms 0.74.1 → 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 +18 -0
- package/dist/js/array/{chunk.mjs → chunk.js} +2 -2
- package/dist/js/array/compact.cjs +6 -0
- package/dist/js/array/{compact.mjs → compact.js} +0 -1
- package/dist/js/array/count.cjs +14 -0
- package/dist/js/array/count.js +14 -0
- package/dist/js/array/exists.cjs +14 -0
- package/dist/js/array/exists.js +14 -0
- package/dist/js/array/filter.cjs +14 -0
- package/dist/js/array/filter.js +14 -0
- package/dist/js/array/find.cjs +14 -0
- package/dist/js/array/find.js +14 -0
- package/dist/js/array/group-by.cjs +35 -0
- package/dist/js/array/group-by.js +35 -0
- package/dist/js/array/index-of.cjs +14 -0
- package/dist/js/array/index-of.js +14 -0
- package/dist/js/array/index.cjs +40 -0
- package/dist/js/array/index.js +34 -381
- package/dist/js/array/insert.cjs +31 -0
- package/dist/js/array/insert.js +31 -0
- package/dist/js/array/models.cjs +1 -0
- package/dist/js/array/models.js +1 -0
- package/dist/js/array/shuffle.cjs +13 -0
- package/dist/js/array/{shuffle.mjs → shuffle.js} +2 -3
- package/dist/js/array/sort.cjs +53 -0
- package/dist/js/array/{sort.mjs → sort.js} +13 -9
- package/dist/js/array/splice.cjs +14 -0
- package/dist/js/array/splice.js +14 -0
- package/dist/js/array/to-map.cjs +27 -0
- package/dist/js/array/to-map.js +27 -0
- package/dist/js/array/to-record.cjs +12 -0
- package/dist/js/array/to-record.js +12 -0
- package/dist/js/array/unique.cjs +7 -0
- package/dist/js/array/unique.js +7 -0
- package/dist/js/colour/base.cjs +18 -0
- package/dist/js/colour/{base.mjs → base.js} +4 -3
- package/dist/js/colour/functions.cjs +96 -0
- package/dist/js/colour/{functions.mjs → functions.js} +18 -19
- package/dist/js/colour/hex.cjs +56 -0
- package/dist/js/colour/{hex.mjs → hex.js} +34 -15
- package/dist/js/colour/hsl.cjs +85 -0
- package/dist/js/colour/{hsl.mjs → hsl.js} +40 -12
- package/dist/js/colour/index.cjs +29 -0
- package/dist/js/colour/index.js +14 -272
- package/dist/js/colour/is.cjs +27 -0
- package/dist/js/colour/{is.mjs → is.js} +7 -6
- package/dist/js/colour/rgb.cjs +87 -0
- package/dist/js/colour/{rgb.mjs → rgb.js} +43 -12
- package/dist/js/{emitter.mjs → emitter.cjs} +88 -56
- package/dist/js/emitter.js +85 -56
- package/dist/js/{function.mjs → function.cjs} +66 -46
- package/dist/js/function.js +61 -48
- package/dist/js/index.cjs +136 -0
- package/dist/js/index.js +133 -1724
- 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 +51 -0
- package/dist/js/internal/array/find.js +51 -0
- package/dist/js/internal/value/handle.cjs +21 -0
- package/dist/js/internal/{value-handle.mjs → value/handle.js} +0 -1
- package/dist/js/{is.mjs → is.cjs} +17 -18
- package/dist/js/is.js +15 -27
- package/dist/js/logger.cjs +108 -0
- package/dist/js/logger.js +44 -8
- package/dist/js/{math.mjs → math.cjs} +7 -8
- package/dist/js/math.js +4 -5
- package/dist/js/models.cjs +1 -0
- package/dist/js/models.js +1 -0
- package/dist/js/{number.mjs → number.cjs} +8 -8
- package/dist/js/number.js +5 -5
- package/dist/js/{query.mjs → query.cjs} +18 -18
- package/dist/js/query.js +17 -86
- package/dist/js/{queue.mjs → queue.cjs} +12 -13
- package/dist/js/queue.js +9 -10
- package/dist/js/{random.mjs → random.cjs} +14 -21
- package/dist/js/random.js +9 -26
- package/dist/js/{sized.mjs → sized.cjs} +68 -33
- package/dist/js/sized.js +66 -38
- package/dist/js/string/case.cjs +41 -0
- package/dist/js/string/{case.mjs → case.js} +13 -9
- package/dist/js/string/index.cjs +59 -0
- package/dist/js/string/index.js +30 -100
- package/dist/js/string/template.cjs +21 -0
- package/dist/js/string/{template.mjs → template.js} +4 -5
- package/dist/js/{touch.mjs → touch.cjs} +4 -7
- package/dist/js/touch.js +3 -5
- package/dist/js/value/clone.cjs +76 -0
- package/dist/js/value/{clone.mjs → clone.js} +5 -6
- package/dist/js/value/compare.cjs +59 -0
- package/dist/js/value/{compare.mjs → compare.js} +4 -6
- package/dist/js/value/diff.cjs +75 -0
- package/dist/js/value/{diff.mjs → diff.js} +13 -11
- package/dist/js/value/equal.cjs +106 -0
- package/dist/js/value/{equal.mjs → equal.js} +13 -8
- package/dist/js/value/get.cjs +21 -0
- package/dist/js/value/{get.mjs → get.js} +9 -4
- package/dist/js/value/index.cjs +30 -0
- package/dist/js/value/index.js +19 -510
- package/dist/js/value/merge.cjs +35 -0
- package/dist/js/value/{merge.mjs → merge.js} +7 -6
- package/dist/js/value/set.cjs +25 -0
- package/dist/js/value/{set.mjs → set.js} +3 -4
- package/dist/js/value/smush.cjs +26 -0
- package/dist/js/value/{smush.mjs → smush.js} +3 -4
- package/dist/js/value/unsmush.cjs +39 -0
- package/dist/js/value/{unsmush.mjs → unsmush.js} +10 -7
- package/package.json +50 -47
- 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 +39 -15
- 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 +32 -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 +17 -17
- 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 +8 -0
- package/types/array/chunk.d.ts +2 -2
- package/types/array/compact.d.cts +12 -0
- package/types/array/compact.d.ts +4 -4
- package/types/array/count.d.cts +24 -0
- package/types/array/count.d.ts +13 -7
- package/types/array/exists.d.cts +26 -0
- package/types/array/exists.d.ts +11 -3
- package/types/array/filter.d.cts +26 -0
- package/types/array/filter.d.ts +14 -6
- package/types/array/find.d.cts +26 -0
- package/types/array/find.d.ts +14 -6
- package/types/array/group-by.d.cts +58 -0
- package/types/array/group-by.d.ts +49 -5
- package/types/array/index-of.d.cts +26 -0
- 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 +16 -0
- 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 +8 -0
- package/types/array/shuffle.d.ts +2 -2
- package/types/array/sort.d.cts +24 -0
- package/types/array/sort.d.ts +10 -10
- package/types/array/splice.d.cts +22 -0
- package/types/array/splice.d.ts +4 -4
- package/types/array/to-map.d.cts +61 -0
- package/types/array/to-map.d.ts +42 -20
- package/types/array/to-record.d.cts +61 -0
- package/types/array/to-record.d.ts +42 -20
- package/types/array/unique.d.cts +21 -0
- package/types/array/unique.d.ts +10 -6
- package/types/colour/base.d.cts +153 -0
- package/types/colour/base.d.ts +2 -2
- package/types/colour/functions.d.cts +173 -0
- package/types/colour/functions.d.ts +3 -3
- package/types/colour/hex.d.cts +157 -0
- package/types/colour/hex.d.ts +4 -4
- package/types/colour/hsl.d.cts +157 -0
- 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 +170 -0
- package/types/colour/is.d.ts +3 -3
- package/types/colour/rgb.d.cts +157 -0
- 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 +1120 -736
- 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.cts +37 -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 -21
- package/types/logger.d.ts +13 -21
- 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/case.d.cts +28 -0
- package/types/string/index.d.cts +30 -27
- package/types/string/index.d.ts +3 -3
- package/types/string/template.d.cts +50 -0
- package/types/string/template.d.ts +4 -1
- package/types/value/clone.d.cts +8 -0
- package/types/value/clone.d.ts +4 -2
- package/types/value/compare.d.cts +8 -0
- package/types/value/diff.d.cts +25 -0
- package/types/value/equal.d.cts +12 -0
- package/types/value/get.d.cts +1319 -0
- 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 +72 -0
- package/types/value/merge.d.ts +2 -2
- package/types/value/set.d.cts +1011 -0
- package/types/value/set.d.ts +1 -1
- package/types/value/smush.d.cts +1368 -0
- package/types/value/smush.d.ts +2 -2
- package/types/value/unsmush.d.cts +141 -0
- package/types/value/unsmush.d.ts +2 -2
- package/dist/js/array/count.mjs +0 -8
- package/dist/js/array/exists.mjs +0 -8
- package/dist/js/array/filter.mjs +0 -8
- package/dist/js/array/find.mjs +0 -8
- package/dist/js/array/group-by.mjs +0 -33
- package/dist/js/array/index-of.mjs +0 -8
- package/dist/js/array/index.mjs +0 -34
- package/dist/js/array/insert.mjs +0 -22
- package/dist/js/array/models.mjs +0 -0
- package/dist/js/array/splice.mjs +0 -9
- package/dist/js/array/to-map.mjs +0 -27
- package/dist/js/array/to-record.mjs +0 -8
- package/dist/js/array/unique.mjs +0 -8
- package/dist/js/colour/index.mjs +0 -30
- package/dist/js/index.mjs +0 -17
- package/dist/js/internal/array-callbacks.mjs +0 -19
- package/dist/js/internal/array-find.mjs +0 -51
- package/dist/js/logger.mjs +0 -69
- package/dist/js/models.mjs +0 -0
- package/dist/js/string/index.mjs +0 -44
- package/dist/js/value/index.mjs +0 -23
- package/src/js/internal/array-callbacks.ts +0 -28
- package/types/internal/array-callbacks.d.ts +0 -2
- package/types/internal/array-find.d.ts +0 -4
package/src/js/array/find.ts
CHANGED
|
@@ -1,29 +1,54 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import type {BooleanCallback, KeyCallback} from './models';
|
|
1
|
+
import type {BooleanCallback, KeyCallback} from '~/array/models';
|
|
2
|
+
import {findValue} from '~/internal/array/find';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
|
-
*
|
|
5
|
+
* Get the first item matching `value` _(or `undefined` if no match is found)_
|
|
7
6
|
*/
|
|
8
|
-
export function find<
|
|
9
|
-
array: Model[],
|
|
10
|
-
value: Value | BooleanCallback<Model>,
|
|
11
|
-
): Model | undefined;
|
|
7
|
+
export function find<Item>(array: Item[], value: Item): Item | undefined;
|
|
12
8
|
|
|
13
9
|
/**
|
|
14
|
-
*
|
|
10
|
+
* Get the first item matching `value` _(or `undefined` if no match is found)_
|
|
11
|
+
*/
|
|
12
|
+
export function find<Item>(
|
|
13
|
+
array: Item[],
|
|
14
|
+
matches: BooleanCallback<Item>,
|
|
15
|
+
): Item | undefined;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* - Get the first matching item _(or `undefined` if no match is found)_
|
|
19
|
+
* - Use `key` to find a comparison value to match with `value`
|
|
20
|
+
*/
|
|
21
|
+
export function find<Item, Key extends keyof Item>(
|
|
22
|
+
array: Item[],
|
|
23
|
+
key: Key,
|
|
24
|
+
value: Item[Key],
|
|
25
|
+
): Item | undefined;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* - Get the first matching item _(or `undefined` if no match is found)_
|
|
15
29
|
* - Use `key` to find a comparison value to match with `value`
|
|
16
30
|
*/
|
|
17
|
-
export function find<
|
|
18
|
-
array:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
):
|
|
31
|
+
export function find<Item, Key extends KeyCallback<Item>>(
|
|
32
|
+
array: Item[],
|
|
33
|
+
key: Key,
|
|
34
|
+
value: ReturnType<Key>,
|
|
35
|
+
): Item | undefined;
|
|
36
|
+
|
|
37
|
+
export function find<Item>(
|
|
38
|
+
array: unknown[],
|
|
39
|
+
...parameters: unknown[]
|
|
40
|
+
): Item | undefined {
|
|
41
|
+
const {length} = parameters;
|
|
22
42
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
43
|
+
return findValue(
|
|
44
|
+
'value',
|
|
45
|
+
array,
|
|
46
|
+
length === 1 && typeof parameters[0] === 'function'
|
|
47
|
+
? parameters[0]
|
|
48
|
+
: undefined,
|
|
49
|
+
length === 2 ? parameters[0] : undefined,
|
|
50
|
+
length === 1 && typeof parameters[0] !== 'function'
|
|
51
|
+
? parameters[0]
|
|
52
|
+
: parameters[1],
|
|
53
|
+
) as Item | undefined;
|
|
29
54
|
}
|
package/src/js/array/group-by.ts
CHANGED
|
@@ -1,47 +1,178 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
1
|
+
import type {KeyCallback, ValueCallback} from '~/array/models';
|
|
2
|
+
import {getCallbacks} from '~/internal/array/callbacks';
|
|
3
|
+
import type {Key, KeyedValue, PlainObject} from '~/models';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Create a record from an array of items using a specific key
|
|
7
7
|
*/
|
|
8
|
-
export function groupBy<
|
|
9
|
-
array:
|
|
10
|
-
key: Key
|
|
11
|
-
): Record<
|
|
12
|
-
|
|
8
|
+
export function groupBy<Item, Key extends keyof Item>(
|
|
9
|
+
array: Item[],
|
|
10
|
+
key: Key,
|
|
11
|
+
): Record<KeyedValue<Item, Key>, Item>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Create a record from an array of items using a specific key, and grouping them into arrays
|
|
15
|
+
*/
|
|
16
|
+
export function groupBy<Item, Key extends keyof Item>(
|
|
17
|
+
array: Item[],
|
|
18
|
+
key: Key,
|
|
19
|
+
arrays: true,
|
|
20
|
+
): Record<KeyedValue<Item, Key>, Item[]>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Create a record from an array of items using a specific key
|
|
24
|
+
*/
|
|
25
|
+
export function groupBy<Item, Key extends KeyCallback<Item>>(
|
|
26
|
+
array: Item[],
|
|
27
|
+
key: Key,
|
|
28
|
+
): Record<ReturnType<Key>, Item>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Create a record from an array of items using a specific key, and grouping them into arrays
|
|
32
|
+
*/
|
|
33
|
+
export function groupBy<Item, Key extends KeyCallback<Item>>(
|
|
34
|
+
array: Item[],
|
|
35
|
+
key: Key,
|
|
36
|
+
arrays: true,
|
|
37
|
+
): Record<ReturnType<Key>, Item[]>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a record from an array of items using a specific key and value
|
|
41
|
+
*/
|
|
42
|
+
export function groupBy<Item, Key extends keyof Item, Value extends keyof Item>(
|
|
43
|
+
array: Item[],
|
|
44
|
+
key: Key,
|
|
45
|
+
value: Value,
|
|
46
|
+
): Record<KeyedValue<Item, Key>, KeyedValue<Item, Value>>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
50
|
+
*/
|
|
51
|
+
export function groupBy<Item, Key extends keyof Item, Value extends keyof Item>(
|
|
52
|
+
array: Item[],
|
|
53
|
+
key: Key,
|
|
54
|
+
value: Value,
|
|
55
|
+
arrays: true,
|
|
56
|
+
): Record<KeyedValue<Item, Key>, Array<KeyedValue<Item, Value>>>;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Create a record from an array of items using a specific key and value
|
|
60
|
+
*/
|
|
61
|
+
export function groupBy<
|
|
62
|
+
Item,
|
|
63
|
+
Key extends keyof Item,
|
|
64
|
+
Value extends ValueCallback<Item>,
|
|
65
|
+
>(
|
|
66
|
+
array: Item[],
|
|
67
|
+
key: Key,
|
|
68
|
+
value: Value,
|
|
69
|
+
): Record<KeyedValue<Item, Key>, ReturnType<Value>>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
73
|
+
*/
|
|
74
|
+
export function groupBy<
|
|
75
|
+
Item,
|
|
76
|
+
Key extends keyof Item,
|
|
77
|
+
Value extends ValueCallback<Item>,
|
|
78
|
+
>(
|
|
79
|
+
array: Item[],
|
|
80
|
+
key: Key,
|
|
81
|
+
value: Value,
|
|
82
|
+
arrays: true,
|
|
83
|
+
): Record<KeyedValue<Item, Key>, Array<ReturnType<Value>>>;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create a record from an array of items using a specific key and value
|
|
87
|
+
*/
|
|
88
|
+
export function groupBy<
|
|
89
|
+
Item,
|
|
90
|
+
Key extends KeyCallback<Item>,
|
|
91
|
+
Value extends keyof Item,
|
|
92
|
+
>(
|
|
93
|
+
array: Item[],
|
|
94
|
+
key: Key,
|
|
95
|
+
value: Value,
|
|
96
|
+
): Record<ReturnType<Key>, KeyedValue<Item, Value>>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
100
|
+
*/
|
|
101
|
+
export function groupBy<
|
|
102
|
+
Item,
|
|
103
|
+
Key extends KeyCallback<Item>,
|
|
104
|
+
Value extends keyof Item,
|
|
105
|
+
>(
|
|
106
|
+
array: Item[],
|
|
107
|
+
key: Key,
|
|
108
|
+
value: Value,
|
|
109
|
+
arrays: true,
|
|
110
|
+
): Record<ReturnType<Key>, Array<KeyedValue<Item, Value>>>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Create a record from an array of items using a specific key and value
|
|
114
|
+
*/
|
|
115
|
+
export function groupBy<
|
|
116
|
+
Item,
|
|
117
|
+
Key extends KeyCallback<Item>,
|
|
118
|
+
Value extends ValueCallback<Item>,
|
|
119
|
+
>(
|
|
120
|
+
array: Item[],
|
|
121
|
+
key: Key,
|
|
122
|
+
value: Value,
|
|
123
|
+
): Record<ReturnType<Key>, ReturnType<Value>>;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
127
|
+
*/
|
|
128
|
+
export function groupBy<
|
|
129
|
+
Item,
|
|
130
|
+
Key extends KeyCallback<Item>,
|
|
131
|
+
Value extends ValueCallback<Item>,
|
|
132
|
+
>(
|
|
133
|
+
array: Item[],
|
|
134
|
+
key: Key,
|
|
135
|
+
value: Value,
|
|
136
|
+
arrays: true,
|
|
137
|
+
): Record<ReturnType<Key>, Array<ReturnType<Value>>>;
|
|
138
|
+
|
|
139
|
+
export function groupBy(
|
|
140
|
+
array: unknown[],
|
|
141
|
+
first?: boolean,
|
|
142
|
+
second?: unknown,
|
|
143
|
+
third?: boolean,
|
|
144
|
+
): PlainObject {
|
|
145
|
+
return groupValues(
|
|
146
|
+
array,
|
|
147
|
+
first,
|
|
148
|
+
second,
|
|
149
|
+
first === true || second === true || third === true,
|
|
150
|
+
) as never;
|
|
13
151
|
}
|
|
14
152
|
|
|
15
|
-
export function groupValues
|
|
16
|
-
array:
|
|
17
|
-
key:
|
|
153
|
+
export function groupValues(
|
|
154
|
+
array: unknown[],
|
|
155
|
+
key: unknown,
|
|
156
|
+
value: unknown,
|
|
18
157
|
arrays: boolean,
|
|
19
|
-
indicable: boolean,
|
|
20
158
|
): Record<Key, unknown> {
|
|
21
|
-
const callbacks = getCallbacks(undefined, key);
|
|
22
|
-
const hasCallback = typeof callbacks?.key === 'function';
|
|
23
|
-
|
|
24
|
-
if (!hasCallback && !indicable) {
|
|
25
|
-
return {};
|
|
26
|
-
}
|
|
27
|
-
|
|
159
|
+
const callbacks = getCallbacks(undefined, key, value);
|
|
28
160
|
const record: Record<Key, unknown> = {};
|
|
29
161
|
const {length} = array;
|
|
30
162
|
|
|
31
163
|
for (let index = 0; index < length; index += 1) {
|
|
32
|
-
const
|
|
164
|
+
const item = array[index];
|
|
33
165
|
|
|
34
|
-
const key =
|
|
35
|
-
|
|
36
|
-
: index;
|
|
166
|
+
const key = callbacks?.key?.(item, index, array) ?? index;
|
|
167
|
+
const value = callbacks?.value?.(item, index, array) ?? item;
|
|
37
168
|
|
|
38
169
|
if (arrays) {
|
|
39
170
|
const existing = record[key];
|
|
40
171
|
|
|
41
|
-
if (
|
|
42
|
-
existing.push(value);
|
|
43
|
-
} else {
|
|
172
|
+
if (existing == null) {
|
|
44
173
|
record[key] = [value];
|
|
174
|
+
} else {
|
|
175
|
+
(existing as unknown[]).push(value);
|
|
45
176
|
}
|
|
46
177
|
} else {
|
|
47
178
|
record[key] = value;
|
package/src/js/array/index-of.ts
CHANGED
|
@@ -1,29 +1,51 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import type {BooleanCallback, KeyCallback} from './models';
|
|
1
|
+
import type {BooleanCallback, KeyCallback} from '~/array/models';
|
|
2
|
+
import {findValue} from '~/internal/array/find';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
|
-
*
|
|
5
|
+
* Get the index for the first item matching `value` _(or `-1` if no match is found)_
|
|
7
6
|
*/
|
|
8
|
-
export function indexOf<
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
export function indexOf<Item>(array: Item[], value: Item): number;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get the index for the first item matching `value` _(or `-1` if no match is found)_
|
|
11
|
+
*/
|
|
12
|
+
export function indexOf<Item>(
|
|
13
|
+
array: Item[],
|
|
14
|
+
matches: BooleanCallback<Item>,
|
|
11
15
|
): number;
|
|
12
16
|
|
|
13
17
|
/**
|
|
14
|
-
* -
|
|
18
|
+
* - Get the index for the first matching item _(or `-1` if no match is found)_
|
|
15
19
|
* - Use `key` to find a comparison value to match with `value`
|
|
16
20
|
*/
|
|
17
|
-
export function indexOf<
|
|
18
|
-
array:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
export function indexOf<Item, Key extends keyof Item>(
|
|
22
|
+
array: Item[],
|
|
23
|
+
key: Key,
|
|
24
|
+
value: Item[Key],
|
|
21
25
|
): number;
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* - Get the index for the first matching item _(or `-1` if no match is found)_
|
|
29
|
+
* - Use `key` to find a comparison value to match with `value`
|
|
30
|
+
*/
|
|
31
|
+
export function indexOf<Item, Key extends KeyCallback<Item>>(
|
|
32
|
+
array: Item[],
|
|
33
|
+
key: Key,
|
|
34
|
+
value: ReturnType<Key>,
|
|
35
|
+
): number;
|
|
36
|
+
|
|
37
|
+
export function indexOf(array: unknown[], ...parameters: unknown[]): number {
|
|
38
|
+
const {length} = parameters;
|
|
39
|
+
|
|
40
|
+
return findValue(
|
|
41
|
+
'index',
|
|
42
|
+
array,
|
|
43
|
+
length === 1 && typeof parameters[0] === 'function'
|
|
44
|
+
? parameters[0]
|
|
45
|
+
: undefined,
|
|
46
|
+
length === 2 ? parameters[0] : undefined,
|
|
47
|
+
length === 1 && typeof parameters[0] !== 'function'
|
|
48
|
+
? parameters[0]
|
|
49
|
+
: parameters[1],
|
|
50
|
+
) as number;
|
|
29
51
|
}
|
package/src/js/array/index.ts
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import {insertValues} from '~/array/insert';
|
|
2
|
+
import type {NestedArrayType} from '~/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Flatten an array _(using native `flat` and maximum depth)_
|
|
6
6
|
*/
|
|
7
|
-
export function flatten<
|
|
8
|
-
return array.flat(Number.POSITIVE_INFINITY) as NestedArrayType<
|
|
7
|
+
export function flatten<Item>(array: Item[]): NestedArrayType<Item>[] {
|
|
8
|
+
return array.flat(Number.POSITIVE_INFINITY) as NestedArrayType<Item>[];
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* -
|
|
13
|
-
* -
|
|
12
|
+
* - Push values to the end of an array
|
|
13
|
+
* - Returns the new length
|
|
14
|
+
* - _(Uses chunking to avoid stack overflow)_
|
|
14
15
|
*/
|
|
15
|
-
export function push<
|
|
16
|
-
return insertValues('push', array,
|
|
16
|
+
export function push<Item>(array: Item[], pushed: Item[]): number {
|
|
17
|
+
return insertValues('push', array, pushed, array.length, 0) as number;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export * from './chunk';
|
package/src/js/array/insert.ts
CHANGED
|
@@ -1,30 +1,44 @@
|
|
|
1
|
-
import {chunk} from '
|
|
2
|
-
import type {InsertType} from '
|
|
1
|
+
import {chunk} from '~/array/chunk';
|
|
2
|
+
import type {InsertType} from '~/array/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* -
|
|
6
|
-
* - Uses chunking to avoid stack
|
|
5
|
+
* - Insert values into an array _(at the end)_
|
|
6
|
+
* - _(Uses chunking to avoid stack overflow_)
|
|
7
7
|
*/
|
|
8
|
-
export function insert<
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
export function insert<Item>(array: Item[], items: Item[]): void;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* - Insert values into an array at a specified index
|
|
12
|
+
* - _(Uses chunking to avoid stack overflow_)
|
|
13
|
+
*/
|
|
14
|
+
export function insert<Item>(array: Item[], index: number, items: Item[]): void;
|
|
15
|
+
|
|
16
|
+
export function insert(
|
|
17
|
+
array: unknown[],
|
|
18
|
+
first: unknown,
|
|
19
|
+
second?: unknown[],
|
|
12
20
|
): void {
|
|
13
|
-
insertValues(
|
|
21
|
+
insertValues(
|
|
22
|
+
'splice',
|
|
23
|
+
array,
|
|
24
|
+
Array.isArray(first) ? first : (second ?? []),
|
|
25
|
+
typeof first === 'number' ? first : array.length,
|
|
26
|
+
0,
|
|
27
|
+
);
|
|
14
28
|
}
|
|
15
29
|
|
|
16
|
-
export function insertValues<
|
|
30
|
+
export function insertValues<Item>(
|
|
17
31
|
type: InsertType,
|
|
18
|
-
array:
|
|
19
|
-
|
|
32
|
+
array: Item[],
|
|
33
|
+
items: Item[],
|
|
20
34
|
start: number,
|
|
21
35
|
deleteCount: number,
|
|
22
36
|
): unknown {
|
|
23
|
-
const chunked = chunk(
|
|
37
|
+
const chunked = chunk(items);
|
|
24
38
|
const lastIndex = chunked.length - 1;
|
|
25
39
|
|
|
26
40
|
let index = Number(chunked.length);
|
|
27
|
-
let returned:
|
|
41
|
+
let returned: Item[] | undefined;
|
|
28
42
|
|
|
29
43
|
while (--index >= 0) {
|
|
30
44
|
const result = array.splice(
|
package/src/js/array/models.ts
CHANGED
|
@@ -1,32 +1,35 @@
|
|
|
1
|
-
import type {Key} from '
|
|
1
|
+
import type {GenericCallback, Key} from '~/models';
|
|
2
2
|
|
|
3
|
-
export type ArrayCallback<
|
|
4
|
-
|
|
3
|
+
export type ArrayCallback<Item, Value> = (
|
|
4
|
+
item: Item,
|
|
5
5
|
index: number,
|
|
6
|
-
array:
|
|
7
|
-
) =>
|
|
6
|
+
array: Item[],
|
|
7
|
+
) => Value;
|
|
8
8
|
|
|
9
|
-
export type BooleanCallback<
|
|
9
|
+
export type BooleanCallback<Item> = ArrayCallback<Item, boolean>;
|
|
10
10
|
|
|
11
|
-
export type Callbacks
|
|
12
|
-
bool?:
|
|
13
|
-
key?:
|
|
11
|
+
export type Callbacks = {
|
|
12
|
+
bool?: GenericCallback;
|
|
13
|
+
key?: GenericCallback;
|
|
14
|
+
value?: GenericCallback;
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
export type FindType = 'index' | 'value';
|
|
17
18
|
|
|
18
19
|
export type InsertType = 'push' | 'splice';
|
|
19
20
|
|
|
20
|
-
export type KeyCallback<
|
|
21
|
+
export type KeyCallback<Item> = ArrayCallback<Item, Key>;
|
|
21
22
|
|
|
22
|
-
export type SortKey<
|
|
23
|
+
export type SortKey<Item> = {
|
|
23
24
|
direction: 'asc' | 'desc';
|
|
24
|
-
value: Key | SortKeyCallback<
|
|
25
|
+
value: Key | SortKeyCallback<Item>;
|
|
25
26
|
};
|
|
26
27
|
|
|
27
|
-
export type SortKeyCallback<
|
|
28
|
+
export type SortKeyCallback<Item> = (item: Item) => unknown;
|
|
28
29
|
|
|
29
|
-
export type SortKeyWithCallback<
|
|
30
|
-
callback: SortKeyCallback<
|
|
30
|
+
export type SortKeyWithCallback<Item> = {
|
|
31
|
+
callback: SortKeyCallback<Item>;
|
|
31
32
|
direction: 'asc' | 'desc';
|
|
32
33
|
};
|
|
34
|
+
|
|
35
|
+
export type ValueCallback<Item> = ArrayCallback<Item, unknown>;
|
package/src/js/array/shuffle.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {getRandomInteger} from '
|
|
1
|
+
import {getRandomInteger} from '~/random';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Shuffle an array
|
|
5
5
|
*/
|
|
6
|
-
export function shuffle<
|
|
6
|
+
export function shuffle<Item>(array: Item[]): Item[] {
|
|
7
7
|
const shuffled = array.slice();
|
|
8
8
|
const {length} = shuffled;
|
|
9
9
|
|
package/src/js/array/sort.ts
CHANGED
|
@@ -1,43 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import type {
|
|
2
|
+
SortKey,
|
|
3
|
+
SortKeyCallback,
|
|
4
|
+
SortKeyWithCallback,
|
|
5
|
+
} from '~/array/models';
|
|
6
|
+
import {isKey} from '~/is';
|
|
7
|
+
import type {Key, PlainObject} from '~/models';
|
|
8
|
+
import {compare} from '~/value/compare';
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
|
-
*
|
|
11
|
+
* Sort an array of items _(defaults to ascending)_
|
|
8
12
|
*/
|
|
9
|
-
export function sort<
|
|
13
|
+
export function sort<Item>(array: Item[], descending?: boolean): Item[];
|
|
10
14
|
|
|
11
15
|
/**
|
|
12
|
-
* -
|
|
13
|
-
* -
|
|
16
|
+
* - Sort an array of items, using a `key` to sort by a specific value
|
|
17
|
+
* - Defaults to ascending, but can be changed by setting `descending` to `true`, or using a `SortKey`
|
|
14
18
|
*/
|
|
15
|
-
export function sort<
|
|
16
|
-
array:
|
|
17
|
-
key: Key | SortKey<
|
|
19
|
+
export function sort<Item>(
|
|
20
|
+
array: Item[],
|
|
21
|
+
key: Key | SortKey<Item> | SortKeyCallback<Item>,
|
|
18
22
|
descending?: boolean,
|
|
19
|
-
):
|
|
23
|
+
): Item[];
|
|
20
24
|
|
|
21
25
|
/**
|
|
22
|
-
* -
|
|
23
|
-
* -
|
|
26
|
+
* - Sort an array of items, using multiple `keys` to sort by specific values
|
|
27
|
+
* - Defaults to ascending, but can be changed by setting `descending` to `true`, or using `SortKey`
|
|
24
28
|
*/
|
|
25
|
-
export function sort<
|
|
26
|
-
array:
|
|
27
|
-
keys: Array<Key | SortKey<
|
|
29
|
+
export function sort<Item>(
|
|
30
|
+
array: Item[],
|
|
31
|
+
keys: Array<Key | SortKey<Item> | SortKeyCallback<Item>>,
|
|
28
32
|
descending?: boolean,
|
|
29
|
-
):
|
|
30
|
-
|
|
31
|
-
export function sort
|
|
32
|
-
array:
|
|
33
|
-
first?:
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
| SortKey<Value>
|
|
37
|
-
| SortKeyCallback<Value>
|
|
38
|
-
| Array<Key | SortKey<Value> | SortKeyCallback<Value>>,
|
|
39
|
-
second?: boolean,
|
|
40
|
-
): Value[] {
|
|
33
|
+
): Item[];
|
|
34
|
+
|
|
35
|
+
export function sort(
|
|
36
|
+
array: unknown[],
|
|
37
|
+
first?: unknown,
|
|
38
|
+
second?: unknown,
|
|
39
|
+
): unknown[] {
|
|
41
40
|
if (array.length < 2) {
|
|
42
41
|
return array;
|
|
43
42
|
}
|
|
@@ -52,23 +51,22 @@ export function sort<Value>(
|
|
|
52
51
|
|
|
53
52
|
const keys = (Array.isArray(first) ? first : [first])
|
|
54
53
|
.map(key => {
|
|
55
|
-
const returned: SortKeyWithCallback<
|
|
54
|
+
const returned: SortKeyWithCallback<unknown> = {
|
|
56
55
|
direction,
|
|
57
56
|
callback: undefined as never,
|
|
58
57
|
};
|
|
59
58
|
|
|
60
59
|
if (isKey(key)) {
|
|
61
|
-
returned.callback = (value
|
|
62
|
-
(value as PlainObject)[key] as never;
|
|
60
|
+
returned.callback = value => (value as PlainObject)[key] as never;
|
|
63
61
|
} else if (typeof key === 'function') {
|
|
64
62
|
returned.callback = key;
|
|
65
63
|
} else if (typeof key?.value === 'function' || isKey(key?.value)) {
|
|
66
64
|
returned.direction = key?.direction ?? direction;
|
|
65
|
+
|
|
67
66
|
returned.callback =
|
|
68
67
|
typeof key.value === 'function'
|
|
69
68
|
? key.value
|
|
70
|
-
: (value
|
|
71
|
-
(value as PlainObject)[key.value as Key] as never;
|
|
69
|
+
: value => (value as PlainObject)[key.value as Key] as never;
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
return returned;
|