Skip to content

Commit 14868c7

Browse files
authored
Merge pull request #14 from jsr-core/pipe
feat: support `@core/pipe` package
2 parents 20b888b + f3fa384 commit 14868c7

Some content is hidden

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

108 files changed

+3180
-3
lines changed

README.md

Lines changed: 662 additions & 3 deletions
Large diffs are not rendered by default.

README_test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { assertEquals } from "@std/assert";
2+
import { pipe } from "@core/pipe";
3+
import * as rootMod from "@core/iterutil";
4+
import * as asyncMod from "@core/iterutil/async";
5+
import * as pipeMod from "@core/iterutil/pipe";
6+
import * as pipeAsyncMod from "@core/iterutil/pipe/async";
7+
8+
Deno.test("README", async (t) => {
9+
await t.step("case 1", () => {
10+
const iter = rootMod.map([1, 2, 3], (v) => v * 2);
11+
assertEquals(Array.from(iter), [2, 4, 6]);
12+
});
13+
14+
await t.step("case 2", async () => {
15+
const iter = asyncMod.map([1, 2, 3], (v) => Promise.resolve(v * 2));
16+
assertEquals(await Array.fromAsync(iter), [2, 4, 6]);
17+
});
18+
19+
await t.step("case 3", () => {
20+
const iter = pipe(
21+
[1, 2, 3],
22+
pipeMod.map((v) => v * 2),
23+
pipeMod.cycle,
24+
pipeMod.take(10),
25+
pipeMod.filter((v) => v % 2 === 0),
26+
);
27+
assertEquals(Array.from(iter), [2, 4, 6, 2, 4, 6, 2, 4, 6, 2]);
28+
});
29+
30+
await t.step("case 4", async () => {
31+
const iter = pipe(
32+
[1, 2, 3],
33+
pipeAsyncMod.map((v) => v * 2),
34+
pipeAsyncMod.cycle,
35+
pipeAsyncMod.take(10),
36+
pipeAsyncMod.filter((v) => v % 2 === 0),
37+
);
38+
assertEquals(await Array.fromAsync(iter), [2, 4, 6, 2, 4, 6, 2, 4, 6, 2]);
39+
});
40+
});

async/uniq.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* Returns an iterable that yields the unique elements of the input iterable.
33
*
4+
* Use {@linkcode https://jsr.io/@core/iterutil/doc/uniq/~/uniq uniq} to get the unique elements synchronously.
5+
*
46
* @param iterable The iterable to get the unique elements of.
57
* @param identify An optional function to transform the elements before checking for uniqueness.
68
* @returns An iterable that yields the unique elements of the input iterable.

deno.jsonc

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,58 @@
5252
"./map": "./map.ts",
5353
"./pairwise": "./pairwise.ts",
5454
"./partition": "./partition.ts",
55+
"./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",
82+
"./pipe/chain": "./pipe/chain.ts",
83+
"./pipe/chunked": "./pipe/chunked.ts",
84+
"./pipe/compact": "./pipe/compact.ts",
85+
"./pipe/compress": "./pipe/compress.ts",
86+
"./pipe/cycle": "./pipe/cycle.ts",
87+
"./pipe/drop": "./pipe/drop.ts",
88+
"./pipe/drop-while": "./pipe/drop_while.ts",
89+
"./pipe/enumerate": "./pipe/enumerate.ts",
90+
"./pipe/every": "./pipe/every.ts",
91+
"./pipe/filter": "./pipe/filter.ts",
92+
"./pipe/find": "./pipe/find.ts",
93+
"./pipe/first": "./pipe/first.ts",
94+
"./pipe/flat-map": "./pipe/flat_map.ts",
95+
"./pipe/flatten": "./pipe/flatten.ts",
96+
"./pipe/for-each": "./pipe/for_each.ts",
97+
"./pipe/last": "./pipe/last.ts",
98+
"./pipe/map": "./pipe/map.ts",
99+
"./pipe/pairwise": "./pipe/pairwise.ts",
100+
"./pipe/partition": "./pipe/partition.ts",
101+
"./pipe/reduce": "./pipe/reduce.ts",
102+
"./pipe/some": "./pipe/some.ts",
103+
"./pipe/take": "./pipe/take.ts",
104+
"./pipe/take-while": "./pipe/take_while.ts",
105+
"./pipe/uniq": "./pipe/uniq.ts",
106+
"./pipe/zip": "./pipe/zip.ts",
55107
"./range": "./range.ts",
56108
"./reduce": "./reduce.ts",
57109
"./some": "./some.ts",
@@ -126,13 +178,70 @@
126178
"@core/iterutil/map": "./map.ts",
127179
"@core/iterutil/pairwise": "./pairwise.ts",
128180
"@core/iterutil/partition": "./partition.ts",
181+
"@core/iterutil/pipe": "./pipe/mod.ts",
182+
"@core/iterutil/pipe/async": "./pipe/async/mod.ts",
183+
"@core/iterutil/pipe/async/chain": "./pipe/async/chain.ts",
184+
"@core/iterutil/pipe/async/chunked": "./pipe/async/chunked.ts",
185+
"@core/iterutil/pipe/async/compact": "./pipe/async/compact.ts",
186+
"@core/iterutil/pipe/async/compress": "./pipe/async/compress.ts",
187+
"@core/iterutil/pipe/async/count": "./pipe/async/count.ts",
188+
"@core/iterutil/pipe/async/cycle": "./pipe/async/cycle.ts",
189+
"@core/iterutil/pipe/async/drop": "./pipe/async/drop.ts",
190+
"@core/iterutil/pipe/async/drop-while": "./pipe/async/drop_while.ts",
191+
"@core/iterutil/pipe/async/enumerate": "./pipe/async/enumerate.ts",
192+
"@core/iterutil/pipe/async/every": "./pipe/async/every.ts",
193+
"@core/iterutil/pipe/async/filter": "./pipe/async/filter.ts",
194+
"@core/iterutil/pipe/async/find": "./pipe/async/find.ts",
195+
"@core/iterutil/pipe/async/first": "./pipe/async/first.ts",
196+
"@core/iterutil/pipe/async/flat-map": "./pipe/async/flat_map.ts",
197+
"@core/iterutil/pipe/async/flatten": "./pipe/async/flatten.ts",
198+
"@core/iterutil/pipe/async/for-each": "./pipe/async/for_each.ts",
199+
"@core/iterutil/pipe/async/iter": "./pipe/async/iter.ts",
200+
"@core/iterutil/pipe/async/last": "./pipe/async/last.ts",
201+
"@core/iterutil/pipe/async/map": "./pipe/async/map.ts",
202+
"@core/iterutil/pipe/async/pairwise": "./pipe/async/pairwise.ts",
203+
"@core/iterutil/pipe/async/partition": "./pipe/async/partition.ts",
204+
"@core/iterutil/pipe/async/reduce": "./pipe/async/reduce.ts",
205+
"@core/iterutil/pipe/async/some": "./pipe/async/some.ts",
206+
"@core/iterutil/pipe/async/take": "./pipe/async/take.ts",
207+
"@core/iterutil/pipe/async/take-while": "./pipe/async/take_while.ts",
208+
"@core/iterutil/pipe/async/uniq": "./pipe/async/uniq.ts",
209+
"@core/iterutil/pipe/async/zip": "./pipe/async/zip.ts",
210+
"@core/iterutil/pipe/chain": "./pipe/chain.ts",
211+
"@core/iterutil/pipe/chunked": "./pipe/chunked.ts",
212+
"@core/iterutil/pipe/compact": "./pipe/compact.ts",
213+
"@core/iterutil/pipe/compress": "./pipe/compress.ts",
214+
"@core/iterutil/pipe/count": "./pipe/count.ts",
215+
"@core/iterutil/pipe/cycle": "./pipe/cycle.ts",
216+
"@core/iterutil/pipe/drop": "./pipe/drop.ts",
217+
"@core/iterutil/pipe/drop-while": "./pipe/drop_while.ts",
218+
"@core/iterutil/pipe/enumerate": "./pipe/enumerate.ts",
219+
"@core/iterutil/pipe/every": "./pipe/every.ts",
220+
"@core/iterutil/pipe/filter": "./pipe/filter.ts",
221+
"@core/iterutil/pipe/find": "./pipe/find.ts",
222+
"@core/iterutil/pipe/first": "./pipe/first.ts",
223+
"@core/iterutil/pipe/flat-map": "./pipe/flat_map.ts",
224+
"@core/iterutil/pipe/flatten": "./pipe/flatten.ts",
225+
"@core/iterutil/pipe/for-each": "./pipe/for_each.ts",
226+
"@core/iterutil/pipe/iter": "./pipe/iter.ts",
227+
"@core/iterutil/pipe/last": "./pipe/last.ts",
228+
"@core/iterutil/pipe/map": "./pipe/map.ts",
229+
"@core/iterutil/pipe/pairwise": "./pipe/pairwise.ts",
230+
"@core/iterutil/pipe/partition": "./pipe/partition.ts",
231+
"@core/iterutil/pipe/reduce": "./pipe/reduce.ts",
232+
"@core/iterutil/pipe/some": "./pipe/some.ts",
233+
"@core/iterutil/pipe/take": "./pipe/take.ts",
234+
"@core/iterutil/pipe/take-while": "./pipe/take_while.ts",
235+
"@core/iterutil/pipe/uniq": "./pipe/uniq.ts",
236+
"@core/iterutil/pipe/zip": "./pipe/zip.ts",
129237
"@core/iterutil/range": "./range.ts",
130238
"@core/iterutil/reduce": "./reduce.ts",
131239
"@core/iterutil/some": "./some.ts",
132240
"@core/iterutil/take": "./take.ts",
133241
"@core/iterutil/take-while": "./take_while.ts",
134242
"@core/iterutil/uniq": "./uniq.ts",
135243
"@core/iterutil/zip": "./zip.ts",
244+
"@core/pipe": "jsr:@core/pipe@^0.2.0",
136245
"@core/unknownutil": "jsr:@core/unknownutil@^4.0.1",
137246
"@std/assert": "jsr:@std/assert@^1.0.2",
138247
"@std/jsonc": "jsr:@std/jsonc@^1.0.0",

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+
});

0 commit comments

Comments
 (0)