@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
@@ -1,9 +1,12 @@
1
- // #region Types
2
-
3
1
  import {getArrayCallback} from '../internal/array/callbacks';
4
2
  import type {PlainObject} from '../models';
5
3
 
6
- export type ArrayPosition = 'end' | 'inside' | 'invalid' | 'outside' | 'same' | 'start';
4
+ // #region Types
5
+
6
+ /**
7
+ * Comparison of an array within another array
8
+ */
9
+ export type ArrayComparison = 'end' | 'inside' | 'invalid' | 'outside' | 'same' | 'start';
7
10
 
8
11
  // #endregion
9
12
 
@@ -13,33 +16,58 @@ export type ArrayPosition = 'end' | 'inside' | 'invalid' | 'outside' | 'same' |
13
16
  * Does the needle array end the haystack array?
14
17
  * @param haystack Haystack array
15
18
  * @param needle Needle array
16
- * @param key Key to get an item's value for matching
17
- * @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
+ * ```
18
30
  */
19
- export function endsWithArray<Item extends PlainObject>(
31
+ export function endsWithArray<Item>(
20
32
  haystack: Item[],
21
33
  needle: Item[],
22
- key: keyof Item,
34
+ callback: (item: Item, index: number, array: Item[]) => unknown,
23
35
  ): boolean;
24
36
 
25
37
  /**
26
38
  * Does the needle array end the haystack array?
39
+ *
27
40
  * @param haystack Haystack array
28
41
  * @param needle Needle array
29
- * @param callback Callback to get an item's value for matching
30
- * @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
+ * ```
31
53
  */
32
- export function endsWithArray<Item>(
54
+ export function endsWithArray<Item extends PlainObject>(
33
55
  haystack: Item[],
34
56
  needle: Item[],
35
- callback: (item: Item, index: number, array: Item[]) => unknown,
57
+ key: keyof Item,
36
58
  ): boolean;
37
59
 
38
60
  /**
39
61
  * Does the needle array end the haystack array?
62
+ *
40
63
  * @param haystack Haystack array
41
64
  * @param needle Needle array
42
- * @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
+ * ```
43
71
  */
44
72
  export function endsWithArray<Item>(haystack: Item[], needle: Item[]): boolean;
45
73
 
@@ -49,59 +77,85 @@ export function endsWithArray(haystack: unknown[], needle: unknown[], key?: unkn
49
77
 
50
78
  /**
51
79
  * Get the position of an array within another array
80
+ *
52
81
  * @param haystack Haystack array
53
82
  * @param needle Needle array
54
- * @param key Key to get an item's value for matching
83
+ * @param callback Callback to get an item's value for matching
55
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
+ * ```
56
94
  */
57
- export function getArrayPosition<Item extends PlainObject>(
95
+ export function getArrayComparison<Item>(
58
96
  haystack: Item[],
59
97
  needle: Item[],
60
- key: keyof Item,
61
- ): ArrayPosition;
98
+ callback: (item: Item, index: number, array: Item[]) => unknown,
99
+ ): ArrayComparison;
62
100
 
63
101
  /**
64
102
  * Get the position of an array within another array
103
+ *
65
104
  * @param haystack Haystack array
66
105
  * @param needle Needle array
67
- * @param callback Callback to get an item's value for matching
106
+ * @param key Key to get an item's value for matching
68
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
+ * ```
69
117
  */
70
- export function getArrayPosition<Item>(
118
+ export function getArrayComparison<Item extends PlainObject>(
71
119
  haystack: Item[],
72
120
  needle: Item[],
73
- callback: (item: Item, index: number, array: Item[]) => unknown,
74
- ): ArrayPosition;
121
+ key: keyof Item,
122
+ ): ArrayComparison;
75
123
 
76
124
  /**
77
125
  * Get the position of an array within another array
126
+ *
78
127
  * @param haystack Haystack array
79
128
  * @param needle Needle array
80
129
  * @returns Position of the needle within the haystack
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * getArrayComparison([1, 2, 3], [2, 3]); // => 'end'
134
+ * ```
81
135
  */
82
- export function getArrayPosition<Item>(haystack: Item[], needle: Item[]): ArrayPosition;
136
+ export function getArrayComparison<Item>(haystack: Item[], needle: Item[]): ArrayComparison;
83
137
 
84
- export function getArrayPosition(
138
+ export function getArrayComparison(
85
139
  haystack: unknown[],
86
140
  needle: unknown[],
87
141
  key?: unknown,
88
- ): ArrayPosition {
142
+ ): ArrayComparison {
89
143
  return getPosition(haystack, needle, key)[1];
90
144
  }
91
145
 
92
- function getName(start: number, haystack: number, needle: number): ArrayPosition {
146
+ function getName(start: number, haystack: number, needle: number): ArrayComparison {
93
147
  if (start === 0) {
94
- return haystack === needle ? POSITION_SAME : POSITION_START;
148
+ return haystack === needle ? COMPARISON_SAME : COMPARISON_START;
95
149
  }
96
150
 
97
- return start + needle === haystack ? POSITION_END : POSITION_INSIDE;
151
+ return start + needle === haystack ? COMPARISON_END : COMPARISON_INSIDE;
98
152
  }
99
153
 
100
154
  function getPosition(
101
155
  haystack: unknown[],
102
156
  needle: unknown[],
103
157
  key?: unknown,
104
- ): readonly [number, ArrayPosition] {
158
+ ): readonly [number, ArrayComparison] {
105
159
  if (!Array.isArray(haystack) || !Array.isArray(needle)) {
106
160
  return invalid;
107
161
  }
@@ -162,35 +216,61 @@ function getPosition(
162
216
 
163
217
  /**
164
218
  * Does the needle array exist within the haystack array?
219
+ *
165
220
  * @param haystack Haystack array
166
221
  * @param needle Needle array
167
- * @param key Key to get an item's value for matching
168
- * @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
+ * ```
169
233
  */
170
- export function includesArray<Item extends PlainObject>(
234
+ export function includesArray<Item>(
171
235
  haystack: Item[],
172
236
  needle: Item[],
173
- key: keyof Item,
237
+ callback: (item: Item, index: number, array: Item[]) => unknown,
174
238
  ): boolean;
175
239
 
176
240
  /**
177
241
  * Does the needle array exist within the haystack array?
242
+ *
178
243
  * @param haystack Haystack array
179
244
  * @param needle Needle array
180
- * @param callback Callback to get an item's value for matching
181
- * @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
+ * ```
182
256
  */
183
- export function includesArray<Item>(
257
+ export function includesArray<Item extends PlainObject>(
184
258
  haystack: Item[],
185
259
  needle: Item[],
186
- callback: (item: Item, index: number, array: Item[]) => unknown,
260
+ key: keyof Item,
187
261
  ): boolean;
188
262
 
189
263
  /**
190
264
  * Does the needle array exist within the haystack array?
265
+ *
191
266
  * @param haystack Haystack array
192
267
  * @param needle Needle array
193
- * @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
+ * ```
194
274
  */
195
275
  export function includesArray<Item>(haystack: Item[], needle: Item[]): boolean;
196
276
 
@@ -200,35 +280,61 @@ export function includesArray(haystack: unknown[], needle: unknown[], key?: unkn
200
280
 
201
281
  /**
202
282
  * Get the index of an array within another array
283
+ *
203
284
  * @param haystack Haystack array
204
285
  * @param needle Needle array
205
- * @param key Key to get an item's value for matching
206
- * @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
+ * ```
207
297
  */
208
- export function indexOfArray<Item extends PlainObject>(
298
+ export function indexOfArray<Item>(
209
299
  haystack: Item[],
210
300
  needle: Item[],
211
- key: keyof Item,
301
+ callback: (item: Item, index: number, array: Item[]) => unknown,
212
302
  ): number;
213
303
 
214
304
  /**
215
305
  * Get the index of an array within another array
306
+ *
216
307
  * @param haystack Haystack array
217
308
  * @param needle Needle array
218
- * @param callback Callback to get an item's value for matching
219
- * @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
+ * ```
220
320
  */
221
- export function indexOfArray<Item>(
321
+ export function indexOfArray<Item extends PlainObject>(
222
322
  haystack: Item[],
223
323
  needle: Item[],
224
- callback: (item: Item, index: number, array: Item[]) => unknown,
324
+ key: keyof Item,
225
325
  ): number;
226
326
 
227
327
  /**
228
328
  * Get the index of an array within another array
329
+ *
229
330
  * @param haystack Haystack array
230
331
  * @param needle Needle array
231
- * @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
+ * ```
232
338
  */
233
339
  export function indexOfArray<Item>(haystack: Item[], needle: Item[]): number;
234
340
 
@@ -238,35 +344,61 @@ export function indexOfArray(haystack: unknown[], needle: unknown[], key?: unkno
238
344
 
239
345
  /**
240
346
  * Does the needle array start the haystack array?
347
+ *
241
348
  * @param haystack Haystack array
242
349
  * @param needle Needle array
243
- * @param key Key to get an item's value for matching
244
- * @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
+ * ```
245
361
  */
246
- export function startsWithArray<Item extends PlainObject>(
362
+ export function startsWithArray<Item>(
247
363
  haystack: Item[],
248
364
  needle: Item[],
249
- key: keyof Item,
365
+ callback: (item: Item, index: number, array: Item[]) => unknown,
250
366
  ): boolean;
251
367
 
252
368
  /**
253
369
  * Does the needle array start the haystack array?
370
+ *
254
371
  * @param haystack Haystack array
255
372
  * @param needle Needle array
256
- * @param callback Callback to get an item's value for matching
257
- * @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
+ * ```
258
384
  */
259
- export function startsWithArray<Item>(
385
+ export function startsWithArray<Item extends PlainObject>(
260
386
  haystack: Item[],
261
387
  needle: Item[],
262
- callback: (item: Item, index: number, array: Item[]) => unknown,
388
+ key: keyof Item,
263
389
  ): boolean;
264
390
 
265
391
  /**
266
392
  * Does the needle array start the haystack array?
393
+ *
267
394
  * @param haystack Haystack array
268
395
  * @param needle Needle array
269
- * @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
+ * ```
270
402
  */
271
403
  export function startsWithArray<Item>(haystack: Item[], needle: Item[]): boolean;
272
404
 
@@ -278,26 +410,26 @@ export function startsWithArray(haystack: unknown[], needle: unknown[], key?: un
278
410
 
279
411
  // #region Variables
280
412
 
281
- const POSITION_END: ArrayPosition = 'end';
413
+ const COMPARISON_END: ArrayComparison = 'end';
282
414
 
283
- const POSITION_INSIDE: ArrayPosition = 'inside';
415
+ const COMPARISON_INSIDE: ArrayComparison = 'inside';
284
416
 
285
- const POSITION_INVALID: ArrayPosition = 'invalid';
417
+ const COMPARISON_INVALID: ArrayComparison = 'invalid';
286
418
 
287
- const POSITION_OUTSIDE: ArrayPosition = 'outside';
419
+ const COMPARISON_OUTSIDE: ArrayComparison = 'outside';
288
420
 
289
- const POSITION_SAME: ArrayPosition = 'same';
421
+ const COMPARISON_SAME: ArrayComparison = 'same';
290
422
 
291
- const POSITION_START: ArrayPosition = 'start';
423
+ const COMPARISON_START: ArrayComparison = 'start';
292
424
 
293
- const endings = new Set<ArrayPosition>([POSITION_END, POSITION_SAME]);
425
+ const endings = new Set<ArrayComparison>([COMPARISON_END, COMPARISON_SAME]);
294
426
 
295
- const invalid = [-1, POSITION_INVALID] as const;
427
+ const invalid = [-1, COMPARISON_INVALID] as const;
296
428
 
297
- const outside = [-1, POSITION_OUTSIDE] as const;
429
+ const outside = [-1, COMPARISON_OUTSIDE] as const;
298
430
 
299
- const outsides = new Set<ArrayPosition>([POSITION_INVALID, POSITION_OUTSIDE]);
431
+ const outsides = new Set<ArrayComparison>([COMPARISON_INVALID, COMPARISON_OUTSIDE]);
300
432
 
301
- const starts = new Set<ArrayPosition>([POSITION_START, POSITION_SAME]);
433
+ const starts = new Set<ArrayComparison>([COMPARISON_START, COMPARISON_SAME]);
302
434
 
303
435
  // #endregion
package/src/array/move.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import {arraysOverlap} from '../internal/array/overlap';
2
2
  import type {PlainObject} from '../models';
3
- import {indexOfArray} from './position';
3
+ import {indexOfArray} from './match';
4
+
5
+ // #region Functions
4
6
 
5
7
  /**
6
8
  * Move an item _(or array of items)_ to the position of another item _(or array of items)_ within an array
@@ -8,17 +10,27 @@ import {indexOfArray} from './position';
8
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.
9
11
  *
10
12
  * If either of values are not present in the array, or if they overlap, the array will be returned unchanged
13
+ *
11
14
  * @param array Array to move within
12
15
  * @param from Item or items to move
13
16
  * @param to Item or items to move to
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 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
+ * ```
16
28
  */
17
- export function move<Item extends PlainObject, ItemKey extends keyof Item>(
29
+ export function move<Item>(
18
30
  array: Item[],
19
31
  from: Item | Item[],
20
32
  to: Item | Item[],
21
- key: ItemKey,
33
+ callback: (item: Item, index: number, array: Item[]) => unknown,
22
34
  ): Item[];
23
35
 
24
36
  /**
@@ -27,17 +39,27 @@ export function move<Item extends PlainObject, ItemKey extends keyof Item>(
27
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.
28
40
  *
29
41
  * If either of values are not present in the array, or if they overlap, the array will be returned unchanged
42
+ *
30
43
  * @param array Array to move within
31
44
  * @param from Item or items to move
32
45
  * @param to Item or items to move to
33
- * @param callback Callback to get an item's value for matching
46
+ * @param key Key to get an item's value for matching
34
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
+ * ```
35
57
  */
36
- export function move<Item>(
58
+ export function move<Item extends PlainObject, ItemKey extends keyof Item>(
37
59
  array: Item[],
38
60
  from: Item | Item[],
39
61
  to: Item | Item[],
40
- callback: (item: Item, index: number, array: Item[]) => unknown,
62
+ key: ItemKey,
41
63
  ): Item[];
42
64
 
43
65
  /**
@@ -46,10 +68,20 @@ export function move<Item>(
46
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.
47
69
  *
48
70
  * If either of values are not present in the array, or if they overlap, the array will be returned unchanged
71
+ *
49
72
  * @param array Array to move within
50
73
  * @param from Item or items to move
51
74
  * @param to Item or items to move to
52
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
+ * ```
53
85
  */
54
86
  export function move<Item>(array: Item[], from: Item | Item[], to: Item | Item[]): Item[];
55
87
 
@@ -114,10 +146,20 @@ move.toIndex = moveToIndex;
114
146
  * If the from index is out of bounds, the array will be returned unchanged
115
147
  *
116
148
  * Available as `moveIndices` and `move.indices`
149
+ *
117
150
  * @param array Array to move within
118
151
  * @param from Index to move from
119
152
  * @param to Index to move to
120
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
+ * ```
121
163
  */
122
164
  export function moveIndices<Item>(array: Item[], from: number, to: number): Item[] {
123
165
  if (!Array.isArray(array)) {
@@ -154,17 +196,27 @@ export function moveIndices<Item>(array: Item[], from: number, to: number): Item
154
196
  * If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
155
197
  *
156
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
+ *
157
209
  * @param array Array to move within
158
210
  * @param value Item or items to move
159
211
  * @param index Index to move to
160
- * @param key Key to get an item's value for matching
212
+ * @param callback Callback to get an item's value for matching
161
213
  * @returns Original array with items moved _(or unchanged if unable to move)_
162
214
  */
163
- export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item>(
215
+ export function moveToIndex<Item>(
164
216
  array: Item[],
165
217
  value: Item | Item[],
166
218
  index: number,
167
- key: ItemKey,
219
+ callback: (item: Item, index: number, array: Item[]) => unknown,
168
220
  ): Item[];
169
221
 
170
222
  /**
@@ -173,17 +225,27 @@ export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item
173
225
  * If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
174
226
  *
175
227
  * Available as `moveToIndex` and `move.toIndex`
228
+ *
176
229
  * @param array Array to move within
177
230
  * @param value Item or items to move
178
231
  * @param index Index to move to
179
- * @param callback Callback to get an item's value for matching
232
+ * @param key Key to get an item's value for matching
180
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
+ * ```
181
243
  */
182
- export function moveToIndex<Item>(
244
+ export function moveToIndex<Item extends PlainObject, ItemKey extends keyof Item>(
183
245
  array: Item[],
184
246
  value: Item | Item[],
185
247
  index: number,
186
- callback: (item: Item, index: number, array: Item[]) => unknown,
248
+ key: ItemKey,
187
249
  ): Item[];
188
250
 
189
251
  /**
@@ -192,10 +254,20 @@ export function moveToIndex<Item>(
192
254
  * If the value is not present in the array, or if the index is out of bounds, the array will be returned unchanged
193
255
  *
194
256
  * Available as `moveToIndex` and `move.toIndex`
257
+ *
195
258
  * @param array Array to move within
196
259
  * @param value Item or items to move
197
260
  * @param index Index to move to
198
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
+ * ```
199
271
  */
200
272
  export function moveToIndex<Item>(array: Item[], value: Item | Item[], index: number): Item[];
201
273
 
@@ -239,3 +311,5 @@ export function moveToIndex(
239
311
 
240
312
  return array;
241
313
  }
314
+
315
+ // #endregion