X Tutup
Skip to content

Commit 5d2d64b

Browse files
radekmiePerryvw
authored andcommitted
Fixed array.flat[Map] on [[]] (fixes #719). (#737)
* Fixed array.flat[Map] on [[]] (fixes #719). * Added failing flat[Map] tests. * Added empty table checks. * Added a clean workaround for next. * Documented workaround.
1 parent 53be6c3 commit 5d2d64b

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/lualib/ArrayFlat.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
function __TS__ArrayFlat(this: void, array: any[], depth = 1): any[] {
22
let result: any[] = [];
33
for (const value of array) {
4-
if (depth > 0 && type(value) === "table" && 1 in value) {
4+
if (
5+
depth > 0 &&
6+
type(value) === "table" &&
7+
// Workaround to determine if value is an array or not (fails in case of objects without keys)
8+
// See discussion in: https://github.com/TypeScriptToLua/TypeScriptToLua/pull/737
9+
(1 in value || (next as NextEmptyCheck)(value, undefined) === undefined)
10+
) {
511
result = result.concat(__TS__ArrayFlat(value, depth - 1));
612
} else {
713
result[result.length] = value;

src/lualib/ArrayFlatMap.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ function __TS__ArrayFlatMap<T, U>(
66
let result: U[] = [];
77
for (let i = 0; i < array.length; i++) {
88
const value = callback(array[i], i, array);
9-
if (type(value) === "table" && 1 in value) {
9+
if (
10+
type(value) === "table" &&
11+
// Workaround to determine if value is an array or not (fails in case of objects without keys)
12+
// See discussion in: https://github.com/TypeScriptToLua/TypeScriptToLua/pull/737
13+
(1 in value || (next as NextEmptyCheck)(value as any, undefined) === undefined)
14+
) {
1015
result = result.concat(value);
1116
} else {
1217
result[result.length] = value as U;

src/lualib/declarations/global.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ declare var __TS__originalTraceback:
55
| ((this: void, thread?: any, message?: string, level?: number) => string)
66
| undefined;
77

8+
// Override next declaration so we can omit extra return values
9+
declare type NextEmptyCheck = (this: void, table: any, index: undefined) => unknown | undefined;
10+
811
declare function tonumber(value: any, base?: number): number | undefined;
912
declare function type(
1013
value: any

test/unit/builtins/array.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ test.each([
487487
});
488488

489489
test.each([
490+
{ array: [[]], expected: [] },
491+
{ array: [{ a: 1 }, { a: 2 }, { a: 3 }], expected: [{ a: 1 }, { a: 2 }, { a: 3 }] },
490492
{ array: [1, [2, 3], 4], expected: [1, 2, 3, 4] },
491493
{ array: [1, [2, 3], 4], depth: 0, expected: [1, [2, 3], 4] },
492494
{ array: [1, [[2], [3]], 4], expected: [1, [2], [3], 4] },
@@ -497,6 +499,8 @@ test.each([
497499
});
498500

499501
test.each([
502+
{ array: [[]], map: <T>(v: T) => v, expected: [] },
503+
{ array: [1, 2, 3], map: (v: number) => ({ a: v * 2 }), expected: [{ a: 2 }, { a: 4 }, { a: 6 }] },
500504
{ array: [1, [2, 3], [4]], map: <T>(value: T) => value, expected: [1, 2, 3, 4] },
501505
{ array: [1, 2, 3], map: (v: number) => v * 2, expected: [2, 4, 6] },
502506
{ array: [1, 2, 3], map: (v: number) => [v, v * 2], expected: [1, 2, 2, 4, 3, 6] },

0 commit comments

Comments
 (0)
X Tutup