Skip to content

Commit a875b03

Browse files
committed
feat: add pipe/async module to support @core/pipe
1 parent 5eeccf3 commit a875b03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1244
-0
lines changed

deno.jsonc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@
5353
"./pairwise": "./pairwise.ts",
5454
"./partition": "./partition.ts",
5555
"./pipe": "./pipe/mod.ts",
56+
"./pipe/async": "./pipe/async/mod.ts",
57+
"./pipe/async/chain": "./pipe/async/chain.ts",
58+
"./pipe/async/chunked": "./pipe/async/chunked.ts",
59+
"./pipe/async/compact": "./pipe/async/compact.ts",
60+
"./pipe/async/compress": "./pipe/async/compress.ts",
61+
"./pipe/async/cycle": "./pipe/async/cycle.ts",
62+
"./pipe/async/drop": "./pipe/async/drop.ts",
63+
"./pipe/async/drop-while": "./pipe/async/drop_while.ts",
64+
"./pipe/async/enumerate": "./pipe/async/enumerate.ts",
65+
"./pipe/async/every": "./pipe/async/every.ts",
66+
"./pipe/async/filter": "./pipe/async/filter.ts",
67+
"./pipe/async/find": "./pipe/async/find.ts",
68+
"./pipe/async/first": "./pipe/async/first.ts",
69+
"./pipe/async/flat-map": "./pipe/async/flat_map.ts",
70+
"./pipe/async/flatten": "./pipe/async/flatten.ts",
71+
"./pipe/async/for-each": "./pipe/async/for_each.ts",
72+
"./pipe/async/last": "./pipe/async/last.ts",
73+
"./pipe/async/map": "./pipe/async/map.ts",
74+
"./pipe/async/pairwise": "./pipe/async/pairwise.ts",
75+
"./pipe/async/partition": "./pipe/async/partition.ts",
76+
"./pipe/async/reduce": "./pipe/async/reduce.ts",
77+
"./pipe/async/some": "./pipe/async/some.ts",
78+
"./pipe/async/take": "./pipe/async/take.ts",
79+
"./pipe/async/take-while": "./pipe/async/take_while.ts",
80+
"./pipe/async/uniq": "./pipe/async/uniq.ts",
81+
"./pipe/async/zip": "./pipe/async/zip.ts",
5682
"./pipe/chain": "./pipe/chain.ts",
5783
"./pipe/chunked": "./pipe/chunked.ts",
5884
"./pipe/compact": "./pipe/compact.ts",
@@ -152,6 +178,33 @@
152178
"@core/iterutil/map": "./map.ts",
153179
"@core/iterutil/pairwise": "./pairwise.ts",
154180
"@core/iterutil/partition": "./partition.ts",
181+
"@core/iterutil/pipe/async/chain": "./pipe/async/chain.ts",
182+
"@core/iterutil/pipe/async/chunked": "./pipe/async/chunked.ts",
183+
"@core/iterutil/pipe/async/compact": "./pipe/async/compact.ts",
184+
"@core/iterutil/pipe/async/compress": "./pipe/async/compress.ts",
185+
"@core/iterutil/pipe/async/count": "./pipe/async/count.ts",
186+
"@core/iterutil/pipe/async/cycle": "./pipe/async/cycle.ts",
187+
"@core/iterutil/pipe/async/drop": "./pipe/async/drop.ts",
188+
"@core/iterutil/pipe/async/drop-while": "./pipe/async/drop_while.ts",
189+
"@core/iterutil/pipe/async/enumerate": "./pipe/async/enumerate.ts",
190+
"@core/iterutil/pipe/async/every": "./pipe/async/every.ts",
191+
"@core/iterutil/pipe/async/filter": "./pipe/async/filter.ts",
192+
"@core/iterutil/pipe/async/find": "./pipe/async/find.ts",
193+
"@core/iterutil/pipe/async/first": "./pipe/async/first.ts",
194+
"@core/iterutil/pipe/async/flat-map": "./pipe/async/flat_map.ts",
195+
"@core/iterutil/pipe/async/flatten": "./pipe/async/flatten.ts",
196+
"@core/iterutil/pipe/async/for-each": "./pipe/async/for_each.ts",
197+
"@core/iterutil/pipe/async/iter": "./pipe/async/iter.ts",
198+
"@core/iterutil/pipe/async/last": "./pipe/async/last.ts",
199+
"@core/iterutil/pipe/async/map": "./pipe/async/map.ts",
200+
"@core/iterutil/pipe/async/pairwise": "./pipe/async/pairwise.ts",
201+
"@core/iterutil/pipe/async/partition": "./pipe/async/partition.ts",
202+
"@core/iterutil/pipe/async/reduce": "./pipe/async/reduce.ts",
203+
"@core/iterutil/pipe/async/some": "./pipe/async/some.ts",
204+
"@core/iterutil/pipe/async/take": "./pipe/async/take.ts",
205+
"@core/iterutil/pipe/async/take-while": "./pipe/async/take_while.ts",
206+
"@core/iterutil/pipe/async/uniq": "./pipe/async/uniq.ts",
207+
"@core/iterutil/pipe/async/zip": "./pipe/async/zip.ts",
155208
"@core/iterutil/pipe/chain": "./pipe/chain.ts",
156209
"@core/iterutil/pipe/chunked": "./pipe/chunked.ts",
157210
"@core/iterutil/pipe/compact": "./pipe/compact.ts",

pipe/async/chain.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { type Chain, chain as base } from "@core/iterutil/async/chain";
2+
3+
/**
4+
* Returns an operator that chains multiple iterables to the iterable.
5+
*
6+
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/chain/~/chain chain} for native chain.
7+
*
8+
* @param iterables The iterables to chain to the iterable.
9+
* @returns An operator that chains multiple iterables to the iterable.
10+
*
11+
* @example
12+
* ```ts
13+
* import { pipe } from "@core/pipe";
14+
* import { chain } from "@core/iterutil/pipe/async/chain";
15+
*
16+
* const iter = pipe(
17+
* [1, 2, 3],
18+
* chain(["a", "b"], [true]),
19+
* );
20+
* console.log(await Array.fromAsync(iter)); // [1, 2, 3, "a", "b", true]
21+
* ```
22+
*/
23+
export function chain<
24+
U extends readonly [
25+
Iterable<unknown> | AsyncIterable<unknown>,
26+
...(Iterable<unknown> | AsyncIterable<unknown>)[],
27+
],
28+
>(
29+
...iterables: U
30+
): <T>(
31+
iterable: Iterable<T> | AsyncIterable<T>,
32+
) => AsyncIterable<T | Chain<U>> {
33+
return <T>(iterable: Iterable<T> | AsyncIterable<T>) =>
34+
base(iterable, ...iterables) as AsyncIterable<T | Chain<U>>;
35+
}

pipe/async/chain_test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { assertEquals } from "@std/assert";
2+
import { assertType, type IsExact } from "@std/testing/types";
3+
import { pipe } from "@core/pipe";
4+
import { chain } from "./chain.ts";
5+
6+
Deno.test("chain", async (t) => {
7+
await t.step("usage", async () => {
8+
const result = pipe(
9+
[1, 2, 3],
10+
chain(["a", "b"], [true]),
11+
);
12+
const expected = [1, 2, 3, "a", "b", true];
13+
assertEquals(await Array.fromAsync(result), expected);
14+
assertType<
15+
IsExact<typeof result, AsyncIterable<number | string | boolean>>
16+
>(
17+
true,
18+
);
19+
});
20+
});

pipe/async/chunked.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { chunked as base } from "@core/iterutil/async/chunked";
2+
3+
/**
4+
* Returns an operator that chunks the iterable into arrays of `size`.
5+
*
6+
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/chunked/~/chunked chunked} for native chunked.
7+
*
8+
* @param size The size of each chunk.
9+
* @return An operator that chunks the iterable into arrays of `size`.
10+
*
11+
* @example
12+
* ```ts
13+
* import { pipe } from "@core/pipe";
14+
* import { chunked } from "@core/iterutil/pipe/async/chunked";
15+
*
16+
* const iter = pipe(
17+
* [1, 2, 3, 4, 5],
18+
* chunked(2),
19+
* );
20+
* console.log(await Array.fromAsync(iter)); // [[1, 2], [3, 4], [5]]
21+
* ```
22+
*/
23+
export function chunked(
24+
size: number,
25+
): <T>(iterable: Iterable<T> | AsyncIterable<T>) => AsyncIterable<T[]> {
26+
return (iterable) => base(iterable, size);
27+
}

pipe/async/chunked_test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { assertEquals } from "@std/assert";
2+
import { assertType, type IsExact } from "@std/testing/types";
3+
import { pipe } from "@core/pipe";
4+
import { chunked } from "./chunked.ts";
5+
6+
Deno.test("chunked", async (t) => {
7+
await t.step("usage", async () => {
8+
const result = pipe([1, 2, 3, 4, 5, 6], chunked(2));
9+
const expected = [[1, 2], [3, 4], [5, 6]];
10+
assertEquals(await Array.fromAsync(result), expected);
11+
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
12+
});
13+
});

pipe/async/compact.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { compact } from "@core/iterutil/async/compact";
2+
3+
export {
4+
/**
5+
* An operator to remove all nullish (`null` or `undefined`) values from an iterable.
6+
*
7+
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/compact/~/compact compact} for native compact.
8+
*
9+
* @example
10+
* ```ts
11+
* import { pipe } from "@core/pipe";
12+
* import { compact } from "@core/iterutil/pipe/async/compact";
13+
*
14+
* const iter = pipe(
15+
* [1, undefined, 2, null, 3],
16+
* compact,
17+
* );
18+
* console.log(await Array.fromAsync(iter)); // [1, 2, 3]
19+
* ```
20+
*/
21+
compact,
22+
};

pipe/async/compact_test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { assertEquals } from "@std/assert";
2+
import { assertType, type IsExact } from "@std/testing/types";
3+
import { pipe } from "@core/pipe";
4+
import { compact } from "./compact.ts";
5+
6+
Deno.test("compact", async (t) => {
7+
await t.step("usage", async () => {
8+
const result = pipe([1, undefined, 2, null, 3], compact);
9+
const expected = [1, 2, 3];
10+
assertEquals(await Array.fromAsync(result), expected);
11+
assertType<IsExact<typeof result, AsyncIterable<number>>>(true);
12+
});
13+
});

pipe/async/compress.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { compress as base } from "@core/iterutil/async/compress";
2+
3+
/**
4+
* Returns an operator that compresses an iterable by selecting elements using a selector iterable.
5+
*
6+
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/compress/~/compress compress} for native compress.
7+
*
8+
* @param selectors The selectors to use.
9+
* @returns An operator that compresses an iterable by selecting elements using a selector iterable.
10+
*
11+
* @example
12+
* ```ts
13+
* import { pipe } from "@core/pipe";
14+
* import { compress } from "@core/iterutil/pipe/async/compress";
15+
*
16+
* const iter = pipe(
17+
* [1, 2, 3, 4, 5],
18+
* compress([true, false, true, false, true]),
19+
* );
20+
* console.log(await Array.fromAsync(iter)); // [1, 3, 5]
21+
* ```
22+
*/
23+
export function compress(
24+
selectors: Iterable<boolean> | AsyncIterable<boolean>,
25+
): <T>(iterable: Iterable<T> | AsyncIterable<T>) => AsyncIterable<T> {
26+
return (iterable) => base(iterable, selectors);
27+
}

pipe/async/compress_test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { assertEquals } from "@std/assert";
2+
import { assertType, type IsExact } from "@std/testing/types";
3+
import { pipe } from "@core/pipe";
4+
import { compress } from "./compress.ts";
5+
6+
Deno.test("compress", async (t) => {
7+
await t.step("usage", async () => {
8+
const result = pipe(
9+
[1, 2, 3, 4, 5],
10+
compress([true, false, true, false, true]),
11+
);
12+
const expected = [1, 3, 5];
13+
assertEquals(await Array.fromAsync(result), expected);
14+
assertType<IsExact<typeof result, AsyncIterable<number>>>(true);
15+
});
16+
});

pipe/async/cycle.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { cycle } from "@core/iterutil/async/cycle";
2+
3+
export {
4+
/**
5+
* An operator to return a function that cycles the elements of an iterable.
6+
*
7+
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/cycle/~/cycle cycle} for native cycle.
8+
*
9+
* @example
10+
* ```ts
11+
* import { pipe } from "@core/pipe";
12+
* import { cycle } from "@core/iterutil/pipe/async/cycle";
13+
* import { take } from "@core/iterutil/pipe/async/take";
14+
*
15+
* const iter = pipe(
16+
* [1, 2, 3],
17+
* cycle,
18+
* take(5),
19+
* );
20+
* console.log(await Array.fromAsync(iter)); // [1, 2, 3, 1, 2]
21+
* ```
22+
*/
23+
cycle,
24+
};

0 commit comments

Comments
 (0)