@oscarpalmer/atoms 0.77.0 → 0.77.2
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 +1 -1
- package/dist/js/array/chunk.js +1 -1
- package/dist/js/value/equal.cjs +4 -1
- package/dist/js/value/equal.js +4 -1
- package/package.json +1 -1
- package/src/js/array/chunk.ts +1 -1
- package/src/js/array/count.ts +6 -6
- package/src/js/array/exists.ts +6 -6
- package/src/js/array/filter.ts +6 -6
- package/src/js/array/find.ts +6 -6
- package/src/js/array/group-by.ts +67 -55
- package/src/js/array/index-of.ts +6 -6
- package/src/js/array/sort.ts +20 -0
- package/src/js/array/to-map.ts +55 -47
- package/src/js/array/to-record.ts +61 -57
- package/src/js/array/unique.ts +6 -6
- package/src/js/internal/value/handle.ts +5 -5
- package/src/js/value/clone.ts +3 -1
- package/src/js/value/equal.ts +4 -1
- package/src/js/value/get.ts +9 -9
- package/src/js/value/set.ts +10 -11
- package/types/array/count.d.cts +34 -2
- package/types/array/count.d.ts +3 -3
- package/types/array/exists.d.cts +34 -2
- package/types/array/exists.d.ts +3 -3
- package/types/array/filter.d.cts +34 -2
- package/types/array/filter.d.ts +3 -3
- package/types/array/find.d.cts +34 -2
- package/types/array/find.d.ts +3 -3
- package/types/array/group-by.d.cts +44 -12
- package/types/array/group-by.d.ts +14 -14
- package/types/array/index-of.d.cts +34 -2
- package/types/array/index-of.d.ts +3 -3
- package/types/array/index.d.cts +88 -46
- package/types/array/sort.d.cts +42 -0
- package/types/array/sort.d.ts +11 -1
- package/types/array/to-map.d.cts +42 -10
- package/types/array/to-map.d.ts +13 -13
- package/types/array/to-record.d.cts +44 -12
- package/types/array/to-record.d.ts +13 -13
- package/types/array/unique.d.cts +34 -2
- package/types/array/unique.d.ts +3 -3
- package/types/index.d.cts +1034 -317
- package/types/internal/value/handle.d.cts +27 -2
- package/types/internal/value/handle.d.ts +2 -2
- package/types/models.d.cts +467 -210
- package/types/value/clone.d.cts +1 -1
- package/types/value/clone.d.ts +1 -1
- package/types/value/get.d.cts +470 -211
- package/types/value/get.d.ts +3 -3
- package/types/value/index.d.cts +515 -224
- package/types/value/set.d.cts +357 -170
- package/types/value/set.d.ts +3 -3
- package/types/value/smush.d.cts +463 -209
package/dist/js/array/chunk.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const number = require("../number.cjs");
|
|
4
4
|
function chunk(array, size) {
|
|
5
|
-
const chunkSize = number.clamp(size ??
|
|
5
|
+
const chunkSize = number.clamp(size ?? 5e3, 1, 5e3);
|
|
6
6
|
const { length } = array;
|
|
7
7
|
if (length <= chunkSize) {
|
|
8
8
|
return [array];
|
package/dist/js/array/chunk.js
CHANGED
package/dist/js/value/equal.cjs
CHANGED
|
@@ -40,7 +40,10 @@ function equalArrayBuffer(first, second) {
|
|
|
40
40
|
) : false;
|
|
41
41
|
}
|
|
42
42
|
function equalDataView(first, second) {
|
|
43
|
-
return first.byteOffset === second.byteOffset ? equalArrayBuffer(
|
|
43
|
+
return first.byteOffset === second.byteOffset ? equalArrayBuffer(
|
|
44
|
+
first.buffer,
|
|
45
|
+
second.buffer
|
|
46
|
+
) : false;
|
|
44
47
|
}
|
|
45
48
|
function equalMap(first, second) {
|
|
46
49
|
const { size } = first;
|
package/dist/js/value/equal.js
CHANGED
|
@@ -38,7 +38,10 @@ function equalArrayBuffer(first, second) {
|
|
|
38
38
|
) : false;
|
|
39
39
|
}
|
|
40
40
|
function equalDataView(first, second) {
|
|
41
|
-
return first.byteOffset === second.byteOffset ? equalArrayBuffer(
|
|
41
|
+
return first.byteOffset === second.byteOffset ? equalArrayBuffer(
|
|
42
|
+
first.buffer,
|
|
43
|
+
second.buffer
|
|
44
|
+
) : false;
|
|
42
45
|
}
|
|
43
46
|
function equalMap(first, second) {
|
|
44
47
|
const { size } = first;
|
package/package.json
CHANGED
package/src/js/array/chunk.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {clamp} from '~/number';
|
|
|
4
4
|
* Chunk an array _(into smaller arrays of a specified size)_
|
|
5
5
|
*/
|
|
6
6
|
export function chunk<Item>(array: Item[], size?: number): Item[][] {
|
|
7
|
-
const chunkSize = clamp(size ??
|
|
7
|
+
const chunkSize = clamp(size ?? 5_000, 1, 5_000);
|
|
8
8
|
const {length} = array;
|
|
9
9
|
|
|
10
10
|
if (length <= chunkSize) {
|
package/src/js/array/count.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {findValues} from '~/internal/array/find';
|
|
2
|
-
import type {Key
|
|
2
|
+
import type {Key, PlainObject} from '~/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Get the number of items _(count)_ that match the given value
|
|
@@ -17,10 +17,10 @@ export function count<Item>(
|
|
|
17
17
|
/**
|
|
18
18
|
* Get the number of items _(count)_ that match the given value
|
|
19
19
|
*/
|
|
20
|
-
export function count<Item,
|
|
20
|
+
export function count<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
21
21
|
array: Item[],
|
|
22
|
-
key:
|
|
23
|
-
value: Item[
|
|
22
|
+
key: ItemKey,
|
|
23
|
+
value: Item[ItemKey],
|
|
24
24
|
): number;
|
|
25
25
|
|
|
26
26
|
/**
|
|
@@ -28,8 +28,8 @@ export function count<Item, Key extends keyof Item>(
|
|
|
28
28
|
*/
|
|
29
29
|
export function count<
|
|
30
30
|
Item,
|
|
31
|
-
|
|
32
|
-
>(array: Item[], key:
|
|
31
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
32
|
+
>(array: Item[], key: ItemKey, value: ReturnType<ItemKey>): number;
|
|
33
33
|
|
|
34
34
|
export function count(array: unknown[], ...parameters: unknown[]): number {
|
|
35
35
|
return findValues('all', array, parameters).length;
|
package/src/js/array/exists.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {findValue} from '~/internal/array/find';
|
|
2
|
-
import type {Key
|
|
2
|
+
import type {Key, PlainObject} from '~/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Does the value exist in array?
|
|
@@ -18,10 +18,10 @@ export function exists<Item>(
|
|
|
18
18
|
* - Does the value exist in array?
|
|
19
19
|
* - Use `key` to find a comparison value to match with `value`
|
|
20
20
|
*/
|
|
21
|
-
export function exists<Item,
|
|
21
|
+
export function exists<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
22
22
|
array: Item[],
|
|
23
|
-
key:
|
|
24
|
-
value: Item[
|
|
23
|
+
key: ItemKey,
|
|
24
|
+
value: Item[ItemKey],
|
|
25
25
|
): boolean;
|
|
26
26
|
|
|
27
27
|
/**
|
|
@@ -30,8 +30,8 @@ export function exists<Item, Key extends keyof Item>(
|
|
|
30
30
|
*/
|
|
31
31
|
export function exists<
|
|
32
32
|
Item,
|
|
33
|
-
|
|
34
|
-
>(array: Item[], key:
|
|
33
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
34
|
+
>(array: Item[], key: ItemKey, value: ReturnType<ItemKey>): boolean;
|
|
35
35
|
|
|
36
36
|
export function exists(array: unknown[], ...parameters: unknown[]): boolean {
|
|
37
37
|
return (findValue('index', array, parameters) as number) > -1;
|
package/src/js/array/filter.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {findValues} from '~/internal/array/find';
|
|
2
|
-
import type {Key
|
|
2
|
+
import type {Key, PlainObject} from '~/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Get a filtered array of items matching `value`
|
|
@@ -18,10 +18,10 @@ export function filter<Item>(
|
|
|
18
18
|
* - Get a filtered array of items
|
|
19
19
|
* - Use `key` to find a comparison value to match with `value`
|
|
20
20
|
*/
|
|
21
|
-
export function filter<Item,
|
|
21
|
+
export function filter<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
22
22
|
array: Item[],
|
|
23
|
-
key:
|
|
24
|
-
value: Item[
|
|
23
|
+
key: ItemKey,
|
|
24
|
+
value: Item[ItemKey],
|
|
25
25
|
): Item[];
|
|
26
26
|
|
|
27
27
|
/**
|
|
@@ -30,8 +30,8 @@ export function filter<Item, Key extends keyof Item>(
|
|
|
30
30
|
*/
|
|
31
31
|
export function filter<
|
|
32
32
|
Item,
|
|
33
|
-
|
|
34
|
-
>(array: Item[], key:
|
|
33
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
34
|
+
>(array: Item[], key: ItemKey, value: ReturnType<ItemKey>): Item[];
|
|
35
35
|
|
|
36
36
|
export function filter(array: unknown[], ...parameters: unknown[]): unknown[] {
|
|
37
37
|
return findValues('all', array, parameters);
|
package/src/js/array/find.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {findValue} from '~/internal/array/find';
|
|
2
|
-
import type {Key
|
|
2
|
+
import type {Key, PlainObject} from '~/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Get the first item matching `value` _(or `undefined` if no match is found)_
|
|
@@ -18,10 +18,10 @@ export function find<Item>(
|
|
|
18
18
|
* - Get the first matching item _(or `undefined` if no match is found)_
|
|
19
19
|
* - Use `key` to find a comparison value to match with `value`
|
|
20
20
|
*/
|
|
21
|
-
export function find<Item,
|
|
21
|
+
export function find<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
22
22
|
array: Item[],
|
|
23
|
-
key:
|
|
24
|
-
value: Item[
|
|
23
|
+
key: ItemKey,
|
|
24
|
+
value: Item[ItemKey],
|
|
25
25
|
): Item | undefined;
|
|
26
26
|
|
|
27
27
|
/**
|
|
@@ -30,8 +30,8 @@ export function find<Item, Key extends keyof Item>(
|
|
|
30
30
|
*/
|
|
31
31
|
export function find<
|
|
32
32
|
Item,
|
|
33
|
-
|
|
34
|
-
>(array: Item[], key:
|
|
33
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
34
|
+
>(array: Item[], key: ItemKey, value: ReturnType<ItemKey>): Item | undefined;
|
|
35
35
|
|
|
36
36
|
export function find<Item>(
|
|
37
37
|
array: unknown[],
|
package/src/js/array/group-by.ts
CHANGED
|
@@ -1,138 +1,150 @@
|
|
|
1
1
|
import {getCallbacks} from '~/internal/array/callbacks';
|
|
2
|
-
import type {Key
|
|
2
|
+
import type {Key, KeyedValue, PlainObject} from '~/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Create a record from an array of items using a specific key
|
|
6
6
|
*/
|
|
7
|
-
export function groupBy<Item,
|
|
7
|
+
export function groupBy<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
8
8
|
array: Item[],
|
|
9
|
-
key:
|
|
10
|
-
): Record<KeyedValue<Item,
|
|
9
|
+
key: ItemKey,
|
|
10
|
+
): Record<KeyedValue<Item, ItemKey>, Item>;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Create a record from an array of items using a specific key, and grouping them into arrays
|
|
14
14
|
*/
|
|
15
|
-
export function groupBy<Item,
|
|
15
|
+
export function groupBy<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
16
16
|
array: Item[],
|
|
17
|
-
key:
|
|
17
|
+
key: ItemKey,
|
|
18
18
|
arrays: true,
|
|
19
|
-
): Record<KeyedValue<Item,
|
|
19
|
+
): Record<KeyedValue<Item, ItemKey>, Item[]>;
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Create a record from an array of items using a specific key
|
|
23
23
|
*/
|
|
24
24
|
export function groupBy<
|
|
25
25
|
Item,
|
|
26
|
-
|
|
27
|
-
>(array: Item[], key:
|
|
26
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
27
|
+
>(array: Item[], key: ItemKey): Record<ReturnType<ItemKey>, Item>;
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Create a record from an array of items using a specific key, and grouping them into arrays
|
|
31
31
|
*/
|
|
32
32
|
export function groupBy<
|
|
33
33
|
Item,
|
|
34
|
-
|
|
35
|
-
>(
|
|
34
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
35
|
+
>(
|
|
36
|
+
array: Item[],
|
|
37
|
+
key: ItemKey,
|
|
38
|
+
arrays: true,
|
|
39
|
+
): Record<ReturnType<ItemKey>, Item[]>;
|
|
36
40
|
|
|
37
41
|
/**
|
|
38
42
|
* Create a record from an array of items using a specific key and value
|
|
39
43
|
*/
|
|
40
|
-
export function groupBy<
|
|
44
|
+
export function groupBy<
|
|
45
|
+
Item extends PlainObject,
|
|
46
|
+
ItemKey extends keyof Item,
|
|
47
|
+
ItemValue extends keyof Item,
|
|
48
|
+
>(
|
|
41
49
|
array: Item[],
|
|
42
|
-
key:
|
|
43
|
-
value:
|
|
44
|
-
): Record<KeyedValue<Item,
|
|
50
|
+
key: ItemKey,
|
|
51
|
+
value: ItemValue,
|
|
52
|
+
): Record<KeyedValue<Item, ItemKey>, KeyedValue<Item, ItemValue>>;
|
|
45
53
|
|
|
46
54
|
/**
|
|
47
55
|
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
48
56
|
*/
|
|
49
|
-
export function groupBy<
|
|
57
|
+
export function groupBy<
|
|
58
|
+
Item extends PlainObject,
|
|
59
|
+
ItemKey extends keyof Item,
|
|
60
|
+
ItemValue extends keyof Item,
|
|
61
|
+
>(
|
|
50
62
|
array: Item[],
|
|
51
|
-
key:
|
|
52
|
-
value:
|
|
63
|
+
key: ItemKey,
|
|
64
|
+
value: ItemValue,
|
|
53
65
|
arrays: true,
|
|
54
|
-
): Record<KeyedValue<Item,
|
|
66
|
+
): Record<KeyedValue<Item, ItemKey>, Array<KeyedValue<Item, ItemValue>>>;
|
|
55
67
|
|
|
56
68
|
/**
|
|
57
69
|
* Create a record from an array of items using a specific key and value
|
|
58
70
|
*/
|
|
59
71
|
export function groupBy<
|
|
60
|
-
Item,
|
|
61
|
-
|
|
62
|
-
|
|
72
|
+
Item extends PlainObject,
|
|
73
|
+
ItemKey extends keyof Item,
|
|
74
|
+
ItemValue extends (item: Item, index: number, array: Item[]) => unknown,
|
|
63
75
|
>(
|
|
64
76
|
array: Item[],
|
|
65
|
-
key:
|
|
66
|
-
value:
|
|
67
|
-
): Record<KeyedValue<Item,
|
|
77
|
+
key: ItemKey,
|
|
78
|
+
value: ItemValue,
|
|
79
|
+
): Record<KeyedValue<Item, ItemKey>, ReturnType<ItemValue>>;
|
|
68
80
|
|
|
69
81
|
/**
|
|
70
82
|
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
71
83
|
*/
|
|
72
84
|
export function groupBy<
|
|
73
|
-
Item,
|
|
74
|
-
|
|
75
|
-
|
|
85
|
+
Item extends PlainObject,
|
|
86
|
+
ItemKey extends keyof Item,
|
|
87
|
+
ItemValue extends (item: Item, index: number, array: Item[]) => unknown,
|
|
76
88
|
>(
|
|
77
89
|
array: Item[],
|
|
78
|
-
key:
|
|
79
|
-
value:
|
|
90
|
+
key: ItemKey,
|
|
91
|
+
value: ItemValue,
|
|
80
92
|
arrays: true,
|
|
81
|
-
): Record<KeyedValue<Item,
|
|
93
|
+
): Record<KeyedValue<Item, ItemKey>, Array<ReturnType<ItemValue>>>;
|
|
82
94
|
|
|
83
95
|
/**
|
|
84
96
|
* Create a record from an array of items using a specific key and value
|
|
85
97
|
*/
|
|
86
98
|
export function groupBy<
|
|
87
|
-
Item,
|
|
88
|
-
|
|
89
|
-
|
|
99
|
+
Item extends PlainObject,
|
|
100
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
101
|
+
ItemValue extends keyof Item,
|
|
90
102
|
>(
|
|
91
103
|
array: Item[],
|
|
92
|
-
key:
|
|
93
|
-
value:
|
|
94
|
-
): Record<ReturnType<
|
|
104
|
+
key: ItemKey,
|
|
105
|
+
value: ItemValue,
|
|
106
|
+
): Record<ReturnType<ItemKey>, KeyedValue<Item, ItemValue>>;
|
|
95
107
|
|
|
96
108
|
/**
|
|
97
109
|
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
98
110
|
*/
|
|
99
111
|
export function groupBy<
|
|
100
|
-
Item,
|
|
101
|
-
|
|
102
|
-
|
|
112
|
+
Item extends PlainObject,
|
|
113
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
114
|
+
ItemValue extends keyof Item,
|
|
103
115
|
>(
|
|
104
116
|
array: Item[],
|
|
105
|
-
key:
|
|
106
|
-
value:
|
|
117
|
+
key: ItemKey,
|
|
118
|
+
value: ItemValue,
|
|
107
119
|
arrays: true,
|
|
108
|
-
): Record<ReturnType<
|
|
120
|
+
): Record<ReturnType<ItemKey>, Array<KeyedValue<Item, ItemValue>>>;
|
|
109
121
|
|
|
110
122
|
/**
|
|
111
123
|
* Create a record from an array of items using a specific key and value
|
|
112
124
|
*/
|
|
113
125
|
export function groupBy<
|
|
114
126
|
Item,
|
|
115
|
-
|
|
116
|
-
|
|
127
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
128
|
+
ItemValue extends (item: Item, index: number, array: Item[]) => unknown,
|
|
117
129
|
>(
|
|
118
130
|
array: Item[],
|
|
119
|
-
key:
|
|
120
|
-
value:
|
|
121
|
-
): Record<ReturnType<
|
|
131
|
+
key: ItemKey,
|
|
132
|
+
value: ItemValue,
|
|
133
|
+
): Record<ReturnType<ItemKey>, ReturnType<ItemValue>>;
|
|
122
134
|
|
|
123
135
|
/**
|
|
124
136
|
* Create a record from an array of items using a specific key and value, and grouping them into arrays
|
|
125
137
|
*/
|
|
126
138
|
export function groupBy<
|
|
127
139
|
Item,
|
|
128
|
-
|
|
129
|
-
|
|
140
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
141
|
+
ItemValue extends (item: Item, index: number, array: Item[]) => unknown,
|
|
130
142
|
>(
|
|
131
143
|
array: Item[],
|
|
132
|
-
key:
|
|
133
|
-
value:
|
|
144
|
+
key: ItemKey,
|
|
145
|
+
value: ItemValue,
|
|
134
146
|
arrays: true,
|
|
135
|
-
): Record<ReturnType<
|
|
147
|
+
): Record<ReturnType<ItemKey>, Array<ReturnType<ItemValue>>>;
|
|
136
148
|
|
|
137
149
|
export function groupBy(
|
|
138
150
|
array: unknown[],
|
|
@@ -153,9 +165,9 @@ export function groupValues(
|
|
|
153
165
|
key: unknown,
|
|
154
166
|
value: unknown,
|
|
155
167
|
arrays: boolean,
|
|
156
|
-
): Record<
|
|
168
|
+
): Record<Key, unknown> {
|
|
157
169
|
const callbacks = getCallbacks(undefined, key, value);
|
|
158
|
-
const record: Record<
|
|
170
|
+
const record: Record<Key, unknown> = {};
|
|
159
171
|
const {length} = array;
|
|
160
172
|
|
|
161
173
|
for (let index = 0; index < length; index += 1) {
|
package/src/js/array/index-of.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {findValue} from '~/internal/array/find';
|
|
2
|
-
import type {Key
|
|
2
|
+
import type {Key, PlainObject} from '~/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Get the index for the first item matching `value` _(or `-1` if no match is found)_
|
|
@@ -18,10 +18,10 @@ export function indexOf<Item>(
|
|
|
18
18
|
* - Get the index for the first matching item _(or `-1` if no match is found)_
|
|
19
19
|
* - Use `key` to find a comparison value to match with `value`
|
|
20
20
|
*/
|
|
21
|
-
export function indexOf<Item,
|
|
21
|
+
export function indexOf<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
22
22
|
array: Item[],
|
|
23
|
-
key:
|
|
24
|
-
value: Item[
|
|
23
|
+
key: ItemKey,
|
|
24
|
+
value: Item[ItemKey],
|
|
25
25
|
): number;
|
|
26
26
|
|
|
27
27
|
/**
|
|
@@ -30,8 +30,8 @@ export function indexOf<Item, Key extends keyof Item>(
|
|
|
30
30
|
*/
|
|
31
31
|
export function indexOf<
|
|
32
32
|
Item,
|
|
33
|
-
|
|
34
|
-
>(array: Item[], key:
|
|
33
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
34
|
+
>(array: Item[], key: ItemKey, value: ReturnType<ItemKey>): number;
|
|
35
35
|
|
|
36
36
|
export function indexOf(array: unknown[], ...parameters: unknown[]): number {
|
|
37
37
|
return findValue('index', array, parameters) as number;
|
package/src/js/array/sort.ts
CHANGED
|
@@ -18,6 +18,16 @@ export function sort<Item>(
|
|
|
18
18
|
descending?: boolean,
|
|
19
19
|
): Item[];
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* - Sort an array of items, using a `key` to sort by a specific value
|
|
23
|
+
* - Defaults to ascending, but can be changed by setting `descending` to `true`, or using a `SortKey`
|
|
24
|
+
*/
|
|
25
|
+
export function sort<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
26
|
+
array: Item[],
|
|
27
|
+
key: ItemKey | Key | SortKey<Item> | ((item: Item) => Key),
|
|
28
|
+
descending?: boolean,
|
|
29
|
+
): Item[];
|
|
30
|
+
|
|
21
31
|
/**
|
|
22
32
|
* - Sort an array of items, using multiple `keys` to sort by specific values
|
|
23
33
|
* - Defaults to ascending, but can be changed by setting `descending` to `true`, or using `SortKey`
|
|
@@ -28,6 +38,16 @@ export function sort<Item>(
|
|
|
28
38
|
descending?: boolean,
|
|
29
39
|
): Item[];
|
|
30
40
|
|
|
41
|
+
/**
|
|
42
|
+
* - Sort an array of items, using multiple `keys` to sort by specific values
|
|
43
|
+
* - Defaults to ascending, but can be changed by setting `descending` to `true`, or using `SortKey`
|
|
44
|
+
*/
|
|
45
|
+
export function sort<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
46
|
+
array: Item[],
|
|
47
|
+
keys: Array<ItemKey | Key | SortKey<Item> | ((item: Item) => Key)>,
|
|
48
|
+
descending?: boolean,
|
|
49
|
+
): Item[];
|
|
50
|
+
|
|
31
51
|
export function sort(
|
|
32
52
|
array: unknown[],
|
|
33
53
|
first?: unknown,
|
package/src/js/array/to-map.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {getCallbacks} from '~/internal/array/callbacks';
|
|
2
|
-
import type {KeyedValue,
|
|
2
|
+
import type {Key, KeyedValue, PlainObject} from '~/models';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Create a map from an array of items _(using their indices as keys)_
|
|
@@ -9,115 +9,123 @@ export function toMap<Item>(array: Item[]): Map<number, Item>;
|
|
|
9
9
|
/**
|
|
10
10
|
* Create a map from an array of items using a specific key
|
|
11
11
|
*/
|
|
12
|
-
export function toMap<Item,
|
|
12
|
+
export function toMap<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
13
13
|
array: Item[],
|
|
14
|
-
key:
|
|
15
|
-
): Map<KeyedValue<Item,
|
|
14
|
+
key: ItemKey,
|
|
15
|
+
): Map<KeyedValue<Item, ItemKey>, Item>;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Create a map from an array of items using a specific key, and grouping them into arrays
|
|
19
19
|
*/
|
|
20
|
-
export function toMap<Item,
|
|
20
|
+
export function toMap<Item extends PlainObject, ItemKey extends keyof Item>(
|
|
21
21
|
array: Item[],
|
|
22
|
-
key:
|
|
22
|
+
key: ItemKey,
|
|
23
23
|
arrays: true,
|
|
24
|
-
): Map<KeyedValue<Item,
|
|
24
|
+
): Map<KeyedValue<Item, ItemKey>, Item[]>;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Create a map from an array of items using a specific key
|
|
28
28
|
*/
|
|
29
29
|
export function toMap<
|
|
30
30
|
Item,
|
|
31
|
-
|
|
32
|
-
>(array: Item[], key:
|
|
31
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
32
|
+
>(array: Item[], key: ItemKey): Map<ReturnType<ItemKey>, Item>;
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Create a map from an array of items using a specific key, and grouping them into arrays
|
|
36
36
|
*/
|
|
37
37
|
export function toMap<
|
|
38
38
|
Item,
|
|
39
|
-
|
|
40
|
-
>(array: Item[], key:
|
|
39
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
40
|
+
>(array: Item[], key: ItemKey, arrays: true): Map<ReturnType<ItemKey>, Item[]>;
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* Create a map from an array of items using a specific key and value
|
|
44
44
|
*/
|
|
45
|
-
export function toMap<
|
|
45
|
+
export function toMap<
|
|
46
|
+
Item extends PlainObject,
|
|
47
|
+
ItemKey extends keyof Item,
|
|
48
|
+
ItemValue extends keyof Item,
|
|
49
|
+
>(
|
|
46
50
|
array: Item[],
|
|
47
|
-
key:
|
|
48
|
-
value:
|
|
49
|
-
): Map<KeyedValue<Item,
|
|
51
|
+
key: ItemKey,
|
|
52
|
+
value: ItemValue,
|
|
53
|
+
): Map<KeyedValue<Item, ItemKey>, KeyedValue<Item, ItemValue>>;
|
|
50
54
|
|
|
51
55
|
/**
|
|
52
56
|
* Create a map from an array of items using a specific key and value, and grouping them into arrays
|
|
53
57
|
*/
|
|
54
|
-
export function toMap<
|
|
58
|
+
export function toMap<
|
|
59
|
+
Item extends PlainObject,
|
|
60
|
+
ItemKey extends keyof Item,
|
|
61
|
+
ItemValue extends keyof Item,
|
|
62
|
+
>(
|
|
55
63
|
array: Item[],
|
|
56
|
-
key:
|
|
57
|
-
value:
|
|
64
|
+
key: ItemKey,
|
|
65
|
+
value: ItemValue,
|
|
58
66
|
arrays: true,
|
|
59
|
-
): Map<KeyedValue<Item,
|
|
67
|
+
): Map<KeyedValue<Item, ItemKey>, Array<KeyedValue<Item, ItemValue>>>;
|
|
60
68
|
|
|
61
69
|
/**
|
|
62
70
|
* Create a map from an array of items using a specific key and value
|
|
63
71
|
*/
|
|
64
72
|
export function toMap<
|
|
65
|
-
Item,
|
|
66
|
-
|
|
67
|
-
|
|
73
|
+
Item extends PlainObject,
|
|
74
|
+
ItemKey extends keyof Item,
|
|
75
|
+
ItemValue extends (item: Item, index: number, array: Item[]) => unknown,
|
|
68
76
|
>(
|
|
69
77
|
array: Item[],
|
|
70
|
-
key:
|
|
71
|
-
value:
|
|
72
|
-
): Map<KeyedValue<Item,
|
|
78
|
+
key: ItemKey,
|
|
79
|
+
value: ItemValue,
|
|
80
|
+
): Map<KeyedValue<Item, ItemKey>, ReturnType<ItemValue>>;
|
|
73
81
|
|
|
74
82
|
/**
|
|
75
83
|
* Create a map from an array of items using a specific key and value, and grouping them into arrays
|
|
76
84
|
*/
|
|
77
85
|
export function toMap<
|
|
78
|
-
Item,
|
|
79
|
-
|
|
80
|
-
|
|
86
|
+
Item extends PlainObject,
|
|
87
|
+
ItemKey extends keyof Item,
|
|
88
|
+
ItemValue extends (item: Item, index: number, array: Item[]) => unknown,
|
|
81
89
|
>(
|
|
82
90
|
array: Item[],
|
|
83
|
-
key:
|
|
84
|
-
value:
|
|
91
|
+
key: ItemKey,
|
|
92
|
+
value: ItemValue,
|
|
85
93
|
arrays: true,
|
|
86
|
-
): Map<KeyedValue<Item,
|
|
94
|
+
): Map<KeyedValue<Item, ItemKey>, Array<ReturnType<ItemValue>>>;
|
|
87
95
|
|
|
88
96
|
/**
|
|
89
97
|
* Create a map from an array of items using a specific key and value
|
|
90
98
|
*/
|
|
91
99
|
export function toMap<
|
|
92
|
-
Item,
|
|
93
|
-
|
|
94
|
-
|
|
100
|
+
Item extends PlainObject,
|
|
101
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
102
|
+
ItemValue extends keyof Item,
|
|
95
103
|
>(
|
|
96
104
|
array: Item[],
|
|
97
|
-
key:
|
|
98
|
-
value:
|
|
99
|
-
): Map<ReturnType<
|
|
105
|
+
key: ItemKey,
|
|
106
|
+
value: ItemValue,
|
|
107
|
+
): Map<ReturnType<ItemKey>, KeyedValue<Item, ItemValue>>;
|
|
100
108
|
|
|
101
109
|
/**
|
|
102
110
|
* Create a map from an array of items using a specific key and value, and grouping them into arrays
|
|
103
111
|
*/
|
|
104
112
|
export function toMap<
|
|
105
|
-
Item,
|
|
106
|
-
|
|
107
|
-
|
|
113
|
+
Item extends PlainObject,
|
|
114
|
+
ItemKey extends (item: Item, index: number, array: Item[]) => Key,
|
|
115
|
+
ItemValue extends keyof Item,
|
|
108
116
|
>(
|
|
109
117
|
array: Item[],
|
|
110
|
-
key:
|
|
111
|
-
value:
|
|
118
|
+
key: ItemKey,
|
|
119
|
+
value: ItemValue,
|
|
112
120
|
arrays: true,
|
|
113
|
-
): Map<ReturnType<
|
|
121
|
+
): Map<ReturnType<ItemKey>, Array<KeyedValue<Item, ItemValue>>>;
|
|
114
122
|
|
|
115
123
|
/**
|
|
116
124
|
* Create a map from an array of items using a specific key and value
|
|
117
125
|
*/
|
|
118
126
|
export function toMap<
|
|
119
127
|
Item,
|
|
120
|
-
Key extends (item: Item, index: number, array: Item[]) =>
|
|
128
|
+
Key extends (item: Item, index: number, array: Item[]) => Key,
|
|
121
129
|
Value extends (item: Item, index: number, array: Item[]) => unknown,
|
|
122
130
|
>(
|
|
123
131
|
array: Item[],
|
|
@@ -130,7 +138,7 @@ export function toMap<
|
|
|
130
138
|
*/
|
|
131
139
|
export function toMap<
|
|
132
140
|
Item,
|
|
133
|
-
Key extends (item: Item, index: number, array: Item[]) =>
|
|
141
|
+
Key extends (item: Item, index: number, array: Item[]) => Key,
|
|
134
142
|
Value extends (item: Item, index: number, array: Item[]) => unknown,
|
|
135
143
|
>(
|
|
136
144
|
array: Item[],
|
|
@@ -144,10 +152,10 @@ export function toMap(
|
|
|
144
152
|
first?: unknown,
|
|
145
153
|
second?: unknown,
|
|
146
154
|
third?: unknown,
|
|
147
|
-
): Map<
|
|
155
|
+
): Map<Key, unknown> {
|
|
148
156
|
const asArrays = first === true || second === true || third === true;
|
|
149
157
|
const callbacks = getCallbacks(undefined, first, second);
|
|
150
|
-
const map = new Map<
|
|
158
|
+
const map = new Map<Key, unknown>();
|
|
151
159
|
const {length} = array;
|
|
152
160
|
|
|
153
161
|
for (let index = 0; index < length; index += 1) {
|