/
array.ts
446 lines (387 loc) · 12.5 KB
/
array.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import { getNthIndex } from './utils'
interface ArrayConstructor<T> extends Function {
from: (value: T[]) => T[]
}
/**
* Returns the first (x) element(s) in the array or undefined.
*/
export function first<T>(value: T[], count?: number) {
if (count) {
return value.slice(0, count)
}
return value[0]
}
/**
* Returns the second element in the array or undefined.
*/
export function second<T>(value: T[]) {
return value[1]
}
/**
* Returns last (x) element(s) in array or undefined.
* Alternatively, pass in a callback to get the last item that passes the given truth test (inverse of `find`).
*/
export function last<T>(value: T[], countOrFn?: number | ((value: any) => any[])) {
if (typeof countOrFn === 'number') {
return value.slice(value.length - countOrFn)
} else if (typeof countOrFn === 'function') {
const filteredItems = value.filter(countOrFn)
return filteredItems[filteredItems.length - 1]
}
return value[value.length - 1]
}
export function isEmpty<T>(value: T[]) {
return value.length === 0
}
/**
* Returns element at given index or undefined. If given value is negative, it searches from behind.
*/
export function nth<T>(value: T[], index: number) {
return value[getNthIndex(value, index)]
}
/**
* Returns the items until either the given value is found, or the given callback returns `true`.
*/
export function until<T>(value: T[], comparison) {
const newArray = []
for (const item of value) {
const reachedEnd = (typeof comparison === "function" && comparison(item)) || item === comparison
if (reachedEnd) {
break
}
newArray.push(item)
}
return newArray
}
/**
* Return all items that don't pass the given truth test. Inverse of `Array.filter`.
*/
export function reject<T>(value: T[], callback: (item: T, index?: number) => boolean) {
return value.filter((item, index) => !callback(item, index))
}
/**
* Moves an item in the array using the given source index to either "before" or "after" the given target.
*/
export function move<T>(value: T[], source: number | 'first' | 'last', position: 'before' | 'after', target: number | 'first' | 'last') {
if (source === 'first') {
source = 0
} else if (source === 'last') {
source = value.length - 1
}
if (target === 'first') {
target = 0
} else if (target === 'last') {
target = value.length - 1
}
if (source === target) {
return value
}
const comparison = position === 'before' ? (_, index) => index < target : (_, index) => index <= target
const [before, after] = partition(value, comparison)
const result = before.concat(value[source], ...after)
const newSourceIndex = source > target ? source + 1 : source
result.splice(newSourceIndex, 1)
return result
}
/**
* Breaks the array into multiple, smaller arrays of a given size.
*/
export function chunk<T>(value: T[], n) {
const remaining = [...value]
const chunks = []
while (remaining.length > 0) {
chunks.push(remaining.splice(0, n))
}
return chunks
}
/**
* Returns the items for the given page and size.
*/
export function forPage<T>(value: T[], page: number, size: number) {
const from = ((page - 1) * size)
return value.slice(from, from + size)
}
/**
* Fills up the array with the given value.
*/
export function pad<T>(value: T[], size: number, paddedValue: T) {
const copy = (value.constructor as ArrayConstructor<T>).from(value)
while (copy.length < size) copy.push(paddedValue)
return copy
}
/**
* Points to a specific index inside the array to do further actions on it.
*/
export function point<T>(value: T[], indexOrFn: number | ((item: T) => boolean)) {
const index = getNthIndex(value, indexOrFn)
const array = value
const pointer = {
/**
* Sets the value at the current index and returns a new array.
*/
set(callback: (item: T) => T) {
const copy = (array.constructor as ArrayConstructor<T>).from(array)
copy[index] = callback(copy[index])
return copy
},
/**
* Appends given value to array in between the currently pointed item and its next item and returns a new array.
*/
append(...items: T[]) {
const [before, after] = partition(array, (item, i) => i <= index)
return [...before, ...items, ...after]
},
/**
* Prepends given value to array in between the currently pointed item and its previous item and returns a new array.
*/
prepend(...items: T[]) {
const [before, after] = partition(array, (item, i) => i < index)
return [...before, ...items, ...after]
},
/**
* Removes the current index and returns a new array.
*/
remove() {
return array.filter((_, i) => index !== i)
},
/**
* Returns value for current pointer position.
*/
value() {
return array[index]
},
/**
* Splits the array at the current index
*/
split() {
const left = array.slice(0, index)
const right = array.slice(index + 1)
return [left, right]
},
/**
* Steps forward or backward given the number of steps.
*/
step(steps: number) {
return point(array, index + steps)
}
}
return pointer
}
/**
* @deprecated Alias for point
*/
export const at = point
/**
* Filters array by given value or key/value pair.
*/
export function where<T>(array: T[], keyOrValue, value) {
if (value === undefined) {
return array.filter((item) => item === keyOrValue)
}
return array.filter((item) => item[keyOrValue] === value)
}
/**
* Removes items from array by the given key or key/value pair.
*/
export function whereNot<T>(array: T[], keyOrValue, value) {
if (value === undefined) {
return array.filter((item) => item !== keyOrValue)
}
return array.filter((item) => item[keyOrValue] !== value)
}
/**
* Filters array by given values or key/values pair.
*/
export function whereIn<T>(array: T[], keyOrValue, value) {
if (value === undefined) {
return array.filter((item) => keyOrValue.includes(item))
}
return array.filter((item) => value.includes(item[keyOrValue]))
}
/**
* Removes items from array by the given value or key/values pair.
*/
export function whereNotIn<T>(array: T[], keyOrValue, value) {
if (value === undefined) {
return array.filter((item) => !keyOrValue.includes(item))
}
return array.filter((item) => !value.includes(item[keyOrValue]))
}
/**
* Only returns items which are not empty.
*/
export function filled<T>(value: T[], key?: string) {
if (!key) {
return value.filter((value) => !!value)
}
return value.filter((item) => !!item[key])
}
/**
* Mutates the original array with the return value of the given callback.
*/
export function mutate<T>(value: T[], callback: ((array: T[]) => T[])) {
const mutation = callback(value)
value.splice(0)
value.splice(0, 0, ...mutation)
return value
}
/**
* Groups an array by the given key and returns a flooent map.
*/
export function groupBy<T, K extends keyof T>(value: T[], key: K | ((item: T) => T[K]) ) {
return value.reduce<Map<T[K], T[]>>((result, item) => {
const group = typeof key === "function" ? key(item) : item[key]
if (result.has(group)) {
result.get(group).push(item)
} else {
result.set(group, (value.constructor as ArrayConstructor<T>).from([item]))
}
return result
}, new Map<T[K], T[]>())
}
/**
* Keys the collection by the given key. If multiple items have the same key, only the last one will appear in the new collection.
*/
export function keyBy<T, K extends keyof T>(value: T[], key: K | ((item: T) => T[K]) ) {
return value.reduce<Map<T[K], T>>((result, item) => {
const group = typeof key === "function" ? key(item) : item[key]
result.set(group, item)
return result
}, new Map<T[K], T>())
}
/**
* Returns the sum of the array.
* For arrays of objects: Pass field or callback as argument.
*/
export function sum<T>(value: T[], key?: string | ((item: T) => number)) {
return value.reduce<number>((result, item) => {
let number = item
if (key) {
number = typeof key === "function" ? key(item) : item[key]
}
return result + (number as unknown as number)
}, 0)
}
/**
* Omits given keys from all objects in the array.
*/
export function omit<T>(value: T[], keys: string[]) {
return (value as unknown as Object[]).map((item) => {
const copy = {...item}
keys.forEach(key => delete copy[key])
return copy
}) as T[]
}
/**
* Pluck the given field out of each object in the array.
*/
export function pluck<T>(value: T[], key: keyof T) {
return value.map((item) => item[key]) as T[keyof T][]
}
/**
* Returns array of unique values.
* For array ob objects: Pass key or callback to use it for the comparison.
*/
export function unique<T>(value: T[], key?: string | ((item: T) => string)) {
if (!key) {
return [...new Set(value)]
}
const cache = new Map()
const unique = []
if (typeof key === "function") {
for (const item of value) {
const value = key(item)
if (!cache.has(value)) {
cache.set(value, 1)
unique.push(item)
}
}
return unique
}
for (const item of value) {
if (cache.has(item[key])) continue
cache.set(item[key], 1)
unique.push(item)
}
return unique
}
/**
* Shuffles and returns a new array.
*/
export function shuffle<T>(value: T[]) {
const array = (value.constructor as ArrayConstructor<T>).from(value)
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array
}
/**
* Returns a tuple separating the items that pass the given truth test.
*/
export function partition<T>(value: T[], callback: (item: T, index?: number) => boolean) {
const tuple = [[], []] as [T[], T[]]
value.forEach((item, index) => {
const tupleIndex = callback(item, index) ? 0 : 1
tuple[tupleIndex].push(item)
})
return tuple
}
/**
* Prepends the given items to the array. Unlike `unshift`, it is immutable and returns a new array.
*/
export function prepend<T>(value: T[], ...items: T[]): T[] {
return [...items, ...value]
}
/**
* Appends the given items to the array. Unlike `push`, it is immutable and returns a new array.
*/
export function append<T>(value: T[], ...items: T[]): T[] {
return [...value, ...items]
}
/**
* Sorts an array in descending order and **returns a new array**.
* For array of objects: Pass index, field or callback to use it for sorting.
*/
export function sortDesc<T>(value: T[], key?: string | number | ((item: T) => any)) {
return sortAsc(value, key).reverse()
}
/**
* Sorts an array in ascending order and **returns a new array**.
* For array of objects: Pass index, field or callback to use it for sorting.
*/
export function sortAsc<T>(value: T[], key?: string | number | ((item: T, index: number) => any)) {
if (!key) {
return [...value].sort()
}
const compare = (a: T, b: T) => {
if (typeof a === 'string') {
return (a as unknown as string).localeCompare(b as unknown as string)
}
// This also works on dates
return (a as unknown as number) - (b as unknown as number)
}
if (typeof key === 'function') {
return value.map((item, index) => {
return { sortKey: key(item, index), item }
})
.sort((a, b) => compare(a.sortKey, b.sortKey))
.map(item => item.item)
}
return [...value].sort((a, b) => compare(a[key], b[key]))
}
/**
* Turns an array in the structure of `[ ['key', 'value'] ]` into a map.
*/
export function toMap<T>(value: T[]) {
return new Map(value as any)
}
/**
* Turns the given array into a map with each element becoming a key in the map.
*/
export function toKeyedMap<T, DV>(array: T[], defaultValueOrCallback: DV | ((item: T) => DV)) {
return new Map(array.map(item => {
const value = typeof defaultValueOrCallback === 'function' ? (defaultValueOrCallback as Function)(item) : defaultValueOrCallback
return [item, value] as [T, DV]
}))
}