From cf6e663113d338398e61ed48bd6d53efbaf2d823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 31 Oct 2018 11:03:27 +0100 Subject: [PATCH] rename toAsyncIterator, add tests --- js/deno.ts | 2 +- js/files_test.ts | 13 +++++++++++++ js/io.ts | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/js/deno.ts b/js/deno.ts index 7a8ca5983e3b6e..e32071ed20ba1a 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -7,7 +7,7 @@ export { chdir, cwd } from "./dir"; export { File, open, stdin, stdout, stderr, read, write, close } from "./files"; export { copy, - readerIterator, + toAsyncIterator, ReadResult, Reader, Writer, diff --git a/js/files_test.ts b/js/files_test.ts index 9759e0b261aeeb..b69612fee86117 100644 --- a/js/files_test.ts +++ b/js/files_test.ts @@ -17,3 +17,16 @@ test(async function filesCopyToStdout() { assertEqual(bytesWritten, fileSize); console.log("bytes written", bytesWritten); }); + +test(async function filesToAsyncIterator() { + const filename = 'tests/hello.txt'; + const file = await deno.open(filename); + const fileSize = deno.statSync(filename).len; + + let totalSize = 0; + for await (const buf of deno.toAsyncIterator(file)) { + totalSize += buf.byteLength; + } + + assertEqual(totalSize, fileSize); +}); \ No newline at end of file diff --git a/js/io.ts b/js/io.ts index 383f28341a9782..4e37355bbb7a9a 100644 --- a/js/io.ts +++ b/js/io.ts @@ -15,7 +15,7 @@ export interface Reader { * of bytes read (`0` <= `n` <= `p.byteLength`) and any error encountered. * Even if `read()` returns `n` < `p.byteLength`, it may use all of `p` as * scratch space during the call. If some data is available but not - * `p.byteLength` , `read()` conventionally returns what is available + * `p.byteLength` bytes, `read()` conventionally returns what is available * instead of waiting for more. * * When `read()` encounters an error or end-of-file condition after @@ -123,7 +123,7 @@ export async function copy(dst: Writer, src: Reader): Promise { * console.log(chunk) * } */ -export function readerIterator( +export function toAsyncIterator( r: Reader ): AsyncIterableIterator { const b = new Uint8Array(1024);