-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathArrayFrom.ts
More file actions
37 lines (33 loc) · 1.08 KB
/
ArrayFrom.ts
File metadata and controls
37 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/** @noSelfInFile */
import { __TS__Iterator } from "./Iterator";
function arrayLikeStep(this: ArrayLike<unknown>, index: number): LuaMultiReturn<[number, unknown] | []> {
index += 1;
if (index > this.length) return $multi();
return $multi(index, this[index]);
}
const arrayLikeIterator: (
this: void,
arr: ArrayLike<unknown> | Iterable<unknown>
) => LuaIterable<LuaMultiReturn<[number, unknown]>> = ((arr: any) => {
if (typeof arr.length === "number") return $multi(arrayLikeStep, arr, 0);
return __TS__Iterator(arr);
}) as any;
export function __TS__ArrayFrom(
this: void,
arrayLike: ArrayLike<unknown> | Iterable<unknown>,
mapFn?: (this: unknown, element: unknown, index: number) => unknown,
thisArg?: unknown
): unknown[] {
const result = [];
if (mapFn === undefined) {
for (const [, v] of arrayLikeIterator(arrayLike)) {
result.push(v);
}
} else {
let i = 0;
for (const [, v] of arrayLikeIterator(arrayLike)) {
result.push(mapFn.call(thisArg, v, i++));
}
}
return result;
}