Page MenuHomeSealhub

tempstream.ts
No OneTemporary

Authored By
Unknown
Size
1 KB
Referenced Files
None
Subscribers
None

tempstream.ts

/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable no-await-in-loop */
//
// this version does not actually create a stream - it waits for the
// entire string ti bulid and returns it. This might be more efficient
// and is recommended to use when actual streaming is not necessary
import { Readable } from "node:stream";
import { Templatable, streamToString } from "./index.js";
export function handleResult(value: unknown): Promise<string> | string {
let result = "";
if (value instanceof Promise) {
return value.then(handleResult);
}
if (value === null || value == undefined) {
result = "";
} else if (Array.isArray(value)) {
if (value.some((e) => e instanceof Promise)) {
return Promise.all(value.map(handleResult)).then((e) =>
handleResult(e.join(""))
);
} else {
return value.map(handleResult).join("");
}
} else if (value instanceof Readable) {
return streamToString(value).then(handleResult);
} else if (
value !== undefined &&
value !== null &&
typeof value !== "string" &&
value.toString
) {
result = value.toString();
} else {
result = value as string;
}
return result;
}
export function tempstream(
strings: TemplateStringsArray,
...params: Templatable[]
): Promise<string> | string {
let result = "";
if (params.some((param) => param instanceof Promise)) {
return Promise.all(params.map(handleResult)).then((params) =>
tempstream(strings, ...params)
);
}
if (params.some((param) => Array.isArray(param))) {
return tempstream(
strings,
...params
.map((e) => (Array.isArray(e) ? e.flat() : e))
.map(handleResult)
);
}
params = params.map(handleResult);
for (const string of strings) {
result += string;
result += params.shift() || "";
}
result += params.shift() || "";
return result;
}

File Metadata

Mime Type
application/javascript
Expires
Fri, Nov 27, 20:25 (3 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1229570
Default Alt Text
tempstream.ts (1 KB)

Event Timeline