-
Notifications
You must be signed in to change notification settings - Fork 1
/
99-playground.ts
122 lines (105 loc) · 2.9 KB
/
99-playground.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import * as E from "fp-ts/Either";
import { flow, identity, pipe, SK } from "fp-ts/function";
import * as RA from "fp-ts/ReadonlyArray";
import { Tuple2 } from "fp-ts/Tuple2";
import { keyBy } from "lodash-es";
import { combineLatest, filter, firstValueFrom, map, of } from "rxjs";
import { isDefined } from "./utils";
describe("With Either", () => {
it("All rights", async () => {
interface Card { id: string; name: string};
interface Deck { id: string; name: string; idsCard: string[];};
const card1 = { id: "c1", name: "card 1" };
const card2 = { id: "c2", name: "card 2" };
const deck1 = { id: "d1", name: "deck 1", idsCard: ["c1", "c2"] };
const cards = of(E.right([card1, card2]));
const decks = of(E.right([deck1]));
const test: Tuple2<Deck[], Card[]> = [[card1, card2], [deck1], ];
expect(
await pipe(
[[deck1], [card1, card2]],
RA.map((data) => {
const decks = data[0];
const cards = data[1];
const mapCards = keyBy(cards, (c) => c.id);
return pipe(
decks,
RA.map((d) => "todo");
);
}
)
map(
flow(
E.traverseReadonlyArrayWithIndex(SK),
E.matchW(
(e) => {
console.warn(e);
return undefined;
},
([decks, cards]) => ({ decks, cards }),
),
),
),
filter(isDefined),
firstValueFrom,
),
).toEqual({ decks: [deck1], cards: [card1, card2] });
expect(
await pipe(
combineLatest([decks, cards]),
map(
flow(
E.traverseReadonlyArrayWithIndex(SK),
E.matchW(
(e) => {
console.warn(e);
return undefined;
},
([decks, cards]) => ({ decks, cards }),
),
),
),
filter(isDefined),
firstValueFrom,
),
).toEqual({ decks: [deck1], cards: [card1, card2] });
expect(
pipe(
[1, "2"],
E.traverseReadonlyArrayWithIndex(flow(SK, E.right)), //
),
).toEqual(E.right([1, "2"]));
// NOTE: This is less efficient and should not be used.
expect(
pipe(
[E.right(1)],
RA.traverse(E.Applicative)(identity), //
),
).toEqual(
pipe(
[E.right(1)],
E.traverseReadonlyArrayWithIndex(SK), //
),
);
});
it("Some lefts", async () => {
expect(
pipe(
[E.left(1), E.right(2)],
E.traverseReadonlyArrayWithIndex(SK), //
),
).toEqual(E.left(1));
expect(
pipe(
[E.right(1), E.left(2)],
E.traverseReadonlyArrayWithIndex(SK), //
),
).toEqual(E.left(2));
expect(
pipe(
[E.left(1), E.left(2)],
E.traverseReadonlyArrayWithIndex(SK), //
),
).toEqual(E.left(1));
});
});