forked from Yadro/time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayHelper.ts
More file actions
46 lines (42 loc) · 1.08 KB
/
ArrayHelper.ts
File metadata and controls
46 lines (42 loc) · 1.08 KB
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
type CallbackPrev<T, R> = (prev: T | undefined, cur: T, index: number) => R;
export function iterPrevCurrent<T, R = any>(
items: T[],
callback: CallbackPrev<T, R>
) {
for (let i = 0; i < items.length; i++) {
if (i === 0) {
callback(undefined, items[i], i);
} else {
callback(items[i - 1], items[i], i);
}
}
}
export function mapPrevCurrent<T, R = any>(
items: T[],
callback: CallbackPrev<T, R>
): R[] {
const result: R[] = [];
for (let i = 0; i < items.length; i++) {
if (i === 0) {
result.push(callback(undefined, items[i], i));
} else {
result.push(callback(items[i - 1], items[i], i));
}
}
return result;
}
type CallbackNext<T, R> = (cur: T, next: T | undefined, index: number) => R;
export function mapCurrentNext<T, R = any>(
items: T[],
callback: CallbackNext<T, R>
): R[] {
const result: R[] = [];
for (let i = 0; i < items.length; i++) {
if (i === items.length - 1) {
result.push(callback(items[i], undefined, i));
} else {
result.push(callback(items[i], items[i + 1], i));
}
}
return result;
}