forked from firebase/firebase-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional.spec.ts
163 lines (139 loc) · 4.16 KB
/
functional.spec.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { expect } from "chai";
import { flatten } from "lodash";
import { SameType } from "./metaprogramming";
import * as f from "./functional";
describe("functional", () => {
describe("flatten", () => {
it("can iterate an empty object", () => {
expect([...f.flatten({})]).to.deep.equal([]);
});
it("can iterate an object that's already flat", () => {
expect([...f.flatten({ a: "b" })]).to.deep.equal([["a", "b"]]);
});
it("Gets the right type for flattening arrays", () => {
const arr = [[["a"], "b"], ["c"]];
const flattened = [...f.flattenArray(arr)];
const test: SameType<typeof flattened, string[]> = true;
expect(test).to.be.true;
});
it("can handle nested objects", () => {
const init = {
outer: {
inner: {
value: 42,
},
},
other: {
value: null,
},
};
const expected = [
["outer.inner.value", 42],
["other.value", null],
];
expect([...f.flatten(init)]).to.deep.equal(expected);
});
it("can handle objects with array values", () => {
const init = { values: ["a", "b"] };
const expected = [
["values.0", "a"],
["values.1", "b"],
];
expect([...f.flatten(init)]).to.deep.equal(expected);
});
it("can iterate an empty array", () => {
expect([...flatten([])]).to.deep.equal([]);
});
it("can noop arrays", () => {
const init = ["a", "b", "c"];
expect([...f.flatten(init)]).to.deep.equal(init);
});
it("can flatten", () => {
const init = [[[1]], [2], 3];
expect([...f.flatten(init)]).to.deep.equal([1, 2, 3]);
});
});
describe("reduceFlat", () => {
it("can noop", () => {
const init = ["a", "b", "c"];
expect(init.reduce(f.reduceFlat, [])).to.deep.equal(["a", "b", "c"]);
});
it("can flatten", () => {
const init = [[[1]], [2], 3];
expect(init.reduce(f.reduceFlat, [])).to.deep.equal([1, 2, 3]);
});
});
describe("zip", () => {
it("can handle an empty array", () => {
expect([...f.zip([], [])]).to.deep.equal([]);
});
it("can zip", () => {
expect([...f.zip([1], ["a"])]).to.deep.equal([[1, "a"]]);
});
it("throws on length mismatch", () => {
expect(() => [...f.zip([1], [])]).to.throw();
});
});
it("zipIn", () => {
expect([1, 2].map(f.zipIn(["a", "b"]))).to.deep.equal([
[1, "a"],
[2, "b"],
]);
});
it("assertExhaustive", () => {
interface Bird {
type: "bird";
}
interface Fish {
type: "fish";
}
type Animal = Bird | Fish;
// eslint-disable-next-line
function passtime(animal: Animal): string {
if (animal.type === "bird") {
return "fly";
} else if (animal.type === "fish") {
return "swim";
}
// This line must make the containing function compile:
f.assertExhaustive(animal);
}
// eslint-disable-next-line
function speak(animal: Animal): void {
if (animal.type === "bird") {
console.log("chirp");
return;
}
// This line must cause the containing function to fail
// compilation if uncommented
// f.assertExhaustive(animal);
}
});
describe("partition", () => {
it("should split an array into true and false", () => {
const arr = ["T1", "F1", "T2", "F2"];
expect(f.partition<string>(arr, (s: string) => s.startsWith("T"))).to.deep.equal([
["T1", "T2"],
["F1", "F2"],
]);
});
it("can handle an empty array", () => {
expect(f.partition<string>([], (s: string) => s.startsWith("T"))).to.deep.equal([[], []]);
});
});
describe("partitionRecord", () => {
it("should split a record into true and false", () => {
const rec = { T1: 1, F1: 2, T2: 3, F2: 4 };
expect(f.partitionRecord<number>(rec, (s: string) => s.startsWith("T"))).to.deep.equal([
{ T1: 1, T2: 3 },
{ F1: 2, F2: 4 },
]);
});
it("can handle an empty record", () => {
expect(f.partitionRecord<string>({}, (s: string) => s.startsWith("T"))).to.deep.equal([
{},
{},
]);
});
});
});