Skip to content

Commit

Permalink
chore: use Array.fromAsync() where possible (denoland#3788)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua committed Nov 10, 2023
1 parent b317958 commit 56e2bc1
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 278 deletions.
7 changes: 4 additions & 3 deletions archive/tar_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ Deno.test("directoryEntryType", async function () {

const reader = await Deno.open(outputFile, { read: true });
const untar = new Untar(reader);
for await (const entry of untar) {
assertEquals(entry.type, "directory");
}
await Array.fromAsync(
untar,
(entry) => assertEquals(entry.type, "directory"),
);

reader.close();
await Deno.remove(outputFile);
Expand Down
5 changes: 1 addition & 4 deletions async/abortable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ Deno.test("[async] abortable (AsyncIterable)", async () => {
await promise;
yield "World";
};
const items: string[] = [];
for await (const item of abortable(a(), c.signal)) {
items.push(item);
}
const items = await Array.fromAsync(abortable(a(), c.signal));
assertEquals(items, ["Hello", "World"]);
clearTimeout(t);
});
Expand Down
17 changes: 3 additions & 14 deletions async/mux_async_iterator_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,15 @@ Deno.test("[async] MuxAsyncIterator", async function () {
const mux = new MuxAsyncIterator<number>();
mux.add(gen123());
mux.add(gen456());
const results = new Set();
for await (const value of mux) {
results.add(value);
}
const results = new Set(await Array.fromAsync(mux));
assertEquals(results.size, 6);
assertEquals(results, new Set([1, 2, 3, 4, 5, 6]));
});

Deno.test("[async] MuxAsyncIterator takes async iterable as source", async function () {
const mux = new MuxAsyncIterator<number>();
mux.add(new CustomAsyncIterable());
const results = new Set();
for await (const value of mux) {
results.add(value);
}
const results = new Set(await Array.fromAsync(mux));
assertEquals(results.size, 3);
assertEquals(results, new Set([1, 2, 3]));
});
Expand All @@ -54,13 +48,8 @@ Deno.test({
const mux = new MuxAsyncIterator<number>();
mux.add(gen123());
mux.add(genThrows());
const results = new Set();
await assertRejects(
async () => {
for await (const value of mux) {
results.add(value);
}
},
async () => await Array.fromAsync(mux),
Error,
"something went wrong",
);
Expand Down
31 changes: 12 additions & 19 deletions async/pool_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ Deno.test("[async] pooledMap", async function () {
[1, 2, 3],
(i) => new Promise<number>((r) => setTimeout(() => r(i), 1000)),
);
const array = [];
for await (const value of results) {
array.push(value);
}
const array = await Array.fromAsync(results);
assertEquals(array, [1, 2, 3]);
const diff = new Date().getTime() - start.getTime();
assert(diff >= 2000);
Expand All @@ -34,13 +31,15 @@ Deno.test("[async] pooledMap errors", async () => {
return n;
}
const mappedNumbers: number[] = [];
const error = await assertRejects(async () => {
for await (const m of pooledMap(3, [1, 2, 3, 4], mapNumber)) {
mappedNumbers.push(m);
}
}, AggregateError);
assert(error instanceof AggregateError);
assert(error.message === ERROR_WHILE_MAPPING_MESSAGE);
const error = await assertRejects(
async () => {
for await (const m of pooledMap(3, [1, 2, 3, 4], mapNumber)) {
mappedNumbers.push(m);
}
},
AggregateError,
ERROR_WHILE_MAPPING_MESSAGE,
);
assertEquals(error.errors.length, 2);
assertStringIncludes(error.errors[0].stack, "Error: Bad number: 1");
assertStringIncludes(error.errors[1].stack, "Error: Bad number: 2");
Expand All @@ -63,10 +62,7 @@ Deno.test("pooledMap returns ordered items", async () => {
),
);

const returned = [];
for await (const value of results) {
returned.push(value);
}
const returned = await Array.fromAsync(results);
assertEquals(returned, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});

Expand All @@ -81,10 +77,7 @@ Deno.test("[async] pooledMap (browser compat)", async function () {
[1, 2, 3],
(i) => new Promise<number>((r) => setTimeout(() => r(i), 100)),
);
const array = [];
for await (const value of results) {
array.push(value);
}
const array = await Array.fromAsync(results);
assertEquals(array, [1, 2, 3]);
} finally {
ReadableStream.prototype[Symbol.asyncIterator] = asyncIterFunc;
Expand Down
17 changes: 5 additions & 12 deletions async/tee_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@ const gen = async function* iter() {
yield 3;
};

/** Testing utility for accumulating the values in async iterable. */
async function accumulate<T>(src: AsyncIterable<T>): Promise<T[]> {
const res: T[] = [];
for await (const item of src) {
res.push(item);
}
return res;
}

Deno.test("async/tee - 2 branches", async () => {
const iter = gen();
const [res0, res1] = tee(iter).map(accumulate);
const [res0, res1] = tee(iter).map(async (src) => await Array.fromAsync(src));
assertEquals(
await Promise.all([res0, res1]),
[
Expand All @@ -32,7 +23,9 @@ Deno.test("async/tee - 2 branches", async () => {

Deno.test("async/tee - 3 branches - immediate consumption", async () => {
const iter = gen();
const [res0, res1, res2] = tee(iter, 3).map(accumulate);
const [res0, res1, res2] = tee(iter, 3).map(async (src) =>
await Array.fromAsync(src)
);
assertEquals(
await Promise.all([res0, res1, res2]),
[
Expand All @@ -52,7 +45,7 @@ Deno.test("async/tee - 3 branches - delayed consumption", async () => {
});

assertEquals(
await Promise.all(iters.map(accumulate)),
await Promise.all(iters.map(async (src) => await Array.fromAsync(src))),
[
[1, 2, 3],
[1, 2, 3],
Expand Down
10 changes: 2 additions & 8 deletions csv/csv_parse_stream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ Deno.test({
const readable = file.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new CsvParseStream());
const records = [] as Array<Array<string>>;
for await (const record of readable) {
records.push(record);
}
const records = await Array.fromAsync(readable);
assertEquals(records, [
["id", "name"],
["1", "foobar"],
Expand Down Expand Up @@ -333,10 +330,7 @@ x,,,
.pipeThrough(new CsvParseStream(options));

if (testCase.output) {
const actual = [];
for await (const record of readable) {
actual.push(record);
}
const actual = await Array.fromAsync(readable);
assertEquals(actual, testCase.output);
} else {
await assertRejects(async () => {
Expand Down
72 changes: 23 additions & 49 deletions csv/csv_stringify_stream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ Deno.test({
["id", "name"],
[1, "foo"],
[2, "bar"],
]);
const output: Array<string> = [];
for await (const r of readable.pipeThrough(new CsvStringifyStream())) {
output.push(r);
}
]).pipeThrough(new CsvStringifyStream());
const output = await Array.fromAsync(readable);
assertEquals(output, [
"id,name\r\n",
"1,foo\r\n",
Expand All @@ -28,31 +25,21 @@ Deno.test({
const readable = ReadableStream.from([
[1, "foo"],
[2, "bar"],
]);
await assertRejects(async () => {
for await (
const _ of readable.pipeThrough(
// @ts-expect-error `columns` option is not allowed
new CsvStringifyStream({ columns: ["id", "name"] }),
)
);
}, StringifyError);
// @ts-expect-error `columns` option is not allowed
]).pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }));
await assertRejects(
async () => await Array.fromAsync(readable),
StringifyError,
);
});

await t.step("with `separator`", async () => {
const readable = ReadableStream.from([
[1, "one"],
[2, "two"],
[3, "three"],
]);
const output: Array<string> = [];
for await (
const r of readable.pipeThrough(
new CsvStringifyStream({ separator: "\t" }),
)
) {
output.push(r);
}
]).pipeThrough(new CsvStringifyStream({ separator: "\t" }));
const output = await Array.fromAsync(readable);
assertEquals(output, [
"1\tone\r\n",
"2\ttwo\r\n",
Expand All @@ -63,30 +50,20 @@ Deno.test({
await t.step("with invalid `separator`", async () => {
const readable = ReadableStream.from([
["one", "two", "three"],
]);
await assertRejects(async () => {
for await (
const _ of readable.pipeThrough(
new CsvStringifyStream({ separator: "\r\n" }),
)
);
}, StringifyError);
]).pipeThrough(new CsvStringifyStream({ separator: "\r\n" }));
await assertRejects(
async () => await Array.fromAsync(readable),
StringifyError,
);
});

await t.step("with objects", async () => {
const readable = ReadableStream.from([
{ id: 1, name: "foo" },
{ id: 2, name: "bar" },
{ id: 3, name: "baz" },
]);
const output: Array<string> = [];
for await (
const r of readable.pipeThrough(
new CsvStringifyStream({ columns: ["id", "name"] }),
)
) {
output.push(r);
}
]).pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }));
const output = await Array.fromAsync(readable);
assertEquals(output, [
"id,name\r\n",
"1,foo\r\n",
Expand All @@ -100,15 +77,12 @@ Deno.test({
{ id: 1, name: "foo" },
{ id: 2, name: "bar" },
{ id: 3, name: "baz" },
]);
await assertRejects(async () => {
for await (
const _ of readable.pipeThrough(
// @ts-expect-error `columns` option is required
new CsvStringifyStream(),
)
);
}, StringifyError);
// @ts-expect-error `columns` option is required
]).pipeThrough(new CsvStringifyStream());
await assertRejects(
async () => await Array.fromAsync(readable),
StringifyError,
);
});
},
});
5 changes: 1 addition & 4 deletions fs/empty_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import { toPathString } from "./_util.ts";
*/
export async function emptyDir(dir: string | URL) {
try {
const items = [];
for await (const dirEntry of Deno.readDir(dir)) {
items.push(dirEntry);
}
const items = await Array.fromAsync(Deno.readDir(dir));

await Promise.all(items.map((item) => {
if (item && item.name) {
Expand Down
8 changes: 4 additions & 4 deletions fs/expand_glob_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ async function expandGlobArray(
options: ExpandGlobOptions,
{ forceRoot = "" } = {},
): Promise<string[]> {
const paths: string[] = [];
for await (const { path } of expandGlob(globString, options)) {
paths.push(path);
}
const paths = await Array.fromAsync(
expandGlob(globString, options),
({ path }) => path,
);
paths.sort();
const pathsSync = [...expandGlobSync(globString, options)].map(
({ path }): string => path,
Expand Down
21 changes: 7 additions & 14 deletions fs/walk_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { walk, WalkEntry, WalkError, WalkOptions, walkSync } from "./walk.ts";
import { walk, WalkError, WalkOptions, walkSync } from "./walk.ts";
import {
assertArrayIncludes,
assertEquals,
Expand All @@ -10,21 +10,13 @@ import { fromFileUrl, resolve } from "../path/mod.ts";

const testdataDir = resolve(fromFileUrl(import.meta.url), "../testdata/walk");

async function toArray(iterator: AsyncIterableIterator<WalkEntry>) {
const entries = [];
for await (const entry of iterator) {
entries.push(entry);
}
return entries;
}

async function assertWalkPaths(
rootPath: string,
expectedPaths: string[],
options?: WalkOptions,
) {
const root = resolve(testdataDir, rootPath);
const entries = await toArray(walk(root, options));
const entries = await Array.fromAsync(walk(root, options));
const entriesSync = Array.from(walkSync(root, options));

const expected = expectedPaths.map((path) => resolve(root, path));
Expand Down Expand Up @@ -93,7 +85,7 @@ Deno.test("[fs/walk] symlink without followSymlink", async () => {
Deno.test("[fs/walk] non-existent root", async () => {
const root = resolve(testdataDir, "non_existent");
await assertRejects(
async () => await toArray(walk(root)),
async () => await Array.fromAsync(walk(root)),
Deno.errors.NotFound,
);
assertThrows(() => Array.from(walkSync(root)), Deno.errors.NotFound);
Expand Down Expand Up @@ -135,8 +127,9 @@ Deno.test("[fs/walk] error", async () => {
const root = resolve(testdataDir, "error");
await Deno.mkdir(root);
await assertRejects(async () => {
for await (const _entry of walk(root)) {
await Deno.remove(root, { recursive: true });
}
await Array.fromAsync(
walk(root),
async () => await Deno.remove(root, { recursive: true }),
);
}, WalkError);
});
Loading

0 comments on commit 56e2bc1

Please sign in to comment.