-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from jsr-core/pipe
feat: support `@core/pipe` package
- Loading branch information
Showing
108 changed files
with
3,180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { pipe } from "@core/pipe"; | ||
import * as rootMod from "@core/iterutil"; | ||
import * as asyncMod from "@core/iterutil/async"; | ||
import * as pipeMod from "@core/iterutil/pipe"; | ||
import * as pipeAsyncMod from "@core/iterutil/pipe/async"; | ||
|
||
Deno.test("README", async (t) => { | ||
await t.step("case 1", () => { | ||
const iter = rootMod.map([1, 2, 3], (v) => v * 2); | ||
assertEquals(Array.from(iter), [2, 4, 6]); | ||
}); | ||
|
||
await t.step("case 2", async () => { | ||
const iter = asyncMod.map([1, 2, 3], (v) => Promise.resolve(v * 2)); | ||
assertEquals(await Array.fromAsync(iter), [2, 4, 6]); | ||
}); | ||
|
||
await t.step("case 3", () => { | ||
const iter = pipe( | ||
[1, 2, 3], | ||
pipeMod.map((v) => v * 2), | ||
pipeMod.cycle, | ||
pipeMod.take(10), | ||
pipeMod.filter((v) => v % 2 === 0), | ||
); | ||
assertEquals(Array.from(iter), [2, 4, 6, 2, 4, 6, 2, 4, 6, 2]); | ||
}); | ||
|
||
await t.step("case 4", async () => { | ||
const iter = pipe( | ||
[1, 2, 3], | ||
pipeAsyncMod.map((v) => v * 2), | ||
pipeAsyncMod.cycle, | ||
pipeAsyncMod.take(10), | ||
pipeAsyncMod.filter((v) => v % 2 === 0), | ||
); | ||
assertEquals(await Array.fromAsync(iter), [2, 4, 6, 2, 4, 6, 2, 4, 6, 2]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { type Chain, chain as base } from "@core/iterutil/async/chain"; | ||
|
||
/** | ||
* Returns an operator that chains multiple iterables to the iterable. | ||
* | ||
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/chain/~/chain chain} for native chain. | ||
* | ||
* @param iterables The iterables to chain to the iterable. | ||
* @returns An operator that chains multiple iterables to the iterable. | ||
* | ||
* @example | ||
* ```ts | ||
* import { pipe } from "@core/pipe"; | ||
* import { chain } from "@core/iterutil/pipe/async/chain"; | ||
* | ||
* const iter = pipe( | ||
* [1, 2, 3], | ||
* chain(["a", "b"], [true]), | ||
* ); | ||
* console.log(await Array.fromAsync(iter)); // [1, 2, 3, "a", "b", true] | ||
* ``` | ||
*/ | ||
export function chain< | ||
U extends readonly [ | ||
Iterable<unknown> | AsyncIterable<unknown>, | ||
...(Iterable<unknown> | AsyncIterable<unknown>)[], | ||
], | ||
>( | ||
...iterables: U | ||
): <T>( | ||
iterable: Iterable<T> | AsyncIterable<T>, | ||
) => AsyncIterable<T | Chain<U>> { | ||
return <T>(iterable: Iterable<T> | AsyncIterable<T>) => | ||
base(iterable, ...iterables) as AsyncIterable<T | Chain<U>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { assertType, type IsExact } from "@std/testing/types"; | ||
import { pipe } from "@core/pipe"; | ||
import { chain } from "./chain.ts"; | ||
|
||
Deno.test("chain", async (t) => { | ||
await t.step("usage", async () => { | ||
const result = pipe( | ||
[1, 2, 3], | ||
chain(["a", "b"], [true]), | ||
); | ||
const expected = [1, 2, 3, "a", "b", true]; | ||
assertEquals(await Array.fromAsync(result), expected); | ||
assertType< | ||
IsExact<typeof result, AsyncIterable<number | string | boolean>> | ||
>( | ||
true, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { chunked as base } from "@core/iterutil/async/chunked"; | ||
|
||
/** | ||
* Returns an operator that chunks the iterable into arrays of `size`. | ||
* | ||
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/chunked/~/chunked chunked} for native chunked. | ||
* | ||
* @param size The size of each chunk. | ||
* @return An operator that chunks the iterable into arrays of `size`. | ||
* | ||
* @example | ||
* ```ts | ||
* import { pipe } from "@core/pipe"; | ||
* import { chunked } from "@core/iterutil/pipe/async/chunked"; | ||
* | ||
* const iter = pipe( | ||
* [1, 2, 3, 4, 5], | ||
* chunked(2), | ||
* ); | ||
* console.log(await Array.fromAsync(iter)); // [[1, 2], [3, 4], [5]] | ||
* ``` | ||
*/ | ||
export function chunked( | ||
size: number, | ||
): <T>(iterable: Iterable<T> | AsyncIterable<T>) => AsyncIterable<T[]> { | ||
return (iterable) => base(iterable, size); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { assertType, type IsExact } from "@std/testing/types"; | ||
import { pipe } from "@core/pipe"; | ||
import { chunked } from "./chunked.ts"; | ||
|
||
Deno.test("chunked", async (t) => { | ||
await t.step("usage", async () => { | ||
const result = pipe([1, 2, 3, 4, 5, 6], chunked(2)); | ||
const expected = [[1, 2], [3, 4], [5, 6]]; | ||
assertEquals(await Array.fromAsync(result), expected); | ||
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { compact } from "@core/iterutil/async/compact"; | ||
|
||
export { | ||
/** | ||
* An operator to remove all nullish (`null` or `undefined`) values from an iterable. | ||
* | ||
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/compact/~/compact compact} for native compact. | ||
* | ||
* @example | ||
* ```ts | ||
* import { pipe } from "@core/pipe"; | ||
* import { compact } from "@core/iterutil/pipe/async/compact"; | ||
* | ||
* const iter = pipe( | ||
* [1, undefined, 2, null, 3], | ||
* compact, | ||
* ); | ||
* console.log(await Array.fromAsync(iter)); // [1, 2, 3] | ||
* ``` | ||
*/ | ||
compact, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { assertType, type IsExact } from "@std/testing/types"; | ||
import { pipe } from "@core/pipe"; | ||
import { compact } from "./compact.ts"; | ||
|
||
Deno.test("compact", async (t) => { | ||
await t.step("usage", async () => { | ||
const result = pipe([1, undefined, 2, null, 3], compact); | ||
const expected = [1, 2, 3]; | ||
assertEquals(await Array.fromAsync(result), expected); | ||
assertType<IsExact<typeof result, AsyncIterable<number>>>(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { compress as base } from "@core/iterutil/async/compress"; | ||
|
||
/** | ||
* Returns an operator that compresses an iterable by selecting elements using a selector iterable. | ||
* | ||
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/compress/~/compress compress} for native compress. | ||
* | ||
* @param selectors The selectors to use. | ||
* @returns An operator that compresses an iterable by selecting elements using a selector iterable. | ||
* | ||
* @example | ||
* ```ts | ||
* import { pipe } from "@core/pipe"; | ||
* import { compress } from "@core/iterutil/pipe/async/compress"; | ||
* | ||
* const iter = pipe( | ||
* [1, 2, 3, 4, 5], | ||
* compress([true, false, true, false, true]), | ||
* ); | ||
* console.log(await Array.fromAsync(iter)); // [1, 3, 5] | ||
* ``` | ||
*/ | ||
export function compress( | ||
selectors: Iterable<boolean> | AsyncIterable<boolean>, | ||
): <T>(iterable: Iterable<T> | AsyncIterable<T>) => AsyncIterable<T> { | ||
return (iterable) => base(iterable, selectors); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { assertType, type IsExact } from "@std/testing/types"; | ||
import { pipe } from "@core/pipe"; | ||
import { compress } from "./compress.ts"; | ||
|
||
Deno.test("compress", async (t) => { | ||
await t.step("usage", async () => { | ||
const result = pipe( | ||
[1, 2, 3, 4, 5], | ||
compress([true, false, true, false, true]), | ||
); | ||
const expected = [1, 3, 5]; | ||
assertEquals(await Array.fromAsync(result), expected); | ||
assertType<IsExact<typeof result, AsyncIterable<number>>>(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { cycle } from "@core/iterutil/async/cycle"; | ||
|
||
export { | ||
/** | ||
* An operator to return a function that cycles the elements of an iterable. | ||
* | ||
* See {@linkcode https://jsr.io/@core/iterutil/doc/async/cycle/~/cycle cycle} for native cycle. | ||
* | ||
* @example | ||
* ```ts | ||
* import { pipe } from "@core/pipe"; | ||
* import { cycle } from "@core/iterutil/pipe/async/cycle"; | ||
* import { take } from "@core/iterutil/pipe/async/take"; | ||
* | ||
* const iter = pipe( | ||
* [1, 2, 3], | ||
* cycle, | ||
* take(5), | ||
* ); | ||
* console.log(await Array.fromAsync(iter)); // [1, 2, 3, 1, 2] | ||
* ``` | ||
*/ | ||
cycle, | ||
}; |
Oops, something went wrong.