-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom.test.ts
85 lines (78 loc) · 1.88 KB
/
custom.test.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
import { describe, it } from "@std/testing/bdd";
import { expect } from "@std/expect";
import { compact, compactObject, flatmap } from "./custom.ts";
import { repeat } from "./itertools.ts";
describe("compact", () => {
it("compact w/ empty list", () => {
expect(compact([])).toEqual([]);
});
it("icompact removes undefined values", () => {
expect(compact("abc")).toEqual(["a", "b", "c"]);
expect(compact(["x", undefined])).toEqual(["x"]);
expect(compact([0, null, undefined, NaN, Infinity])).toEqual([
0,
null,
NaN,
Infinity,
]);
});
});
describe("compactObject", () => {
it("compactObject w/ empty object", () => {
expect(compactObject({})).toEqual({});
});
it("compactObject removes undefined values", () => {
expect(compactObject({ a: 1, b: "foo", c: 0 })).toEqual({
a: 1,
b: "foo",
c: 0,
});
expect(compactObject({ a: undefined, b: false, c: 0 })).toEqual({
b: false,
c: 0,
});
});
});
describe("flatmap", () => {
it("flatmap w/ empty list", () => {
expect(Array.from(flatmap([], (x) => [x]))).toEqual([]);
});
it("flatmap works", () => {
const dupeEvens = (x: number) => (x % 2 === 0 ? [x, x] : [x]);
const triple = (x: string) => [x, x, x];
const nothin = () => [];
expect(Array.from(flatmap([1, 2, 3, 4, 5], dupeEvens))).toEqual([
1,
2,
2,
3,
4,
4,
5,
]);
expect(Array.from(flatmap(["hi", "ha"], triple))).toEqual([
"hi",
"hi",
"hi",
"ha",
"ha",
"ha",
]);
expect(Array.from(flatmap(["hi", "ha"], nothin))).toEqual([]);
});
it("flatmap example", () => {
const repeatN = (n: number) => repeat(n, n);
expect(Array.from(flatmap([0, 1, 2, 3, 4], repeatN))).toEqual([
1,
2,
2,
3,
3,
3,
4,
4,
4,
4,
]);
});
});