mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
9c3c53e65b
This commit improves docs of the streams module. Towards #3764 --------- Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
const textDecoder = new TextDecoder();
|
|
|
|
/**
|
|
* Converts a {@linkcode ReadableSteam} of strings or {@linkcode Uint8Array}s
|
|
* to a single string. Works the same as {@linkcode Response.text}.
|
|
*
|
|
* @param readableStream A `ReadableStream` to convert into a `string`.
|
|
* @returns A `Promise` that resolves to the `string`.
|
|
*
|
|
* @example Basic usage
|
|
* ```ts
|
|
* import { toText } from "@std/streams/to-text";
|
|
* import { assertEquals } from "@std/assert/assert-equals";
|
|
*
|
|
* const stream = ReadableStream.from(["Hello, ", "world!"]);
|
|
* assertEquals(await toText(stream), "Hello, world!");
|
|
* ```
|
|
*/
|
|
export async function toText(
|
|
readableStream: ReadableStream,
|
|
): Promise<string> {
|
|
const reader = readableStream.getReader();
|
|
let result = "";
|
|
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) {
|
|
break;
|
|
}
|
|
|
|
result += typeof value === "string" ? value : textDecoder.decode(value);
|
|
}
|
|
|
|
return result;
|
|
}
|