2023-05-14 17:33:46 +00:00
|
|
|
export function prettyObject(msg: any) {
|
2023-05-24 13:08:32 +00:00
|
|
|
const obj = msg;
|
2023-05-20 11:58:12 +00:00
|
|
|
if (typeof msg !== "string") {
|
|
|
|
msg = JSON.stringify(msg, null, " ");
|
|
|
|
}
|
2023-05-24 13:08:32 +00:00
|
|
|
if (msg === "{}") {
|
|
|
|
return obj.toString();
|
|
|
|
}
|
2023-05-31 11:39:39 +00:00
|
|
|
if (msg.startsWith("```json")) {
|
|
|
|
return msg;
|
|
|
|
}
|
2023-05-24 13:08:32 +00:00
|
|
|
return ["```json", msg, "```"].join("\n");
|
2023-05-14 17:33:46 +00:00
|
|
|
}
|
2023-09-18 19:18:34 +00:00
|
|
|
|
|
|
|
export function* chunks(s: string, maxBytes = 1000 * 1000) {
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
let buf = new TextEncoder().encode(s);
|
|
|
|
while (buf.length) {
|
|
|
|
let i = buf.lastIndexOf(32, maxBytes + 1);
|
|
|
|
// If no space found, try forward search
|
|
|
|
if (i < 0) i = buf.indexOf(32, maxBytes);
|
|
|
|
// If there's no space at all, take all
|
|
|
|
if (i < 0) i = buf.length;
|
|
|
|
// This is a safe cut-off point; never half-way a multi-byte
|
|
|
|
yield decoder.decode(buf.slice(0, i));
|
|
|
|
buf = buf.slice(i + 1); // Skip space (if any)
|
|
|
|
}
|
|
|
|
}
|