Skip to content

Commit a9ecfcc

Browse files
authored
Merge pull request #18 from adomokos/ad/update-eq-examples
Fix fp-ts deprecation warnings in equality examples
2 parents 1ceb74b + 6a06229 commit a9ecfcc

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

docs/equality.md

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,28 @@ We show the most common usages here, but if you need more ways to check for equa
77
## Primitive equality
88

99
```ts
10-
import { eq } from "fp-ts";
10+
import { boolean, date, number, string } from "fp-ts";
1111

12-
eq.eqBoolean.equals(true, true); // true
13-
eq.eqDate.equals(new Date("1984-01-27"), new Date("1984-01-27")); // true
14-
eq.eqNumber.equals(3, 3); // true
15-
eq.eqString.equals("Cyndi", "Cyndi"); // true
12+
boolean.Eq.equals(true, true); // true
13+
date.Eq.equals(new Date("1984-01-27"), new Date("1984-01-27")); // true
14+
number.Eq.equals(3, 3); // true
15+
string.Eq.equals("Cyndi", "Cyndi"); // true
1616
```
1717

1818
## Compare structures
1919

2020
```ts
21-
import { eq } from "fp-ts";
21+
import { number } from "fp-ts";
22+
import { Eq, struct } from "fp-ts/Eq";
2223

2324
type Point = {
2425
x: number;
2526
y: number;
2627
};
2728

28-
const eqPoint: eq.Eq<Point> = eq.getStructEq({
29-
x: eq.eqNumber,
30-
y: eq.eqNumber,
29+
const eqPoint: Eq<Point> = struct({
30+
x: number.Eq,
31+
y: number.Eq,
3132
});
3233

3334
eqPoint.equals({ x: 0, y: 0 }, { x: 0, y: 0 }); // true
@@ -36,29 +37,30 @@ eqPoint.equals({ x: 0, y: 0 }, { x: 0, y: 0 }); // true
3637
This structure can be combined further:
3738

3839
```ts
39-
import { eq } from "fp-ts";
40+
import { number } from "fp-ts";
41+
import { Eq, struct } from "fp-ts/Eq";
4042

4143
type Point = {
4244
x: number;
4345
y: number;
4446
};
4547

46-
const eqPoint: eq.Eq<Point> = eq.getStructEq({
47-
x: eq.eqNumber,
48-
y: eq.eqNumber,
48+
const eqPoint: Eq<Point> = struct({
49+
x: number.Eq,
50+
y: number.Eq,
4951
});
5052

5153
type Vector = {
5254
from: Point;
5355
to: Point;
5456
};
5557

56-
const eqVector: eq.Eq<Vector> = eq.getStructEq({
58+
const eqVector: Eq<Vector> = struct({
5759
from: eqPoint,
5860
to: eqPoint,
5961
});
6062

61-
eqVector.equals(
63+
const x = eqVector.equals(
6264
{ from: { x: 0, y: 0 }, to: { x: 0, y: 0 } },
6365
{ from: { x: 0, y: 0 }, to: { x: 0, y: 0 } }
6466
); // true
@@ -67,29 +69,30 @@ eqVector.equals(
6769
## Compare arrays
6870

6971
```ts
70-
import { array, eq } from "fp-ts";
72+
import { array, string } from "fp-ts";
7173

72-
const eqArrayOfStrings = array.getEq(eq.eqString);
74+
const eqArrayOfStrings = array.getEq(string.Eq);
7375

7476
eqArrayOfStrings.equals(["Time", "After", "Time"], ["Time", "After", "Time"]); // true
7577
```
7678

7779
Test the equality of structures nested within arrays:
7880

7981
```ts
80-
import { array, eq } from "fp-ts";
82+
import { array, number } from "fp-ts";
83+
import { Eq, struct } from "fp-ts/Eq";
8184

8285
type Point = {
8386
x: number;
8487
y: number;
8588
};
8689

87-
const eqPoint: eq.Eq<Point> = eq.getStructEq({
88-
x: eq.eqNumber,
89-
y: eq.eqNumber,
90+
const eqPoint: Eq<Point> = struct({
91+
x: number.Eq,
92+
y: number.Eq,
9093
});
9194

92-
const eqArrayOfPoints: eq.Eq<Array<Point>> = array.getEq(eqPoint);
95+
const eqArrayOfPoints = array.getEq(eqPoint);
9396

9497
eqArrayOfPoints.equals(
9598
[
@@ -108,14 +111,14 @@ eqArrayOfPoints.equals(
108111
In this example, two users are equal if their `userId` field is equal.
109112

110113
```ts
111-
import { eq } from "fp-ts";
114+
import { eq, number } from "fp-ts";
112115

113116
type User = {
114117
userId: number;
115118
name: string;
116119
};
117120

118-
const eqUserId = eq.contramap((user: User) => user.userId)(eq.eqNumber);
121+
const eqUserId = eq.contramap((user: User) => user.userId)(number.Eq);
119122

120123
eqUserId.equals({ userId: 1, name: "Giulio" }, { userId: 1, name: "Giulio Canti" }); // true
121124
eqUserId.equals({ userId: 1, name: "Giulio" }, { userId: 2, name: "Giulio" }); // false
@@ -126,9 +129,9 @@ eqUserId.equals({ userId: 1, name: "Giulio" }, { userId: 2, name: "Giulio" }); /
126129
Many data types provide `Eq` instances. Here's [Option](https://gcanti.github.io/fp-ts/modules/Option.ts):
127130

128131
```ts
129-
import { eq, option } from "fp-ts";
132+
import { option, number } from "fp-ts";
130133

131-
const E = option.getEq(eq.eqNumber);
134+
const E = option.getEq(number.Eq);
132135

133136
E.equals(option.some(3), option.some(3)); // true
134137
E.equals(option.none, option.some(4)); // false
@@ -138,9 +141,9 @@ E.equals(option.none, option.none); // true
138141
It works similarly for [Either](https://gcanti.github.io/fp-ts/modules/Either.ts) and other types where it is possible to determine equality:
139142

140143
```ts
141-
import { either, eq } from "fp-ts";
144+
import { either, number, string } from "fp-ts";
142145

143-
const E = either.getEq(eq.eqString, eq.eqNumber);
146+
const E = either.getEq(string.Eq, number.Eq);
144147

145148
E.equals(either.right(3), either.right(3)); // true
146149
E.equals(either.left("3"), either.right(3)); // false

0 commit comments

Comments
 (0)