-
Notifications
You must be signed in to change notification settings - Fork 56
/
array.ts
97 lines (89 loc) · 2.91 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
/**
* Flatten a two dimensional array into a one dimensional array
* @param arr The array to be flattened.
* @returns A one dimensional array.
*/
export function flat<T>(arr: T[][]): T[] {
return arr.reduce((flattened, subArr) => [...flattened, ...subArr], []);
}
/**
* Remove all duplicates from array
* @param arr - Array that might contain duplicates
* @returns Array of unique items
*/
export function unique<T>(arr: T[]): T[] {
return Array.from(new Set(arr));
}
/**
* Get the last item from an array. Returns undefined if the array is empty.
* @param arr - Array to get the last item of
* @returns Last item of the array or undefined if the array was empty
*/
export function last<T>(arr: T[]): T | undefined {
return arr.length ? arr[arr.length - 1] : undefined;
}
/**
* Get the first item from an array. Returns undefined if the array is empty.
* @param arr - Array to get the first item of
* @returns Fist item of the array or undefined if the array was empty
*/
export function first<T>(arr: T[]): T | undefined {
return arr[0];
}
/**
* Split the given array in chunks
* @param arr - Array to be splitted. The last aray could be shorter.
* @param chunkSize - Size of the chunks
* @returns Array with arrays of chunks size.
*/
export function splitInChunks<T>(arr: T[], chunkSize: number): T[][] {
let result: T[][] = [];
if (arr) {
for (let i = 0; i < arr.length; i += chunkSize) {
result = [...result, arr.slice(i, i + chunkSize)];
}
}
return result;
}
/**
* We want to provide methods which accept a variable single number of elements and arrays.
* The overloaded signature to achieve this is:
* function doSomething(array: T[])
* function doSomething(...varArgs: T[])
* functiondoSomething(first: undefined | T | T[], ...rest: T[]) {
* //implementation
* }
* This wrapper methods makes it easy build an array from the input.
* @param firstOrArray - Either an array, the first element of the var args or undefined if no argument was given.
* @param rest - Second to last element if var args were used, empty array if the frist argument is an array.
* @returns Array from the input or empty array if no input was given.
*/
export function variadicArgumentToArray<T>(
firstOrArray: undefined | T | T[],
rest: T[]
): T[] {
if (Array.isArray(firstOrArray)) {
return [...firstOrArray, ...rest];
}
return firstOrArray ? [firstOrArray, ...rest] : [...rest];
}
/**
* Flattens a array: [1,[2,[3,4]],5] will become [1,2,3,4,5].
* Non primitive values are copied by reference.
*
* @param input - array to be flattened
* @returns the flat array.
*/
export const flatten = (input: any[]): any[] => {
const flatResult: any[] = [];
const stack: any[] = [...input];
while (stack.length > 0) {
const current = stack.pop();
if (!Array.isArray(current)) {
flatResult.push(current);
} else {
stack.push(...current);
}
}
return flatResult.reverse();
};