@werker/html
Version:
HTML templating and streaming response library for worker environments such as Cloudflare Workers
60 lines • 1.49 kB
JavaScript
/**
* Alternates items from the first and second iterable in the output iterable, until either input runs out of items.
*/
export function* interleave(xs, ys) {
const itx = xs[Symbol.iterator]();
const ity = ys[Symbol.iterator]();
while (true) {
const rx = itx.next();
if (rx.done)
break;
else
yield rx.value;
const ry = ity.next();
if (ry.done)
break;
else
yield ry.value;
}
}
/**
* It's like interleave, but will flatten items of the second (async) iterable.
*/
export async function* aInterleaveFlattenSecond(xs, ys) {
const itx = xs[Symbol.iterator]();
const ity = ys[Symbol.iterator]();
while (true) {
const rx = itx.next();
if (rx.done)
break;
else
yield rx.value;
const ry = ity.next();
if (ry.done)
break;
else
yield* ry.value;
}
}
export function map(f) {
return function* (iterable) {
for (const x of iterable)
yield f(x);
};
}
export function aMap(f) {
return async function* (iterable) {
for await (const x of iterable)
yield f(x);
};
}
export function* join(iterable) {
yield [...iterable].join('');
}
export async function* aJoin(iterable) {
const chunks = [];
for await (const x of iterable)
chunks.push(x);
yield chunks.join('');
}
//# sourceMappingURL=iter.js.map