Skip to content

Commit

Permalink
feat: add fx
Browse files Browse the repository at this point in the history
  • Loading branch information
ppeeou committed Mar 10, 2024
1 parent 46b7186 commit ee3d1f3
Show file tree
Hide file tree
Showing 25 changed files with 1,062 additions and 15 deletions.
567 changes: 567 additions & 0 deletions src/Lazy/fx.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Lazy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import entries from "./entries";
import filter from "./filter";
import flat from "./flat";
import flatMap from "./flatMap";
import fx from "./fx";
import intersection from "./intersection";
import intersectionBy from "./intersectionBy";
import keys from "./keys";
Expand Down Expand Up @@ -60,6 +61,7 @@ export {
filter,
flat,
flatMap,
fx,
intersection,
intersectionBy,
keys,
Expand Down
15 changes: 15 additions & 0 deletions test/Lazy/append.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
append,
concurrent,
delay,
fx,
map,
pipe,
range,
Expand All @@ -25,6 +26,11 @@ describe("append", function () {
const res = pipe(range(1, 4), append(4), append(5), append(6), toArray);
expect(res).toEqual([1, 2, 3, 4, 5, 6]);
});

it("should be able to be used chaining method with chain in the `fx`", function () {
const res = fx(range(1, 4)).chain(append(4)).chain(append(5)).toArray();
expect(res).toEqual([1, 2, 3, 4, 5]);
});
});

describe("async", function () {
Expand All @@ -42,6 +48,15 @@ describe("append", function () {
expect(res).toEqual([1, 2, 3, 4]);
});

it("should be able to be used chaining method with chain in the `fx`", async function () {
const res = await fx(range(1, 4))
.toAsync()
.chain(append(4))
.chain(append(5))
.toArray();
expect(res).toEqual([1, 2, 3, 4, 5]);
});

it("should be appended sequentially", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 3000);
Expand Down
13 changes: 13 additions & 0 deletions test/Lazy/concurrent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
concurrent,
delay,
filter,
fx,
map,
peek,
pipe,
Expand Down Expand Up @@ -60,6 +61,18 @@ describe("concurrent", function () {
expect(arr).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}, 550);

it("should be able to be used as a chaining method in the `fx`", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 500);

const arr = await fx(toAsync(range(1, 11)))
.map((a) => delay(100, a))
.concurrent(2)
.toArray();
expect(fn).toBeCalled();
expect(arr).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}, 550);

it("should be affected only one concurrent below it, when nested concurrent", async function () {
let fn: jest.Mock<any, any>;
fn = jest.fn();
Expand Down
21 changes: 21 additions & 0 deletions test/Lazy/drop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
delay,
drop,
filter,
fx,
map,
pipe,
toArray,
Expand Down Expand Up @@ -33,6 +34,16 @@ describe("drop", function () {

expect(res).toEqual([16, 18]);
});

it("should be able to be used as a chaining method in the `fx`", function () {
const res = fx([1, 2, 3, 4, 5, 6, 7, 8])
.map((a) => a + 10)
.filter((a) => a % 2 === 0)
.drop(2)
.toArray();

expect(res).toEqual([16, 18]);
});
});

describe("async", function () {
Expand Down Expand Up @@ -60,6 +71,16 @@ describe("drop", function () {
expect(res).toEqual([16, 18]);
});

it("should be able to be used as a chaining method in the `fx`", async function () {
const res = await fx(toAsync([1, 2, 3, 4, 5, 6, 7, 8]))
.map((a) => a + 10)
.filter((a) => a % 2 === 0)
.drop(2)
.toArray();

expect(res).toEqual([16, 18]);
});

it("should be discarded elements by length concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 400);
Expand Down
17 changes: 17 additions & 0 deletions test/Lazy/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
concurrent,
delay,
filter,
fx,
map,
pipe,
range,
Expand Down Expand Up @@ -54,6 +55,14 @@ describe("filter", function () {

expect(res).toEqual([2, 4]);
});

it("should be able to be used as a chaining method in the `fx`", function () {
const res = fx([1, 2, 3, 4])
.filter((a) => a % 2 === 0)
.toArray();

expect(res).toEqual([2, 4]);
});
});

describe("async", function () {
Expand Down Expand Up @@ -283,6 +292,14 @@ describe("filter", function () {
expect(res).toEqual([2, 4]);
});

it("should be able to be used as a chaining method in the `fx`", async function () {
const res = await fx(toAsync([1, 2, 3, 4]))
.filter((a) => a % 2 === 0)
.toArray();

expect(res).toEqual([2, 4]);
});

it("should be consumed 'AsyncIterable' as many times as called with 'next'", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 2000);
Expand Down
19 changes: 19 additions & 0 deletions test/Lazy/flat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
delay,
filter,
flat,
fx,
map,
pipe,
range,
Expand Down Expand Up @@ -37,6 +38,15 @@ describe("flat", function () {

expect(res).toEqual([11, 12, 13, 14, 15]);
});

it("should be able to be used as a chaining method in the `fx`", function () {
const res = fx([1, 2, 3, [4, 5]])
.flat()
.map((a) => a + 10)
.toArray();

expect(res).toEqual([11, 12, 13, 14, 15]);
});
});

describe("async", function () {
Expand Down Expand Up @@ -64,6 +74,15 @@ describe("flat", function () {
expect(res).toEqual([11, 12, 13, 14, 15]);
});

it("should be able to be used as a chaining method in the `fx`", async function () {
const res = await fx(toAsync([1, 2, 3, [4, 5]]))
.flat()
.map((a) => a + 10)
.toArray();

expect(res).toEqual([11, 12, 13, 14, 15]);
});

it("should be flattened concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 1000);
Expand Down
20 changes: 19 additions & 1 deletion test/Lazy/flatMap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flatMap, map, pipe, toArray, toAsync } from "../../src/index";
import { flatMap, fx, map, pipe, toArray, toAsync } from "../../src/index";
import { Concurrent } from "../../src/Lazy/concurrent";
import { generatorMock } from "../utils";

Expand All @@ -25,6 +25,15 @@ describe("flatMap", function () {

expect(res).toEqual(["IT", "IS", "A", "GOOD", "DAY"]);
});

it("should be able to be used as a chaining method in the `fx`", function () {
const res = fx(["It is", "a good", "day"])
.flatMap((s) => s.split(" "))
.map((a) => a.toUpperCase())
.toArray();

expect(res).toEqual(["IT", "IS", "A", "GOOD", "DAY"]);
});
});

describe("async", function () {
Expand All @@ -48,6 +57,15 @@ describe("flatMap", function () {
expect(res).toEqual(["IT", "IS", "A", "GOOD", "DAY"]);
});

it("should be able to be used as a chaining method in the `fx`", async function () {
const res = await fx(toAsync(["It is", "a good", "day"]))
.flatMap((s) => s.split(" "))
.map((a) => a.toUpperCase())
.toArray();

expect(res).toEqual(["IT", "IS", "A", "GOOD", "DAY"]);
});

it("should be passed concurrent object when job works concurrently", async function () {
const mock = generatorMock();
const iter = flatMap((a) => a, mock);
Expand Down
27 changes: 27 additions & 0 deletions test/Lazy/fx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { fx, toAsync } from "../../src";

describe("fx", function () {
describe("sync", function () {
it("handle fx iterable", function () {
const res = fx([1, 2, 3, 4, 5])
.map((a) => a + 10)
.toArray();
expect(res).toEqual([11, 12, 13, 14, 15]);
});
});

describe("async", () => {
it("handle fx asyncIterable", async function () {
const res1 = await fx(toAsync([1, 2, 3, 4, 5]))
.map(async (a) => a + 10)
.toArray();
expect(res1).toEqual([11, 12, 13, 14, 15]);

const res2 = await fx([1, 2, 3, 4, 5])
.toAsync()
.map(async (a) => a + 10)
.toArray();
expect(res2).toEqual([11, 12, 13, 14, 15]);
});
});
});
21 changes: 20 additions & 1 deletion test/Lazy/map.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AsyncFunctionException } from "../../src/_internal/error";
import { map, pipe, range, toArray, toAsync } from "../../src/index";
import { fx, map, pipe, range, toArray, toAsync } from "../../src/index";
import { Concurrent } from "../../src/Lazy/concurrent";
import { generatorMock } from "../utils";

Expand Down Expand Up @@ -32,6 +32,16 @@ describe("map", function () {

expect(res).toEqual(["1", "2", "3", "4"]);
});

it("should be able to be used as a chaining method in the `fx`", function () {
const res = fx([1, 2, 3, 4])
.map((a) => a)
.map((a) => String(a))
.map((a) => a)
.toArray();

expect(res).toEqual(["1", "2", "3", "4"]);
});
});

describe("async", function () {
Expand Down Expand Up @@ -83,6 +93,15 @@ describe("map", function () {
expect(res).toEqual(["1", "2", "3", "4"]);
});

it("should be able to be used as a chaining method in the `fx`", async function () {
const res = await fx(toAsync([1, 2, 3, 4]))
.map((a) => Promise.resolve(a))
.map((a) => String(a))
.toArray();

expect(res).toEqual(["1", "2", "3", "4"]);
});

it("should be passed concurrent object when job works concurrently", async function () {
const mock = generatorMock();
const iter = map((a) => a, mock);
Expand Down
26 changes: 25 additions & 1 deletion test/Lazy/peek.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map, peek, pipe, toArray, toAsync } from "../../src/index";
import { fx, map, peek, pipe, toArray, toAsync } from "../../src/index";
import { Concurrent } from "../../src/Lazy/concurrent";
import { generatorMock } from "../utils";

Expand Down Expand Up @@ -27,6 +27,18 @@ describe("peek", function () {
expect(res).toEqual([21, 22, 23, 24]);
});

it("should be able to be used as a chaining method in the `fx`", function () {
let sum = 0;
const res = fx([1, 2, 3, 4])
.map((a) => a + 10)
.peek((a) => (sum = sum + a))
.map((a) => a + 10)
.toArray();

expect(sum).toEqual(50);
expect(res).toEqual([21, 22, 23, 24]);
});

it("should be able to handle an error", function () {
const res: number[] = [];
expect(() =>
Expand Down Expand Up @@ -70,6 +82,18 @@ describe("peek", function () {
expect(res).toEqual([21, 22, 23, 24]);
});

it("should be able to be used as a chaining method in the `fx`", async function () {
let sum = 0;
const res = await fx(toAsync([1, 2, 3, 4]))
.map((a) => a + 10)
.peek((a) => (sum = sum + a))
.map((a) => a + 10)
.toArray();

expect(sum).toEqual(50);
expect(res).toEqual([21, 22, 23, 24]);
});

it("should be able to handle an error", async function () {
const res: number[] = [];

Expand Down
18 changes: 17 additions & 1 deletion test/Lazy/slice.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe, slice, toArray, toAsync } from "../../src";
import { fx, pipe, slice, toArray, toAsync } from "../../src";
import { Concurrent } from "../../src/Lazy/concurrent";
import { generatorMock } from "../utils";

Expand Down Expand Up @@ -41,6 +41,14 @@ describe("slice", function () {
const res2 = pipe([1, 2, 3, 4, 5], slice(1, 3), toArray);
expect(res2).toEqual([2, 3]);
});

it("should be able to be used as a chaining method in the `fx`", function () {
const res1 = fx([1, 2, 3, 4, 5]).slice(2).toArray();
expect(res1).toEqual([3, 4, 5]);

const res2 = fx([1, 2, 3, 4, 5]).slice(1, 3).toArray();
expect(res2).toEqual([2, 3]);
});
});

describe("async", function () {
Expand Down Expand Up @@ -84,6 +92,14 @@ describe("slice", function () {
expect(res2).toEqual([2, 3]);
});

it("should be able to be used as a chaining method in the `fx`", async function () {
const res1 = await fx([1, 2, 3, 4, 5]).toAsync().slice(2).toArray();
expect(res1).toEqual([3, 4, 5]);

const res2 = await fx([1, 2, 3, 4, 5]).toAsync().slice(1, 3).toArray();
expect(res2).toEqual([2, 3]);
});

it("should be passed concurrent object when job works concurrently", async function () {
const mock = generatorMock();
const iter = slice(1, 2, mock);
Expand Down
Loading

0 comments on commit ee3d1f3

Please sign in to comment.