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
8 changes: 7 additions & 1 deletion src/lualib/ArrayFlat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
function __TS__ArrayFlat(this: void, array: any[], depth = 1): any[] {
let result: any[] = [];
for (const value of array) {
if (depth > 0 && type(value) === "table" && 1 in value) {
if (
depth > 0 &&
type(value) === "table" &&
// Workaround to determine if value is an array or not (fails in case of objects without keys)
// See discussion in: https://github.com/TypeScriptToLua/TypeScriptToLua/pull/737
(1 in value || (next as NextEmptyCheck)(value, undefined) === undefined)
) {
result = result.concat(__TS__ArrayFlat(value, depth - 1));
} else {
result[result.length] = value;
Expand Down
7 changes: 6 additions & 1 deletion src/lualib/ArrayFlatMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ function __TS__ArrayFlatMap<T, U>(
let result: U[] = [];
for (let i = 0; i < array.length; i++) {
const value = callback(array[i], i, array);
if (type(value) === "table" && 1 in value) {
if (
type(value) === "table" &&
// Workaround to determine if value is an array or not (fails in case of objects without keys)
// See discussion in: https://github.com/TypeScriptToLua/TypeScriptToLua/pull/737
(1 in value || (next as NextEmptyCheck)(value as any, undefined) === undefined)
) {
result = result.concat(value);
} else {
result[result.length] = value as U;
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 @@ -5,6 +5,9 @@ declare var __TS__originalTraceback:
| ((this: void, thread?: any, message?: string, level?: number) => string)
| undefined;

// Override next declaration so we can omit extra return values
declare type NextEmptyCheck = (this: void, table: any, index: undefined) => unknown | undefined;

declare function tonumber(value: any, base?: number): number | undefined;
declare function type(
value: any
Expand Down
4 changes: 4 additions & 0 deletions test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ test.each([
});

test.each([
{ array: [[]], expected: [] },
{ array: [{ a: 1 }, { a: 2 }, { a: 3 }], expected: [{ a: 1 }, { a: 2 }, { a: 3 }] },
{ array: [1, [2, 3], 4], expected: [1, 2, 3, 4] },
{ array: [1, [2, 3], 4], depth: 0, expected: [1, [2, 3], 4] },
{ array: [1, [[2], [3]], 4], expected: [1, [2], [3], 4] },
Expand All @@ -497,6 +499,8 @@ test.each([
});

test.each([
{ array: [[]], map: <T>(v: T) => v, expected: [] },
{ array: [1, 2, 3], map: (v: number) => ({ a: v * 2 }), expected: [{ a: 2 }, { a: 4 }, { a: 6 }] },
{ array: [1, [2, 3], [4]], map: <T>(value: T) => value, expected: [1, 2, 3, 4] },
{ array: [1, 2, 3], map: (v: number) => v * 2, expected: [2, 4, 6] },
{ array: [1, 2, 3], map: (v: number) => [v, v * 2], expected: [1, 2, 2, 4, 3, 6] },
Expand Down