@oscarpalmer/atoms 0.200.0 → 0.202.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.
@@ -1,33 +1,13 @@
1
- import { Key, KeyedValue, PlainObject, Simplify } from "../models.mjs";
1
+ import { Key, KeyOrCallback, KeyOrCallbackKey, KeyOrCallbackValue, Simplify } from "../models.mjs";
2
2
  //#region src/array/group-by.d.ts
3
3
  /**
4
- * Create a record from an array of items using a specific key and value
5
- *
6
- * If multiple items have the same key, the latest item's value will be used
7
- *
8
- * @param array Array to group
9
- * @param key Callback to get an item's grouping key
10
- * @param value Callback to get an item's value
11
- * @returns Record of keyed values
12
- *
13
- * @example
14
- * ```typescript
15
- * groupBy(
16
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
17
- * item => item.value,
18
- * item => item,
19
- * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
20
- * ```
21
- */
22
- declare function groupBy<Item, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: KeyCallback, value: ValueCallback): Simplify<Record<ReturnType<KeyCallback>, ReturnType<ValueCallback>>>;
23
- /**
24
- * Create a record from an array of items using a specific key and value
4
+ * Create a record from an array of items using a specific key
25
5
  *
26
6
  * If multiple items have the same key, the latest item's value will be used
27
7
  *
28
8
  * @param array Array to group
29
- * @param key Callback to get an item's grouping key
30
- * @param value Key to use for value
9
+ * @param key Callback or key to use for grouping
10
+ * @param value Callback or key to use for value, defaulting to the item itself
31
11
  * @returns Record of keyed values
32
12
  *
33
13
  * @example
@@ -35,58 +15,18 @@ declare function groupBy<Item, KeyCallback extends (item: Item, index: number, a
35
15
  * groupBy(
36
16
  * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
37
17
  * item => item.value,
38
- * 'id'
39
- * ); // => {10: 3, 20: 2}
40
- * ```
41
- */
42
- declare function groupBy<Item extends PlainObject, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item>(array: Item[], key: KeyCallback, value: ItemValue): Record<ReturnType<KeyCallback>, Item[ItemValue]>;
43
- /**
44
- * Create a record from an array of items using a specific key and value
45
- *
46
- * If multiple items have the same key, the latest item's value will be used
47
- *
48
- * @param array Array to group
49
- * @param key Key to use for grouping
50
- * @param value Callback to get an item's value
51
- * @returns Record of keyed values
52
- *
53
- * @example
54
- * ```typescript
55
- * groupBy(
56
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
57
- * 'value',
58
18
  * item => item,
59
19
  * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
60
20
  * ```
61
21
  */
62
- declare function groupBy<Item extends PlainObject, ItemKey extends keyof Item, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: ItemKey, value: ValueCallback): Simplify<Record<KeyedValue<Item, ItemKey>, ReturnType<ValueCallback>>>;
63
- /**
64
- * Create a record from an array of items using a specific key and value
65
- *
66
- * If multiple items have the same key, the latest item's value will be used
67
- *
68
- * @param array Array to group
69
- * @param key Key to use for grouping
70
- * @param value Key to use for value
71
- * @returns Record of keyed values
72
- *
73
- * @example
74
- * ```typescript
75
- * groupBy(
76
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
77
- * 'value',
78
- * 'id'
79
- * ); // => {10: 3, 20: 2}
80
- * ```
81
- */
82
- declare function groupBy<Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item>(array: Item[], key: ItemKey, value: ItemValue): Simplify<Record<KeyedValue<Item, ItemKey>, Item[ItemValue]>>;
22
+ declare function groupBy<Item, KeyArg extends KeyOrCallback<Item, Key>, ValueArg extends KeyOrCallback<Item>>(array: Item[], key: KeyArg, value: ValueArg): Simplify<Record<KeyOrCallbackKey<Item, KeyArg>, KeyOrCallbackValue<Item, ValueArg>>>;
83
23
  /**
84
24
  * Create a record from an array of items using a specific key
85
25
  *
86
26
  * If multiple items have the same key, the latest item will be used
87
27
  *
88
28
  * @param array Array to group
89
- * @param callback Callback to get an item's grouping key
29
+ * @param key Callback or key to use for grouping
90
30
  * @returns Record of keyed items
91
31
  *
92
32
  * @example
@@ -97,25 +37,7 @@ declare function groupBy<Item extends PlainObject, ItemKey extends keyof Item, I
97
37
  * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
98
38
  * ```
99
39
  */
100
- declare function groupBy<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(array: Item[], callback: Callback): Record<ReturnType<Callback>, Item>;
101
- /**
102
- * Create a record from an array of items using a specific key
103
- *
104
- * If multiple items have the same key, the latest item will be used
105
- *
106
- * @param array Array to group
107
- * @param key Key to use for grouping
108
- * @returns Record of keyed items
109
- *
110
- * @example
111
- * ```typescript
112
- * groupBy(
113
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
114
- * 'value',
115
- * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
116
- * ```
117
- */
118
- declare function groupBy<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Simplify<Record<KeyedValue<Item, ItemKey>, Item>>;
40
+ declare function groupBy<Item, KeyArg extends KeyOrCallback<Item, Key>>(array: Item[], key: KeyArg): Simplify<Record<KeyOrCallbackKey<Item, KeyArg>, Item>>;
119
41
  /**
120
42
  * Create a record from an array of items _(using indices as keys)_
121
43
  *
@@ -134,13 +56,13 @@ declare namespace groupBy {
134
56
  var arrays: typeof groupArraysBy;
135
57
  }
136
58
  /**
137
- * Create a record from an array of items using a specific key and value, grouping values into arrays
59
+ * Create a record from an array of items using a specific key, grouping values into arrays
138
60
  *
139
61
  * _Available as `groupArraysBy` and `groupBy.arrays`_
140
62
  *
141
63
  * @param array Array to group
142
- * @param key Callback to get an item's grouping key
143
- * @param value Callback to get an item's value
64
+ * @param key Callback or key to use for grouping
65
+ * @param value Callback or key to use for value, defaulting to the item itself
144
66
  * @returns Record of keyed values
145
67
  *
146
68
  * @example
@@ -155,83 +77,14 @@ declare namespace groupBy {
155
77
  * // }
156
78
  * ```
157
79
  */
158
- declare function groupArraysBy<Item, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: KeyCallback, value: ValueCallback): Record<ReturnType<KeyCallback>, Array<ReturnType<ValueCallback>>>;
159
- /**
160
- * Create a record from an array of items using a specific key and value, grouping values into arrays
161
- *
162
- * _Available as `groupArraysBy` and `groupBy.arrays`_
163
- *
164
- * @param array Array to group
165
- * @param key Callback to get an item's grouping key
166
- * @param value Key to use for value
167
- * @returns Record of keyed values
168
- *
169
- * @example
170
- * ```typescript
171
- * groupArraysBy(
172
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
173
- * item => item.value,
174
- * 'id',
175
- * ); // => {
176
- * // 10: [1, 3],
177
- * // 20: [2],
178
- * // }
179
- * ```
180
- */
181
- declare function groupArraysBy<Item extends PlainObject, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item>(array: Item[], key: KeyCallback, value: ItemValue): Record<ReturnType<KeyCallback>, Item[ItemValue][]>;
182
- /**
183
- * Create a record from an array of items using a specific key and value, grouping values into arrays
184
- *
185
- * _Available as `groupArraysBy` and `groupBy.arrays`_
186
- *
187
- * @param array Array to group
188
- * @param key Key to use for grouping
189
- * @param value Callback to get an item's value
190
- * @returns Record of keyed values
191
- *
192
- * @example
193
- * ```typescript
194
- * groupArraysBy(
195
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
196
- * 'value',
197
- * item => item,
198
- * ); // => {
199
- * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
200
- * // 20: [{id: 2, value: 20}],
201
- * // }
202
- * ```
203
- */
204
- declare function groupArraysBy<Item extends PlainObject, ItemKey extends keyof Item, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: ItemKey, value: ValueCallback): Simplify<Record<KeyedValue<Item, ItemKey>, Array<ReturnType<ValueCallback>>>>;
205
- /**
206
- * Create a record from an array of items using a specific key and value, grouping values into arrays
207
- *
208
- * _Available as `groupArraysBy` and `groupBy.arrays`_
209
- *
210
- * @param array Array to group
211
- * @param key Key to use for grouping
212
- * @param value Key to use for value
213
- * @returns Record of keyed values
214
- *
215
- * @example
216
- * ```typescript
217
- * groupArraysBy(
218
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
219
- * 'value',
220
- * 'id',
221
- * ); // => {
222
- * // 10: [1, 3],
223
- * // 20: [2],
224
- * // }
225
- * ```
226
- */
227
- declare function groupArraysBy<Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item>(array: Item[], key: ItemKey, value: ItemValue): Simplify<Record<KeyedValue<Item, ItemKey>, Item[ItemValue][]>>;
80
+ declare function groupArraysBy<Item, KeyArg extends KeyOrCallback<Item, Key>, ValueArg extends KeyOrCallback<Item>>(array: Item[], key: KeyArg, value: ValueArg): Simplify<Record<KeyOrCallbackKey<Item, KeyArg>, Array<KeyOrCallbackValue<Item, ValueArg>>>>;
228
81
  /**
229
82
  * Create a record from an array of items using a specific key, grouping items into arrays
230
83
  *
231
84
  * _Available as `groupArraysBy` and `groupBy.arrays`_
232
85
  *
233
86
  * @param array Array to group
234
- * @param callback Callback to get an item's grouping key
87
+ * @param key Callback or key to use for grouping
235
88
  * @returns Record of keyed items
236
89
  *
237
90
  * @example
@@ -245,27 +98,6 @@ declare function groupArraysBy<Item extends PlainObject, ItemKey extends keyof I
245
98
  * // }
246
99
  * ```
247
100
  */
248
- declare function groupArraysBy<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(array: Item[], callback: Callback): Record<ReturnType<Callback>, Item[]>;
249
- /**
250
- * Create a record from an array of items using a specific key, grouping items into arrays
251
- *
252
- * _Available as `groupArraysBy` and `groupBy.arrays`_
253
- *
254
- * @param array Array to group
255
- * @param key Key to use for grouping
256
- * @returns Record of keyed items
257
- *
258
- * @example
259
- * ```typescript
260
- * groupArraysBy(
261
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
262
- * 'value',
263
- * ); // => {
264
- * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
265
- * // 20: [{id: 2, value: 20}],
266
- * // }
267
- * ```
268
- */
269
- declare function groupArraysBy<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Simplify<Record<KeyedValue<Item, ItemKey>, Item[]>>;
101
+ declare function groupArraysBy<Item, KeyArg extends KeyOrCallback<Item, Key>>(array: Item[], key: KeyArg): Simplify<Record<KeyOrCallbackKey<Item, KeyArg>, Item[]>>;
270
102
  //#endregion
271
103
  export { groupArraysBy, groupBy };
@@ -14,7 +14,7 @@ import { intersection } from "./intersection.mjs";
14
14
  import { partition } from "./partition.mjs";
15
15
  import { push } from "./push.mjs";
16
16
  import { reverse } from "./reverse.mjs";
17
- import { select } from "./select.mjs";
17
+ import { reverseSelect, select } from "./select.mjs";
18
18
  import { single } from "./single.mjs";
19
19
  import { drop, slice, take } from "./slice.mjs";
20
20
  import { splice } from "./splice.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 { 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 };
26
+ export { ArrayComparison, chunk, compact, difference, drop, endsWithArray, exists, find, findLast, flatten, getArray, getArrayComparison, includesArray, indexOf, indexOfArray, insert, intersection, lastIndexOf, partition, push, range, reverse, reverseSelect, select, shuffle, single, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
@@ -14,7 +14,7 @@ import { intersection } from "./intersection.mjs";
14
14
  import { partition } from "./partition.mjs";
15
15
  import { push } from "./push.mjs";
16
16
  import { reverse } from "./reverse.mjs";
17
- import { select } from "./select.mjs";
17
+ import { reverseSelect, select } from "./select.mjs";
18
18
  import { single } from "./single.mjs";
19
19
  import { drop, slice, take } from "./slice.mjs";
20
20
  import { splice } from "./splice.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, getArrayComparison, includesArray, indexOf, indexOfArray, insert, intersection, lastIndexOf, partition, push, range, reverse, select, shuffle, single, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
26
+ export { chunk, compact, difference, drop, endsWithArray, exists, find, findLast, flatten, getArray, getArrayComparison, includesArray, indexOf, indexOfArray, insert, intersection, lastIndexOf, partition, push, range, reverse, reverseSelect, select, shuffle, single, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
@@ -1,109 +1,111 @@
1
1
  import { PlainObject } from "../models.mjs";
2
2
  //#region src/array/select.d.ts
3
+ type MapKeyOrCallback<Item> = ((item: Item, index: number, array: Item[]) => unknown) | (Item extends PlainObject ? keyof Item : never);
4
+ type Mapped<Item, Map extends MapKeyOrCallback<Item>> = Map extends ((item: Item, index: number, array: Item[]) => unknown) ? ReturnType<Map> : Map extends keyof Item ? Item[Map] : never;
5
+ type ObjectKeysOf<Item> = { [Key in keyof Item]: Item[Key] extends PlainObject ? Key : never; }[keyof Item];
3
6
  /**
4
- * Get a filtered and mapped array of items
7
+ * Get a mapped and filtered array of items
5
8
  *
6
9
  * @param array Array to search in
7
- * @param filterCallback Callback to get an item's value for matching
10
+ * @param map Callback or key to map the items
11
+ * @param filterCallback Callback to get a mapped value's value for matching
8
12
  * @param filterValue Value to match against
9
- * @param mapCallback Callback to map the matched items
10
- * @returns Filtered and mapped array of items
13
+ * @returns Mapped and filtered array of items
11
14
  *
12
15
  * @example
13
16
  * ```typescript
14
- * select(
17
+ * reverseSelect(
15
18
  * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
16
- * item => item.id,
17
- * 2,
18
19
  * item => item.name,
20
+ * value => value.length,
21
+ * 3,
19
22
  * ); // => ['Bob']
20
23
  * ```
21
24
  */
22
- declare function select<Item, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
25
+ declare function reverseSelect<Item, Map extends MapKeyOrCallback<Item>, FilterCallback extends (item: Mapped<Item, Map>, index: number, array: Item[]) => unknown>(array: Item[], map: Map, filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>): Array<Mapped<Item, Map>>;
23
26
  /**
24
- * Get a filtered and mapped array of items
27
+ * Get a mapped and filtered array of items
25
28
  *
26
29
  * @param array Array to search in
27
- * @param filterCallback Callback to get an item's value for matching
30
+ * @param map Callback to map the items
31
+ * @param filterKey Key to get a mapped value's value for matching
28
32
  * @param filterValue Value to match against
29
- * @param mapKey Key to get an item's value for mapping
30
- * @returns Filtered and mapped array of items
33
+ * @returns Mapped and filtered array of items
31
34
  *
32
35
  * @example
33
36
  * ```typescript
34
- * select(
37
+ * reverseSelect(
35
38
  * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
36
- * item => item.id,
37
- * 2,
38
- * 'name',
39
- * ); // => ['Bob']
39
+ * item => ({label: item.name}),
40
+ * 'label',
41
+ * 'Bob',
42
+ * ); // => [{label: 'Bob'}]
40
43
  * ```
41
44
  */
42
- declare function select<Item extends PlainObject, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapKey extends keyof Item>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapKey: MapKey): Array<Item[MapKey]>;
45
+ declare function reverseSelect<Item, MapCallback extends (item: Item, index: number, array: Item[]) => PlainObject, ItemKey extends keyof ReturnType<MapCallback>>(array: Item[], map: MapCallback, filterKey: ItemKey, filterValue: ReturnType<MapCallback>[ItemKey]): Array<ReturnType<MapCallback>>;
43
46
  /**
44
- * Get a filtered and mapped array of items
47
+ * Get a mapped and filtered array of items
45
48
  *
46
49
  * @param array Array to search in
47
- * @param filterKey Key to get an item's value for matching
50
+ * @param map Key to map the items
51
+ * @param filterKey Key to get a mapped value's value for matching
48
52
  * @param filterValue Value to match against
49
- * @param mapCallback Callback to map the matched items
50
- * @returns Filtered and mapped array of items
53
+ * @returns Mapped and filtered array of items
51
54
  *
52
55
  * @example
53
56
  * ```typescript
54
- * select(
55
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
56
- * 'id',
57
- * 2,
58
- * item => item.name,
59
- * ); // => ['Bob']
57
+ * reverseSelect(
58
+ * [{meta: {tag: 'a'}}, {meta: {tag: 'b'}}],
59
+ * 'meta',
60
+ * 'tag',
61
+ * 'b',
62
+ * ); // => [{tag: 'b'}]
60
63
  * ```
61
64
  */
62
- declare function select<Item extends PlainObject, ItemKey extends keyof Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterKey: ItemKey, filterValue: Item[ItemKey], mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
65
+ declare function reverseSelect<Item, MapKey extends ObjectKeysOf<Item>, ItemKey extends keyof Item[MapKey]>(array: Item[], map: MapKey, filterKey: ItemKey, filterValue: Item[MapKey][ItemKey]): Array<Item[MapKey]>;
63
66
  /**
64
- * Get a filtered and mapped array of items
67
+ * Get a mapped and filtered array of items
65
68
  *
66
69
  * @param array Array to search in
67
- * @param filterKey Key to get an item's value for matching
68
- * @param filterValue Value to match against
69
- * @param mapKey Key to get an item's value for mapping
70
- * @returns Filtered and mapped array of items
70
+ * @param map Callback or key to map the items
71
+ * @param filter Filter callback to match mapped values
72
+ * @returns Mapped and filtered array of items
71
73
  *
72
74
  * @example
73
75
  * ```typescript
74
- * select(
76
+ * reverseSelect(
75
77
  * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
76
- * 'id',
77
- * 2,
78
- * 'name',
78
+ * item => item.name,
79
+ * value => value.startsWith('B'),
79
80
  * ); // => ['Bob']
80
81
  * ```
81
82
  */
82
- declare function select<Item extends PlainObject, ItemKey extends keyof Item, MapKey extends keyof Item>(array: Item[], filterKey: ItemKey, filterValue: Item[ItemKey], mapKey: MapKey): Array<Item[MapKey]>;
83
+ declare function reverseSelect<Item, Map extends MapKeyOrCallback<Item>>(array: Item[], map: Map, filter: (item: Mapped<Item, Map>, index: number, array: Item[]) => boolean): Array<Mapped<Item, Map>>;
83
84
  /**
84
- * Get a filtered and mapped array of items
85
+ * Get a mapped and filtered array of items
85
86
  *
86
87
  * @param array Array to search in
87
- * @param filterCallback Filter callback to match items
88
- * @param mapCallback Callback to map the matched items
89
- * @returns Filtered and mapped array of items
88
+ * @param map Callback or key to map the items
89
+ * @param value Mapped value to match against
90
+ * @returns Mapped and filtered array of items
90
91
  *
91
92
  * @example
92
93
  * ```typescript
93
- * select(
94
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
95
- * item => item.id === 2,
96
- * item => item.name,
97
- * ); // => ['Bob']
94
+ * reverseSelect(
95
+ * [1, 2, 3, 2, 1],
96
+ * value => value ** 2,
97
+ * 4,
98
+ * ); // => [4, 4]
98
99
  * ```
99
100
  */
100
- declare function select<Item, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
101
+ declare function reverseSelect<Item, Map extends MapKeyOrCallback<Item>>(array: Item[], map: Map, value: Mapped<Item, Map>): Array<Mapped<Item, Map>>;
101
102
  /**
102
103
  * Get a filtered and mapped array of items
103
104
  *
104
105
  * @param array Array to search in
105
- * @param filterCallback Filter callback to match items
106
- * @param mapKey Key to get an item's value for mapping
106
+ * @param filterCallback Callback to get an item's value for matching
107
+ * @param filterValue Value to match against
108
+ * @param map Callback or key to map the matched items
107
109
  * @returns Filtered and mapped array of items
108
110
  *
109
111
  * @example
@@ -112,35 +114,37 @@ declare function select<Item, FilterCallback extends (item: Item, index: number,
112
114
  * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
113
115
  * item => item.id,
114
116
  * 2,
115
- * 'name'
117
+ * item => item.name,
116
118
  * ); // => ['Bob']
117
119
  * ```
118
120
  */
119
- declare function select<Item extends PlainObject, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapKey extends keyof Item>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapKey: MapKey): Array<Item[MapKey]>;
121
+ declare function select<Item, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, Map extends MapKeyOrCallback<Item>>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, map: Map): Array<Mapped<Item, Map>>;
120
122
  /**
121
123
  * Get a filtered and mapped array of items
122
124
  *
123
125
  * @param array Array to search in
124
- * @param filter Filter callback to match items
125
- * @param map Callback to map the matched items
126
+ * @param filterKey Key to get an item's value for matching
127
+ * @param filterValue Value to match against
128
+ * @param map Callback or key to map the matched items
126
129
  * @returns Filtered and mapped array of items
127
130
  *
128
131
  * @example
129
132
  * ```typescript
130
133
  * select(
131
134
  * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
132
- * item => item.id === 2,
133
- * item => item.name,
135
+ * 'id',
136
+ * 2,
137
+ * 'name',
134
138
  * ); // => ['Bob']
135
139
  * ```
136
140
  */
137
- declare function select<Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, map: MapCallback): Array<ReturnType<MapCallback>>;
141
+ declare function select<Item extends PlainObject, ItemKey extends keyof Item, Map extends MapKeyOrCallback<Item>>(array: Item[], filterKey: ItemKey, filterValue: Item[ItemKey], map: Map): Array<Mapped<Item, Map>>;
138
142
  /**
139
143
  * Get a filtered and mapped array of items
140
144
  *
141
145
  * @param array Array to search in
142
146
  * @param filter Filter callback to match items
143
- * @param map Key to get an item's value for mapping
147
+ * @param map Callback or key to map the matched items
144
148
  * @returns Filtered and mapped array of items
145
149
  *
146
150
  * @example
@@ -148,17 +152,17 @@ declare function select<Item, MapCallback extends (item: Item, index: number, ar
148
152
  * select(
149
153
  * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
150
154
  * item => item.id === 2,
151
- * 'name'
155
+ * item => item.name,
152
156
  * ); // => ['Bob']
153
157
  * ```
154
158
  */
155
- declare function select<Item extends PlainObject, MapKey extends keyof Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, map: MapKey): Array<Item[MapKey]>;
159
+ declare function select<Item, Map extends MapKeyOrCallback<Item>>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, map: Map): Array<Mapped<Item, Map>>;
156
160
  /**
157
161
  * Get a filtered and mapped array of items
158
162
  *
159
163
  * @param array Array to search in
160
164
  * @param item Item to match against
161
- * @param map Callback to map the matched items
165
+ * @param map Callback or key to map the matched items
162
166
  * @returns Filtered and mapped array of items
163
167
  *
164
168
  * @example
@@ -170,6 +174,9 @@ declare function select<Item extends PlainObject, MapKey extends keyof Item>(arr
170
174
  * ); // => [9]
171
175
  * ```
172
176
  */
173
- declare function select<Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], item: Item, map: MapCallback): Array<ReturnType<MapCallback>>;
177
+ declare function select<Item, Map extends MapKeyOrCallback<Item>>(array: Item[], item: Item, map: Map): Array<Mapped<Item, Map>>;
178
+ declare namespace select {
179
+ var reverse: typeof reverseSelect;
180
+ }
174
181
  //#endregion
175
- export { select };
182
+ export { reverseSelect, select };
@@ -1,8 +1,23 @@
1
1
  import { findValues } from "../internal/array/find.mjs";
2
2
  //#region src/array/select.ts
3
+ function reverseSelect(array, ...parameters) {
4
+ const mapper = parameters.shift();
5
+ return findValues("all", array, parameters, {
6
+ callback: mapper,
7
+ reverse: true
8
+ }).matched;
9
+ }
3
10
  function select(array, ...parameters) {
11
+ return selectValues(array, parameters);
12
+ }
13
+ function selectValues(array, parameters) {
4
14
  const mapper = parameters.pop();
5
- return findValues("all", array, parameters, mapper).matched;
15
+ return findValues("all", array, parameters, {
16
+ callback: mapper,
17
+ reverse: false
18
+ }).matched;
6
19
  }
20
+ select.reverse = reverseSelect;
21
+ Object.defineProperty(select, "reverse", { value: reverseSelect });
7
22
  //#endregion
8
- export { select };
23
+ export { reverseSelect, select };