Repository navigation
Expand file tree
/
Copy pathwhitespace-utils.js
More file actions
115 lines (107 loc) · 2.93 KB
/
Copy pathwhitespace-utils.js
File metadata and controls
115 lines (107 loc) · 2.93 KB
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
import WhitespaceUtils from "../../src/utils/whitespace-utils.js";
it("constructor", () => {
expect(() => new WhitespaceUtils()).toThrow(TypeError);
expect(() => new WhitespaceUtils(1)).toThrow(TypeError);
expect(() => new WhitespaceUtils("")).toThrow(TypeError);
expect(() => new WhitespaceUtils([])).toThrow(TypeError);
expect(() => new WhitespaceUtils([""])).toThrow(TypeError);
expect(() => new WhitespaceUtils(["a"])).toThrow(TypeError);
expect(() => new WhitespaceUtils("a")).toThrow(TypeError);
expect(() => new WhitespaceUtils(["\r\n"])).toThrow(TypeError);
expect(new WhitespaceUtils("\r\n")).toBeInstanceOf(WhitespaceUtils);
expect(new WhitespaceUtils(["\r", "\n"])).toBeInstanceOf(WhitespaceUtils);
});
const utils = new WhitespaceUtils(" ");
describe("trimStart", () => {
for (const [string, expected] of [
["", ""],
[" ", ""],
[" ".repeat(3), ""],
["a", "a"],
["a" + " ".repeat(3), "a "],
[" ".repeat(3) + "a", "a"],
[" ".repeat(3) + "a" + " ".repeat(3), "a "],
]) {
it(JSON.stringify(string), () => {
expect(utils.trimStart(string)).toBe(expected);
});
}
});
describe("trimEnd", () => {
for (const [string, expected] of [
["", ""],
[" ", ""],
[" ".repeat(3), ""],
["a", "a"],
["a" + " ".repeat(3), "a"],
[" ".repeat(3) + "a", " a"],
[" ".repeat(3) + "a" + " ".repeat(3), " a"],
]) {
it(JSON.stringify(string), () => {
expect(utils.trimEnd(string)).toBe(expected);
});
}
});
describe("trim", () => {
for (const [string, expected] of [
["", ""],
[" ", ""],
[" ".repeat(3), ""],
["a", "a"],
["a" + " ".repeat(3), "a"],
[" ".repeat(3) + "a", "a"],
[" ".repeat(3) + "a" + " ".repeat(3), "a"],
]) {
it(JSON.stringify(string), () => {
expect(utils.trim(string)).toBe(expected);
});
}
});
describe("getLeadingWhitespaceCount", () => {
for (const [string, expected] of [
["", 0],
[" ", 1],
[" ".repeat(3), 3],
["a", 0],
["a" + " ".repeat(3), 0],
[" ".repeat(3) + "a", 3],
[" ".repeat(3) + "a" + " ".repeat(3), 3],
]) {
it(JSON.stringify(string), () => {
expect(utils.getLeadingWhitespaceCount(string)).toBe(expected);
});
}
});
describe("getTrailingWhitespaceCount", () => {
for (const [string, expected] of [
["", 0],
[" ", 1],
[" ".repeat(3), 3],
["a", 0],
["a" + " ".repeat(3), 3],
[" ".repeat(3) + "a", 0],
[" ".repeat(3) + "a" + " ".repeat(3), 3],
]) {
it(JSON.stringify(string), () => {
expect(utils.getTrailingWhitespaceCount(string)).toBe(expected);
});
}
});
describe("split", () => {
for (const string of [
"",
" ",
" ".repeat(3),
" a",
" ".repeat(3) + "a",
"a ",
"a" + " ".repeat(3),
" a ",
" ".repeat(3) + "a" + " ".repeat(3),
" a a a ",
]) {
it(JSON.stringify(string), () => {
expect(utils.split(string)).toEqual(string.split(/ +/u));
});
}
});