From 27b5d21cba5a3d24c632f4bee93646d8b2d87fe7 Mon Sep 17 00:00:00 2001 From: hw Date: Tue, 22 Oct 2024 00:29:15 +0900 Subject: [PATCH] feat: add `chunk` to `fx` --- src/Lazy/fx.ts | 21 +++++++++++++++++++ test/Lazy/chunk.spec.ts | 39 +++++++++++++++++++++++++++++++++++ type-check/Lazy/chunk.test.ts | 18 ++++++++++++---- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/Lazy/fx.ts b/src/Lazy/fx.ts index 6265c128..f7f511e2 100644 --- a/src/Lazy/fx.ts +++ b/src/Lazy/fx.ts @@ -15,6 +15,7 @@ import type { DeepFlat } from "../types/DeepFlat"; import type IterableInfer from "../types/IterableInfer"; import type Key from "../types/Key"; import type { SyncReducer } from "../types/Reducer"; +import chunk from "./chunk"; import concurrent from "./concurrent"; import drop from "./drop"; import filter from "./filter"; @@ -189,6 +190,16 @@ class FxAsyncIterable { return new FxAsyncIterable(f(this.asyncIterable)); } + /** + * Returns AsyncIterable of elements split into groups the length of size. + * If AsyncIterableIterator can't be split evenly, the final chunk will be the remaining elements. + * + * see {@link https://fxts.dev/docs/chunk | chunk} + */ + chunk(size: number) { + return new FxAsyncIterable(chunk(size, this.asyncIterable)); + } + /** * Concurrent is used to balance the load of multiple asynchronous requests. * 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 { return new FxIterable(f(this.iterable)); } + /** + * Returns Iterable of elements split into groups the length of size. + * If iterableIterator can't be split evenly, the final chunk will be the remaining elements. + * + * see {@link https://fxts.dev/docs/chunk | chunk} + */ + chunk(size: number) { + return new FxIterable(chunk(size, this.iterable)); + } + /** * Returns AsyncIterable, `toAsync` used when you want to handle Promise values inside Iterable. * diff --git a/test/Lazy/chunk.spec.ts b/test/Lazy/chunk.spec.ts index 5362a9c2..d82ab4f1 100644 --- a/test/Lazy/chunk.spec.ts +++ b/test/Lazy/chunk.spec.ts @@ -3,6 +3,7 @@ import { concurrent, delay, filter, + fx, map, pipe, range, @@ -46,6 +47,25 @@ describe("chunk", function () { const res = pipe(range(1, 12), chunk(3), toArray); expect(res).toEqual(expected); }); + + it.each([ + [ + [1, 2, 3, 4], + [ + [1, 2], + [3, 4], + ], + ], + [ + [1, 2, 3, 4, 5], + [[1, 2], [3, 4], [5]], + ], + ])( + "should be able to be used as a chaining method in the `fx`", + function (data: number[], result: number[][]) { + expect(fx(data).chunk(2).toArray()).toEqual(result); + }, + ); }); describe("async", function () { @@ -132,6 +152,25 @@ describe("chunk", function () { expect(res).toEqual(expected); }); + it.each([ + [ + [1, 2, 3, 4], + [ + [1, 2], + [3, 4], + ], + ], + [ + [1, 2, 3, 4, 5], + [[1, 2], [3, 4], [5]], + ], + ])( + "should be able to be used as a chaining method in the `fx`", + async function (data: number[], result: number[][]) { + expect(await fx(data).toAsync().chunk(2).toArray()).toEqual(result); + }, + ); + it("should be passed concurrent object when job works concurrently", async function () { const mock = generatorMock(); const iter = chunk(3, mock); diff --git a/type-check/Lazy/chunk.test.ts b/type-check/Lazy/chunk.test.ts index 8ee38b52..9836876a 100644 --- a/type-check/Lazy/chunk.test.ts +++ b/type-check/Lazy/chunk.test.ts @@ -1,4 +1,5 @@ -import { chunk, pipe, toAsync } from "../../src"; +import { chunk, fx, pipe, toAsync } from "../../src"; +import type Cast from "../../src/types/Cast"; import * as Test from "../../src/types/Test"; const { checks, check } = Test; @@ -6,18 +7,27 @@ const { checks, check } = Test; const res1 = chunk(1, []); const res2 = chunk(1, [1, 2, 3]); const res3 = chunk(1, toAsync([1, 2, 3])); - const res4 = pipe([1, 2, 3, 4], chunk(1)); const res5 = pipe(Promise.resolve([1, 2, 3, 4]), chunk(1)); const res6 = pipe(toAsync([1, 2, 3, 4]), chunk(1)); +const res7 = fx([1, 2, 3, 4]).chunk(2); +const res8 = fx([1, 2, 3, 4]).toAsync().chunk(2); checks([ check, Test.Pass>(), - check, Test.Pass>(), check, Test.Pass>(), - check, Test.Pass>(), check>, Test.Pass>(), check, Test.Pass>(), + check< + typeof res7, + Cast, typeof res7>, + Test.Pass + >(), + check< + typeof res8, + Cast, typeof res8>, + Test.Pass + >(), ]);