@oscarpalmer/atoms 0.185.0 → 0.186.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.
Files changed (94) hide show
  1. package/dist/array/difference.d.mts +29 -0
  2. package/dist/array/exists.d.mts +35 -0
  3. package/dist/array/filter.d.mts +72 -2
  4. package/dist/array/find.d.mts +70 -0
  5. package/dist/array/first.d.mts +77 -2
  6. package/dist/array/flatten.d.mts +6 -0
  7. package/dist/array/flatten.mjs +6 -0
  8. package/dist/array/from.d.mts +36 -0
  9. package/dist/array/get.d.mts +21 -13
  10. package/dist/array/group-by.d.mts +142 -0
  11. package/dist/array/insert.d.mts +16 -0
  12. package/dist/array/intersection.d.mts +29 -0
  13. package/dist/array/last.d.mts +75 -2
  14. package/dist/array/match.d.mts +161 -32
  15. package/dist/array/move.d.mts +78 -8
  16. package/dist/array/move.mjs +10 -0
  17. package/dist/array/partition.d.mts +35 -0
  18. package/dist/array/push.d.mts +8 -0
  19. package/dist/array/push.mjs +8 -0
  20. package/dist/array/reverse.d.mts +1 -0
  21. package/dist/array/reverse.mjs +1 -0
  22. package/dist/array/select.d.mts +94 -8
  23. package/dist/array/single.d.mts +29 -0
  24. package/dist/array/slice.d.mts +106 -16
  25. package/dist/array/sort.d.mts +21 -0
  26. package/dist/array/splice.d.mts +48 -0
  27. package/dist/array/splice.mjs +2 -1
  28. package/dist/array/swap.d.mts +113 -8
  29. package/dist/array/swap.mjs +1 -0
  30. package/dist/array/to-map.d.mts +124 -0
  31. package/dist/array/to-record.d.mts +124 -0
  32. package/dist/array/to-set.d.mts +24 -0
  33. package/dist/array/toggle.d.mts +38 -3
  34. package/dist/array/union.d.mts +29 -0
  35. package/dist/array/unique.d.mts +24 -0
  36. package/dist/array/update.d.mts +38 -3
  37. package/dist/index.d.mts +1892 -135
  38. package/dist/index.mjs +64 -18
  39. package/dist/internal/array/chunk.d.mts +6 -0
  40. package/dist/internal/array/chunk.mjs +6 -0
  41. package/dist/internal/array/compact.d.mts +12 -0
  42. package/dist/internal/array/index-of.d.mts +70 -0
  43. package/dist/internal/math/aggregate.d.mts +29 -0
  44. package/dist/internal/value/get.d.mts +25 -3
  45. package/dist/internal/value/has.d.mts +4 -4
  46. package/dist/models.d.mts +14 -1
  47. package/dist/value/collection.d.mts +1 -1
  48. package/dist/value/merge.d.mts +28 -25
  49. package/dist/value/merge.mjs +29 -18
  50. package/dist/value/transform.d.mts +1 -1
  51. package/dist/value/unsmush.d.mts +1 -5
  52. package/package.json +5 -5
  53. package/src/array/difference.ts +29 -0
  54. package/src/array/exists.ts +35 -0
  55. package/src/array/filter.ts +72 -2
  56. package/src/array/find.ts +70 -0
  57. package/src/array/first.ts +77 -3
  58. package/src/array/flatten.ts +6 -0
  59. package/src/array/from.ts +36 -0
  60. package/src/array/get.ts +21 -15
  61. package/src/array/group-by.ts +142 -0
  62. package/src/array/insert.ts +16 -2
  63. package/src/array/intersection.ts +29 -0
  64. package/src/array/last.ts +75 -2
  65. package/src/array/match.ts +171 -42
  66. package/src/array/move.ts +82 -12
  67. package/src/array/partition.ts +35 -0
  68. package/src/array/push.ts +8 -2
  69. package/src/array/reverse.ts +1 -0
  70. package/src/array/select.ts +94 -13
  71. package/src/array/single.ts +29 -0
  72. package/src/array/slice.ts +114 -24
  73. package/src/array/sort.ts +21 -0
  74. package/src/array/splice.ts +52 -4
  75. package/src/array/swap.ts +117 -12
  76. package/src/array/to-map.ts +124 -0
  77. package/src/array/to-record.ts +124 -0
  78. package/src/array/to-set.ts +24 -0
  79. package/src/array/toggle.ts +38 -3
  80. package/src/array/union.ts +29 -0
  81. package/src/array/unique.ts +24 -0
  82. package/src/array/update.ts +38 -3
  83. package/src/internal/array/chunk.ts +6 -0
  84. package/src/internal/array/compact.ts +12 -0
  85. package/src/internal/array/index-of.ts +70 -0
  86. package/src/internal/math/aggregate.ts +29 -0
  87. package/src/internal/string.ts +0 -2
  88. package/src/internal/value/get.ts +25 -3
  89. package/src/internal/value/has.ts +4 -4
  90. package/src/models.ts +18 -0
  91. package/src/value/collection.ts +1 -1
  92. package/src/value/merge.ts +88 -66
  93. package/src/value/transform.ts +1 -1
  94. package/src/value/unsmush.ts +1 -10
package/src/array/sort.ts CHANGED
@@ -112,6 +112,7 @@ export type SortDirection = 'ascending' | 'descending';
112
112
  export type Sorter<Item> = {
113
113
  /**
114
114
  * Sort an array of items
115
+ *
115
116
  * @param array Array to sort
116
117
  * @returns Sorted array
117
118
  */
@@ -121,6 +122,7 @@ export type Sorter<Item> = {
121
122
  * Get the index for an item _(to be inserted into an array of items)_
122
123
  *
123
124
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
125
+ *
124
126
  * @param array Array to get the index from
125
127
  * @param item Item to get the index for
126
128
  * @returns Index for item
@@ -129,6 +131,7 @@ export type Sorter<Item> = {
129
131
 
130
132
  /**
131
133
  * Is the array sorted?
134
+ *
132
135
  * @param array Array to check
133
136
  * @returns `true` if sorted, otherwise `false`
134
137
  */
@@ -251,6 +254,7 @@ function getObjectSorter(obj: PlainObject, modifier: number): InternalSorter | u
251
254
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
252
255
  *
253
256
  * Available as `getSortedIndex` and `sort.getIndex`
257
+ *
254
258
  * @param array Array to get the index from
255
259
  * @param item Item to get the index for
256
260
  * @param sorters Sorters to use to determine sorting
@@ -270,6 +274,7 @@ export function getSortedIndex<Item>(
270
274
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
271
275
  *
272
276
  * Available as `getSortedIndex` and `sort.getIndex`
277
+ *
273
278
  * @param array Array to get the index from
274
279
  * @param item Item to get the index for
275
280
  * @param sorter Sorter to use to determine sorting
@@ -289,6 +294,7 @@ export function getSortedIndex<Item>(
289
294
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
290
295
  *
291
296
  * Available as `getSortedIndex` and `sort.getIndex`
297
+ *
292
298
  * @param array Array to get the index from
293
299
  * @param item Item to get the index for
294
300
  * @param descending Sorted in descending order? _(defaults to `false`)_
@@ -366,6 +372,7 @@ function getValueSorter(value: string | Function, modifier: number): InternalSor
366
372
  * Initialize a sort handler with sorters _(and an optional default direction)_
367
373
  *
368
374
  * Available as `initializeSorter` and `sort.initialize`
375
+ *
369
376
  * @param sorters Sorters to use for sorting
370
377
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
371
378
  * @returns Sort handler
@@ -379,6 +386,7 @@ export function initializeSorter<Item>(
379
386
  * Initialize a sort handler with a sorter _(and an optional default direction)_
380
387
  *
381
388
  * Available as `initializeSorter` and `sort.initialize`
389
+ *
382
390
  * @param sorter Sorter to use for sorting
383
391
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
384
392
  * @returns Sort handler
@@ -392,6 +400,7 @@ export function initializeSorter<Item>(
392
400
  * Initialize a sort handler _(with an optional default direction)_
393
401
  *
394
402
  * Available as `initializeSorter` and `sort.initialize`
403
+ *
395
404
  * @param descending Sort in descending order? _(defaults to `false`)_
396
405
  * @returns Sort handler
397
406
  */
@@ -410,6 +419,7 @@ export function initializeSorter(first?: unknown, second?: unknown): Sorter<unkn
410
419
 
411
420
  /**
412
421
  * Is the array sorted according to the sorters _(and the optional default direction)_?
422
+ *
413
423
  * @param array Array to check
414
424
  * @param sorters Sorters to determine sorting
415
425
  * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
@@ -423,6 +433,7 @@ export function isSorted<Item>(
423
433
 
424
434
  /**
425
435
  * Is the array sorted according to the sorter _(and the optional default direction)_?
436
+ *
426
437
  * @param array Array to check
427
438
  * @param sorter Sorter to determine sorting
428
439
  * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
@@ -438,6 +449,7 @@ export function isSorted<Item>(
438
449
  * Is the array sorted?
439
450
  *
440
451
  * Available as `isSorted` and `sort.is`
452
+ *
441
453
  * @param array Array to check
442
454
  * @param descending Sorted in descending order? _(defaults to `false`)_
443
455
  * @returns `true` if sorted, otherwise `false`
@@ -500,10 +512,19 @@ function isSortedArray(array: unknown[], sorters: InternalSorter[]): boolean {
500
512
 
501
513
  /**
502
514
  * Sort an array of items using a comparison callback
515
+ *
503
516
  * @param array Array to sort
504
517
  * @param comparator Comparator to use for sorting
505
518
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
506
519
  * @returns Sorted array
520
+ *
521
+ * @example
522
+ * ```typescript
523
+ * sort(
524
+ * [{id: 3}, {id: 1}, {id: 2}],
525
+ * (first, second) => first.id - second.id,
526
+ * ); // => [{id: 1}, {id: 2}, {id: 3}]
527
+ * ```
507
528
  */
508
529
  export function sort<Item>(
509
530
  array: Item[],
@@ -2,41 +2,87 @@ import {INSERT_TYPE_SPLICE, insertValues} from '../internal/array/insert';
2
2
 
3
3
  // #region Functions
4
4
 
5
- // Uses chunking to avoid call stack size being exceeded
6
-
7
5
  /**
8
6
  * Adds items into an array at a specific index and removes a specific amount of items
7
+ *
8
+ * _(Uses chunking to avoid call stack size being exceeded)_
9
+ *
9
10
  * @param array Original array
10
11
  * @param start Index to start splicing from
11
12
  * @param amount Number of items to remove
12
13
  * @param added Added items
13
14
  * @returns Removed items
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * splice(
19
+ * [1, 2, 3, 4, 5],
20
+ * 2,
21
+ * 2,
22
+ * [6, 7]
23
+ * ); // => [3, 4], array becomes [1, 2, 6, 7, 5]
24
+ * ```
14
25
  */
15
26
  export function splice<Item>(array: Item[], start: number, amount: number, added: Item[]): Item[];
16
27
 
17
28
  /**
18
29
  * Adds items into an array at a specific index
30
+ *
31
+ * _(Uses chunking to avoid call stack size being exceeded)_
32
+ *
19
33
  * @param array Original array
20
34
  * @param start Index to start splicing from
21
35
  * @param added Added items
22
36
  * @returns Removed items
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * splice(
41
+ * [1, 2, 3, 4, 5],
42
+ * 2,
43
+ * [6, 7]
44
+ * ); // => [], array becomes [1, 2, 6, 7, 3, 4, 5]
45
+ * ```
23
46
  */
24
47
  export function splice<Item>(array: Item[], start: number, added: Item[]): Item[];
25
48
 
26
49
  /**
27
50
  * Removes and returns _(up to)_ a specific amount of items from an array, starting from a specific index
51
+ *
52
+ * _(Uses chunking to avoid call stack size being exceeded)_
53
+ *
28
54
  * @param array Original array
29
55
  * @param start Index to start splicing from
30
56
  * @param amount Number of items to remove
31
57
  * @returns Removed items
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * splice(
62
+ * [1, 2, 3, 4, 5],
63
+ * 2,
64
+ * 2,
65
+ * ); // => [3, 4], array becomes [1, 2, 5]
66
+ * ```
32
67
  */
33
68
  export function splice<Item>(array: Item[], start: number, amount: number): Item[];
34
69
 
35
70
  /**
36
71
  * Removes and returns all items from an array starting from a specific index
72
+ *
73
+ * _(Uses chunking to avoid call stack size being exceeded)_
74
+ *
37
75
  * @param array Original array
38
76
  * @param start Index to start splicing from
39
77
  * @returns Removed items
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * splice(
82
+ * [1, 2, 3, 4, 5],
83
+ * 2,
84
+ * ); // => [3, 4, 5], array becomes [1, 2]
85
+ * ```
40
86
  */
41
87
  export function splice<Item>(array: Item[], start: number): Item[];
42
88
 
@@ -46,12 +92,14 @@ export function splice(
46
92
  deleteCountOrItems?: unknown,
47
93
  items?: unknown,
48
94
  ): unknown[] {
95
+ const deleteCountIsNumber = typeof deleteCountOrItems === 'number';
96
+
49
97
  return insertValues(
50
98
  INSERT_TYPE_SPLICE,
51
99
  array,
52
- typeof deleteCountOrItems === 'number' ? items : deleteCountOrItems,
100
+ deleteCountIsNumber ? items : deleteCountOrItems,
53
101
  start,
54
- typeof deleteCountOrItems === 'number' ? deleteCountOrItems : 0,
102
+ deleteCountIsNumber ? deleteCountOrItems : 0,
55
103
  ) as unknown[];
56
104
  }
57
105
 
package/src/array/swap.ts CHANGED
@@ -10,44 +10,100 @@ import {indexOfArray} from './match';
10
10
  * Swap two smaller arrays within a larger array
11
11
  *
12
12
  * If either of the smaller arrays are not present in the larger array, or if they overlap, the larger array will be returned unchanged
13
+ *
13
14
  * @param array Array of items to swap
14
15
  * @param first First array
15
16
  * @param second Second array
16
- * @param key Key to get an item's value for matching
17
+ * @param callback Callback to get an item's value for matching
17
18
  * @returns Original array with items swapped _(or unchanged if unable to swap)_
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * swap(
23
+ * [
24
+ * {id: 1, name: 'Alice'},
25
+ * {id: 2, name: 'Bob'},
26
+ * {id: 3, name: 'Charlie'},
27
+ * ],
28
+ * [
29
+ * {id: 2, name: 'Bob'},
30
+ * ],
31
+ * [
32
+ * {id: 3, name: 'Charlie'},
33
+ * ],
34
+ * item => item.id,
35
+ * ); // => [
36
+ * // {id: 1, name: 'Alice'},
37
+ * // {id: 3, name: 'Charlie'},
38
+ * // {id: 2, name: 'Bob'},
39
+ * // ]
40
+ * ```
18
41
  */
19
- export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
42
+ export function swap<Item>(
20
43
  array: Item[],
21
44
  first: Item[],
22
45
  second: Item[],
23
- key: ItemKey,
46
+ callback: (item: Item, index: number, array: Item[]) => unknown,
24
47
  ): Item[];
25
48
 
26
49
  /**
27
50
  * Swap two smaller arrays within a larger array
28
51
  *
29
52
  * If either of the smaller arrays are not present in the larger array, or if they overlap, the larger array will be returned unchanged
53
+ *
30
54
  * @param array Array of items to swap
31
55
  * @param first First array
32
56
  * @param second Second array
33
- * @param callback Callback to get an item's value for matching
57
+ * @param key Key to get an item's value for matching
34
58
  * @returns Original array with items swapped _(or unchanged if unable to swap)_
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * swap(
63
+ * [
64
+ * {id: 1, name: 'Alice'},
65
+ * {id: 2, name: 'Bob'},
66
+ * {id: 3, name: 'Charlie'},
67
+ * ],
68
+ * [
69
+ * {id: 2, name: 'Bob'},
70
+ * ],
71
+ * [
72
+ * {id: 3, name: 'Charlie'},
73
+ * ],
74
+ * 'id',
75
+ * ); // => [
76
+ * // {id: 1, name: 'Alice'},
77
+ * // {id: 3, name: 'Charlie'},
78
+ * // {id: 2, name: 'Bob'},
79
+ * // ]
80
+ * ```
35
81
  */
36
- export function swap<Item>(
82
+ export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
37
83
  array: Item[],
38
84
  first: Item[],
39
85
  second: Item[],
40
- callback: (item: Item, index: number, array: Item[]) => unknown,
86
+ key: ItemKey,
41
87
  ): Item[];
42
88
 
43
89
  /**
44
90
  * Swap two smaller arrays within a larger array
45
91
  *
46
92
  * If either of the smaller arrays are not present in the larger array, or if they overlap, the larger array will be returned unchanged
93
+ *
47
94
  * @param array Array of items to swap
48
95
  * @param first First array
49
96
  * @param second Second array
50
97
  * @returns Original array with items swapped _(or unchanged if unable to swap)_
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * swap(
102
+ * [1, 2, 3, 4, 5, 6],
103
+ * [1, 2],
104
+ * [5, 6],
105
+ * ); // => [5, 6, 3, 4, 1, 2]
106
+ * ```
51
107
  */
52
108
  export function swap<Item>(array: Item[], first: Item[], second: Item[]): Item[];
53
109
 
@@ -55,42 +111,90 @@ export function swap<Item>(array: Item[], first: Item[], second: Item[]): Item[]
55
111
  * Swap two indiced items in an array
56
112
  *
57
113
  * If either of the items are not present in the array, the array will be returned unchanged
114
+ *
58
115
  * @param array Array of items to swap
59
116
  * @param first First item
60
117
  * @param second Second item
61
- * @param key Key to get an item's value for matching
118
+ * @param callback Callback to get an item's value for matching
62
119
  * @returns Original array with items swapped _(or unchanged if unable to swap)_
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * swap(
124
+ * [
125
+ * {id: 1, name: 'Alice'},
126
+ * {id: 2, name: 'Bob'},
127
+ * {id: 3, name: 'Charlie'},
128
+ * ],
129
+ * {id: 2, name: 'Bob'},
130
+ * {id: 3, name: 'Charlie'},
131
+ * item => item.id,
132
+ * ); // => [
133
+ * // {id: 1, name: 'Alice'},
134
+ * // {id: 3, name: 'Charlie'},
135
+ * // {id: 2, name: 'Bob'},
136
+ * // ]
137
+ * ```
63
138
  */
64
- export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
139
+ export function swap<Item>(
65
140
  array: Item[],
66
141
  first: Item,
67
142
  second: Item,
68
- key: ItemKey,
143
+ callback: (item: Item, index: number, array: Item[]) => unknown,
69
144
  ): Item[];
70
145
 
71
146
  /**
72
147
  * Swap two indiced items in an array
73
148
  *
74
149
  * If either of the items are not present in the array, the array will be returned unchanged
150
+ *
75
151
  * @param array Array of items to swap
76
152
  * @param first First item
77
153
  * @param second Second item
78
- * @param callback Callback to get an item's value for matching
154
+ * @param key Key to get an item's value for matching
79
155
  * @returns Original array with items swapped _(or unchanged if unable to swap)_
156
+ *
157
+ * @example
158
+ * ```typescript
159
+ * swap(
160
+ * [
161
+ * {id: 1, name: 'Alice'},
162
+ * {id: 2, name: 'Bob'},
163
+ * {id: 3, name: 'Charlie'},
164
+ * ],
165
+ * {id: 2, name: 'Bob'},
166
+ * {id: 3, name: 'Charlie'},
167
+ * 'id',
168
+ * ); // => [
169
+ * // {id: 1, name: 'Alice'},
170
+ * // {id: 3, name: 'Charlie'},
171
+ * // {id: 2, name: 'Bob'},
172
+ * // ]
173
+ * ```
80
174
  */
81
- export function swap<Item>(
175
+ export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
82
176
  array: Item[],
83
177
  first: Item,
84
178
  second: Item,
85
- callback: (item: Item, index: number, array: Item[]) => unknown,
179
+ key: ItemKey,
86
180
  ): Item[];
87
181
 
88
182
  /**
89
183
  * Swap two indiced items in an array
184
+ *
90
185
  * @param array Array of items to swap
91
186
  * @param first First item
92
187
  * @param second Second item
93
188
  * @returns Original array with items swapped _(or unchanged if unable to swap)_
189
+ *
190
+ * @example
191
+ * ```typescript
192
+ * swap(
193
+ * [1, 2, 3, 4, 5, 6],
194
+ * 2,
195
+ * 5,
196
+ * ); // => [1, 5, 3, 4, 2, 6]
197
+ * ```
94
198
  */
95
199
  export function swap<Item>(array: Item[], first: Item, second: Item): Item[];
96
200
 
@@ -167,6 +271,7 @@ function swapArrays(array: unknown[], from: unknown[], to: unknown[], key: unkno
167
271
  * If either index is out of bounds, the array will be returned unchanged
168
272
  *
169
273
  * Available as `swapIndices` and `swap.indices`
274
+ *
170
275
  * @param array Array of items to swap
171
276
  * @param first First index _(can be negative to count from the end)_
172
277
  * @param second Second index _(can be negative to count from the end)_
@@ -43,10 +43,20 @@ function getMapValues(
43
43
  * Create a Map from an array of items using callbacks
44
44
  *
45
45
  * If multiple items have the same key, the latest item's value will be used
46
+ *
46
47
  * @param array Array to convert
47
48
  * @param key Callback to get an item's grouping key
48
49
  * @param value Callback to get an item's value
49
50
  * @returns Map of keyed values
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * toMap(
55
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
56
+ * item => item.value,
57
+ * item => item.id,
58
+ * ); // => Map { 10 => 3, 20 => 2 }
59
+ * ```
50
60
  */
51
61
  export function toMap<
52
62
  Item,
@@ -62,10 +72,20 @@ export function toMap<
62
72
  * Create a Map from an array of items using a callback and value
63
73
  *
64
74
  * If multiple items have the same key, the latest item's value will be used
75
+ *
65
76
  * @param array Array to convert
66
77
  * @param key Callback to get an item's grouping key
67
78
  * @param value Key to use for value
68
79
  * @returns Map of keyed values
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * toMap(
84
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
85
+ * item => item.value,
86
+ * 'id',
87
+ * ); // => Map { 10 => 3, 20 => 2 }
88
+ * ```
69
89
  */
70
90
  export function toMap<
71
91
  Item extends PlainObject,
@@ -77,10 +97,20 @@ export function toMap<
77
97
  * Create a Map from an array of items using a key and callback
78
98
  *
79
99
  * If multiple items have the same key, the latest item's value will be used
100
+ *
80
101
  * @param array Array to convert
81
102
  * @param key Key to use for grouping
82
103
  * @param value Callback to get an item's value
83
104
  * @returns Map of keyed values
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * toMap(
109
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
110
+ * 'value',
111
+ * item => item.id,
112
+ * ); // => Map { 10 => 3, 20 => 2 }
113
+ * ```
84
114
  */
85
115
  export function toMap<
86
116
  Item extends PlainObject,
@@ -92,10 +122,20 @@ export function toMap<
92
122
  * Create a Map from an array of items using a key and value
93
123
  *
94
124
  * If multiple items have the same key, the latest item's value will be used
125
+ *
95
126
  * @param array Array to convert
96
127
  * @param key Key to use for grouping
97
128
  * @param value Key to use for value
98
129
  * @returns Map of keyed values
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * toMap(
134
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
135
+ * 'value',
136
+ * 'id',
137
+ * ); // => Map { 10 => 3, 20 => 2 }
138
+ * ```
99
139
  */
100
140
  export function toMap<
101
141
  Item extends PlainObject,
@@ -107,9 +147,18 @@ export function toMap<
107
147
  * Create a Map from an array of items using a callback
108
148
  *
109
149
  * If multiple items have the same key, the latest item will be used
150
+ *
110
151
  * @param array Array to convert
111
152
  * @param callback Callback to get an item's grouping key
112
153
  * @returns Map of keyed items
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * toMap(
158
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
159
+ * item => item.value,
160
+ * ); // => Map { 10 => {id: 3, value: 10}, 20 => {id: 2, value: 20} }
161
+ * ```
113
162
  */
114
163
  export function toMap<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(
115
164
  array: Item[],
@@ -120,9 +169,18 @@ export function toMap<Item, Callback extends (item: Item, index: number, array:
120
169
  * Create a Map from an array of items using a key
121
170
  *
122
171
  * If multiple items have the same key, the latest item will be used
172
+ *
123
173
  * @param array Array to convert
124
174
  * @param key Key to use for grouping
125
175
  * @returns Map of keyed items
176
+ *
177
+ * @example
178
+ * ```typescript
179
+ * toMap(
180
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
181
+ * 'value',
182
+ * ); // => Map { 10 => {id: 3, value: 10}, 20 => {id: 2, value: 20} }
183
+ * ```
126
184
  */
127
185
  export function toMap<Item extends PlainObject, ItemKey extends keyof Item>(
128
186
  array: Item[],
@@ -131,8 +189,16 @@ export function toMap<Item extends PlainObject, ItemKey extends keyof Item>(
131
189
 
132
190
  /**
133
191
  * Create a Map from an array of items _(using indices as keys)_
192
+ *
134
193
  * @param array Array to convert
135
194
  * @returns Map of indiced items
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * toMap(
199
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
200
+ * ); // => Map { 0 => {id: 1, value: 10}, 1 => {id: 2, value: 20}, 2 => {id: 3, value: 10} }
201
+ * ```
136
202
  */
137
203
  export function toMap<Item>(array: Item[]): Map<number, Item>;
138
204
 
@@ -146,10 +212,20 @@ toMap.arrays = toMapArrays;
146
212
  * Create a Map from an array of items using callbacks, grouping values into arrays
147
213
  *
148
214
  * Available as `toMapArrays` and `toMap.arrays`
215
+ *
149
216
  * @param array Array to convert
150
217
  * @param key Callback to get an item's grouping key
151
218
  * @param value Callback to get an item's value
152
219
  * @returns Map of keyed arrays of values
220
+ *
221
+ * @example
222
+ * ```typescript
223
+ * toMapArrays(
224
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
225
+ * item => item.value,
226
+ * item => item.id,
227
+ * ); // => Map { 10 => [1, 3], 20 => [2] }
228
+ * ```
153
229
  */
154
230
  export function toMapArrays<
155
231
  Item,
@@ -165,10 +241,20 @@ export function toMapArrays<
165
241
  * Create a Map from an array of items using a callback and value, grouping values into arrays
166
242
  *
167
243
  * Available as `toMapArrays` and `toMap.arrays`
244
+ *
168
245
  * @param array Array to convert
169
246
  * @param key Callback to get an item's grouping key
170
247
  * @param value Key to use for value
171
248
  * @returns Map of keyed arrays of values
249
+ *
250
+ * @example
251
+ * ```typescript
252
+ * toMapArrays(
253
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
254
+ * item => item.value,
255
+ * 'id',
256
+ * ); // => Map { 10 => [1, 3], 20 => [2] }
257
+ * ```
172
258
  */
173
259
  export function toMapArrays<
174
260
  Item extends PlainObject,
@@ -184,10 +270,20 @@ export function toMapArrays<
184
270
  * Create a Map from an array of items using a key and callback, grouping values into arrays
185
271
  *
186
272
  * Available as `toMapArrays` and `toMap.arrays`
273
+ *
187
274
  * @param array Array to convert
188
275
  * @param key Key to use for grouping
189
276
  * @param value Callback to get an item's value
190
277
  * @returns Map of keyed arrays of values
278
+ *
279
+ * @example
280
+ * ```typescript
281
+ * toMapArrays(
282
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
283
+ * 'value',
284
+ * item => item.id,
285
+ * ); // => Map { 10 => [1, 3], 20 => [2] }
286
+ * ```
191
287
  */
192
288
  export function toMapArrays<
193
289
  Item extends PlainObject,
@@ -203,10 +299,20 @@ export function toMapArrays<
203
299
  * Create a Map from an array of items using a key and value, grouping values into arrays
204
300
  *
205
301
  * Available as `toMapArrays` and `toMap.arrays`
302
+ *
206
303
  * @param array Array to convert
207
304
  * @param key Key to use for grouping
208
305
  * @param value Key to use for value
209
306
  * @returns Map of keyed arrays of values
307
+ *
308
+ * @example
309
+ * ```typescript
310
+ * toMapArrays(
311
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
312
+ * 'value',
313
+ * 'id',
314
+ * ); // => Map { 10 => [1, 3], 20 => [2] }
315
+ * ```
210
316
  */
211
317
  export function toMapArrays<
212
318
  Item extends PlainObject,
@@ -218,9 +324,18 @@ export function toMapArrays<
218
324
  * Create a Map from an array of items using a callback, grouping items into arrays
219
325
  *
220
326
  * Available as `toMapArrays` and `toMap.arrays`
327
+ *
221
328
  * @param array Array to convert
222
329
  * @param callback Callback to get an item's grouping key
223
330
  * @returns Map of keyed arrays of items
331
+ *
332
+ * @example
333
+ * ```typescript
334
+ * toMapArrays(
335
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
336
+ * item => item.value,
337
+ * ); // => Map { 10 => [{id: 1, value: 10}, {id: 3, value: 10}], 20 => [{id: 2, value: 20}] }
338
+ * ```
224
339
  */
225
340
  export function toMapArrays<
226
341
  Item,
@@ -231,9 +346,18 @@ export function toMapArrays<
231
346
  * Create a Map from an array of items using a key, grouping items into arrays
232
347
  *
233
348
  * Available as `toMapArrays` and `toMap.arrays`
349
+ *
234
350
  * @param array Array to convert
235
351
  * @param key Key to use for grouping
236
352
  * @returns Map of keyed arrays of items
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * toMapArrays(
357
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
358
+ * 'value',
359
+ * ); // => Map { 10 => [{id: 1, value: 10}, {id: 3, value: 10}], 20 => [{id: 2, value: 20}] }
360
+ * ```
237
361
  */
238
362
  export function toMapArrays<Item extends PlainObject, ItemKey extends keyof Item>(
239
363
  array: Item[],