-
Notifications
You must be signed in to change notification settings - Fork 13
/
for.ts
115 lines (96 loc) · 2.65 KB
/
for.ts
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type { Token } from "../src/tokenizer.ts";
import type { Environment, Plugin } from "../src/environment.ts";
export default function (): Plugin {
return (env: Environment) => {
env.tags.push(forTag);
env.utils.toIterator = toIterator;
};
}
function forTag(
env: Environment,
code: string,
output: string,
tokens: Token[],
): string | undefined {
if (!code.startsWith("for ")) {
return;
}
const compiled: string[] = [];
const match = code.match(
/^for\s+(await\s+)?(\w+)(?:,\s*(\w+))?\s+of\s+([\s|\S]+)$/,
);
if (!match) {
throw new Error(`Invalid for loop: ${code}`);
}
const [_, aw, var1, var2, collection] = match;
if (var2) {
compiled.push(
`for ${aw || ""}(let [${var1}, ${var2}] of __env.utils.toIterator(${
env.compileFilters(tokens, collection)
}, true)) {`,
);
} else {
compiled.push(
`for ${aw || ""}(let ${var1} of __env.utils.toIterator(${
env.compileFilters(tokens, collection)
})) {`,
);
}
compiled.push(...env.compileTokens(tokens, output, ["/for"]));
tokens.shift();
compiled.push("}");
return compiled.join("\n");
}
function toIterator(
// deno-lint-ignore no-explicit-any
item: any,
withKeys = false,
): Iterable<unknown> | AsyncIterable<unknown> | Array<unknown> {
if (item === undefined || item === null) {
return [];
}
if (Array.isArray(item)) {
return withKeys ? item.map((value, i) => [i, value]) : item;
}
if (typeof item === "function") {
return toIterator(item(), withKeys);
}
if (typeof item === "object" && item !== null) {
if (typeof item[Symbol.iterator] === "function") {
if (withKeys) {
return iterableToEntries(item as Iterable<unknown>);
}
return item as Iterable<unknown>;
}
if (typeof item[Symbol.asyncIterator] === "function") {
if (withKeys) {
return asyncIterableToEntries(item as AsyncIterable<unknown>);
}
return item as AsyncIterable<unknown>;
}
return withKeys ? Object.entries(item) : Object.values(item);
}
if (typeof item === "string") {
return toIterator(item.split(""), withKeys);
}
if (typeof item === "number") {
return toIterator(new Array(item).fill(0).map((_, i) => i + 1), withKeys);
}
return toIterator([item], withKeys);
}
function* iterableToEntries(
iterator: Iterable<unknown>,
): Generator<[number, unknown]> {
let i = 0;
for (const value of iterator) {
yield [i++, value];
}
}
async function* asyncIterableToEntries(
iterator: AsyncIterable<unknown>,
): AsyncGenerator<[number, unknown]> {
let i = 0;
for await (const value of iterator) {
yield [i++, value];
}
}