-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
105 lines (91 loc) · 2.12 KB
/
test.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
103
104
105
import assert from 'node:assert/strict'
import test from 'node:test'
import {trimLines} from './index.js'
test('trimLines(value)', function () {
// @ts-expect-error coerce.
assert.equal(trimLines(true), 'true', 'should coerce to string')
assert.equal(
trimLines(' foo\t\n\n bar \n\tbaz '),
' foo\n\nbar\nbaz ',
'should work'
)
assert.equal(
trimLines('a \r b \r\n c \n d'),
'a\rb\r\nc\nd',
'should preseve line endings'
)
assert.equal(
trimLines(' \n \n \n '),
'\n\n\n',
'should handle complete empty space'
)
})
test('performance', async () => {
const whitespace = ' '.repeat(70_000)
await new Promise((resolve) => {
const timer = setTimeout(() => {
assert.fail('did not pass in 30ms')
}, 30)
assert.equal(
trimLines('a' + whitespace + 'b'),
'a' + whitespace + 'b',
'whitespace in line (1)'
)
setTimeout(() => {
clearTimeout(timer)
resolve(undefined)
}, 0)
})
await new Promise((resolve) => {
const timer = setTimeout(() => {
assert.fail('did not pass in 30ms')
}, 30)
assert.equal(
trimLines('\na' + whitespace + 'b\n'),
'\na' + whitespace + 'b\n',
'whitespace in line (2)'
)
setTimeout(() => {
clearTimeout(timer)
resolve(undefined)
}, 0)
})
await new Promise((resolve) => {
const timer = setTimeout(() => {
assert.fail('did not pass in 30ms')
}, 30)
assert.equal(
trimLines(whitespace + '\na\n' + whitespace),
'\na\n',
'whitespace-only lines'
)
setTimeout(() => {
clearTimeout(timer)
resolve(undefined)
}, 0)
})
await new Promise((resolve) => {
const timer = setTimeout(() => {
assert.fail('did not pass in 30ms')
}, 30)
assert.equal(
trimLines(
'a' +
whitespace +
'\n' +
whitespace +
'b' +
whitespace +
'\n' +
whitespace +
'c'
),
'a\nb\nc',
'whitespace around line endings'
)
setTimeout(() => {
clearTimeout(timer)
resolve(undefined)
}, 0)
})
})