Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/lualib/ArrayReduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ function __TS__ArrayReduce<T>(
this: void,
arr: T[],
callbackFn: (accumulator: T, currentValue: T, index: number, array: T[]) => T,
initial?: T
...initial: Vararg<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) {
let accumulator = undefined;

// Check if initial value is present in function call
if (select("#", ...initial) !== 0) {
accumulator = select(1, ...initial);
} else if (len > 0) {
accumulator = arr[0];
k++;
k = 1;
} else {
// tslint:disable-next-line: no-string-throw
throw "Reduce of empty array with no initial value";
}

while (k < len) {
accumulator = callbackFn(accumulator, arr[k], k, arr);
k = k + 1;
for (const i of forRange(k, len - 1)) {
accumulator = callbackFn(accumulator, arr[i], i, arr);
}

return accumulator;
Expand Down
3 changes: 3 additions & 0 deletions src/lualib/declarations/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ declare function rawset<T, K extends keyof T>(table: T, key: K, val: T[K]): void
declare function next<K, V>(table: Record<any, V>, index?: K): [K, V];
declare function pcall(func: () => any): any;
declare function unpack<T>(list: T[], i?: number, j?: number): T[];

declare function select<T>(index: number, ...args: T[]): T;
declare function select<T>(index: "#", ...args: T[]): number;
6 changes: 6 additions & 0 deletions src/lualib/declarations/tstl.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/** @noSelfInFile */

/** @vararg */
interface Vararg<T> extends Array<T> {}

/** @forRange */
declare function forRange(start: number, limit: number, step?: number): number[];

interface LuaClass {
prototype: LuaObject;
____super?: LuaClass;
Expand Down
16 changes: 16 additions & 0 deletions test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,22 @@ test.each<[[(total: number, currentItem: number, index: number, array: number[])
util.testExpression`[1, 3, 5, 7].reduce(${util.valuesToString(args)})`.expectToMatchJsResult();
});

test("array.reduce empty undefined initial", () => {
util.testExpression`[].reduce(() => {}, undefined)`.expectToMatchJsResult();
});

test("array.reduce empty no initial", () => {
util.testExpression`[].reduce(() => {})`.expectToMatchJsResult(true);
});

test("array.reduce undefined returning callback", () => {
util.testFunction`
const calls: Array<{ a: void, b: string }> = [];
["a", "b"].reduce<void>((a, b) => { calls.push({ a, b }) }, undefined);
return calls;
`.expectToMatchJsResult();
});

const genericChecks = [
"function generic<T extends number[]>(array: T)",
"function generic<T extends [...number[]]>(array: T)",
Expand Down