We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 53be6c3 commit 5d2d64bCopy full SHA for 5d2d64b
4 files changed
src/lualib/ArrayFlat.ts
@@ -1,7 +1,13 @@
1
function __TS__ArrayFlat(this: void, array: any[], depth = 1): any[] {
2
let result: any[] = [];
3
for (const value of array) {
4
- if (depth > 0 && type(value) === "table" && 1 in value) {
+ 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
+ ) {
11
result = result.concat(__TS__ArrayFlat(value, depth - 1));
12
} else {
13
result[result.length] = value;
src/lualib/ArrayFlatMap.ts
@@ -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) {
+ (1 in value || (next as NextEmptyCheck)(value as any, undefined) === undefined)
14
15
result = result.concat(value);
16
17
result[result.length] = value as U;
src/lualib/declarations/global.d.ts
@@ -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
test/unit/builtins/array.spec.ts
@@ -487,6 +487,8 @@ test.each([
487
});
488
489
test.each([
490
+ { array: [[]], expected: [] },
491
+ { array: [{ a: 1 }, { a: 2 }, { a: 3 }], expected: [{ a: 1 }, { a: 2 }, { a: 3 }] },
492
{ array: [1, [2, 3], 4], expected: [1, 2, 3, 4] },
493
{ array: [1, [2, 3], 4], depth: 0, expected: [1, [2, 3], 4] },
494
{ array: [1, [[2], [3]], 4], expected: [1, [2], [3], 4] },
@@ -497,6 +499,8 @@ test.each([
497
499
498
500
501
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 }] },
504
{ array: [1, [2, 3], [4]], map: <T>(value: T) => value, expected: [1, 2, 3, 4] },
505
{ array: [1, 2, 3], map: (v: number) => v * 2, expected: [2, 4, 6] },
506
{ array: [1, 2, 3], map: (v: number) => [v, v * 2], expected: [1, 2, 2, 4, 3, 6] },
0 commit comments