@@ -7,27 +7,28 @@ We show the most common usages here, but if you need more ways to check for equa
7
7
## Primitive equality
8
8
9
9
``` ts
10
- import { eq } from " fp-ts" ;
10
+ import { boolean , date , number , string } from " fp-ts" ;
11
11
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
16
16
```
17
17
18
18
## Compare structures
19
19
20
20
``` ts
21
- import { eq } from " fp-ts" ;
21
+ import { number } from " fp-ts" ;
22
+ import { Eq , struct } from " fp-ts/Eq" ;
22
23
23
24
type Point = {
24
25
x: number ;
25
26
y: number ;
26
27
};
27
28
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 ,
31
32
});
32
33
33
34
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
36
37
This structure can be combined further:
37
38
38
39
``` ts
39
- import { eq } from " fp-ts" ;
40
+ import { number } from " fp-ts" ;
41
+ import { Eq , struct } from " fp-ts/Eq" ;
40
42
41
43
type Point = {
42
44
x: number ;
43
45
y: number ;
44
46
};
45
47
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 ,
49
51
});
50
52
51
53
type Vector = {
52
54
from: Point ;
53
55
to: Point ;
54
56
};
55
57
56
- const eqVector: eq . Eq <Vector > = eq . getStructEq ({
58
+ const eqVector: Eq <Vector > = struct ({
57
59
from: eqPoint ,
58
60
to: eqPoint ,
59
61
});
60
62
61
- eqVector .equals (
63
+ const x = eqVector .equals (
62
64
{ from: { x: 0 , y: 0 }, to: { x: 0 , y: 0 } },
63
65
{ from: { x: 0 , y: 0 }, to: { x: 0 , y: 0 } }
64
66
); // true
@@ -67,29 +69,30 @@ eqVector.equals(
67
69
## Compare arrays
68
70
69
71
``` ts
70
- import { array , eq } from " fp-ts" ;
72
+ import { array , string } from " fp-ts" ;
71
73
72
- const eqArrayOfStrings = array .getEq (eq . eqString );
74
+ const eqArrayOfStrings = array .getEq (string . Eq );
73
75
74
76
eqArrayOfStrings .equals ([" Time" , " After" , " Time" ], [" Time" , " After" , " Time" ]); // true
75
77
```
76
78
77
79
Test the equality of structures nested within arrays:
78
80
79
81
``` ts
80
- import { array , eq } from " fp-ts" ;
82
+ import { array , number } from " fp-ts" ;
83
+ import { Eq , struct } from " fp-ts/Eq" ;
81
84
82
85
type Point = {
83
86
x: number ;
84
87
y: number ;
85
88
};
86
89
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 ,
90
93
});
91
94
92
- const eqArrayOfPoints: eq . Eq < Array < Point >> = array .getEq (eqPoint );
95
+ const eqArrayOfPoints = array .getEq (eqPoint );
93
96
94
97
eqArrayOfPoints .equals (
95
98
[
@@ -108,14 +111,14 @@ eqArrayOfPoints.equals(
108
111
In this example, two users are equal if their ` userId ` field is equal.
109
112
110
113
``` ts
111
- import { eq } from " fp-ts" ;
114
+ import { eq , number } from " fp-ts" ;
112
115
113
116
type User = {
114
117
userId: number ;
115
118
name: string ;
116
119
};
117
120
118
- const eqUserId = eq .contramap ((user : User ) => user .userId )(eq . eqNumber );
121
+ const eqUserId = eq .contramap ((user : User ) => user .userId )(number . Eq );
119
122
120
123
eqUserId .equals ({ userId: 1 , name: " Giulio" }, { userId: 1 , name: " Giulio Canti" }); // true
121
124
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" }); /
126
129
Many data types provide ` Eq ` instances. Here's [ Option] ( https://gcanti.github.io/fp-ts/modules/Option.ts ) :
127
130
128
131
``` ts
129
- import { eq , option } from " fp-ts" ;
132
+ import { option , number } from " fp-ts" ;
130
133
131
- const E = option .getEq (eq . eqNumber );
134
+ const E = option .getEq (number . Eq );
132
135
133
136
E .equals (option .some (3 ), option .some (3 )); // true
134
137
E .equals (option .none , option .some (4 )); // false
@@ -138,9 +141,9 @@ E.equals(option.none, option.none); // true
138
141
It works similarly for [ Either] ( https://gcanti.github.io/fp-ts/modules/Either.ts ) and other types where it is possible to determine equality:
139
142
140
143
``` ts
141
- import { either , eq } from " fp-ts" ;
144
+ import { either , number , string } from " fp-ts" ;
142
145
143
- const E = either .getEq (eq . eqString , eq . eqNumber );
146
+ const E = either .getEq (string . Eq , number . Eq );
144
147
145
148
E .equals (either .right (3 ), either .right (3 )); // true
146
149
E .equals (either .left (" 3" ), either .right (3 )); // false
0 commit comments