forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayReduce.ts
More file actions
28 lines (24 loc) · 718 Bytes
/
ArrayReduce.ts
File metadata and controls
28 lines (24 loc) · 718 Bytes
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
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.reduce
function __TS__ArrayReduce<T>(
this: void,
arr: T[],
callbackFn: (accumulator: T, currentValue: T, index: number, array: T[]) => T,
initial?: T
): T {
const len = arr.length;
if (len === 0 && initial === undefined) {
// tslint:disable-next-line: no-string-throw
throw "Reduce of empty array with no initial value";
}
let k = 0;
let accumulator = initial;
if (initial === undefined) {
accumulator = arr[0];
k++;
}
while (k < len) {
accumulator = callbackFn(accumulator, arr[k], k, arr);
k = k + 1;
}
return accumulator;
}