@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/last.ts CHANGED
@@ -5,10 +5,20 @@ import type {PlainObject} from '../models';
5
5
 
6
6
  /**
7
7
  * Get the last items matching the given value
8
+ *
8
9
  * @param array Array to search in
9
10
  * @param callback Callback to get an item's value for matching
10
11
  * @param value Value to match against
11
12
  * @returns Last item that matches the value, or `undefined` if no match is found
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * last(
17
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
18
+ * item => item.value,
19
+ * 10,
20
+ * ); // => {id: 3, value: 10}
21
+ * ```
12
22
  */
13
23
  export function last<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
14
24
  array: Item[],
@@ -18,10 +28,20 @@ export function last<Item, Callback extends (item: Item, index: number, array: I
18
28
 
19
29
  /**
20
30
  * Get the first item matching the given value by key
31
+ *
21
32
  * @param array Array to search in
22
33
  * @param key Key to get an item's value for matching
23
34
  * @param value Value to match against
24
35
  * @returns Last item that matches the value, or `undefined` if no match is found
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * last(
40
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
41
+ * 'value',
42
+ * 10,
43
+ * ); // => {id: 3, value: 10}
44
+ * ```
25
45
  */
26
46
  export function last<Item extends PlainObject, ItemKey extends keyof Item>(
27
47
  array: Item[],
@@ -31,9 +51,18 @@ export function last<Item extends PlainObject, ItemKey extends keyof Item>(
31
51
 
32
52
  /**
33
53
  * Get the last item matching the filter
54
+ *
34
55
  * @param array Array to search in
35
56
  * @param filter Filter callback to match items
36
57
  * @returns Last item that matches the filter, or `undefined` if no match is found
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * last(
62
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
63
+ * item => item.value === 10,
64
+ * ); // => {id: 3, value: 10}
65
+ * ```
37
66
  */
38
67
  export function last<Item>(
39
68
  array: Item[],
@@ -42,8 +71,14 @@ export function last<Item>(
42
71
 
43
72
  /**
44
73
  * Get the last item from an array
74
+ *
45
75
  * @param array Array to get from
46
- * @return Last item from the array, or `undefined` if the array is empty
76
+ * @returns Last item from the array, or `undefined` if the array is empty
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * last([1, 2, 3]); // => 3
81
+ * ```
47
82
  */
48
83
  export function last<Item>(array: Item[]): Item | undefined;
49
84
 
@@ -57,11 +92,22 @@ last.default = lastOrDefault;
57
92
  * Get the last item matching the given value
58
93
  *
59
94
  * Available as `lastOrDefault` and `last.default`
95
+ *
60
96
  * @param array Array to search in
61
97
  * @param defaultValue Default value to return if no match is found
62
98
  * @param callback Callback to get an item's value for matching
63
99
  * @param value Value to match against
64
100
  * @returns Last item that matches the value, or the default value if no match is found
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * lastOrDefault(
105
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
106
+ * {id: -1, value: 30},
107
+ * item => item.value,
108
+ * 30,
109
+ * ); // => {id: -1, value: 30}
110
+ * ```
65
111
  */
66
112
  export function lastOrDefault<
67
113
  Item,
@@ -72,11 +118,22 @@ export function lastOrDefault<
72
118
  * Get the last item matching the given value by key
73
119
  *
74
120
  * Available as `lastOrDefault` and `last.default`
121
+ *
75
122
  * @param array Array to search in
76
123
  * @param defaultValue Default value to return if no match is found
77
124
  * @param key Key to get an item's value for matching
78
125
  * @param value Value to match against
79
126
  * @returns Last item that matches the value, or the default value if no match is found
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * lastOrDefault(
131
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
132
+ * {id: -1, value: 30},
133
+ * 'value',
134
+ * 30,
135
+ * ); // => {id: -1, value: 30}
136
+ * ```
80
137
  */
81
138
  export function lastOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(
82
139
  array: Item[],
@@ -89,10 +146,20 @@ export function lastOrDefault<Item extends PlainObject, ItemKey extends keyof It
89
146
  * Get the last item matching the filter
90
147
  *
91
148
  * Available as `lastOrDefault` and `last.default`
149
+ *
92
150
  * @param array Array to search in
93
151
  * @param defaultValue Default value to return if no match is found
94
152
  * @param filter Filter callback to match items
95
153
  * @returns Last item that matches the filter, or the default value if no match is found
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * lastOrDefault(
158
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
159
+ * {id: -1, value: 30},
160
+ * item => item.value === 30,
161
+ * ); // => {id: -1, value: 30}
162
+ * ```
96
163
  */
97
164
  export function lastOrDefault<Item>(
98
165
  array: Item[],
@@ -104,9 +171,15 @@ export function lastOrDefault<Item>(
104
171
  * Get the last item from an array
105
172
  *
106
173
  * Available as `lastOrDefault` and `last.default`
174
+ *
107
175
  * @param array Array to get from
108
176
  * @param defaultValue Default value to return if the array is empty
109
- * @return Last item from the array, or the default value if the array is empty
177
+ * @returns Last item from the array, or the default value if the array is empty
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * lastOrDefault([], {id: -1, value: 30}); // => {id: -1, value: 30}
182
+ * ```
110
183
  */
111
184
  export function lastOrDefault<Item>(array: Item[], defaultValue: Item): Item;
112
185
 
@@ -16,33 +16,58 @@ export type ArrayComparison = 'end' | 'inside' | 'invalid' | 'outside' | 'same'
16
16
  * Does the needle array end the haystack array?
17
17
  * @param haystack Haystack array
18
18
  * @param needle Needle array
19
- * @param key Key to get an item's value for matching
20
- * @return `true` if the haystack ends with the needle, otherwise `false`
19
+ * @param callback Callback to get an item's value for matching
20
+ * @returns `true` if the haystack ends with the needle, otherwise `false`
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * endsWithArray(
25
+ * [{id: 1}, {id: 2}, {id: 3}],
26
+ * [{id: 2}, {id: 3}],
27
+ * item => item.id,
28
+ * ); // => true
29
+ * ```
21
30
  */
22
- export function endsWithArray<Item extends PlainObject>(
31
+ export function endsWithArray<Item>(
23
32
  haystack: Item[],
24
33
  needle: Item[],
25
- key: keyof Item,
34
+ callback: (item: Item, index: number, array: Item[]) => unknown,
26
35
  ): boolean;
27
36
 
28
37
  /**
29
38
  * Does the needle array end the haystack array?
39
+ *
30
40
  * @param haystack Haystack array
31
41
  * @param needle Needle array
32
- * @param callback Callback to get an item's value for matching
33
- * @return `true` if the haystack ends with the needle, otherwise `false`
42
+ * @param key Key to get an item's value for matching
43
+ * @returns `true` if the haystack ends with the needle, otherwise `false`
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * endsWithArray(
48
+ * [{id: 1}, {id: 2}, {id: 3}],
49
+ * [{id: 2}, {id: 3}],
50
+ * 'id',
51
+ * ); // => true
52
+ * ```
34
53
  */
35
- export function endsWithArray<Item>(
54
+ export function endsWithArray<Item extends PlainObject>(
36
55
  haystack: Item[],
37
56
  needle: Item[],
38
- callback: (item: Item, index: number, array: Item[]) => unknown,
57
+ key: keyof Item,
39
58
  ): boolean;
40
59
 
41
60
  /**
42
61
  * Does the needle array end the haystack array?
62
+ *
43
63
  * @param haystack Haystack array
44
64
  * @param needle Needle array
45
- * @return `true` if the haystack ends with the needle, otherwise `false`
65
+ * @returns `true` if the haystack ends with the needle, otherwise `false`
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * endsWithArray([1, 2, 3], [2, 3]); // => true
70
+ * ```
46
71
  */
47
72
  export function endsWithArray<Item>(haystack: Item[], needle: Item[]): boolean;
48
73
 
@@ -52,35 +77,61 @@ export function endsWithArray(haystack: unknown[], needle: unknown[], key?: unkn
52
77
 
53
78
  /**
54
79
  * Get the position of an array within another array
80
+ *
55
81
  * @param haystack Haystack array
56
82
  * @param needle Needle array
57
- * @param key Key to get an item's value for matching
83
+ * @param callback Callback to get an item's value for matching
58
84
  * @returns Position of the needle within the haystack
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * getArrayComparison(
89
+ * [{id: 1}, {id: 2}, {id: 3}],
90
+ * [{id: 2}, {id: 3}],
91
+ * item => item.id,
92
+ * ); // => 'end'
93
+ * ```
59
94
  */
60
- export function getArrayComparison<Item extends PlainObject>(
95
+ export function getArrayComparison<Item>(
61
96
  haystack: Item[],
62
97
  needle: Item[],
63
- key: keyof Item,
98
+ callback: (item: Item, index: number, array: Item[]) => unknown,
64
99
  ): ArrayComparison;
65
100
 
66
101
  /**
67
102
  * Get the position of an array within another array
103
+ *
68
104
  * @param haystack Haystack array
69
105
  * @param needle Needle array
70
- * @param callback Callback to get an item's value for matching
106
+ * @param key Key to get an item's value for matching
71
107
  * @returns Position of the needle within the haystack
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * getArrayComparison(
112
+ * [{id: 1}, {id: 2}, {id: 3}],
113
+ * [{id: 2}, {id: 3}],
114
+ * 'id',
115
+ * ); // => 'end'
116
+ * ```
72
117
  */
73
- export function getArrayComparison<Item>(
118
+ export function getArrayComparison<Item extends PlainObject>(
74
119
  haystack: Item[],
75
120
  needle: Item[],
76
- callback: (item: Item, index: number, array: Item[]) => unknown,
121
+ key: keyof Item,
77
122
  ): ArrayComparison;
78
123
 
79
124
  /**
80
125
  * Get the position of an array within another array
126
+ *
81
127
  * @param haystack Haystack array
82
128
  * @param needle Needle array
83
129
  * @returns Position of the needle within the haystack
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * getArrayComparison([1, 2, 3], [2, 3]); // => 'end'
134
+ * ```
84
135
  */
85
136
  export function getArrayComparison<Item>(haystack: Item[], needle: Item[]): ArrayComparison;
86
137
 
@@ -165,35 +216,61 @@ function getPosition(
165
216
 
166
217
  /**
167
218
  * Does the needle array exist within the haystack array?
219
+ *
168
220
  * @param haystack Haystack array
169
221
  * @param needle Needle array
170
- * @param key Key to get an item's value for matching
171
- * @return `true` if the haystack includes the needle, otherwise `false`
222
+ * @param callback Callback to get an item's value for matching
223
+ * @returns `true` if the haystack includes the needle, otherwise `false`
224
+ *
225
+ * @example
226
+ * ```typescript
227
+ * includesArray(
228
+ * [{id: 1}, {id: 2}, {id: 3}],
229
+ * [{id: 2}, {id: 3}],
230
+ * item => item.id,
231
+ * ); // => true
232
+ * ```
172
233
  */
173
- export function includesArray<Item extends PlainObject>(
234
+ export function includesArray<Item>(
174
235
  haystack: Item[],
175
236
  needle: Item[],
176
- key: keyof Item,
237
+ callback: (item: Item, index: number, array: Item[]) => unknown,
177
238
  ): boolean;
178
239
 
179
240
  /**
180
241
  * Does the needle array exist within the haystack array?
242
+ *
181
243
  * @param haystack Haystack array
182
244
  * @param needle Needle array
183
- * @param callback Callback to get an item's value for matching
184
- * @return `true` if the haystack includes the needle, otherwise `false`
245
+ * @param key Key to get an item's value for matching
246
+ * @returns `true` if the haystack includes the needle, otherwise `false`
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * includesArray(
251
+ * [{id: 1}, {id: 2}, {id: 3}],
252
+ * [{id: 2}, {id: 3}],
253
+ * 'id',
254
+ * ); // => true
255
+ * ```
185
256
  */
186
- export function includesArray<Item>(
257
+ export function includesArray<Item extends PlainObject>(
187
258
  haystack: Item[],
188
259
  needle: Item[],
189
- callback: (item: Item, index: number, array: Item[]) => unknown,
260
+ key: keyof Item,
190
261
  ): boolean;
191
262
 
192
263
  /**
193
264
  * Does the needle array exist within the haystack array?
265
+ *
194
266
  * @param haystack Haystack array
195
267
  * @param needle Needle array
196
- * @return `true` if the haystack includes the needle, otherwise `false`
268
+ * @returns `true` if the haystack includes the needle, otherwise `false`
269
+ *
270
+ * @example
271
+ * ```typescript
272
+ * includesArray([1, 2, 3], [2, 3]); // => true
273
+ * ```
197
274
  */
198
275
  export function includesArray<Item>(haystack: Item[], needle: Item[]): boolean;
199
276
 
@@ -203,35 +280,61 @@ export function includesArray(haystack: unknown[], needle: unknown[], key?: unkn
203
280
 
204
281
  /**
205
282
  * Get the index of an array within another array
283
+ *
206
284
  * @param haystack Haystack array
207
285
  * @param needle Needle array
208
- * @param key Key to get an item's value for matching
209
- * @return Index of the needle's start within the haystack, or `-1` if it is not found
286
+ * @param callback Callback to get an item's value for matching
287
+ * @returns Index of the needle's start within the haystack, or `-1` if it is not found
288
+ *
289
+ * @example
290
+ * ```typescript
291
+ * indexOfArray(
292
+ * [{id: 1}, {id: 2}, {id: 3}],
293
+ * [{id: 2}, {id: 3}],
294
+ * item => item.id,
295
+ * ); // => 1
296
+ * ```
210
297
  */
211
- export function indexOfArray<Item extends PlainObject>(
298
+ export function indexOfArray<Item>(
212
299
  haystack: Item[],
213
300
  needle: Item[],
214
- key: keyof Item,
301
+ callback: (item: Item, index: number, array: Item[]) => unknown,
215
302
  ): number;
216
303
 
217
304
  /**
218
305
  * Get the index of an array within another array
306
+ *
219
307
  * @param haystack Haystack array
220
308
  * @param needle Needle array
221
- * @param callback Callback to get an item's value for matching
222
- * @return Index of the needle's start within the haystack, or `-1` if it is not found
309
+ * @param key Key to get an item's value for matching
310
+ * @returns Index of the needle's start within the haystack, or `-1` if it is not found
311
+ *
312
+ * @example
313
+ * ```typescript
314
+ * indexOfArray(
315
+ * [{id: 1}, {id: 2}, {id: 3}],
316
+ * [{id: 2}, {id: 3}],
317
+ * 'id',
318
+ * ); // => 1
319
+ * ```
223
320
  */
224
- export function indexOfArray<Item>(
321
+ export function indexOfArray<Item extends PlainObject>(
225
322
  haystack: Item[],
226
323
  needle: Item[],
227
- callback: (item: Item, index: number, array: Item[]) => unknown,
324
+ key: keyof Item,
228
325
  ): number;
229
326
 
230
327
  /**
231
328
  * Get the index of an array within another array
329
+ *
232
330
  * @param haystack Haystack array
233
331
  * @param needle Needle array
234
- * @return Index of the needle's start within the haystack, or `-1` if it is not found
332
+ * @returns Index of the needle's start within the haystack, or `-1` if it is not found
333
+ *
334
+ * @example
335
+ * ```typescript
336
+ * indexOfArray([1, 2, 3], [2, 3]); // => 1
337
+ * ```
235
338
  */
236
339
  export function indexOfArray<Item>(haystack: Item[], needle: Item[]): number;
237
340
 
@@ -241,35 +344,61 @@ export function indexOfArray(haystack: unknown[], needle: unknown[], key?: unkno
241
344
 
242
345
  /**
243
346
  * Does the needle array start the haystack array?
347
+ *
244
348
  * @param haystack Haystack array
245
349
  * @param needle Needle array
246
- * @param key Key to get an item's value for matching
247
- * @return `true` if the haystack starts with the needle, otherwise `false`
350
+ * @param callback Callback to get an item's value for matching
351
+ * @returns `true` if the haystack starts with the needle, otherwise `false`
352
+ *
353
+ * @example
354
+ * ```typescript
355
+ * startsWithArray(
356
+ * [{id: 1}, {id: 2}, {id: 3}],
357
+ * [{id: 1}, {id: 2}],
358
+ * item => item.id,
359
+ * ); // => true
360
+ * ```
248
361
  */
249
- export function startsWithArray<Item extends PlainObject>(
362
+ export function startsWithArray<Item>(
250
363
  haystack: Item[],
251
364
  needle: Item[],
252
- key: keyof Item,
365
+ callback: (item: Item, index: number, array: Item[]) => unknown,
253
366
  ): boolean;
254
367
 
255
368
  /**
256
369
  * Does the needle array start the haystack array?
370
+ *
257
371
  * @param haystack Haystack array
258
372
  * @param needle Needle array
259
- * @param callback Callback to get an item's value for matching
260
- * @return `true` if the haystack starts with the needle, otherwise `false`
373
+ * @param key Key to get an item's value for matching
374
+ * @returns `true` if the haystack starts with the needle, otherwise `false`
375
+ *
376
+ * @example
377
+ * ```typescript
378
+ * startsWithArray(
379
+ * [{id: 1}, {id: 2}, {id: 3}],
380
+ * [{id: 1}, {id: 2}],
381
+ * 'id',
382
+ * ); // => true
383
+ * ```
261
384
  */
262
- export function startsWithArray<Item>(
385
+ export function startsWithArray<Item extends PlainObject>(
263
386
  haystack: Item[],
264
387
  needle: Item[],
265
- callback: (item: Item, index: number, array: Item[]) => unknown,
388
+ key: keyof Item,
266
389
  ): boolean;
267
390
 
268
391
  /**
269
392
  * Does the needle array start the haystack array?
393
+ *
270
394
  * @param haystack Haystack array
271
395
  * @param needle Needle array
272
- * @return `true` if the haystack starts with the needle, otherwise `false`
396
+ * @returns `true` if the haystack starts with the needle, otherwise `false`
397
+ *
398
+ * @example
399
+ * ```typescript
400
+ * startsWithArray([1, 2, 3], [1, 2]); // => true
401
+ * ```
273
402
  */
274
403
  export function startsWithArray<Item>(haystack: Item[], needle: Item[]): boolean;
275
404
 
package/src/array/move.ts CHANGED
@@ -10,17 +10,27 @@ import {indexOfArray} from './match';
10
10
  * When moving to the front of the array, the moved items will be placed __before__ the target item. When moving to the back of the array, the moved items will be placed __after__ the target item.
11
11
  *
12
12
  * If either of values are not present in the array, or if they overlap, the array will be returned unchanged
13
+ *
13
14
  * @param array Array to move within
14
15
  * @param from Item or items to move
15
16
  * @param to Item or items to move to
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 moved _(or unchanged if unable to move)_
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const array = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
23
+ *
24
+ * move(array, {id: 1}, {id: 3}, item => item.id); // => [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
25
+ * move(array, [{id: 2}, {id: 3}], {id: 4}, item => item.id); // => [{id: 1}, {id: 4}, {id: 2}, {id: 3}]
26
+ * move(array, {id: 5}, {id: 1}, item => item.id); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}] (unchanged)
27
+ * ```
18
28
  */
19
- export function move<Item extends PlainObject, ItemKey extends keyof Item>(
29
+ export function move<Item>(
20
30
  array: Item[],
21
31
  from: Item | Item[],
22
32
  to: Item | Item[],
23
- key: ItemKey,
33
+ callback: (item: Item, index: number, array: Item[]) => unknown,
24
34
  ): Item[];
25
35
 
26
36
  /**
@@ -29,17 +39,27 @@ export function move<Item extends PlainObject, ItemKey extends keyof Item>(
29
39
  * When moving to the front of the array, the moved items will be placed __before__ the target item. When moving to the back of the array, the moved items will be placed __after__ the target item.
30
40
  *
31
41
  * If either of values are not present in the array, or if they overlap, the array will be returned unchanged
42
+ *
32
43
  * @param array Array to move within
33
44
  * @param from Item or items to move
34
45
  * @param to Item or items to move to
35
- * @param callback Callback to get an item's value for matching
46
+ * @param key Key to get an item's value for matching
36
47
  * @returns Original array with items moved _(or unchanged if unable to move)_
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const array = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
52
+ *
53
+ * move(array, {id: 1}, {id: 3}, 'id'); // => [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
54
+ * move(array, [{id: 2}, {id: 3}], {id: 4}, 'id'); // => [{id: 1}, {id: 4}, {id: 2}, {id: 3}]
55
+ * move(array, {id: 5}, {id: 1}, 'id'); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}] (unchanged)
56
+ * ```
37
57
  */
38
- export function move<Item>(
58
+ export function move<Item extends PlainObject, ItemKey extends keyof Item>(
39
59
  array: Item[],
40
60
  from: Item | Item[],
41
61
  to: Item | Item[],
42
- callback: (item: Item, index: number, array: Item[]) => unknown,
62
+ key: ItemKey,
43
63
  ): Item[];
44
64
 
45
65
  /**
@@ -48,10 +68,20 @@ export function move<Item>(
48
68
  * When moving to the front of the array, the moved items will be placed __before__ the target item. When moving to the back of the array, the moved items will be placed __after__ the target item.
49
69
  *
50
70
  * If either of values are not present in the array, or if they overlap, the array will be returned unchanged
71
+ *
51
72
  * @param array Array to move within
52
73
  * @param from Item or items to move
53
74
  * @param to Item or items to move to
54
75
  * @returns Original array with items moved _(or unchanged if unable to move)_
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const array = [1, 2, 3, 4];
80
+ *
81
+ * move(array, 1, 3); // => [2, 3, 1, 4]
82
+ * move(array, [2, 3], 4); // => [1, 4, 2, 3]
83
+ * move(array, 5, 1); // => [1, 2, 3, 4] (unchanged)
84
+ * ```
55
85
  */
56
86
  export function move<Item>(array: Item[], from: Item | Item[], to: Item | Item[]): Item[];
57
87
 
@@ -116,10 +146,20 @@ move.toIndex = moveToIndex;
116
146
  * If the from index is out of bounds, the array will be returned unchanged
117
147
  *
118
148
  * Available as `moveIndices` and `move.indices`
149
+ *
119
150
  * @param array Array to move within
120
151
  * @param from Index to move from
121
152
  * @param to Index to move to
122
153
  * @returns Original array with item moved _(or unchanged if unable to move)_
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * const array = [1, 2, 3, 4];
158
+ *
159
+ * moveIndices(array, 0, 2); // => [2, 3, 1, 4]
160
+ * moveIndices(array, -1, 0); // => [4, 2, 3, 1]
161
+ * moveIndices(array, 5, 1); // => [4, 2, 3, 1] (unchanged)
162
+ * ```
123
163
  */
124
164
  export function moveIndices<Item>(array: Item[], from: number, to: number): Item[] {
125
165
  if (!Array.isArray(array)) {
@@ -156,17 +196,27 @@ export function moveIndices<Item>(array: Item[], from: number, to: number): Item
156
196
  * If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
157
197
  *
158
198
  * Available as `moveToIndex` and `move.toIndex`
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * const array = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
203
+ *
204
+ * moveToIndex(array, {id: 1}, 2, item => item.id); // => [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
205
+ * moveToIndex(array, [{id: 2}, {id: 3}], 3, item => item.id); // => [{id: 1}, {id: 4}, {id: 2}, {id: 3}]
206
+ * moveToIndex(array, {id: 5}, 1, item => item.id); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}] (unchanged)
207
+ * ```
208
+ *
159
209
  * @param array Array to move within
160
210
  * @param value Item or items to move
161
211
  * @param index Index to move to
162
- * @param key Key to get an item's value for matching
212
+ * @param callback Callback to get an item's value for matching
163
213
  * @returns Original array with items moved _(or unchanged if unable to move)_
164
214
  */
165
- export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item>(
215
+ export function moveToIndex<Item>(
166
216
  array: Item[],
167
217
  value: Item | Item[],
168
218
  index: number,
169
- key: ItemKey,
219
+ callback: (item: Item, index: number, array: Item[]) => unknown,
170
220
  ): Item[];
171
221
 
172
222
  /**
@@ -175,17 +225,27 @@ export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item
175
225
  * If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
176
226
  *
177
227
  * Available as `moveToIndex` and `move.toIndex`
228
+ *
178
229
  * @param array Array to move within
179
230
  * @param value Item or items to move
180
231
  * @param index Index to move to
181
- * @param callback Callback to get an item's value for matching
232
+ * @param key Key to get an item's value for matching
182
233
  * @returns Original array with items moved _(or unchanged if unable to move)_
234
+ *
235
+ * @example
236
+ * ```typescript
237
+ * const array = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
238
+ *
239
+ * moveToIndex(array, {id: 1}, 2, 'id'); // => [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
240
+ * moveToIndex(array, [{id: 2}, {id: 3}], 3, 'id'); // => [{id: 1}, {id: 4}, {id: 2}, {id: 3}]
241
+ * moveToIndex(array, {id: 5}, 1, 'id'); // => [{id: 1}, {id: 2}, {id: 3}, {id: 4}] (unchanged)
242
+ * ```
183
243
  */
184
- export function moveToIndex<Item>(
244
+ export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item>(
185
245
  array: Item[],
186
246
  value: Item | Item[],
187
247
  index: number,
188
- callback: (item: Item, index: number, array: Item[]) => unknown,
248
+ key: ItemKey,
189
249
  ): Item[];
190
250
 
191
251
  /**
@@ -194,10 +254,20 @@ export function moveToIndex<Item>(
194
254
  * If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
195
255
  *
196
256
  * Available as `moveToIndex` and `move.toIndex`
257
+ *
197
258
  * @param array Array to move within
198
259
  * @param value Item or items to move
199
260
  * @param index Index to move to
200
261
  * @returns Original array with items moved _(or unchanged if unable to move)_
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * const array = [1, 2, 3, 4];
266
+ *
267
+ * moveToIndex(array, 1, 2); // => [2, 3, 1, 4]
268
+ * moveToIndex(array, [2, 3], 3); // => [1, 4, 2, 3]
269
+ * moveToIndex(array, 5, 1); // => [1, 2, 3, 4] (unchanged)
270
+ * ```
201
271
  */
202
272
  export function moveToIndex<Item>(array: Item[], value: Item | Item[], index: number): Item[];
203
273