-
Notifications
You must be signed in to change notification settings - Fork 0
/
head_test.ts
66 lines (54 loc) · 1.72 KB
/
head_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
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
import { head } from "./head.ts";
import { Indexable } from "./types.ts";
import {
assertEquals,
assertType,
describe,
IsExact,
it,
} from "./_dev_deps.ts";
describe("head", () => {
it("should return first element", () => {
const table: [Indexable<unknown>, unknown][] = [
[[""], ""],
[[,], undefined],
[["a", "b", "c"], "a"],
["a", "a"],
["abc", "a"],
[[], undefined],
["", undefined],
];
table.forEach(([seq, expected]) => {
assertEquals(head(seq), expected);
});
});
it("should infer undefined if empty array", () => {
const result = head([]);
assertType<IsExact<typeof result, undefined>>(true);
});
it("should infer undefined or string if empty string", () => {
const result = head("");
assertType<IsExact<typeof result, undefined | string>>(true);
});
it("should infer string or undefined if array", () => {
const result = head([] as string[]);
assertType<IsExact<typeof result, string | undefined>>(true);
});
it("should infer string or undefined if string", () => {
const result = head("" as string);
assertType<IsExact<typeof result, string | undefined>>(true);
});
it("should infer first element if tuple", () => {
const result = head([""]);
assertType<IsExact<typeof result, "">>(true);
const result2 = head(["a", "b", "c"]);
assertType<IsExact<typeof result2, "a">>(true);
});
it("should infer first element if template string literal", () => {
const result = head("a");
assertType<IsExact<typeof result, "a">>(true);
const result2 = head("abc");
assertType<IsExact<typeof result2, "a">>(true);
});
});