Skip to content

Commit 440ef27

Browse files
authored
Added array.reduce (#604)
* Added array.reduce * Removed unused import * Update src/lualib/ArrayReduce.ts Co-Authored-By: ark120202 <ark120202@gmail.com> * Fixed error test * Updated tests * Added undefined deleteCount test
1 parent dfcfa1f commit 440ef27

File tree

6 files changed

+495
-407
lines changed

6 files changed

+495
-407
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export enum LuaLibFeature {
1010
ArrayIndexOf = "ArrayIndexOf",
1111
ArrayMap = "ArrayMap",
1212
ArrayPush = "ArrayPush",
13+
ArrayReduce = "ArrayReduce",
1314
ArrayReverse = "ArrayReverse",
1415
ArrayShift = "ArrayShift",
1516
ArrayUnshift = "ArrayUnshift",

src/LuaTransformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,6 +4404,8 @@ export class LuaTransformer {
44044404
return this.transformLuaLibFunction(LuaLibFeature.ArrayMap, node, caller, ...params);
44054405
case "filter":
44064406
return this.transformLuaLibFunction(LuaLibFeature.ArrayFilter, node, caller, ...params);
4407+
case "reduce":
4408+
return this.transformLuaLibFunction(LuaLibFeature.ArrayReduce, node, caller, ...params);
44074409
case "some":
44084410
return this.transformLuaLibFunction(LuaLibFeature.ArraySome, node, caller, ...params);
44094411
case "every":

src/lualib/ArrayReduce.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.reduce
2+
function __TS__ArrayReduce<T>(
3+
this: void,
4+
arr: T[],
5+
callbackFn: (accumulator: T, currentValue: T, index: number, array: T[]) => T,
6+
initial?: T
7+
): T {
8+
const len = arr.length;
9+
10+
if (len === 0 && initial === undefined) {
11+
// tslint:disable-next-line: no-string-throw
12+
throw "Reduce of empty array with no initial value";
13+
}
14+
15+
let k = 0;
16+
let accumulator = initial;
17+
if (initial === undefined) {
18+
accumulator = arr[0];
19+
k++;
20+
}
21+
22+
while (k < len) {
23+
accumulator = callbackFn(accumulator, arr[k], k, arr);
24+
k = k + 1;
25+
}
26+
27+
return accumulator;
28+
}

src/lualib/ArraySlice.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
// https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf 22.1.3.23
1+
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.slice
22
function __TS__ArraySlice<T>(this: void, list: T[], first: number, last: number): T[] {
33
const len = list.length;
44

5+
const relativeStart = first || 0;
6+
57
let k: number;
6-
if (first < 0) {
7-
k = Math.max(len + first, 0);
8+
if (relativeStart < 0) {
9+
k = Math.max(len + relativeStart, 0);
810
} else {
9-
k = Math.min(first, len);
11+
k = Math.min(relativeStart, len);
1012
}
1113

1214
let relativeEnd = last;

0 commit comments

Comments
 (0)