@oscarpalmer/atoms 0.184.2 → 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 (156) 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/index.d.mts +2 -2
  12. package/dist/array/index.mjs +2 -2
  13. package/dist/array/insert.d.mts +16 -0
  14. package/dist/array/intersection.d.mts +29 -0
  15. package/dist/array/last.d.mts +75 -2
  16. package/dist/array/{position.d.mts → match.d.mts} +168 -36
  17. package/dist/array/{position.mjs → match.mjs} +16 -16
  18. package/dist/array/move.d.mts +78 -8
  19. package/dist/array/move.mjs +11 -1
  20. package/dist/array/partition.d.mts +35 -0
  21. package/dist/array/push.d.mts +8 -0
  22. package/dist/array/push.mjs +8 -0
  23. package/dist/array/reverse.d.mts +1 -0
  24. package/dist/array/reverse.mjs +1 -0
  25. package/dist/array/select.d.mts +94 -8
  26. package/dist/array/single.d.mts +29 -0
  27. package/dist/array/slice.d.mts +106 -16
  28. package/dist/array/sort.d.mts +30 -4
  29. package/dist/array/sort.mjs +1 -1
  30. package/dist/array/splice.d.mts +48 -0
  31. package/dist/array/splice.mjs +2 -1
  32. package/dist/array/swap.d.mts +113 -8
  33. package/dist/array/swap.mjs +2 -1
  34. package/dist/array/to-map.d.mts +124 -0
  35. package/dist/array/to-record.d.mts +124 -0
  36. package/dist/array/to-set.d.mts +24 -0
  37. package/dist/array/toggle.d.mts +38 -3
  38. package/dist/array/union.d.mts +29 -0
  39. package/dist/array/unique.d.mts +24 -0
  40. package/dist/array/update.d.mts +38 -3
  41. package/dist/beacon.d.mts +12 -0
  42. package/dist/beacon.mjs +9 -0
  43. package/dist/color/instance.d.mts +8 -0
  44. package/dist/color/instance.mjs +3 -0
  45. package/dist/color/models.d.mts +30 -0
  46. package/dist/function/assert.d.mts +29 -8
  47. package/dist/function/assert.mjs +29 -8
  48. package/dist/function/memoize.d.mts +3 -0
  49. package/dist/function/memoize.mjs +3 -0
  50. package/dist/function/retry.d.mts +3 -0
  51. package/dist/function/retry.mjs +3 -0
  52. package/dist/function/work.mjs +1 -1
  53. package/dist/index.d.mts +2158 -288
  54. package/dist/index.mjs +294 -181
  55. package/dist/internal/array/chunk.d.mts +6 -0
  56. package/dist/internal/array/chunk.mjs +6 -0
  57. package/dist/internal/array/compact.d.mts +12 -0
  58. package/dist/internal/array/index-of.d.mts +70 -0
  59. package/dist/internal/math/aggregate.d.mts +29 -0
  60. package/dist/internal/value/compare.d.mts +2 -1
  61. package/dist/internal/value/equal.d.mts +5 -0
  62. package/dist/internal/value/get.d.mts +27 -5
  63. package/dist/internal/value/has.d.mts +7 -7
  64. package/dist/internal/value/has.mjs +1 -1
  65. package/dist/internal/value/misc.d.mts +2 -2
  66. package/dist/internal/value/misc.mjs +10 -4
  67. package/dist/logger.d.mts +11 -0
  68. package/dist/logger.mjs +11 -0
  69. package/dist/models.d.mts +14 -1
  70. package/dist/promise/helpers.mjs +1 -1
  71. package/dist/promise/index.d.mts +0 -6
  72. package/dist/promise/models.d.mts +36 -0
  73. package/dist/promise/models.mjs +6 -0
  74. package/dist/queue.d.mts +13 -1
  75. package/dist/queue.mjs +9 -0
  76. package/dist/result/index.d.mts +0 -8
  77. package/dist/result/index.mjs +0 -8
  78. package/dist/result/match.d.mts +4 -4
  79. package/dist/result/work/flow.d.mts +12 -36
  80. package/dist/result/work/pipe.d.mts +11 -33
  81. package/dist/sized/set.d.mts +3 -2
  82. package/dist/sized/set.mjs +3 -2
  83. package/dist/value/collection.d.mts +1 -1
  84. package/dist/value/handle.mjs +1 -1
  85. package/dist/value/merge.d.mts +28 -25
  86. package/dist/value/merge.mjs +29 -18
  87. package/dist/value/shake.d.mts +3 -0
  88. package/dist/value/smush.d.mts +3 -0
  89. package/dist/value/transform.d.mts +10 -1
  90. package/dist/value/unsmush.d.mts +2 -3
  91. package/package.json +5 -5
  92. package/src/array/difference.ts +33 -0
  93. package/src/array/exists.ts +35 -0
  94. package/src/array/filter.ts +72 -2
  95. package/src/array/find.ts +70 -0
  96. package/src/array/first.ts +77 -3
  97. package/src/array/flatten.ts +6 -0
  98. package/src/array/from.ts +40 -0
  99. package/src/array/get.ts +21 -15
  100. package/src/array/group-by.ts +142 -0
  101. package/src/array/index.ts +1 -1
  102. package/src/array/insert.ts +16 -2
  103. package/src/array/intersection.ts +33 -0
  104. package/src/array/last.ts +75 -2
  105. package/src/array/{position.ts → match.ts} +197 -65
  106. package/src/array/move.ts +87 -13
  107. package/src/array/partition.ts +35 -0
  108. package/src/array/push.ts +8 -2
  109. package/src/array/reverse.ts +5 -0
  110. package/src/array/select.ts +96 -13
  111. package/src/array/single.ts +29 -0
  112. package/src/array/slice.ts +114 -24
  113. package/src/array/sort.ts +30 -4
  114. package/src/array/splice.ts +52 -4
  115. package/src/array/swap.ts +122 -13
  116. package/src/array/to-map.ts +124 -0
  117. package/src/array/to-record.ts +124 -0
  118. package/src/array/to-set.ts +24 -0
  119. package/src/array/toggle.ts +42 -3
  120. package/src/array/union.ts +33 -0
  121. package/src/array/unique.ts +24 -0
  122. package/src/array/update.ts +38 -3
  123. package/src/beacon.ts +12 -0
  124. package/src/color/index.ts +0 -3
  125. package/src/color/instance.ts +9 -1
  126. package/src/color/models.ts +30 -0
  127. package/src/function/assert.ts +66 -7
  128. package/src/function/memoize.ts +3 -0
  129. package/src/function/once.ts +5 -1
  130. package/src/function/retry.ts +3 -0
  131. package/src/internal/array/chunk.ts +6 -0
  132. package/src/internal/array/compact.ts +12 -0
  133. package/src/internal/array/index-of.ts +70 -0
  134. package/src/internal/math/aggregate.ts +29 -0
  135. package/src/internal/string.ts +0 -2
  136. package/src/internal/value/compare.ts +2 -1
  137. package/src/internal/value/equal.ts +5 -0
  138. package/src/internal/value/get.ts +27 -5
  139. package/src/internal/value/has.ts +10 -10
  140. package/src/internal/value/misc.ts +24 -13
  141. package/src/logger.ts +11 -0
  142. package/src/models.ts +18 -0
  143. package/src/promise/index.ts +0 -6
  144. package/src/promise/models.ts +36 -0
  145. package/src/queue.ts +13 -1
  146. package/src/result/index.ts +0 -8
  147. package/src/result/match.ts +4 -4
  148. package/src/result/work/flow.ts +12 -36
  149. package/src/result/work/pipe.ts +11 -33
  150. package/src/sized/set.ts +4 -3
  151. package/src/value/collection.ts +1 -1
  152. package/src/value/merge.ts +88 -66
  153. package/src/value/shake.ts +3 -0
  154. package/src/value/smush.ts +3 -0
  155. package/src/value/transform.ts +10 -1
  156. package/src/value/unsmush.ts +2 -8
package/src/array/sort.ts CHANGED
@@ -104,9 +104,15 @@ type InternalSorterCompare = {
104
104
  */
105
105
  export type SortDirection = 'ascending' | 'descending';
106
106
 
107
+ /**
108
+ * Sorter for an array with predefined sorters
109
+ *
110
+ * Can be used to sort an array, get the predicted index for an item, and check if an array is sorted
111
+ */
107
112
  export type Sorter<Item> = {
108
113
  /**
109
114
  * Sort an array of items
115
+ *
110
116
  * @param array Array to sort
111
117
  * @returns Sorted array
112
118
  */
@@ -116,6 +122,7 @@ export type Sorter<Item> = {
116
122
  * Get the index for an item _(to be inserted into an array of items)_
117
123
  *
118
124
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
125
+ *
119
126
  * @param array Array to get the index from
120
127
  * @param item Item to get the index for
121
128
  * @returns Index for item
@@ -124,6 +131,7 @@ export type Sorter<Item> = {
124
131
 
125
132
  /**
126
133
  * Is the array sorted?
134
+ *
127
135
  * @param array Array to check
128
136
  * @returns `true` if sorted, otherwise `false`
129
137
  */
@@ -245,7 +253,8 @@ function getObjectSorter(obj: PlainObject, modifier: number): InternalSorter | u
245
253
  *
246
254
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
247
255
  *
248
- * Available as `getSortedIndex` and `sort.index`
256
+ * Available as `getSortedIndex` and `sort.getIndex`
257
+ *
249
258
  * @param array Array to get the index from
250
259
  * @param item Item to get the index for
251
260
  * @param sorters Sorters to use to determine sorting
@@ -264,7 +273,8 @@ export function getSortedIndex<Item>(
264
273
  *
265
274
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
266
275
  *
267
- * Available as `getSortedIndex` and `sort.index`
276
+ * Available as `getSortedIndex` and `sort.getIndex`
277
+ *
268
278
  * @param array Array to get the index from
269
279
  * @param item Item to get the index for
270
280
  * @param sorter Sorter to use to determine sorting
@@ -283,7 +293,8 @@ export function getSortedIndex<Item>(
283
293
  *
284
294
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
285
295
  *
286
- * Available as `getSortedIndex` and `sort.index`
296
+ * Available as `getSortedIndex` and `sort.getIndex`
297
+ *
287
298
  * @param array Array to get the index from
288
299
  * @param item Item to get the index for
289
300
  * @param descending Sorted in descending order? _(defaults to `false`)_
@@ -361,6 +372,7 @@ function getValueSorter(value: string | Function, modifier: number): InternalSor
361
372
  * Initialize a sort handler with sorters _(and an optional default direction)_
362
373
  *
363
374
  * Available as `initializeSorter` and `sort.initialize`
375
+ *
364
376
  * @param sorters Sorters to use for sorting
365
377
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
366
378
  * @returns Sort handler
@@ -374,6 +386,7 @@ export function initializeSorter<Item>(
374
386
  * Initialize a sort handler with a sorter _(and an optional default direction)_
375
387
  *
376
388
  * Available as `initializeSorter` and `sort.initialize`
389
+ *
377
390
  * @param sorter Sorter to use for sorting
378
391
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
379
392
  * @returns Sort handler
@@ -387,6 +400,7 @@ export function initializeSorter<Item>(
387
400
  * Initialize a sort handler _(with an optional default direction)_
388
401
  *
389
402
  * Available as `initializeSorter` and `sort.initialize`
403
+ *
390
404
  * @param descending Sort in descending order? _(defaults to `false`)_
391
405
  * @returns Sort handler
392
406
  */
@@ -405,6 +419,7 @@ export function initializeSorter(first?: unknown, second?: unknown): Sorter<unkn
405
419
 
406
420
  /**
407
421
  * Is the array sorted according to the sorters _(and the optional default direction)_?
422
+ *
408
423
  * @param array Array to check
409
424
  * @param sorters Sorters to determine sorting
410
425
  * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
@@ -418,6 +433,7 @@ export function isSorted<Item>(
418
433
 
419
434
  /**
420
435
  * Is the array sorted according to the sorter _(and the optional default direction)_?
436
+ *
421
437
  * @param array Array to check
422
438
  * @param sorter Sorter to determine sorting
423
439
  * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
@@ -433,6 +449,7 @@ export function isSorted<Item>(
433
449
  * Is the array sorted?
434
450
  *
435
451
  * Available as `isSorted` and `sort.is`
452
+ *
436
453
  * @param array Array to check
437
454
  * @param descending Sorted in descending order? _(defaults to `false`)_
438
455
  * @returns `true` if sorted, otherwise `false`
@@ -495,10 +512,19 @@ function isSortedArray(array: unknown[], sorters: InternalSorter[]): boolean {
495
512
 
496
513
  /**
497
514
  * Sort an array of items using a comparison callback
515
+ *
498
516
  * @param array Array to sort
499
517
  * @param comparator Comparator to use for sorting
500
518
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
501
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
+ * ```
502
528
  */
503
529
  export function sort<Item>(
504
530
  array: Item[],
@@ -552,7 +578,7 @@ function sortArray(array: unknown[], sorters: InternalSorter[]): unknown[] {
552
578
  : array;
553
579
  }
554
580
 
555
- sort.index = getSortedIndex;
581
+ sort.getIndex = getSortedIndex;
556
582
  sort.initialize = initializeSorter;
557
583
  sort.is = isSorted;
558
584
 
@@ -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
@@ -2,50 +2,108 @@ import {getArrayCallback} from '../internal/array/callbacks';
2
2
  import {indexOf} from '../internal/array/index-of';
3
3
  import {arraysOverlap} from '../internal/array/overlap';
4
4
  import type {PlainObject} from '../models';
5
- import {indexOfArray} from './position';
5
+ import {indexOfArray} from './match';
6
+
7
+ // #region Functions
6
8
 
7
9
  /**
8
10
  * Swap two smaller arrays within a larger array
9
11
  *
10
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
+ *
11
14
  * @param array Array of items to swap
12
15
  * @param first First array
13
16
  * @param second Second array
14
- * @param key Key to get an item's value for matching
17
+ * @param callback Callback to get an item's value for matching
15
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
+ * ```
16
41
  */
17
- export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
42
+ export function swap<Item>(
18
43
  array: Item[],
19
44
  first: Item[],
20
45
  second: Item[],
21
- key: ItemKey,
46
+ callback: (item: Item, index: number, array: Item[]) => unknown,
22
47
  ): Item[];
23
48
 
24
49
  /**
25
50
  * Swap two smaller arrays within a larger array
26
51
  *
27
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
+ *
28
54
  * @param array Array of items to swap
29
55
  * @param first First array
30
56
  * @param second Second array
31
- * @param callback Callback to get an item's value for matching
57
+ * @param key Key to get an item's value for matching
32
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
+ * ```
33
81
  */
34
- export function swap<Item>(
82
+ export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
35
83
  array: Item[],
36
84
  first: Item[],
37
85
  second: Item[],
38
- callback: (item: Item, index: number, array: Item[]) => unknown,
86
+ key: ItemKey,
39
87
  ): Item[];
40
88
 
41
89
  /**
42
90
  * Swap two smaller arrays within a larger array
43
91
  *
44
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
+ *
45
94
  * @param array Array of items to swap
46
95
  * @param first First array
47
96
  * @param second Second array
48
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
+ * ```
49
107
  */
50
108
  export function swap<Item>(array: Item[], first: Item[], second: Item[]): Item[];
51
109
 
@@ -53,42 +111,90 @@ export function swap<Item>(array: Item[], first: Item[], second: Item[]): Item[]
53
111
  * Swap two indiced items in an array
54
112
  *
55
113
  * If either of the items are not present in the array, the array will be returned unchanged
114
+ *
56
115
  * @param array Array of items to swap
57
116
  * @param first First item
58
117
  * @param second Second item
59
- * @param key Key to get an item's value for matching
118
+ * @param callback Callback to get an item's value for matching
60
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
+ * ```
61
138
  */
62
- export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
139
+ export function swap<Item>(
63
140
  array: Item[],
64
141
  first: Item,
65
142
  second: Item,
66
- key: ItemKey,
143
+ callback: (item: Item, index: number, array: Item[]) => unknown,
67
144
  ): Item[];
68
145
 
69
146
  /**
70
147
  * Swap two indiced items in an array
71
148
  *
72
149
  * If either of the items are not present in the array, the array will be returned unchanged
150
+ *
73
151
  * @param array Array of items to swap
74
152
  * @param first First item
75
153
  * @param second Second item
76
- * @param callback Callback to get an item's value for matching
154
+ * @param key Key to get an item's value for matching
77
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
+ * ```
78
174
  */
79
- export function swap<Item>(
175
+ export function swap<Item extends PlainObject, ItemKey extends keyof Item>(
80
176
  array: Item[],
81
177
  first: Item,
82
178
  second: Item,
83
- callback: (item: Item, index: number, array: Item[]) => unknown,
179
+ key: ItemKey,
84
180
  ): Item[];
85
181
 
86
182
  /**
87
183
  * Swap two indiced items in an array
184
+ *
88
185
  * @param array Array of items to swap
89
186
  * @param first First item
90
187
  * @param second Second item
91
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
+ * ```
92
198
  */
93
199
  export function swap<Item>(array: Item[], first: Item, second: Item): Item[];
94
200
 
@@ -165,6 +271,7 @@ function swapArrays(array: unknown[], from: unknown[], to: unknown[], key: unkno
165
271
  * If either index is out of bounds, the array will be returned unchanged
166
272
  *
167
273
  * Available as `swapIndices` and `swap.indices`
274
+ *
168
275
  * @param array Array of items to swap
169
276
  * @param first First index _(can be negative to count from the end)_
170
277
  * @param second Second index _(can be negative to count from the end)_
@@ -211,3 +318,5 @@ function swapValues(array: unknown[], from: unknown, to: unknown, key?: unknown)
211
318
 
212
319
  return swapIndices(array, first, second);
213
320
  }
321
+
322
+ // #endregion