mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
0b2497f16e
* fix: update codebase to work with Deno RC * work * fix * fix * fix * fixes * work * update * fixes * fix * revert
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
import type { Writer, WriterSync } from "./types.ts";
|
|
|
|
/**
|
|
* Write all the content of the array buffer (`arr`) to the writer (`w`).
|
|
*
|
|
* @example Writing to stdout
|
|
* ```ts no-assert
|
|
* import { writeAll } from "@std/io/write-all";
|
|
*
|
|
* const contentBytes = new TextEncoder().encode("Hello World");
|
|
* await writeAll(Deno.stdout, contentBytes);
|
|
* ```
|
|
*
|
|
* @example Writing to file
|
|
* ```ts ignore no-assert
|
|
* import { writeAll } from "@std/io/write-all";
|
|
*
|
|
* const contentBytes = new TextEncoder().encode("Hello World");
|
|
* using file = await Deno.open('test.file', { write: true });
|
|
* await writeAll(file, contentBytes);
|
|
* ```
|
|
*
|
|
* @param writer The writer to write to
|
|
* @param data The data to write
|
|
*/
|
|
export async function writeAll(writer: Writer, data: Uint8Array) {
|
|
let nwritten = 0;
|
|
while (nwritten < data.length) {
|
|
nwritten += await writer.write(data.subarray(nwritten));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Synchronously write all the content of the array buffer (`arr`) to the
|
|
* writer (`w`).
|
|
*
|
|
* @example "riting to stdout
|
|
* ```ts no-assert
|
|
* import { writeAllSync } from "@std/io/write-all";
|
|
*
|
|
* const contentBytes = new TextEncoder().encode("Hello World");
|
|
* writeAllSync(Deno.stdout, contentBytes);
|
|
* ```
|
|
*
|
|
* @example Writing to file
|
|
* ```ts ignore no-assert
|
|
* import { writeAllSync } from "@std/io/write-all";
|
|
*
|
|
* const contentBytes = new TextEncoder().encode("Hello World");
|
|
* using file = Deno.openSync("test.file", { write: true });
|
|
* writeAllSync(file, contentBytes);
|
|
* ```
|
|
*
|
|
* @param writer The writer to write to
|
|
* @param data The data to write
|
|
*/
|
|
export function writeAllSync(writer: WriterSync, data: Uint8Array) {
|
|
let nwritten = 0;
|
|
while (nwritten < data.length) {
|
|
nwritten += writer.writeSync(data.subarray(nwritten));
|
|
}
|
|
}
|