Skip to content

Commit 5eeccf3

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

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

+1230
-0
lines changed

deno.jsonc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@
5252
"./map": "./map.ts",
5353
"./pairwise": "./pairwise.ts",
5454
"./partition": "./partition.ts",
55+
"./pipe": "./pipe/mod.ts",
56+
"./pipe/chain": "./pipe/chain.ts",
57+
"./pipe/chunked": "./pipe/chunked.ts",
58+
"./pipe/compact": "./pipe/compact.ts",
59+
"./pipe/compress": "./pipe/compress.ts",
60+
"./pipe/cycle": "./pipe/cycle.ts",
61+
"./pipe/drop": "./pipe/drop.ts",
62+
"./pipe/drop-while": "./pipe/drop_while.ts",
63+
"./pipe/enumerate": "./pipe/enumerate.ts",
64+
"./pipe/every": "./pipe/every.ts",
65+
"./pipe/filter": "./pipe/filter.ts",
66+
"./pipe/find": "./pipe/find.ts",
67+
"./pipe/first": "./pipe/first.ts",
68+
"./pipe/flat-map": "./pipe/flat_map.ts",
69+
"./pipe/flatten": "./pipe/flatten.ts",
70+
"./pipe/for-each": "./pipe/for_each.ts",
71+
"./pipe/last": "./pipe/last.ts",
72+
"./pipe/map": "./pipe/map.ts",
73+
"./pipe/pairwise": "./pipe/pairwise.ts",
74+
"./pipe/partition": "./pipe/partition.ts",
75+
"./pipe/reduce": "./pipe/reduce.ts",
76+
"./pipe/some": "./pipe/some.ts",
77+
"./pipe/take": "./pipe/take.ts",
78+
"./pipe/take-while": "./pipe/take_while.ts",
79+
"./pipe/uniq": "./pipe/uniq.ts",
80+
"./pipe/zip": "./pipe/zip.ts",
5581
"./range": "./range.ts",
5682
"./reduce": "./reduce.ts",
5783
"./some": "./some.ts",
@@ -126,13 +152,41 @@
126152
"@core/iterutil/map": "./map.ts",
127153
"@core/iterutil/pairwise": "./pairwise.ts",
128154
"@core/iterutil/partition": "./partition.ts",
155+
"@core/iterutil/pipe/chain": "./pipe/chain.ts",
156+
"@core/iterutil/pipe/chunked": "./pipe/chunked.ts",
157+
"@core/iterutil/pipe/compact": "./pipe/compact.ts",
158+
"@core/iterutil/pipe/compress": "./pipe/compress.ts",
159+
"@core/iterutil/pipe/count": "./pipe/count.ts",
160+
"@core/iterutil/pipe/cycle": "./pipe/cycle.ts",
161+
"@core/iterutil/pipe/drop": "./pipe/drop.ts",
162+
"@core/iterutil/pipe/drop-while": "./pipe/drop_while.ts",
163+
"@core/iterutil/pipe/enumerate": "./pipe/enumerate.ts",
164+
"@core/iterutil/pipe/every": "./pipe/every.ts",
165+
"@core/iterutil/pipe/filter": "./pipe/filter.ts",
166+
"@core/iterutil/pipe/find": "./pipe/find.ts",
167+
"@core/iterutil/pipe/first": "./pipe/first.ts",
168+
"@core/iterutil/pipe/flat-map": "./pipe/flat_map.ts",
169+
"@core/iterutil/pipe/flatten": "./pipe/flatten.ts",
170+
"@core/iterutil/pipe/for-each": "./pipe/for_each.ts",
171+
"@core/iterutil/pipe/iter": "./pipe/iter.ts",
172+
"@core/iterutil/pipe/last": "./pipe/last.ts",
173+
"@core/iterutil/pipe/map": "./pipe/map.ts",
174+
"@core/iterutil/pipe/pairwise": "./pipe/pairwise.ts",
175+
"@core/iterutil/pipe/partition": "./pipe/partition.ts",
176+
"@core/iterutil/pipe/reduce": "./pipe/reduce.ts",
177+
"@core/iterutil/pipe/some": "./pipe/some.ts",
178+
"@core/iterutil/pipe/take": "./pipe/take.ts",
179+
"@core/iterutil/pipe/take-while": "./pipe/take_while.ts",
180+
"@core/iterutil/pipe/uniq": "./pipe/uniq.ts",
181+
"@core/iterutil/pipe/zip": "./pipe/zip.ts",
129182
"@core/iterutil/range": "./range.ts",
130183
"@core/iterutil/reduce": "./reduce.ts",
131184
"@core/iterutil/some": "./some.ts",
132185
"@core/iterutil/take": "./take.ts",
133186
"@core/iterutil/take-while": "./take_while.ts",
134187
"@core/iterutil/uniq": "./uniq.ts",
135188
"@core/iterutil/zip": "./zip.ts",
189+
"@core/pipe": "jsr:@core/pipe@^0.2.0",
136190
"@core/unknownutil": "jsr:@core/unknownutil@^4.0.1",
137191
"@std/assert": "jsr:@std/assert@^1.0.2",
138192
"@std/jsonc": "jsr:@std/jsonc@^1.0.0",

pipe/chain.ts

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

pipe/chain_test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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", () => {
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(Array.from(result), expected);
14+
assertType<IsExact<typeof result, Iterable<number | string | boolean>>>(
15+
true,
16+
);
17+
});
18+
});

pipe/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/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/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/chunked";
15+
*
16+
* const iter = pipe(
17+
* [1, 2, 3, 4, 5],
18+
* chunked(2),
19+
* );
20+
* console.log(Array.from(iter)); // [[1, 2], [3, 4], [5]]
21+
* ```
22+
*/
23+
export function chunked(
24+
size: number,
25+
): <T>(iterable: Iterable<T>) => Iterable<T[]> {
26+
return (iterable) => base(iterable, size);
27+
}

pipe/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", () => {
8+
const result = pipe([1, 2, 3, 4, 5, 6], chunked(2));
9+
const expected = [[1, 2], [3, 4], [5, 6]];
10+
assertEquals(Array.from(result), expected);
11+
assertType<IsExact<typeof result, Iterable<number[]>>>(true);
12+
});
13+
});

pipe/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/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/compact/~/compact compact} for native compact.
8+
*
9+
* @example
10+
* ```ts
11+
* import { pipe } from "@core/pipe";
12+
* import { compact } from "@core/iterutil/pipe/compact";
13+
*
14+
* const iter = pipe(
15+
* [1, undefined, 2, null, 3],
16+
* compact,
17+
* );
18+
* console.log(Array.from(iter)); // [1, 2, 3]
19+
* ```
20+
*/
21+
compact,
22+
};

pipe/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", () => {
8+
const result = pipe([1, undefined, 2, null, 3], compact);
9+
const expected = [1, 2, 3];
10+
assertEquals(Array.from(result), expected);
11+
assertType<IsExact<typeof result, Iterable<number>>>(true);
12+
});
13+
});

pipe/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/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/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/compress";
15+
*
16+
* const iter = pipe(
17+
* [1, 2, 3, 4, 5],
18+
* compress([true, false, true, false, true]),
19+
* );
20+
* console.log(Array.from(iter)); // [1, 3, 5]
21+
* ```
22+
*/
23+
export function compress(
24+
selectors: Iterable<boolean>,
25+
): <T>(iterable: Iterable<T>) => Iterable<T> {
26+
return (iterable) => base(iterable, selectors);
27+
}

pipe/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", () => {
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(Array.from(result), expected);
14+
assertType<IsExact<typeof result, Iterable<number>>>(true);
15+
});
16+
});

pipe/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/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/cycle/~/cycle cycle} for native cycle.
8+
*
9+
* @example
10+
* ```ts
11+
* import { pipe } from "@core/pipe";
12+
* import { cycle } from "@core/iterutil/pipe/cycle";
13+
* import { take } from "@core/iterutil/pipe/take";
14+
*
15+
* const iter = pipe(
16+
* [1, 2, 3],
17+
* cycle,
18+
* take(5),
19+
* );
20+
* console.log(Array.from(iter)); // [1, 2, 3, 1, 2]
21+
* ```
22+
*/
23+
cycle,
24+
};

0 commit comments

Comments
 (0)