/
any.ts
47 lines (46 loc) · 1.49 KB
/
any.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
import { escapeHTML, isHTMLString, markHTMLString } from '../escape.js';
import {
isAstroComponentInstance,
isRenderTemplateResult,
renderAstroTemplateResult,
} from './astro/index.js';
import { SlotString } from './slot.js';
import { bufferIterators } from './util.js';
export async function* renderChild(child: any): AsyncIterable<any> {
child = await child;
if (child instanceof SlotString) {
if (child.instructions) {
yield* child.instructions;
}
yield child;
} else if (isHTMLString(child)) {
yield child;
} else if (Array.isArray(child)) {
const bufferedIterators = bufferIterators(child.map((c) => renderChild(c)));
for (const value of bufferedIterators) {
yield markHTMLString(await value);
}
} else if (typeof child === 'function') {
// Special: If a child is a function, call it automatically.
// This lets you do {() => ...} without the extra boilerplate
// of wrapping it in a function and calling it.
yield* renderChild(child());
} else if (typeof child === 'string') {
yield markHTMLString(escapeHTML(child));
} else if (!child && child !== 0) {
// do nothing, safe to ignore falsey values.
} else if (isRenderTemplateResult(child)) {
yield* renderAstroTemplateResult(child);
} else if (isAstroComponentInstance(child)) {
yield* child.render();
} else if (ArrayBuffer.isView(child)) {
yield child;
} else if (
typeof child === 'object' &&
(Symbol.asyncIterator in child || Symbol.iterator in child)
) {
yield* child;
} else {
yield child;
}
}