This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEncoder.js
102 lines (83 loc) · 2.4 KB
/
Encoder.js
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
import Mint from "./Main.js";
const { Encoder, Just, Nothing, createRecord } = Mint;
const X = createRecord({
a: ["x-y-z", null, Encoder.identity],
c: ["c", null, null],
});
describe("Encoder.identity", () => {
test("returns value", () => {
expect(Encoder.identity("")).toBe("");
expect(Encoder.identity(0)).toBe(0);
});
});
describe("Encoder.time", () => {
test("converts it to an ISO 8601 string", () => {
const date = new Date(2020, 0, 1);
expect(Encoder.time(date)).toBe("2020-01-01T00:00:00.000Z");
});
});
describe("Encoder.maybe", () => {
test("Nothing", () => {
const result = Encoder.maybe((value) => value)(new Nothing());
expect(result).toBe(null);
});
test("Just with encoder", () => {
const result = Encoder.maybe((value) => value)(new Just("a"));
expect(result).toBe("a");
});
test("Just without encoder", () => {
const result = Encoder.maybe()(new Just("a"));
expect(result).toBe("a");
});
});
describe("Record", () => {
test("encodes", () => {
const result = X.encode(new X({ a: "B", c: 0 }));
expect(result).not.toBeInstanceOf(X);
expect(result.a).toBe(undefined);
expect(result["x-y-z"]).toBe("B");
expect(result.c).toBe(0);
});
});
describe("Map", () => {
test("encodes with encoder", () => {
const result = Encoder.map((value) => value)([
["a", "B"],
["c", "0"],
]);
expect(result).not.toBeInstanceOf(Map);
expect(result.a).toBe("B");
expect(result.c).toBe("0");
});
test("encodes without", () => {
const result = Encoder.map()([
["a", "B"],
["c", "0"],
]);
expect(result).not.toBeInstanceOf(Map);
expect(result.a).toBe("B");
expect(result.c).toBe("0");
});
});
describe("Array", () => {
test("encodes with encoder", () => {
const result = Encoder.array((value) => value)(["B", "0"]);
expect(result).not.toBeInstanceOf(Map);
expect(result[0]).toBe("B");
expect(result[1]).toBe("0");
});
test("encodes without encoder", () => {
const result = Encoder.array()(["B", "0"]);
expect(result).not.toBeInstanceOf(Map);
expect(result[0]).toBe("B");
expect(result[1]).toBe("0");
});
});
describe("Tuple", () => {
test("encodes", () => {
const result = Encoder.tuple([(value) => value, null])(["B", "0"]);
expect(result).toBeInstanceOf(Array);
expect(result[0]).toBe("B");
expect(result[1]).toBe("0");
});
});