From 5539dbc4e0dadc053d57c8f27dc83a6035973d31 Mon Sep 17 00:00:00 2001 From: Alisue Date: Mon, 12 Aug 2024 15:14:29 +0900 Subject: [PATCH] refactor!: use native way and remove `toArray` Use `Array.from` and `Array.fromAsync` instead of `toArray` and remove them. --- README.md | 132 ++++++++++---------------------- async/chain.ts | 6 +- async/chain_test.ts | 9 +-- async/chunked.ts | 3 +- async/chunked_test.ts | 17 ++-- async/compact.ts | 3 +- async/compact_test.ts | 17 ++-- async/compress.ts | 3 +- async/compress_test.ts | 13 ++-- async/cycle.ts | 3 +- async/cycle_test.ts | 13 ++-- async/drop.ts | 3 +- async/drop_test.ts | 9 +-- async/drop_while.ts | 3 +- async/drop_while_test.ts | 17 ++-- async/enumerate.ts | 3 +- async/enumerate_test.ts | 13 ++-- async/every.ts | 1 - async/filter.ts | 3 +- async/filter_test.ts | 9 +-- async/flat_map.ts | 3 +- async/flat_map_test.ts | 17 ++-- async/flatten.ts | 3 +- async/flatten_test.ts | 9 +-- async/iter_test.ts | 5 +- async/map.ts | 3 +- async/map_test.ts | 9 +-- async/mod.ts | 1 - async/pairwise.ts | 3 +- async/pairwise_test.ts | 13 ++-- async/take.ts | 3 +- async/take_test.ts | 9 +-- async/take_while.ts | 3 +- async/take_while_test.ts | 17 ++-- async/to_array.ts | 34 -------- async/to_array_test.ts | 27 ------- async/to_async_iterable.ts | 3 +- async/to_async_iterable_test.ts | 3 +- async/uniq.ts | 6 +- async/uniq_test.ts | 13 ++-- async/zip.ts | 3 +- async/zip_test.ts | 17 ++-- chain.ts | 4 +- chain_test.ts | 4 +- chunked.ts | 2 +- chunked_test.ts | 8 +- compact.ts | 2 +- compact_test.ts | 8 +- compress.ts | 2 +- compress_test.ts | 6 +- count.ts | 2 +- count_test.ts | 6 +- cycle.ts | 4 +- cycle_test.ts | 6 +- deno.jsonc | 4 - drop.ts | 2 +- drop_test.ts | 4 +- drop_while.ts | 2 +- drop_while_test.ts | 6 +- enumerate.ts | 2 +- enumerate_test.ts | 6 +- filter.ts | 2 +- filter_test.ts | 2 +- flat_map.ts | 2 +- flat_map_test.ts | 4 +- flatten.ts | 2 +- flatten_test.ts | 4 +- iter_test.ts | 2 +- map.ts | 2 +- map_test.ts | 2 +- mod.ts | 1 - mod_test.ts | 2 +- pairwise.ts | 2 +- pairwise_test.ts | 6 +- range.ts | 4 +- range_test.ts | 6 +- take.ts | 2 +- take_test.ts | 4 +- take_while.ts | 2 +- take_while_test.ts | 6 +- to_array.ts | 25 ------ to_array_test.ts | 14 ---- uniq.ts | 4 +- uniq_test.ts | 4 +- zip.ts | 2 +- zip_test.ts | 8 +- 86 files changed, 240 insertions(+), 438 deletions(-) delete mode 100644 async/to_array.ts delete mode 100644 async/to_array_test.ts delete mode 100644 to_array.ts delete mode 100644 to_array_test.ts diff --git a/README.md b/README.md index df7ab0d..c97c97b 100644 --- a/README.md +++ b/README.md @@ -22,15 +22,14 @@ Chains multiple iterables together. import { chain } from "@core/iterutil/chain"; const iter = chain([1, 2], [3, 4]); -console.log([...iter]); // [1, 2, 3, 4] +console.log(Array.from(iter)); // [1, 2, 3, 4] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { chain } from "@core/iterutil/async/chain"; const iter = chain([1, 2], [3, 4]); -console.log(await toArray(iter)); // [1, 2, 3, 4] +console.log(await Array.fromAsync(iter)); // [1, 2, 3, 4] ``` ### chunked @@ -41,15 +40,14 @@ Chunks an iterable into arrays of a given size. import { chunked } from "@core/iterutil/chunked"; const iter = chunked([1, 2, 3, 4, 5], 2); -console.log([...iter]); // [[1, 2], [3, 4], [5]] +console.log(Array.from(iter)); // [[1, 2], [3, 4], [5]] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { chunked } from "@core/iterutil/async/chunked"; const iter = chunked([1, 2, 3, 4, 5], 2); -console.log(await toArray(iter)); // [[1, 2], [3, 4], [5]] +console.log(await Array.fromAsync(iter)); // [[1, 2], [3, 4], [5]] ``` ### compact @@ -60,15 +58,14 @@ Removes all nullish (`null` or `undefined`) values from an iterable. import { compact } from "@core/iterutil/compact"; const iter = compact([1, undefined, 2, null, 3]); -console.log([...iter]); // [1, 2, 3] +console.log(Array.from(iter)); // [1, 2, 3] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { compact } from "@core/iterutil/async/compact"; const iter = compact([1, undefined, 2, null, 3]); -console.log(await toArray(iter)); // [1, 2, 3] +console.log(await Array.fromAsync(iter)); // [1, 2, 3] ``` ### compress @@ -79,15 +76,14 @@ Compresses an iterable by selecting elements using a selector iterable. import { compress } from "@core/iterutil/compress"; const iter = compress([1, 2, 3, 4, 5], [true, false, true, false, true]); -console.log([...iter]); // [1, 3, 5] +console.log(Array.from(iter)); // [1, 3, 5] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { compress } from "@core/iterutil/async/compress"; const iter = compress([1, 2, 3, 4, 5], [true, false, true, false, true]); -console.log(await toArray(iter)); // [1, 3, 5] +console.log(await Array.fromAsync(iter)); // [1, 3, 5] ``` ### count @@ -100,7 +96,7 @@ import { count } from "@core/iterutil/count"; import { take } from "@core/iterutil/take"; const iter = count(1, 2); -console.log([...take(iter, 5)]); // [1, 3, 5, 7, 9] +console.log(Array.from(take(iter, 5))); // [1, 3, 5, 7, 9] ``` ### cycle @@ -112,16 +108,15 @@ import { cycle } from "@core/iterutil/cycle"; import { take } from "@core/iterutil/take"; const iter = cycle([1, 2, 3]); -console.log([...take(iter, 5)]); // [1, 2, 3, 1, 2] +console.log(Array.from(take(iter, 5))); // [1, 2, 3, 1, 2] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { cycle } from "@core/iterutil/async/cycle"; import { take } from "@core/iterutil/async/take"; const iter = cycle([1, 2, 3]); -console.log(await toArray(take(iter, 5))); // [1, 2, 3, 1, 2] +console.log(await Array.fromAsync(take(iter, 5))); // [1, 2, 3, 1, 2] ``` ### drop @@ -132,15 +127,14 @@ Drops the first `limit` items from the iterable. import { drop } from "@core/iterutil/drop"; const iter = drop([1, 2, 3, 4, 5], 2); -console.log([...iter]); // [3, 4, 5] +console.log(Array.from(iter)); // [3, 4, 5] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { drop } from "@core/iterutil/async/drop"; const iter = drop([1, 2, 3, 4, 5], 2); -console.log(await toArray(iter)); // [3, 4, 5] +console.log(await Array.fromAsync(iter)); // [3, 4, 5] ``` ### dropWhile @@ -151,15 +145,14 @@ Drops elements from the iterable while the predicate returns true. import { dropWhile } from "@core/iterutil/drop-while"; const iter = dropWhile([1, 2, 3, 4, 5], (x) => x < 3); -console.log([...iter]); // [3, 4, 5] +console.log(Array.from(iter)); // [3, 4, 5] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { dropWhile } from "@core/iterutil/async/drop-while"; const iter = dropWhile([1, 2, 3, 4, 5], (x) => x < 3); -console.log(await toArray(iter)); // [3, 4, 5] +console.log(await Array.fromAsync(iter)); // [3, 4, 5] ``` ### enumerate @@ -170,15 +163,14 @@ Enumerates an iterable. import { enumerate } from "@core/iterutil/enumerate"; const iter = enumerate(["a", "b", "c"]); -console.log([...iter]); // [[0, "a"], [1, "b"], [2, "c"]] +console.log(Array.from(iter)); // [[0, "a"], [1, "b"], [2, "c"]] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { enumerate } from "@core/iterutil/async/enumerate"; const iter = enumerate(["a", "b", "c"]); -console.log(await toArray(iter)); // [[0, "a"], [1, "b"], [2, "c"]] +console.log(await Array.fromAsync(iter)); // [[0, "a"], [1, "b"], [2, "c"]] ``` ### every @@ -194,7 +186,6 @@ console.log(every([1, 2, 3], (value) => value > 1)); // false ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { every } from "@core/iterutil/async/every"; console.log(await every([1, 2, 3], (value) => value > 0)); // true @@ -209,15 +200,14 @@ Filters an iterable based on a function. import { filter } from "@core/iterutil/filter"; const iter = filter([1, 2, 3, 4, 5], (value) => value % 2 === 0); -console.log([...iter]); // [2, 4] +console.log(Array.from(iter)); // [2, 4] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { filter } from "@core/iterutil/async/filter"; const iter = filter([1, 2, 3, 4, 5], (value) => value % 2 === 0); -console.log(await toArray(iter)); // [2, 4] +console.log(await Array.fromAsync(iter)); // [2, 4] ``` ### find @@ -266,15 +256,14 @@ Maps each value in an iterable to an iterable, then flattens the result. import { flatMap } from "@core/iterutil/flat-map"; const iter = flatMap([1, 2, 3], (value) => [value, value]); -console.log([...iter]); // [1, 1, 2, 2, 3, 3] +console.log(Array.from(iter)); // [1, 1, 2, 2, 3, 3] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { flatMap } from "@core/iterutil/async/flat-map"; const iter = flatMap([1, 2, 3], (value) => [value, value]); -console.log(await toArray(iter)); // [1, 1, 2, 2, 3, 3] +console.log(await Array.fromAsync(iter)); // [1, 1, 2, 2, 3, 3] ``` ### flatten @@ -285,15 +274,14 @@ Flattens an iterable of iterables into a single iterable. import { flatten } from "@core/iterutil/flatten"; const iter = flatten([[1, 2], [3, 4], [5]]); -console.log([...iter]); // [1, 2, 3, 4, 5] +console.log(Array.from(iter)); // [1, 2, 3, 4, 5] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { flatten } from "@core/iterutil/async/flatten"; const iter = flatten([[1, 2], [3, 4], [5]]); -console.log(await toArray(iter)); // [1, 2, 3, 4, 5] +console.log(await Array.fromAsync(iter)); // [1, 2, 3, 4, 5] ``` ### forEach @@ -376,15 +364,14 @@ Maps an iterable with a function. import { map } from "@core/iterutil/map"; const iter = map([1, 2, 3], (value) => value * 2); -console.log([...iter]); // [2, 4, 6] +console.log(Array.from(iter)); // [2, 4, 6] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { map } from "@core/iterutil/async/map"; const iter = map([1, 2, 3], (value) => value * 2); -console.log(await toArray(iter)); // [2, 4, 6] +console.log(await Array.fromAsync(iter)); // [2, 4, 6] ``` ### pairwise @@ -395,15 +382,14 @@ Returns an iterable that pairs adjacent elements from the input iterable. import { pairwise } from "@core/iterutil/pairwise"; const iter = pairwise([1, 2, 3, 4, 5]); -console.log([...iter]); // [[1, 2], [2, 3], [3, 4], [4, 5]] +console.log(Array.from(iter)); // [[1, 2], [2, 3], [3, 4], [4, 5]] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { pairwise } from "@core/iterutil/async/pairwise"; const iter = pairwise([1, 2, 3, 4, 5]); -console.log(await toArray(iter)); // [[1, 2], [2, 3], [3, 4], [4, 5]] +console.log(await Array.fromAsync(iter)); // [[1, 2], [2, 3], [3, 4], [4, 5]] ``` ### partition @@ -436,8 +422,8 @@ Generates a range of numbers. ```ts import { range } from "@core/iterutil/range"; -console.log([...range(3)]); // [0, 1, 2] -console.log([...range(1, 6, 2)]); // [1, 3, 5] +console.log(Array.from(range(3))); // [0, 1, 2] +console.log(Array.from(range(1, 6, 2))); // [1, 3, 5] ``` ### reduce @@ -498,15 +484,14 @@ Takes the first `limit` items from the iterable. import { take } from "@core/iterutil/take"; const iter = take([1, 2, 3, 4, 5], 2); -console.log([...iter]); // [1, 2] +console.log(Array.from(iter)); // [1, 2] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { take } from "@core/iterutil/async/take"; const iter = take([1, 2, 3, 4, 5], 2); -console.log(await toArray(iter)); // [1, 2] +console.log(await Array.fromAsync(iter)); // [1, 2] ``` ### takeWhile @@ -517,48 +502,14 @@ Takes elements from the iterable while the predicate is true. import { takeWhile } from "@core/iterutil/take-while"; const iter = takeWhile([1, 2, 3, 4, 5], (value) => value < 4); -console.log([...iter]); // [1, 2, 3] +console.log(Array.from(iter)); // [1, 2, 3] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { takeWhile } from "@core/iterutil/async/take-while"; const iter = takeWhile([1, 2, 3, 4, 5], (value) => value < 4); -console.log(await toArray(iter)); // [1, 2, 3] -``` - -### toArray - -Converts an iterable to an array. - -```ts -import { toArray } from "@core/iterutil/to-array"; - -const arr = toArray(function* () { - yield 1; - yield 2; - yield 3; -}()); -console.log(arr); // [1, 2, 3] -``` - -```ts -import { toArray } from "@core/iterutil/async/to-array"; - -const arr1 = await toArray(function* () { - yield 1; - yield 2; - yield 3; -}()); -console.log(arr1); // [1, 2, 3] - -const arr2 = await toArray(async function* () { - yield 1; - yield 2; - yield 3; -}()); -console.log(arr2); // [1, 2, 3] +console.log(await Array.fromAsync(iter)); // [1, 2, 3] ``` ### toAsyncIterable @@ -566,11 +517,10 @@ console.log(arr2); // [1, 2, 3] Converts an iterable to an async iterable. ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { toAsyncIterable } from "@core/iterutil/async/to-async-iterable"; const iter = toAsyncIterable([1, 2, 3]); -console.log(await toArray(iter)); // [1, 2, 3] +console.log(await Array.fromAsync(iter)); // [1, 2, 3] ``` ### uniq @@ -581,27 +531,26 @@ Returns an iterable that yields the unique elements of the input iterable. import { uniq } from "@core/iterutil/uniq"; const iter1 = uniq([1, 2, 2, 3, 3, 3]); -console.log([...iter1]); // [1, 2, 3] +console.log(Array.from(iter1)); // [1, 2, 3] const iter2 = uniq( [1, 2, 3, 1, 2, 3, 10, 20, 30, 11, 21, 31], (v) => Math.floor(v / 10), ); -console.log([...iter2]); // [1, 10, 20, 30] +console.log(Array.from(iter2)); // [1, 10, 20, 30] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { uniq } from "@core/iterutil/async/uniq"; const iter1 = uniq([1, 2, 2, 3, 3, 3]); -console.log(await toArray(iter1)); // [1, 2, 3] +console.log(await Array.fromAsync(iter1)); // [1, 2, 3] const iter2 = uniq( [1, 2, 3, 1, 2, 3, 10, 20, 30, 11, 21, 31], (v) => Math.floor(v / 10), ); -console.log(await toArray(iter2)); // [1, 10, 20, 30] +console.log(await Array.fromAsync(iter2)); // [1, 10, 20, 30] ``` ### zip @@ -612,15 +561,14 @@ Zips multiple iterables into a single iterable. import { zip } from "@core/iterutil/zip"; const iter = zip([1, 2, 3], ["a", "b", "c"]); -console.log([...iter]); // [[1, "a"], [2, "b"], [3, "c"]] +console.log(Array.from(iter)); // [[1, "a"], [2, "b"], [3, "c"]] ``` ```ts -import { toArray } from "@core/iterutil/async/to-array"; import { zip } from "@core/iterutil/async/zip"; const iter = zip([1, 2, 3], ["a", "b", "c"]); -console.log(await toArray(iter)); // [[1, "a"], [2, "b"], [3, "c"]] +console.log(await Array.fromAsync(iter)); // [[1, "a"], [2, "b"], [3, "c"]] ``` ## License diff --git a/async/chain.ts b/async/chain.ts index cf353a8..ec4fbd4 100644 --- a/async/chain.ts +++ b/async/chain.ts @@ -6,20 +6,18 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { chain } from "@core/iterutil/async/chain"; * * const iter = chain([1, 2], [3, 4]); - * console.log(await toArray(iter)); // [1, 2, 3, 4] + * console.log(await Array.fromAsync(iter)); // [1, 2, 3, 4] * ``` * * @example With malformed iterables * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { chain } from "@core/iterutil/async/chain"; * * const iter = chain([1, 2], ["a", "b"], [true]); - * console.log(await toArray(iter)); // [1, 2, "a", "b", true] + * console.log(await Array.fromAsync(iter)); // [1, 2, "a", "b", true] * ``` */ export async function* chain< diff --git a/async/chain_test.ts b/async/chain_test.ts index 516ec54..b049758 100644 --- a/async/chain_test.ts +++ b/async/chain_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { chain } from "./chain.ts"; Deno.test("chain", async (t) => { @@ -12,14 +11,14 @@ Deno.test("chain", async (t) => { toAsyncIterable([5]), ); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with iterable", async () => { const result = chain([1, 2], [3, 4], [5]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -30,7 +29,7 @@ Deno.test("chain", async (t) => { toAsyncIterable([5]), ); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -41,7 +40,7 @@ Deno.test("chain", async (t) => { toAsyncIterable([true]), ); const expected = [1, 2, "a", "b", true]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType< IsExact> >(true); diff --git a/async/chunked.ts b/async/chunked.ts index e9cd288..61d5808 100644 --- a/async/chunked.ts +++ b/async/chunked.ts @@ -7,11 +7,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { chunked } from "@core/iterutil/async/chunked"; * * const iter = chunked([1, 2, 3, 4, 5], 2); - * console.log(await toArray(iter)); // [[1, 2], [3, 4], [5]] + * console.log(await Array.fromAsync(iter)); // [[1, 2], [3, 4], [5]] * ``` */ export async function* chunked( diff --git a/async/chunked_test.ts b/async/chunked_test.ts index 9b38a15..a95f46a 100644 --- a/async/chunked_test.ts +++ b/async/chunked_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { chunked } from "./chunked.ts"; Deno.test("chunked", async (t) => { @@ -9,28 +8,28 @@ Deno.test("chunked", async (t) => { await t.step("the length is divisible by the size", async () => { const result = chunked(toAsyncIterable([1, 2, 3, 4, 5, 6]), 2); const expected = [[1, 2], [3, 4], [5, 6]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("the length is not divisible by the size", async () => { const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 2); const expected = [[1, 2], [3, 4], [5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("the length is equal to the size", async () => { const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 5); const expected = [[1, 2, 3, 4, 5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("the length is less than the size", async () => { const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 6); const expected = [[1, 2, 3, 4, 5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -39,28 +38,28 @@ Deno.test("chunked", async (t) => { await t.step("the length is divisible by the size", async () => { const result = chunked([1, 2, 3, 4, 5, 6], 2); const expected = [[1, 2], [3, 4], [5, 6]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("the length is not divisible by the size", async () => { const result = chunked([1, 2, 3, 4, 5], 2); const expected = [[1, 2], [3, 4], [5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("the length is equal to the size", async () => { const result = chunked([1, 2, 3, 4, 5], 5); const expected = [[1, 2, 3, 4, 5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("the length is less than the size", async () => { const result = chunked([1, 2, 3, 4, 5], 6); const expected = [[1, 2, 3, 4, 5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/compact.ts b/async/compact.ts index d47a836..b0814e2 100644 --- a/async/compact.ts +++ b/async/compact.ts @@ -6,11 +6,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { compact } from "@core/iterutil/async/compact"; * * const iter = compact([1, undefined, 2, null, 3]); - * console.log(await toArray(iter)); // [1, 2, 3] + * console.log(await Array.fromAsync(iter)); // [1, 2, 3] * ``` */ export async function* compact( diff --git a/async/compact_test.ts b/async/compact_test.ts index f0204f0..38560e6 100644 --- a/async/compact_test.ts +++ b/async/compact_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { compact } from "./compact.ts"; Deno.test("compact", async (t) => { @@ -9,7 +8,7 @@ Deno.test("compact", async (t) => { await t.step("without undefined/null", async () => { const result = compact(toAsyncIterable([1, 2, 3, 4, 5])); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -26,7 +25,7 @@ Deno.test("compact", async (t) => { undefined, ])); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -35,7 +34,7 @@ Deno.test("compact", async (t) => { toAsyncIterable([null, 1, 2, null, 3, null, 4, 5, null]), ); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -44,7 +43,7 @@ Deno.test("compact", async (t) => { toAsyncIterable([undefined, 1, 2, null, 3, undefined, 4, 5, null]), ); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -53,7 +52,7 @@ Deno.test("compact", async (t) => { await t.step("without undefined/null", async () => { const result = compact([1, 2, 3, 4, 5]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -70,21 +69,21 @@ Deno.test("compact", async (t) => { undefined, ]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with null", async () => { const result = compact([null, 1, 2, null, 3, null, 4, 5, null]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with undefined/null", async () => { const result = compact([undefined, 1, 2, null, 3, undefined, 4, 5, null]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/compress.ts b/async/compress.ts index 09378ae..4ef02f1 100644 --- a/async/compress.ts +++ b/async/compress.ts @@ -7,11 +7,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { compress } from "@core/iterutil/async/compress"; * * const iter = compress([1, 2, 3, 4, 5], [true, false, true, false, true]); - * console.log(await toArray(iter)); // [1, 3, 5] + * console.log(await Array.fromAsync(iter)); // [1, 3, 5] * ``` */ export async function* compress( diff --git a/async/compress_test.ts b/async/compress_test.ts index b368caa..018b9cc 100644 --- a/async/compress_test.ts +++ b/async/compress_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { compress } from "./compress.ts"; Deno.test("compress", async (t) => { @@ -12,7 +11,7 @@ Deno.test("compress", async (t) => { toAsyncIterable([true, false, true, false, true]), ); const expected = [1, 3, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -22,7 +21,7 @@ Deno.test("compress", async (t) => { toAsyncIterable([true, false, true]), ); const expected = [1, 3]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -32,7 +31,7 @@ Deno.test("compress", async (t) => { toAsyncIterable([true, false, true, false, true]), ); const expected = [1, 3]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -47,21 +46,21 @@ Deno.test("compress", async (t) => { true, ]); const expected = [1, 3, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("the iterable is larger than the selectors", async () => { const result = compress([1, 2, 3, 4, 5], [true, false, true]); const expected = [1, 3]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("the iterable is smaller than the selector", async () => { const result = compress([1, 2, 3], [true, false, true, false, true]); const expected = [1, 3]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/cycle.ts b/async/cycle.ts index 66a4867..8a92714 100644 --- a/async/cycle.ts +++ b/async/cycle.ts @@ -6,12 +6,11 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { cycle } from "@core/iterutil/async/cycle"; * import { take } from "@core/iterutil/async/take"; * * const iter = cycle([1, 2, 3]); - * console.log(await toArray(take(iter, 5))); // [1, 2, 3, 1, 2] + * console.log(await Array.fromAsync(take(iter, 5))); // [1, 2, 3, 1, 2] * ``` */ export async function* cycle( diff --git a/async/cycle_test.ts b/async/cycle_test.ts index 069deab..6c412ca 100644 --- a/async/cycle_test.ts +++ b/async/cycle_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { take } from "./take.ts"; import { cycle } from "./cycle.ts"; @@ -10,21 +9,21 @@ Deno.test("cycle", async (t) => { await t.step("with non empty iterable", async () => { const result = cycle(toAsyncIterable([0, 1, 2])); const expected = [0, 1, 2, 0, 1]; - assertEquals(await toArray(take(result, 5)), expected); + assertEquals(await Array.fromAsync(take(result, 5)), expected); assertType>>(true); }); await t.step("with single value iterable", async () => { const result = cycle(toAsyncIterable([0])); const expected = [0, 0, 0, 0, 0]; - assertEquals(await toArray(take(result, 5)), expected); + assertEquals(await Array.fromAsync(take(result, 5)), expected); assertType>>(true); }); await t.step("with empty iterable", async () => { const result = cycle(toAsyncIterable([] as number[])); const expected: number[] = []; - assertEquals(await toArray(take(result, 5)), expected); + assertEquals(await Array.fromAsync(take(result, 5)), expected); assertType>>(true); }); }); @@ -33,21 +32,21 @@ Deno.test("cycle", async (t) => { await t.step("with non empty iterable", async () => { const result = cycle([0, 1, 2]); const expected = [0, 1, 2, 0, 1]; - assertEquals(await toArray(take(result, 5)), expected); + assertEquals(await Array.fromAsync(take(result, 5)), expected); assertType>>(true); }); await t.step("with single value iterable", async () => { const result = cycle([0]); const expected = [0, 0, 0, 0, 0]; - assertEquals(await toArray(take(result, 5)), expected); + assertEquals(await Array.fromAsync(take(result, 5)), expected); assertType>>(true); }); await t.step("with empty iterable", async () => { const result = cycle([] as number[]); const expected: number[] = []; - assertEquals(await toArray(take(result, 5)), expected); + assertEquals(await Array.fromAsync(take(result, 5)), expected); assertType>>(true); }); }); diff --git a/async/drop.ts b/async/drop.ts index 20750e9..99af176 100644 --- a/async/drop.ts +++ b/async/drop.ts @@ -8,11 +8,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { drop } from "@core/iterutil/async/drop"; * * const iter = drop([1, 2, 3, 4, 5], 2); - * console.log(await toArray(iter)); // [3, 4, 5] + * console.log(await Array.fromAsync(iter)); // [3, 4, 5] * ``` */ export function drop( diff --git a/async/drop_test.ts b/async/drop_test.ts index 680faa5..899fd16 100644 --- a/async/drop_test.ts +++ b/async/drop_test.ts @@ -1,7 +1,6 @@ import { assertEquals, assertThrows } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { drop, DropLimitError } from "./drop.ts"; Deno.test("drop", async (t) => { @@ -9,7 +8,7 @@ Deno.test("drop", async (t) => { await t.step("with positive limit", async () => { const result = drop(toAsyncIterable([0, 1, 2, 3, 4]), 2); const expected = [2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -25,7 +24,7 @@ Deno.test("drop", async (t) => { await t.step("with 0 limit", async () => { const result = drop(toAsyncIterable([0, 1, 2, 3, 4]), 0); const expected = [0, 1, 2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -34,7 +33,7 @@ Deno.test("drop", async (t) => { await t.step("with positive limit", async () => { const result = drop([0, 1, 2, 3, 4], 2); const expected = [2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -50,7 +49,7 @@ Deno.test("drop", async (t) => { await t.step("with 0 limit", async () => { const result = drop([0, 1, 2, 3, 4], 0); const expected = [0, 1, 2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/drop_while.ts b/async/drop_while.ts index efc04c0..56608f7 100644 --- a/async/drop_while.ts +++ b/async/drop_while.ts @@ -10,11 +10,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { dropWhile } from "@core/iterutil/async/drop-while"; * * const iter = dropWhile([1, 2, 3, 4, 5], (x) => x < 3); - * console.log(await toArray(iter)); // [3, 4, 5] + * console.log(await Array.fromAsync(iter)); // [3, 4, 5] * ``` */ export async function* dropWhile( diff --git a/async/drop_while_test.ts b/async/drop_while_test.ts index b16b19a..062fcd0 100644 --- a/async/drop_while_test.ts +++ b/async/drop_while_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { dropWhile } from "./drop_while.ts"; Deno.test("dropWhile", async (t) => { @@ -9,7 +8,7 @@ Deno.test("dropWhile", async (t) => { await t.step("with some true", async () => { const result = dropWhile(toAsyncIterable([0, 1, 2, 3, 4]), (v) => v < 2); const expected = [2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -19,21 +18,21 @@ Deno.test("dropWhile", async (t) => { (v) => Promise.resolve(v < 2), ); const expected = [2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with all true", async () => { const result = dropWhile(toAsyncIterable([0, 1, 2, 3, 4]), () => true); const expected: number[] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with all false", async () => { const result = dropWhile(toAsyncIterable([0, 1, 2, 3, 4]), () => false); const expected = [0, 1, 2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -42,28 +41,28 @@ Deno.test("dropWhile", async (t) => { await t.step("with some true", async () => { const result = dropWhile([0, 1, 2, 3, 4], (v) => v < 2); const expected = [2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with some promise true", async () => { const result = dropWhile([0, 1, 2, 3, 4], (v) => Promise.resolve(v < 2)); const expected = [2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with all true", async () => { const result = dropWhile([0, 1, 2, 3, 4], () => true); const expected: number[] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with all false", async () => { const result = dropWhile([0, 1, 2, 3, 4], () => false); const expected = [0, 1, 2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/enumerate.ts b/async/enumerate.ts index 13aedb5..a64293e 100644 --- a/async/enumerate.ts +++ b/async/enumerate.ts @@ -7,11 +7,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { enumerate } from "@core/iterutil/async/enumerate"; * * const iter = enumerate(["a", "b", "c"]); - * console.log(await toArray(iter)); // [[0, "a"], [1, "b"], [2, "c"]] + * console.log(await Array.fromAsync(iter)); // [[0, "a"], [1, "b"], [2, "c"]] * ``` */ export async function* enumerate( diff --git a/async/enumerate_test.ts b/async/enumerate_test.ts index dd8428e..0bba424 100644 --- a/async/enumerate_test.ts +++ b/async/enumerate_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { enumerate } from "./enumerate.ts"; Deno.test("enumerate", async (t) => { @@ -9,21 +8,21 @@ Deno.test("enumerate", async (t) => { await t.step("default", async () => { const result = enumerate(toAsyncIterable([0, 1, 2])); const expected = [[0, 0], [1, 1], [2, 2]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with start", async () => { const result = enumerate(toAsyncIterable([0, 1, 2]), 1); const expected = [[1, 0], [2, 1], [3, 2]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with start/step", async () => { const result = enumerate(toAsyncIterable([0, 1, 2]), 1, 2); const expected = [[1, 0], [3, 1], [5, 2]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -32,21 +31,21 @@ Deno.test("enumerate", async (t) => { await t.step("default", async () => { const result = enumerate([0, 1, 2]); const expected = [[0, 0], [1, 1], [2, 2]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with start", async () => { const result = enumerate([0, 1, 2], 1); const expected = [[1, 0], [2, 1], [3, 2]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with start/step", async () => { const result = enumerate([0, 1, 2], 1, 2); const expected = [[1, 0], [3, 1], [5, 2]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/every.ts b/async/every.ts index 7ffdd86..389da48 100644 --- a/async/every.ts +++ b/async/every.ts @@ -7,7 +7,6 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { every } from "@core/iterutil/async/every"; * * console.log(await every([1, 2, 3], (value) => value > 0)); // true diff --git a/async/filter.ts b/async/filter.ts index 9ad9b58..9b27b68 100644 --- a/async/filter.ts +++ b/async/filter.ts @@ -7,11 +7,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { filter } from "@core/iterutil/async/filter"; * * const iter = filter([1, 2, 3, 4, 5], (value) => value % 2 === 0); - * console.log(await toArray(iter)); // [2, 4] + * console.log(await Array.fromAsync(iter)); // [2, 4] * ``` */ export async function* filter( diff --git a/async/filter_test.ts b/async/filter_test.ts index 647cab7..9cd946a 100644 --- a/async/filter_test.ts +++ b/async/filter_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { filter } from "./filter.ts"; Deno.test("filter", async (t) => { @@ -18,7 +17,7 @@ Deno.test("filter", async (t) => { }, ); const expected = [2, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -36,7 +35,7 @@ Deno.test("filter", async (t) => { }, ); const expected = [2, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -53,7 +52,7 @@ Deno.test("filter", async (t) => { return value % 2 === 0; }); const expected = [2, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -68,7 +67,7 @@ Deno.test("filter", async (t) => { return Promise.resolve(value % 2 === 0); }); const expected = [2, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); diff --git a/async/flat_map.ts b/async/flat_map.ts index 15c3e0b..65097e7 100644 --- a/async/flat_map.ts +++ b/async/flat_map.ts @@ -7,11 +7,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { flatMap } from "@core/iterutil/async/flat-map"; * * const iter = flatMap([1, 2, 3], (value) => [value, value]); - * console.log(await toArray(iter)); // [1, 1, 2, 2, 3, 3] + * console.log(await Array.fromAsync(iter)); // [1, 1, 2, 2, 3, 3] * ``` */ export async function* flatMap( diff --git a/async/flat_map_test.ts b/async/flat_map_test.ts index 9ad312d..b626bd1 100644 --- a/async/flat_map_test.ts +++ b/async/flat_map_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { flatMap } from "./flat_map.ts"; Deno.test("flatMap", async (t) => { @@ -18,7 +17,7 @@ Deno.test("flatMap", async (t) => { }, ); const expected = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -36,7 +35,7 @@ Deno.test("flatMap", async (t) => { }, ); const expected = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -54,7 +53,7 @@ Deno.test("flatMap", async (t) => { }, ); const expected = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -63,7 +62,7 @@ Deno.test("flatMap", async (t) => { await t.step("multi nest", async () => { const result = flatMap(toAsyncIterable([1, 2, 3, 4, 5]), (v) => [[v, v]]); const expected = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -78,7 +77,7 @@ Deno.test("flatMap", async (t) => { return [value, value]; }); const expected = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -93,7 +92,7 @@ Deno.test("flatMap", async (t) => { return toAsyncIterable([value, value]); }); const expected = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -108,7 +107,7 @@ Deno.test("flatMap", async (t) => { return Promise.resolve([value, value]); }); const expected = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -117,7 +116,7 @@ Deno.test("flatMap", async (t) => { await t.step("multi nest", async () => { const result = flatMap([1, 2, 3, 4, 5], (v) => [[v, v]]); const expected = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/flatten.ts b/async/flatten.ts index d33fe0e..0c627a0 100644 --- a/async/flatten.ts +++ b/async/flatten.ts @@ -6,11 +6,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { flatten } from "@core/iterutil/async/flatten"; * * const iter = flatten([[1, 2], [3, 4], [5]]); - * console.log(await toArray(iter)); // [1, 2, 3, 4, 5] + * console.log(await Array.fromAsync(iter)); // [1, 2, 3, 4, 5] * ``` */ export async function* flatten( diff --git a/async/flatten_test.ts b/async/flatten_test.ts index 376b0ef..98e94dd 100644 --- a/async/flatten_test.ts +++ b/async/flatten_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { flatten } from "./flatten.ts"; Deno.test("flatten", async (t) => { @@ -9,7 +8,7 @@ Deno.test("flatten", async (t) => { await t.step("single nest", async () => { const result = flatten([[1, 2], [3, 4], [5]]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -20,7 +19,7 @@ Deno.test("flatten", async (t) => { toAsyncIterable([5]), ]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -31,14 +30,14 @@ Deno.test("flatten", async (t) => { Promise.resolve([5]), ]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("multi nest", async () => { const result = flatten([[[1, 2], [3, 4]], [[5]]]); const expected = [[1, 2], [3, 4], [5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/iter_test.ts b/async/iter_test.ts index b995602..54c4731 100644 --- a/async/iter_test.ts +++ b/async/iter_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { iter } from "./iter.ts"; Deno.test("iter", async (t) => { @@ -9,7 +8,7 @@ Deno.test("iter", async (t) => { const it = iter(toAsyncIterable([0, 1, 2, 3, 4, 5])); assertEquals(await it.next(), { done: false, value: 0 }); assertEquals(await it.next(), { done: false, value: 1 }); - assertEquals(await toArray(it), [2, 3, 4, 5]); + assertEquals(await Array.fromAsync(it), [2, 3, 4, 5]); assertType>>(true); }); @@ -17,7 +16,7 @@ Deno.test("iter", async (t) => { const it = iter([0, 1, 2, 3, 4, 5]); assertEquals(await it.next(), { done: false, value: 0 }); assertEquals(await it.next(), { done: false, value: 1 }); - assertEquals(await toArray(it), [2, 3, 4, 5]); + assertEquals(await Array.fromAsync(it), [2, 3, 4, 5]); assertType>>(true); }); }); diff --git a/async/map.ts b/async/map.ts index fc1f2a5..95e869a 100644 --- a/async/map.ts +++ b/async/map.ts @@ -7,11 +7,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { map } from "@core/iterutil/async/map"; * * const iter = map([1, 2, 3], (value) => value * 2); - * console.log(await toArray(iter)); // [2, 4, 6] + * console.log(await Array.fromAsync(iter)); // [2, 4, 6] * ``` */ export async function* map( diff --git a/async/map_test.ts b/async/map_test.ts index 12a8b02..a8362ca 100644 --- a/async/map_test.ts +++ b/async/map_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { map } from "./map.ts"; Deno.test("map", async (t) => { @@ -14,7 +13,7 @@ Deno.test("map", async (t) => { return value * 2; }); const expected = [2, 4, 6, 8, 10]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -29,7 +28,7 @@ Deno.test("map", async (t) => { return Promise.resolve(value * 2); }); const expected = [2, 4, 6, 8, 10]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -44,7 +43,7 @@ Deno.test("map", async (t) => { return value * 2; }); const expected = [2, 4, 6, 8, 10]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -59,7 +58,7 @@ Deno.test("map", async (t) => { return Promise.resolve(value * 2); }); const expected = [2, 4, 6, 8, 10]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); diff --git a/async/mod.ts b/async/mod.ts index d99226c..67e6998 100644 --- a/async/mod.ts +++ b/async/mod.ts @@ -22,7 +22,6 @@ export * from "./reduce.ts"; export * from "./some.ts"; export * from "./take.ts"; export * from "./take_while.ts"; -export * from "./to_array.ts"; export * from "./to_async_iterable.ts"; export * from "./uniq.ts"; export * from "./zip.ts"; diff --git a/async/pairwise.ts b/async/pairwise.ts index 85499d3..e79d767 100644 --- a/async/pairwise.ts +++ b/async/pairwise.ts @@ -8,11 +8,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { pairwise } from "@core/iterutil/async/pairwise"; * * const iter = pairwise([1, 2, 3, 4, 5]); - * console.log(await toArray(iter)); // [[1, 2], [2, 3], [3, 4], [4, 5]] + * console.log(await Array.fromAsync(iter)); // [[1, 2], [2, 3], [3, 4], [4, 5]] * ``` */ export async function* pairwise( diff --git a/async/pairwise_test.ts b/async/pairwise_test.ts index 892d28c..4372693 100644 --- a/async/pairwise_test.ts +++ b/async/pairwise_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { pairwise } from "./pairwise.ts"; Deno.test("pairwise", async (t) => { @@ -9,21 +8,21 @@ Deno.test("pairwise", async (t) => { await t.step("with non empty iterable", async () => { const result = pairwise(toAsyncIterable([1, 2, 3, 4, 5])); const expected = [[1, 2], [2, 3], [3, 4], [4, 5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with single value iterable", async () => { const result = pairwise(toAsyncIterable([1])); const expected: number[][] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with empty iterable", async () => { const result = pairwise(toAsyncIterable([] as number[])); const expected: number[][] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -32,21 +31,21 @@ Deno.test("pairwise", async (t) => { await t.step("with non empty iterable", async () => { const result = pairwise([1, 2, 3, 4, 5]); const expected = [[1, 2], [2, 3], [3, 4], [4, 5]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with single value iterable", async () => { const result = pairwise([1]); const expected: number[][] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with empty iterable", async () => { const result = pairwise([] as number[]); const expected: number[][] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/take.ts b/async/take.ts index f2cca28..118b5ab 100644 --- a/async/take.ts +++ b/async/take.ts @@ -8,11 +8,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { take } from "@core/iterutil/async/take"; * * const iter = take([1, 2, 3, 4, 5], 2); - * console.log(await toArray(iter)); // [1, 2] + * console.log(await Array.fromAsync(iter)); // [1, 2] * ``` */ export function take( diff --git a/async/take_test.ts b/async/take_test.ts index d181ec8..d9a273d 100644 --- a/async/take_test.ts +++ b/async/take_test.ts @@ -1,7 +1,6 @@ import { assertEquals, assertThrows } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { take, TakeLimitError } from "./take.ts"; Deno.test("take", async (t) => { @@ -9,7 +8,7 @@ Deno.test("take", async (t) => { await t.step("with positive limit", async () => { const result = take(toAsyncIterable([0, 1, 2, 3, 4]), 2); const expected = [0, 1]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -25,7 +24,7 @@ Deno.test("take", async (t) => { await t.step("with 0 limit", async () => { const result = take(toAsyncIterable([0, 1, 2, 3, 4]), 0); const expected: number[] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -34,7 +33,7 @@ Deno.test("take", async (t) => { await t.step("with positive limit", async () => { const result = take([0, 1, 2, 3, 4], 2); const expected = [0, 1]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -50,7 +49,7 @@ Deno.test("take", async (t) => { await t.step("with 0 limit", async () => { const result = take([0, 1, 2, 3, 4], 0); const expected: number[] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/take_while.ts b/async/take_while.ts index 5ee4bc6..3871bc8 100644 --- a/async/take_while.ts +++ b/async/take_while.ts @@ -7,11 +7,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { takeWhile } from "@core/iterutil/async/take-while"; * * const iter = takeWhile([1, 2, 3, 4, 5], (value) => value < 4); - * console.log(await toArray(iter)); // [1, 2, 3] + * console.log(await Array.fromAsync(iter)); // [1, 2, 3] * ``` */ export async function* takeWhile( diff --git a/async/take_while_test.ts b/async/take_while_test.ts index 7377a83..15df815 100644 --- a/async/take_while_test.ts +++ b/async/take_while_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { takeWhile } from "./take_while.ts"; Deno.test("takeWhile", async (t) => { @@ -9,7 +8,7 @@ Deno.test("takeWhile", async (t) => { await t.step("with some true", async () => { const result = takeWhile(toAsyncIterable([0, 1, 2, 3, 4]), (v) => v < 2); const expected = [0, 1]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -19,21 +18,21 @@ Deno.test("takeWhile", async (t) => { (v) => Promise.resolve(v < 2), ); const expected = [0, 1]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with all true", async () => { const result = takeWhile(toAsyncIterable([0, 1, 2, 3, 4]), () => true); const expected = [0, 1, 2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with all false", async () => { const result = takeWhile(toAsyncIterable([0, 1, 2, 3, 4]), () => false); const expected: number[] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -42,28 +41,28 @@ Deno.test("takeWhile", async (t) => { await t.step("with some true", async () => { const result = takeWhile([0, 1, 2, 3, 4], (v) => v < 2); const expected = [0, 1]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with some promise true", async () => { const result = takeWhile([0, 1, 2, 3, 4], (v) => Promise.resolve(v < 2)); const expected = [0, 1]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with all true", async () => { const result = takeWhile([0, 1, 2, 3, 4], () => true); const expected = [0, 1, 2, 3, 4]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("with all false", async () => { const result = takeWhile([0, 1, 2, 3, 4], () => false); const expected: number[] = []; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/to_array.ts b/async/to_array.ts deleted file mode 100644 index 3d4adf8..0000000 --- a/async/to_array.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Converts an iterable or async iterable to an array. - * - * @param iterable The iterable or async iterable to convert. - * @returns The array. - * - * @example - * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; - * - * const arr1 = await toArray(function*() { - * yield 1; - * yield 2; - * yield 3; - * }()); - * console.log(arr1); // [1, 2, 3] - * - * const arr2 = await toArray(async function*() { - * yield 1; - * yield 2; - * yield 3; - * }()); - * console.log(arr2); // [1, 2, 3] - * ``` - */ -export async function toArray( - iterable: Iterable | AsyncIterable, -): Promise { - const result: T[] = []; - for await (const value of iterable) { - result.push(value); - } - return result; -} diff --git a/async/to_array_test.ts b/async/to_array_test.ts deleted file mode 100644 index a86807c..0000000 --- a/async/to_array_test.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { assertEquals } from "@std/assert"; -import { assertType, type IsExact } from "@std/testing/types"; -import { toArray } from "./to_array.ts"; - -Deno.test("toArray", async (t) => { - await t.step("with async iterable", async () => { - const result = await toArray(async function* () { - yield 1 as number; - yield 2 as number; - yield 3 as number; - }()); - const expected = [1, 2, 3]; - assertEquals(result, expected); - assertType>(true); - }); - - await t.step("with iterable", async () => { - const result = await toArray(function* () { - yield 1 as number; - yield 2 as number; - yield 3 as number; - }()); - const expected = [1, 2, 3]; - assertEquals(result, expected); - assertType>(true); - }); -}); diff --git a/async/to_async_iterable.ts b/async/to_async_iterable.ts index a195860..ee93866 100644 --- a/async/to_async_iterable.ts +++ b/async/to_async_iterable.ts @@ -6,11 +6,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { toAsyncIterable } from "@core/iterutil/async/to-async-iterable"; * * const iter = toAsyncIterable([1, 2, 3]); - * console.log(await toArray(iter)); // [1, 2, 3] + * console.log(await Array.fromAsync(iter)); // [1, 2, 3] * ``` */ export async function* toAsyncIterable( diff --git a/async/to_async_iterable_test.ts b/async/to_async_iterable_test.ts index a82f3d4..96c31ea 100644 --- a/async/to_async_iterable_test.ts +++ b/async/to_async_iterable_test.ts @@ -1,11 +1,10 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; -import { toArray } from "./to_array.ts"; import { toAsyncIterable } from "./to_async_iterable.ts"; Deno.test("toAsyncIterable", async () => { const result = toAsyncIterable([1, 2, 3, 4, 5]); const expected = [1, 2, 3, 4, 5]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); diff --git a/async/uniq.ts b/async/uniq.ts index bc42f22..93dcf4b 100644 --- a/async/uniq.ts +++ b/async/uniq.ts @@ -7,23 +7,21 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { uniq } from "@core/iterutil/async/uniq"; * * const iter = uniq([1, 2, 2, 3, 3, 3]); - * console.log(await toArray(iter)); // [1, 2, 3] + * console.log(await Array.fromAsync(iter)); // [1, 2, 3] * ``` * * @example With identify function * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { uniq } from "@core/iterutil/async/uniq"; * * const iter = uniq( * [1, 2, 3, 1, 2, 3, 10, 20, 30, 11, 21, 31], * (v) => Math.floor(v / 10), * ); - * console.log(await toArray(iter)); // [1, 10, 20, 30] + * console.log(await Array.fromAsync(iter)); // [1, 10, 20, 30] * ``` */ export async function* uniq( diff --git a/async/uniq_test.ts b/async/uniq_test.ts index 4ed8498..055c752 100644 --- a/async/uniq_test.ts +++ b/async/uniq_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { uniq } from "./uniq.ts"; Deno.test("uniq", async (t) => { @@ -11,7 +10,7 @@ Deno.test("uniq", async (t) => { toAsyncIterable([1, 2, 3, 1, 2, 3, 10, 20, 30, 11, 21, 31]), ); const expected = [1, 2, 3, 10, 20, 30, 11, 21, 31]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -21,7 +20,7 @@ Deno.test("uniq", async (t) => { (v) => Math.floor(v / 10), ); const expected = [1, 10, 20, 30]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -31,7 +30,7 @@ Deno.test("uniq", async (t) => { (v) => Promise.resolve(Math.floor(v / 10)), ); const expected = [1, 10, 20, 30]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); @@ -40,7 +39,7 @@ Deno.test("uniq", async (t) => { await t.step("default", async () => { const result = uniq([1, 2, 3, 1, 2, 3, 10, 20, 30, 11, 21, 31]); const expected = [1, 2, 3, 10, 20, 30, 11, 21, 31]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -50,7 +49,7 @@ Deno.test("uniq", async (t) => { (v) => Math.floor(v / 10), ); const expected = [1, 10, 20, 30]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -60,7 +59,7 @@ Deno.test("uniq", async (t) => { (v) => Promise.resolve(Math.floor(v / 10)), ); const expected = [1, 10, 20, 30]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); }); diff --git a/async/zip.ts b/async/zip.ts index 8864afb..b6fb2c1 100644 --- a/async/zip.ts +++ b/async/zip.ts @@ -6,11 +6,10 @@ * * @example * ```ts - * import { toArray } from "@core/iterutil/async/to-array"; * import { zip } from "@core/iterutil/async/zip"; * * const iter = zip([1, 2, 3], ["a", "b", "c"]); - * console.log(await toArray(iter)); // [[1, "a"], [2, "b"], [3, "c"]] + * console.log(await Array.fromAsync(iter)); // [[1, "a"], [2, "b"], [3, "c"]] * ``` */ export async function* zip< diff --git a/async/zip_test.ts b/async/zip_test.ts index beb936d..498faa4 100644 --- a/async/zip_test.ts +++ b/async/zip_test.ts @@ -1,7 +1,6 @@ import { assertEquals } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; -import { toArray } from "./to_array.ts"; import { zip } from "./zip.ts"; Deno.test("zip", async (t) => { @@ -12,7 +11,7 @@ Deno.test("zip", async (t) => { toAsyncIterable(["a", "b", "c"]), ); const expected = [[1, "a"], [2, "b"], [3, "c"]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -22,7 +21,7 @@ Deno.test("zip", async (t) => { toAsyncIterable(["a", "b", "c"]), ); const expected = [[1, "a"], [2, "b"], [3, "c"]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); @@ -33,7 +32,7 @@ Deno.test("zip", async (t) => { [true, false, true], ); const expected = [[1, "a", true], [2, "b", false], [3, "c", true]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType< IsExact> >( @@ -48,7 +47,7 @@ Deno.test("zip", async (t) => { [true, false, true], ); const expected = [[1, "a", true], [2, "b", false], [3, "c", true]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType< IsExact> >( @@ -61,21 +60,21 @@ Deno.test("zip", async (t) => { await t.step("zip with two iterables", async () => { const result = zip([1, 2, 3], ["a", "b", "c"]); const expected = [[1, "a"], [2, "b"], [3, "c"]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("zip with two in-balanced iterables", async () => { const result = zip([1, 2, 3, 4, 5], ["a", "b", "c"]); const expected = [[1, "a"], [2, "b"], [3, "c"]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType>>(true); }); await t.step("zip with three iterables", async () => { const result = zip([1, 2, 3], ["a", "b", "c"], [true, false, true]); const expected = [[1, "a", true], [2, "b", false], [3, "c", true]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType< IsExact> >( @@ -86,7 +85,7 @@ Deno.test("zip", async (t) => { await t.step("zip with three in-balanced iterables", async () => { const result = zip([1, 2, 3, 4, 5], ["a", "b", "c"], [true, false, true]); const expected = [[1, "a", true], [2, "b", false], [3, "c", true]]; - assertEquals(await toArray(result), expected); + assertEquals(await Array.fromAsync(result), expected); assertType< IsExact> >( diff --git a/chain.ts b/chain.ts index 8a1cb5b..9e8fac1 100644 --- a/chain.ts +++ b/chain.ts @@ -9,7 +9,7 @@ * import { chain } from "@core/iterutil/chain"; * * const iter = chain([1, 2], [3, 4]); - * console.log([...iter]); // [1, 2, 3, 4] + * console.log(Array.from(iter)); // [1, 2, 3, 4] * ``` * * @example With malformed iterables @@ -17,7 +17,7 @@ * import { chain } from "@core/iterutil/chain"; * * const iter = chain([1, 2], ["a", "b"], [true]); - * console.log([...iter]); // [1, 2, "a", "b", true] + * console.log(Array.from(iter)); // [1, 2, "a", "b", true] * ``` */ export function* chain[]>( diff --git a/chain_test.ts b/chain_test.ts index ed74e5b..d361a12 100644 --- a/chain_test.ts +++ b/chain_test.ts @@ -6,14 +6,14 @@ Deno.test("chain", async (t) => { await t.step("uniform iterables", () => { const result = chain([1, 2], [3, 4], [5]); const expected = [1, 2, 3, 4, 5]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("malform iterables", () => { const result = chain([1, 2], ["a", "b"], [true]); const expected = [1, 2, "a", "b", true]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>( true, ); diff --git a/chunked.ts b/chunked.ts index cc156bd..23c2294 100644 --- a/chunked.ts +++ b/chunked.ts @@ -10,7 +10,7 @@ * import { chunked } from "@core/iterutil/chunked"; * * const iter = chunked([1, 2, 3, 4, 5], 2); - * console.log([...iter]); // [[1, 2], [3, 4], [5]] + * console.log(Array.from(iter)); // [[1, 2], [3, 4], [5]] * ``` */ export function* chunked( diff --git a/chunked_test.ts b/chunked_test.ts index 9b2b83a..0c096e5 100644 --- a/chunked_test.ts +++ b/chunked_test.ts @@ -6,28 +6,28 @@ Deno.test("chunked", async (t) => { await t.step("the length is divisible by the size", () => { const result = chunked([1, 2, 3, 4, 5, 6], 2); const expected = [[1, 2], [3, 4], [5, 6]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("the length is not divisible by the size", () => { const result = chunked([1, 2, 3, 4, 5], 2); const expected = [[1, 2], [3, 4], [5]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("the length is equal to the size", () => { const result = chunked([1, 2, 3, 4, 5], 5); const expected = [[1, 2, 3, 4, 5]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("the length is less than the size", () => { const result = chunked([1, 2, 3, 4, 5], 6); const expected = [[1, 2, 3, 4, 5]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/compact.ts b/compact.ts index a61c2fb..ac42adc 100644 --- a/compact.ts +++ b/compact.ts @@ -9,7 +9,7 @@ * import { compact } from "@core/iterutil/compact"; * * const iter = compact([1, undefined, 2, null, 3]); - * console.log([...iter]); // [1, 2, 3] + * console.log(Array.from(iter)); // [1, 2, 3] * ``` */ export function* compact(iterable: Iterable): Iterable> { diff --git a/compact_test.ts b/compact_test.ts index df00c68..db11f93 100644 --- a/compact_test.ts +++ b/compact_test.ts @@ -6,7 +6,7 @@ Deno.test("compact", async (t) => { await t.step("without undefined/null", () => { const result = compact([1, 2, 3, 4, 5]); const expected = [1, 2, 3, 4, 5]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); @@ -23,21 +23,21 @@ Deno.test("compact", async (t) => { undefined, ]); const expected = [1, 2, 3, 4, 5]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with null", () => { const result = compact([null, 1, 2, null, 3, null, 4, 5, null]); const expected = [1, 2, 3, 4, 5]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with undefined/null", () => { const result = compact([undefined, 1, 2, null, 3, undefined, 4, 5, null]); const expected = [1, 2, 3, 4, 5]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/compress.ts b/compress.ts index 28f3d37..2549739 100644 --- a/compress.ts +++ b/compress.ts @@ -10,7 +10,7 @@ * import { compress } from "@core/iterutil/compress"; * * const iter = compress([1, 2, 3, 4, 5], [true, false, true, false, true]); - * console.log([...iter]); // [1, 3, 5] + * console.log(Array.from(iter)); // [1, 3, 5] * ``` */ export function* compress( diff --git a/compress_test.ts b/compress_test.ts index 1a97c20..8ac5986 100644 --- a/compress_test.ts +++ b/compress_test.ts @@ -6,21 +6,21 @@ Deno.test("compress", async (t) => { await t.step("the iterable and the selectors are same length", () => { const result = compress([1, 2, 3, 4, 5], [true, false, true, false, true]); const expected = [1, 3, 5]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("the iterable is larger than the selectors", () => { const result = compress([1, 2, 3, 4, 5], [true, false, true]); const expected = [1, 3]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("the iterable is smaller than the selector", () => { const result = compress([1, 2, 3], [true, false, true, false, true]); const expected = [1, 3]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/count.ts b/count.ts index e01d18f..d724fc8 100644 --- a/count.ts +++ b/count.ts @@ -11,7 +11,7 @@ * import { take } from "@core/iterutil/take"; * * const iter = count(1, 2); - * console.log([...take(iter, 5)]); // [1, 3, 5, 7, 9] + * console.log(Array.from(take(iter, 5))); // [1, 3, 5, 7, 9] * ``` */ export function* count(start: number = 0, step: number = 1): Iterable { diff --git a/count_test.ts b/count_test.ts index c31ff2d..cecdf48 100644 --- a/count_test.ts +++ b/count_test.ts @@ -7,21 +7,21 @@ Deno.test("count", async (t) => { await t.step("default", () => { const result = count(); const expected = [0, 1, 2]; - assertEquals([...take(result, 3)], expected); + assertEquals(Array.from(take(result, 3)), expected); assertType>>(true); }); await t.step("with start", () => { const result = count(2); const expected = [2, 3, 4]; - assertEquals([...take(result, 3)], expected); + assertEquals(Array.from(take(result, 3)), expected); assertType>>(true); }); await t.step("with start/step", () => { const result = count(2, 2); const expected = [2, 4, 6]; - assertEquals([...take(result, 3)], expected); + assertEquals(Array.from(take(result, 3)), expected); assertType>>(true); }); }); diff --git a/cycle.ts b/cycle.ts index d9b3b33..2921a9c 100644 --- a/cycle.ts +++ b/cycle.ts @@ -10,11 +10,11 @@ * import { take } from "@core/iterutil/take"; * * const iter = cycle([1, 2, 3]); - * console.log([...take(iter, 5)]); // [1, 2, 3, 1, 2] + * console.log(Array.from(take(iter, 5))); // [1, 2, 3, 1, 2] * ``` */ export function* cycle(iterable: Iterable): Iterable { - const array = [...iterable]; + const array = Array.from(iterable); if (array.length === 0) { return; } diff --git a/cycle_test.ts b/cycle_test.ts index 356de67..784d2a2 100644 --- a/cycle_test.ts +++ b/cycle_test.ts @@ -7,21 +7,21 @@ Deno.test("cycle", async (t) => { await t.step("with non empty iterable", () => { const result = cycle([0, 1, 2]); const expected = [0, 1, 2, 0, 1]; - assertEquals([...take(result, 5)], expected); + assertEquals(Array.from(take(result, 5)), expected); assertType>>(true); }); await t.step("with single value iterable", () => { const result = cycle([0]); const expected = [0, 0, 0, 0, 0]; - assertEquals([...take(result, 5)], expected); + assertEquals(Array.from(take(result, 5)), expected); assertType>>(true); }); await t.step("with empty iterable", () => { const result = cycle([] as number[]); const expected: number[] = []; - assertEquals([...take(result, 5)], expected); + assertEquals(Array.from(take(result, 5)), expected); assertType>>(true); }); }); diff --git a/deno.jsonc b/deno.jsonc index b4c6f8e..5af50ed 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -28,7 +28,6 @@ "./async/some": "./async/some.ts", "./async/take": "./async/take.ts", "./async/take-while": "./async/take_while.ts", - "./async/to-array": "./async/to_array.ts", "./async/to-async-iterable": "./async/to_async_iterable.ts", "./async/uniq": "./async/uniq.ts", "./async/zip": "./async/zip.ts", @@ -58,7 +57,6 @@ "./some": "./some.ts", "./take": "./take.ts", "./take-while": "./take_while.ts", - "./to-array": "./to_array.ts", "./uniq": "./uniq.ts", "./zip": "./zip.ts" }, @@ -104,7 +102,6 @@ "@core/iterutil/async/some": "./async/some.ts", "@core/iterutil/async/take": "./async/take.ts", "@core/iterutil/async/take-while": "./async/take_while.ts", - "@core/iterutil/async/to-array": "./async/to_array.ts", "@core/iterutil/async/to-async-iterable": "./async/to_async_iterable.ts", "@core/iterutil/async/uniq": "./async/uniq.ts", "@core/iterutil/async/zip": "./async/zip.ts", @@ -134,7 +131,6 @@ "@core/iterutil/some": "./some.ts", "@core/iterutil/take": "./take.ts", "@core/iterutil/take-while": "./take_while.ts", - "@core/iterutil/to-array": "./to_array.ts", "@core/iterutil/uniq": "./uniq.ts", "@core/iterutil/zip": "./zip.ts", "@core/unknownutil": "jsr:@core/unknownutil@^4.0.1", diff --git a/drop.ts b/drop.ts index 426b1b2..72c37a3 100644 --- a/drop.ts +++ b/drop.ts @@ -11,7 +11,7 @@ * import { drop } from "@core/iterutil/drop"; * * const iter = drop([1, 2, 3, 4, 5], 2); - * console.log([...iter]); // [3, 4, 5] + * console.log(Array.from(iter)); // [3, 4, 5] * ``` */ export function drop(iterable: Iterable, limit: number): Iterable { diff --git a/drop_test.ts b/drop_test.ts index 42711a0..85a8151 100644 --- a/drop_test.ts +++ b/drop_test.ts @@ -6,7 +6,7 @@ Deno.test("drop", async (t) => { await t.step("with positive limit", () => { const result = drop([0, 1, 2, 3, 4], 2); const expected = [2, 3, 4]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); @@ -22,7 +22,7 @@ Deno.test("drop", async (t) => { await t.step("with 0 limit", () => { const result = drop([0, 1, 2, 3, 4], 0); const expected = [0, 1, 2, 3, 4]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/drop_while.ts b/drop_while.ts index 1a7b5a9..df358af 100644 --- a/drop_while.ts +++ b/drop_while.ts @@ -13,7 +13,7 @@ * import { dropWhile } from "@core/iterutil/drop-while"; * * const iter = dropWhile([1, 2, 3, 4, 5], (x) => x < 3); - * console.log([...iter]); // [3, 4, 5] + * console.log(Array.from(iter)); // [3, 4, 5] * ``` */ export function* dropWhile( diff --git a/drop_while_test.ts b/drop_while_test.ts index 2664a2d..9f1bdea 100644 --- a/drop_while_test.ts +++ b/drop_while_test.ts @@ -6,21 +6,21 @@ Deno.test("dropWhile", async (t) => { await t.step("with some true", () => { const result = dropWhile([0, 1, 2, 3, 4], (v) => v < 2); const expected = [2, 3, 4]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with all true", () => { const result = dropWhile([0, 1, 2, 3, 4], () => true); const expected: number[] = []; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with all false", () => { const result = dropWhile([0, 1, 2, 3, 4], () => false); const expected = [0, 1, 2, 3, 4]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/enumerate.ts b/enumerate.ts index 69fdc77..a2b4e62 100644 --- a/enumerate.ts +++ b/enumerate.ts @@ -10,7 +10,7 @@ * import { enumerate } from "@core/iterutil/enumerate"; * * const iter = enumerate(["a", "b", "c"]); - * console.log([...iter]); // [[0, "a"], [1, "b"], [2, "c"]] + * console.log(Array.from(iter)); // [[0, "a"], [1, "b"], [2, "c"]] * ``` */ export function* enumerate( diff --git a/enumerate_test.ts b/enumerate_test.ts index 56ee2cc..91d94a9 100644 --- a/enumerate_test.ts +++ b/enumerate_test.ts @@ -6,21 +6,21 @@ Deno.test("enumerate", async (t) => { await t.step("default", () => { const result = enumerate([0, 1, 2]); const expected = [[0, 0], [1, 1], [2, 2]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with start", () => { const result = enumerate([0, 1, 2], 1); const expected = [[1, 0], [2, 1], [3, 2]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with start/step", () => { const result = enumerate([0, 1, 2], 1, 2); const expected = [[1, 0], [3, 1], [5, 2]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/filter.ts b/filter.ts index 277f166..adb9be0 100644 --- a/filter.ts +++ b/filter.ts @@ -10,7 +10,7 @@ * import { filter } from "@core/iterutil/filter"; * * const iter = filter([1, 2, 3, 4, 5], (value) => value % 2 === 0); - * console.log([...iter]); // [2, 4] + * console.log(Array.from(iter)); // [2, 4] * ``` */ export function* filter( diff --git a/filter_test.ts b/filter_test.ts index a685943..8cca723 100644 --- a/filter_test.ts +++ b/filter_test.ts @@ -11,7 +11,7 @@ Deno.test("filter", () => { return value % 2 === 0; }); const expected = [2, 4]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); diff --git a/flat_map.ts b/flat_map.ts index 5d35468..77f9d05 100644 --- a/flat_map.ts +++ b/flat_map.ts @@ -10,7 +10,7 @@ * import { flatMap } from "@core/iterutil/flat-map"; * * const iter = flatMap([1, 2, 3], (value) => [value, value]); - * console.log([...iter]); // [1, 1, 2, 2, 3, 3] + * console.log(Array.from(iter)); // [1, 1, 2, 2, 3, 3] * ``` */ export function* flatMap( diff --git a/flat_map_test.ts b/flat_map_test.ts index 44731c3..9815c38 100644 --- a/flat_map_test.ts +++ b/flat_map_test.ts @@ -12,7 +12,7 @@ Deno.test("flatMap", async (t) => { return [value, value]; }); const expected = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); @@ -21,7 +21,7 @@ Deno.test("flatMap", async (t) => { await t.step("multi nest", () => { const result = flatMap([1, 2, 3, 4, 5], (v) => [[v, v]]); const expected = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/flatten.ts b/flatten.ts index 9dfb9df..13ff76d 100644 --- a/flatten.ts +++ b/flatten.ts @@ -9,7 +9,7 @@ * import { flatten } from "@core/iterutil/flatten"; * * const iter = flatten([[1, 2], [3, 4], [5]]); - * console.log([...iter]); // [1, 2, 3, 4, 5] + * console.log(Array.from(iter)); // [1, 2, 3, 4, 5] * ``` */ export function* flatten(iterable: Iterable>): Iterable { diff --git a/flatten_test.ts b/flatten_test.ts index 796469a..2e4ca2e 100644 --- a/flatten_test.ts +++ b/flatten_test.ts @@ -6,14 +6,14 @@ Deno.test("flatten", async (t) => { await t.step("single nest", () => { const result = flatten([[1, 2], [3, 4], [5]]); const expected = [1, 2, 3, 4, 5]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("multi nest", () => { const result = flatten([[[1, 2], [3, 4]], [[5]]]); const expected = [[1, 2], [3, 4], [5]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/iter_test.ts b/iter_test.ts index 7d8e4eb..4da62b9 100644 --- a/iter_test.ts +++ b/iter_test.ts @@ -6,6 +6,6 @@ Deno.test("iter", () => { const it = iter([0, 1, 2, 3, 4, 5]); assertEquals(it.next(), { done: false, value: 0 }); assertEquals(it.next(), { done: false, value: 1 }); - assertEquals([...it], [2, 3, 4, 5]); + assertEquals(Array.from(it), [2, 3, 4, 5]); assertType>>(true); }); diff --git a/map.ts b/map.ts index e955a9a..e1ca6e1 100644 --- a/map.ts +++ b/map.ts @@ -10,7 +10,7 @@ * import { map } from "@core/iterutil/map"; * * const iter = map([1, 2, 3], (value) => value * 2); - * console.log([...iter]); // [2, 4, 6] + * console.log(Array.from(iter)); // [2, 4, 6] * ``` */ export function* map( diff --git a/map_test.ts b/map_test.ts index fcd3881..6d68e80 100644 --- a/map_test.ts +++ b/map_test.ts @@ -11,7 +11,7 @@ Deno.test("map", () => { return value * 2; }); const expected = [2, 4, 6, 8, 10]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertEquals(values, [1, 2, 3, 4, 5]); assertEquals(indices, [0, 1, 2, 3, 4]); assertType>>(true); diff --git a/mod.ts b/mod.ts index 85c3c71..b6fe680 100644 --- a/mod.ts +++ b/mod.ts @@ -24,6 +24,5 @@ export * from "./reduce.ts"; export * from "./some.ts"; export * from "./take.ts"; export * from "./take_while.ts"; -export * from "./to_array.ts"; export * from "./uniq.ts"; export * from "./zip.ts"; diff --git a/mod_test.ts b/mod_test.ts index 09faa3e..4030b75 100644 --- a/mod_test.ts +++ b/mod_test.ts @@ -39,7 +39,7 @@ async function* iterPublicModules(relpath: string): AsyncIterable { async function listModExports(path: string): Promise { const mod = await import(import.meta.resolve(path)); - return [...Object.keys(mod)]; + return Array.from(Object.keys(mod)); } async function listJsrExportEntries(): Promise<[string, string][]> { diff --git a/pairwise.ts b/pairwise.ts index 68a84c9..248abdf 100644 --- a/pairwise.ts +++ b/pairwise.ts @@ -11,7 +11,7 @@ * import { pairwise } from "@core/iterutil/pairwise"; * * const iter = pairwise([1, 2, 3, 4, 5]); - * console.log([...iter]); // [[1, 2], [2, 3], [3, 4], [4, 5]] + * console.log(Array.from(iter)); // [[1, 2], [2, 3], [3, 4], [4, 5]] * ``` */ export function* pairwise(iterable: Iterable): Iterable<[T, T]> { diff --git a/pairwise_test.ts b/pairwise_test.ts index a32e93e..58c1bc1 100644 --- a/pairwise_test.ts +++ b/pairwise_test.ts @@ -6,21 +6,21 @@ Deno.test("pairwise", async (t) => { await t.step("with non empty iterable", () => { const result = pairwise([1, 2, 3, 4, 5]); const expected = [[1, 2], [2, 3], [3, 4], [4, 5]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with single value iterable", () => { const result = pairwise([1]); const expected: number[][] = []; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with empty iterable", () => { const result = pairwise([] as number[]); const expected: number[][] = []; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/range.ts b/range.ts index 3eaf920..ae08313 100644 --- a/range.ts +++ b/range.ts @@ -8,7 +8,7 @@ * ```ts * import { range } from "@core/iterutil/range"; * - * console.log([...range(3)]); // [0, 1, 2] + * console.log(Array.from(range(3))); // [0, 1, 2] * ``` */ export function range(stop: number): Iterable; @@ -23,7 +23,7 @@ export function range(stop: number): Iterable; * ```ts * import { range } from "@core/iterutil/range"; * - * console.log([...range(1, 6, 2)]); // [1, 3, 5] + * console.log(Array.from(range(1, 6, 2))); // [1, 3, 5] * ``` */ export function range( diff --git a/range_test.ts b/range_test.ts index 19f99ea..ddce440 100644 --- a/range_test.ts +++ b/range_test.ts @@ -5,19 +5,19 @@ import { range } from "./range.ts"; Deno.test("range", async (t) => { await t.step("default", () => { const result = range(5); - assertEquals([...result], [0, 1, 2, 3, 4]); + assertEquals(Array.from(result), [0, 1, 2, 3, 4]); assertType>>(true); }); await t.step("with start/stop", () => { const result = range(1, 5); - assertEquals([...result], [1, 2, 3, 4]); + assertEquals(Array.from(result), [1, 2, 3, 4]); assertType>>(true); }); await t.step("with start/stop/step", () => { const result = range(1, 10, 2); - assertEquals([...result], [1, 3, 5, 7, 9]); + assertEquals(Array.from(result), [1, 3, 5, 7, 9]); assertType>>(true); }); }); diff --git a/take.ts b/take.ts index bc68c3d..3194679 100644 --- a/take.ts +++ b/take.ts @@ -11,7 +11,7 @@ * import { take } from "@core/iterutil/take"; * * const iter = take([1, 2, 3, 4, 5], 2); - * console.log([...iter]); // [1, 2] + * console.log(Array.from(iter)); // [1, 2] * ``` */ export function take(iterable: Iterable, limit: number): Iterable { diff --git a/take_test.ts b/take_test.ts index e1ede62..ddac214 100644 --- a/take_test.ts +++ b/take_test.ts @@ -6,7 +6,7 @@ Deno.test("take", async (t) => { await t.step("with positive limit", () => { const result = take([0, 1, 2, 3, 4], 2); const expected = [0, 1]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); @@ -22,7 +22,7 @@ Deno.test("take", async (t) => { await t.step("with 0 limit", () => { const result = take([0, 1, 2, 3, 4], 0); const expected: number[] = []; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/take_while.ts b/take_while.ts index a789a71..3c5cd22 100644 --- a/take_while.ts +++ b/take_while.ts @@ -10,7 +10,7 @@ * import { takeWhile } from "@core/iterutil/take-while"; * * const iter = takeWhile([1, 2, 3, 4, 5], (value) => value < 4); - * console.log([...iter]); // [1, 2, 3] + * console.log(Array.from(iter)); // [1, 2, 3] * ``` */ export function* takeWhile( diff --git a/take_while_test.ts b/take_while_test.ts index 4b81b1e..b90e64a 100644 --- a/take_while_test.ts +++ b/take_while_test.ts @@ -6,21 +6,21 @@ Deno.test("takeWhile", async (t) => { await t.step("with some true", () => { const result = takeWhile([0, 1, 2, 3, 4], (v) => v < 2); const expected = [0, 1]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with all true", () => { const result = takeWhile([0, 1, 2, 3, 4], () => true); const expected = [0, 1, 2, 3, 4]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("with all false", () => { const result = takeWhile([0, 1, 2, 3, 4], () => false); const expected: number[] = []; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/to_array.ts b/to_array.ts deleted file mode 100644 index 3274890..0000000 --- a/to_array.ts +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Converts an iterable to an array. - * - * Note that users should use `Array.from` directly. This function exists for consistency with `async/to-array.toArray`. - * - * @param iterable The iterable to convert. - * @returns The array. - * - * @example - * ```ts - * import { toArray } from "@core/iterutil/to-array"; - * - * const arr = toArray(function*() { - * yield 1; - * yield 2; - * yield 3; - * }()); - * console.log(arr); // [1, 2, 3] - * ``` - */ -export function toArray( - iterable: Iterable, -): T[] { - return Array.from(iterable); -} diff --git a/to_array_test.ts b/to_array_test.ts deleted file mode 100644 index 2b22dbb..0000000 --- a/to_array_test.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { assertEquals } from "@std/assert"; -import { assertType, type IsExact } from "@std/testing/types"; -import { toArray } from "./to_array.ts"; - -Deno.test("toArray", () => { - const result = toArray(function* () { - yield 1 as number; - yield 2 as number; - yield 3 as number; - }()); - const expected = [1, 2, 3]; - assertEquals(result, expected); - assertType>(true); -}); diff --git a/uniq.ts b/uniq.ts index 4b24e93..c4bf01e 100644 --- a/uniq.ts +++ b/uniq.ts @@ -10,7 +10,7 @@ * import { uniq } from "@core/iterutil/uniq"; * * const iter = uniq([1, 2, 2, 3, 3, 3]); - * console.log([...iter]); // [1, 2, 3] + * console.log(Array.from(iter)); // [1, 2, 3] * ``` * * @example With identify function @@ -21,7 +21,7 @@ * [1, 2, 3, 1, 2, 3, 10, 20, 30, 11, 21, 31], * (v) => Math.floor(v / 10), * ); - * console.log([...iter]); // [1, 10, 20, 30] + * console.log(Array.from(iter)); // [1, 10, 20, 30] * ``` */ export function* uniq( diff --git a/uniq_test.ts b/uniq_test.ts index 0bb1c48..68d4ce8 100644 --- a/uniq_test.ts +++ b/uniq_test.ts @@ -6,7 +6,7 @@ Deno.test("uniq", async (t) => { await t.step("default", () => { const result = uniq([1, 2, 3, 1, 2, 3, 10, 20, 30, 11, 21, 31]); const expected = [1, 2, 3, 10, 20, 30, 11, 21, 31]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); @@ -16,7 +16,7 @@ Deno.test("uniq", async (t) => { (v) => Math.floor(v / 10), ); const expected = [1, 10, 20, 30]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); }); diff --git a/zip.ts b/zip.ts index a5e38f3..72207a1 100644 --- a/zip.ts +++ b/zip.ts @@ -9,7 +9,7 @@ * import { zip } from "@core/iterutil/zip"; * * const iter = zip([1, 2, 3], ["a", "b", "c"]); - * console.log([...iter]); // [[1, "a"], [2, "b"], [3, "c"]] + * console.log(Array.from(iter)); // [[1, "a"], [2, "b"], [3, "c"]] * ``` */ export function* zip[]>( diff --git a/zip_test.ts b/zip_test.ts index fec4bd8..940e464 100644 --- a/zip_test.ts +++ b/zip_test.ts @@ -6,21 +6,21 @@ Deno.test("zip", async (t) => { await t.step("zip with two iterables", () => { const result = zip([1, 2, 3], ["a", "b", "c"]); const expected = [[1, "a"], [2, "b"], [3, "c"]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("zip with two in-balanced iterables", () => { const result = zip([1, 2, 3, 4, 5], ["a", "b", "c"]); const expected = [[1, "a"], [2, "b"], [3, "c"]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>(true); }); await t.step("zip with three iterables", () => { const result = zip([1, 2, 3], ["a", "b", "c"], [true, false, true]); const expected = [[1, "a", true], [2, "b", false], [3, "c", true]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>( true, ); @@ -29,7 +29,7 @@ Deno.test("zip", async (t) => { await t.step("zip with three in-balanced iterables", () => { const result = zip([1, 2, 3, 4, 5], ["a", "b", "c"], [true, false, true]); const expected = [[1, "a", true], [2, "b", false], [3, "c", true]]; - assertEquals([...result], expected); + assertEquals(Array.from(result), expected); assertType>>( true, );