forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayFlatMap.ts
More file actions
22 lines (21 loc) · 774 Bytes
/
ArrayFlatMap.ts
File metadata and controls
22 lines (21 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function __TS__ArrayFlatMap<T, U>(
this: void,
array: T[],
callback: (value: T, index: number, array: T[]) => U | readonly U[]
): U[] {
let result: U[] = [];
for (let i = 0; i < array.length; i++) {
const value = callback(array[i], i, array);
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;
}
}
return result;
}