Skip to content

Commit 0263039

Browse files
authored
feat: add chunk to fx (marpple#291)
* feat: add `chunk` to `fx` * chore: codeowners
1 parent 5c01ac2 commit 0263039

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
@ppeeou @shine1594 @hg-pyun
33

44
# source
5-
/src/ @ppeeou @shine1594
5+
/src/ @ppeeou

src/Lazy/fx.ts

+21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { DeepFlat } from "../types/DeepFlat";
1515
import type IterableInfer from "../types/IterableInfer";
1616
import type Key from "../types/Key";
1717
import type { SyncReducer } from "../types/Reducer";
18+
import chunk from "./chunk";
1819
import concurrent from "./concurrent";
1920
import drop from "./drop";
2021
import filter from "./filter";
@@ -189,6 +190,16 @@ class FxAsyncIterable<A> {
189190
return new FxAsyncIterable(f(this.asyncIterable));
190191
}
191192

193+
/**
194+
* Returns AsyncIterable of elements split into groups the length of size.
195+
* If AsyncIterableIterator can't be split evenly, the final chunk will be the remaining elements.
196+
*
197+
* see {@link https://fxts.dev/docs/chunk | chunk}
198+
*/
199+
chunk(size: number) {
200+
return new FxAsyncIterable(chunk(size, this.asyncIterable));
201+
}
202+
192203
/**
193204
* Concurrent is used to balance the load of multiple asynchronous requests.
194205
* The first argument receives a number that controls the number of loads, and the second argument is an AsyncIterable.
@@ -473,6 +484,16 @@ export class FxIterable<A> {
473484
return new FxIterable(f(this.iterable));
474485
}
475486

487+
/**
488+
* Returns Iterable of elements split into groups the length of size.
489+
* If iterableIterator can't be split evenly, the final chunk will be the remaining elements.
490+
*
491+
* see {@link https://fxts.dev/docs/chunk | chunk}
492+
*/
493+
chunk(size: number) {
494+
return new FxIterable(chunk(size, this.iterable));
495+
}
496+
476497
/**
477498
* Returns AsyncIterable, `toAsync` used when you want to handle Promise values inside Iterable.
478499
*

test/Lazy/chunk.spec.ts

+39
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
concurrent,
44
delay,
55
filter,
6+
fx,
67
map,
78
pipe,
89
range,
@@ -46,6 +47,25 @@ describe("chunk", function () {
4647
const res = pipe(range(1, 12), chunk(3), toArray);
4748
expect(res).toEqual(expected);
4849
});
50+
51+
it.each([
52+
[
53+
[1, 2, 3, 4],
54+
[
55+
[1, 2],
56+
[3, 4],
57+
],
58+
],
59+
[
60+
[1, 2, 3, 4, 5],
61+
[[1, 2], [3, 4], [5]],
62+
],
63+
])(
64+
"should be able to be used as a chaining method in the `fx`",
65+
function (data: number[], result: number[][]) {
66+
expect(fx(data).chunk(2).toArray()).toEqual(result);
67+
},
68+
);
4969
});
5070

5171
describe("async", function () {
@@ -132,6 +152,25 @@ describe("chunk", function () {
132152
expect(res).toEqual(expected);
133153
});
134154

155+
it.each([
156+
[
157+
[1, 2, 3, 4],
158+
[
159+
[1, 2],
160+
[3, 4],
161+
],
162+
],
163+
[
164+
[1, 2, 3, 4, 5],
165+
[[1, 2], [3, 4], [5]],
166+
],
167+
])(
168+
"should be able to be used as a chaining method in the `fx`",
169+
async function (data: number[], result: number[][]) {
170+
expect(await fx(data).toAsync().chunk(2).toArray()).toEqual(result);
171+
},
172+
);
173+
135174
it("should be passed concurrent object when job works concurrently", async function () {
136175
const mock = generatorMock();
137176
const iter = chunk(3, mock);

type-check/Lazy/chunk.test.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
import { chunk, pipe, toAsync } from "../../src";
1+
import { chunk, fx, pipe, toAsync } from "../../src";
2+
import type Cast from "../../src/types/Cast";
23
import * as Test from "../../src/types/Test";
34

45
const { checks, check } = Test;
56

67
const res1 = chunk(1, []);
78
const res2 = chunk(1, [1, 2, 3]);
89
const res3 = chunk(1, toAsync([1, 2, 3]));
9-
1010
const res4 = pipe([1, 2, 3, 4], chunk(1));
1111
const res5 = pipe(Promise.resolve([1, 2, 3, 4]), chunk(1));
1212
const res6 = pipe(toAsync([1, 2, 3, 4]), chunk(1));
13+
const res7 = fx([1, 2, 3, 4]).chunk(2);
14+
const res8 = fx([1, 2, 3, 4]).toAsync().chunk(2);
1315

1416
checks([
1517
check<typeof res1, IterableIterator<never[]>, Test.Pass>(),
16-
1718
check<typeof res2, IterableIterator<number[]>, Test.Pass>(),
1819
check<typeof res3, AsyncIterableIterator<number[]>, Test.Pass>(),
19-
2020
check<typeof res4, IterableIterator<number[]>, Test.Pass>(),
2121
check<typeof res5, Promise<IterableIterator<number[]>>, Test.Pass>(),
2222
check<typeof res6, AsyncIterableIterator<number[]>, Test.Pass>(),
23+
check<
24+
typeof res7,
25+
Cast<IterableIterator<number[]>, typeof res7>,
26+
Test.Pass
27+
>(),
28+
check<
29+
typeof res8,
30+
Cast<AsyncIterableIterator<number[]>, typeof res8>,
31+
Test.Pass
32+
>(),
2333
]);

0 commit comments

Comments
 (0)