Skip to content

Commit 25f1bd6

Browse files
committed
Make FP comatible with Lodash and fp-ts (closes #3641)
Fixed types compatability with Lodash's `flow` and fp-ts's `pipe`.
1 parent 7b115b7 commit 25f1bd6

File tree

5 files changed

+84
-21
lines changed

5 files changed

+84
-21
lines changed

.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ module.exports = {
2424
plugins: ["@typescript-eslint"],
2525
ignorePatterns: ["lib", "tmp", "examples"],
2626
rules: {
27-
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
27+
"@typescript-eslint/no-unused-vars": [
28+
"error",
29+
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_" },
30+
],
2831
"@typescript-eslint/consistent-type-imports": "error",
2932
// The following rules are deprecated: https://eslint.org/blog/2023/10/deprecating-formatting-rules
3033
"array-bracket-newline": "off",

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6025,6 +6025,7 @@
60256025
"@octokit/core": "^3.2.5",
60266026
"@size-limit/esbuild": "^11.0.1",
60276027
"@size-limit/file": "^11.0.1",
6028+
"@types/lodash": "^4.14.202",
60286029
"@types/node": "^20.10.2",
60296030
"@types/sinon": "^9.0.6",
60306031
"@typescript-eslint/eslint-plugin": "^6.13.1",
@@ -6036,7 +6037,9 @@
60366037
"coveralls": "^3.1.1",
60376038
"eslint": "^8.55.0",
60386039
"firebase": "^3.7.1",
6040+
"fp-ts": "^2.16.2",
60396041
"js-fns": "^2.5.1",
6042+
"lodash": "^4.17.21",
60406043
"playwright": "^1.40.1",
60416044
"prettier": "^3.1.0",
60426045
"simple-git": "^2.35.2",

src/fp/_lib/convertToFP/test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/* eslint-env mocha */
22

33
import assert from "assert";
4+
import { pipe } from "fp-ts/function";
5+
import { flow as jsFnsFlow } from "js-fns";
6+
import lodashFlow from "lodash/flow";
47
import { describe, it } from "vitest";
8+
import { addDays, addHours, isEqual } from "../../index.js";
59
import { convertToFP } from "./index.js";
610

711
describe("convertToFP", () => {
@@ -77,4 +81,42 @@ describe("convertToFP", () => {
7781
assert(result === "undefined undefined undefined");
7882
});
7983
});
84+
85+
describe("types", () => {
86+
it("resolves proper types", () => {
87+
const fn1 = addDays();
88+
const fn2 = fn1(1);
89+
const result = fn2(new Date(1987, 1));
90+
assert(result.getFullYear() === 1987);
91+
});
92+
});
93+
94+
describe("Lodash", () => {
95+
it("works with flow", () => {
96+
const fn = lodashFlow(addDays(1), addHours(1));
97+
const result = fn(new Date(1987, 1, 11));
98+
assert(result.getFullYear() === 1987);
99+
assert.deepStrictEqual(result, new Date(1987, 1, 12, 1));
100+
});
101+
});
102+
103+
describe("fp-ts", () => {
104+
it("works with pipe", () => {
105+
const result = pipe(
106+
new Date(1987, 1, 11),
107+
isEqual(new Date(1987, 1, 11)),
108+
);
109+
const _assign: boolean = result;
110+
assert(result);
111+
});
112+
});
113+
114+
describe("js-fns", () => {
115+
it("works with flow", () => {
116+
const fn = jsFnsFlow(addDays(1), addHours(1));
117+
const result = fn(new Date(1987, 1, 11));
118+
assert(result.getFullYear() === 1987);
119+
assert.deepStrictEqual(result, new Date(1987, 1, 12, 1));
120+
});
121+
});
80122
});

src/fp/types.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,24 @@ export type FPFn<Fn extends FPFnInput, Arity extends FPArity> = Arity extends 4
3939
*/
4040
export interface FPFn1<Result, Arg> {
4141
/**
42-
* Returns the result of the function call.
42+
* Curried version of the function. Returns itself.
4343
*/
44-
(arg: Arg): Result;
44+
(): FPFn1<Result, Arg>;
4545

4646
/**
47-
* Curried version of the function. Returns itself.
47+
* Returns the result of the function call.
4848
*/
49-
(): FPFn1<Result, Arg>;
49+
(arg: Arg): Result;
5050
}
5151

5252
/**
5353
* FP function interface with 2 arguments.
5454
*/
5555
export interface FPFn2<Result, Arg2, Arg1> {
5656
/**
57-
* Returns the result of the function call.
57+
* Curried version of the function. Returns itself.
5858
*/
59-
(arg2: Arg2, arg1: Arg1): Result;
59+
(): FPFn2<Result, Arg2, Arg1>;
6060

6161
/**
6262
* Curried version of the function. Returns a function that accepts the rest
@@ -65,52 +65,52 @@ export interface FPFn2<Result, Arg2, Arg1> {
6565
(arg2: Arg2): FPFn1<Result, Arg1>;
6666

6767
/**
68-
* Curried version of the function. Returns itself.
68+
* Returns the result of the function call.
6969
*/
70-
(): FPFn2<Result, Arg2, Arg1>;
70+
(arg2: Arg2, arg1: Arg1): Result;
7171
}
7272

7373
/**
7474
* FP function interface with 3 arguments.
7575
*/
7676
export interface FPFn3<Result, Arg3, Arg2, Arg1> {
7777
/**
78-
* Returns the result of the function call.
78+
* Curried version of the function. Returns itself.
7979
*/
80-
(arg3: Arg3, arg2: Arg2, arg1: Arg1): Result;
80+
(): FPFn3<Result, Arg3, Arg2, Arg1>;
8181

8282
/**
8383
* Curried version of the function. Returns a function that accepts the rest
8484
* arguments.
8585
*/
86-
(arg3: Arg3, arg2: Arg2): FPFn1<Result, Arg1>;
86+
(arg3: Arg3): FPFn2<Result, Arg2, Arg1>;
8787

8888
/**
8989
* Curried version of the function. Returns a function that accepts the rest
9090
* arguments.
9191
*/
92-
(arg3: Arg3): FPFn2<Result, Arg2, Arg1>;
92+
(arg3: Arg3, arg2: Arg2): FPFn1<Result, Arg1>;
9393

9494
/**
95-
* Curried version of the function. Returns itself.
95+
* Returns the result of the function call.
9696
*/
97-
(): FPFn3<Result, Arg3, Arg2, Arg1>;
97+
(arg3: Arg3, arg2: Arg2, arg1: Arg1): Result;
9898
}
9999

100100
/**
101101
* FP function interface with 4 arguments.
102102
*/
103103
export interface FPFn4<Result, Arg4, Arg3, Arg2, Arg1> {
104104
/**
105-
* Returns the result of the function call.
105+
* Curried version of the function. Returns itself.
106106
*/
107-
(arg4: Arg4, arg3: Arg3, arg2: Arg2, arg1: Arg1): Result;
107+
(): FPFn4<Result, Arg4, Arg3, Arg2, Arg1>;
108108

109109
/**
110110
* Curried version of the function. Returns a function that accepts the rest
111111
* arguments.
112112
*/
113-
(arg4: Arg4, arg3: Arg3, arg2: Arg2): FPFn1<Result, Arg1>;
113+
(arg4: Arg4): FPFn3<Result, Arg3, Arg2, Arg1>;
114114

115115
/**
116116
* Curried version of the function. Returns a function that accepts the rest
@@ -122,10 +122,10 @@ export interface FPFn4<Result, Arg4, Arg3, Arg2, Arg1> {
122122
* Curried version of the function. Returns a function that accepts the rest
123123
* arguments.
124124
*/
125-
(arg4: Arg4): FPFn3<Result, Arg3, Arg2, Arg1>;
125+
(arg4: Arg4, arg3: Arg3, arg2: Arg2): FPFn1<Result, Arg1>;
126126

127127
/**
128-
* Curried version of the function. Returns itself.
128+
* Returns the result of the function call.
129129
*/
130-
(): FPFn4<Result, Arg4, Arg3, Arg2, Arg1>;
130+
(arg4: Arg4, arg3: Arg3, arg2: Arg2, arg1: Arg1): Result;
131131
}

0 commit comments

Comments
 (0)