-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathreading-time.spec.ts
172 lines (144 loc) · 3.96 KB
/
reading-time.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
164
165
166
167
168
169
170
171
172
/*!
* reading-time
* Copyright (c) Nicolas Gryman <[email protected]>
* MIT Licensed
*/
import readingTime, { Options, ReadingTimeResult } from '../src'
import chai from 'chai'
chai.should()
const test = (words: number | string, expect: Partial<ReadingTimeResult>, options?: Options) =>
(done: () => void) => {
const text = 'number' === typeof words ? generateText(words) : words
if ('string' === typeof words) {
if (text.includes(' ')) {
words = words.split(/.+ +.+/g).length + 1
}
else if (text.length > 0) {
words = 1
}
else {
words = 0
}
}
const res = readingTime(text, options)
if (expect.minutes) {
res.should.have.property('minutes', expect.minutes)
}
if (expect.time) {
res.should.have.property('time', expect.time)
}
if (expect.words) {
res.should.have.property('words').to.deep.equal(expect.words)
}
done()
}
function generateText(words: number) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789àâéèêôùûçÀÂÉÈÔÙÛÇ'
const charsLength = chars.length
let text = ''
for (let i = 0; i < words; i++) {
const wordLength = Math.ceil(Math.random() * 10)
for (let j = 0; j < wordLength; j++) {
text += chars[Math.floor(Math.random() * charsLength)]
}
text += ' '
}
return text
}
describe('readingTime()', () => {
it('should handle less than 1 minute text',
test(2, {
minutes: 1,
time: 600
}))
it('should handle less than 1 minute text',
test(50, {
minutes: 1,
time: 15000
}))
it('should handle 1 minute text',
test(100, {
minutes: 1,
time: 30000
}))
it('should handle 2 minutes text',
test(300, {
minutes: 2,
time: 90000
}))
it('should handle a very long text',
test(500, {
minutes: 3,
time: 150000
}))
it('should handle text containing multiple successive whitespaces',
test('word word word', {
minutes: 1,
time: 900
}))
it('should handle text starting with whitespaces',
test(' word word word', {
minutes: 1,
time: 900
}))
it('should handle text ending with whitespaces',
test('word word word ', {
minutes: 1,
time: 900
}))
it('should handle text containing links',
test('word http://ngryman.sh word', {
minutes: 1,
time: 900
}))
it('should handle text containing markdown links',
test('word [blog](http://ngryman.sh) word', {
minutes: 1,
time: 900
}))
it('should handle text containing one word correctly',
test('0', {
minutes: 1,
time: 300
}))
it('should handle text containing a black hole',
test('', {
minutes: 0,
time: 0
}))
it('should accept a custom word per minutes value',
test(200, {
minutes: 2,
time: 120000
}, { wordsPerMinute: 100 }))
})
describe('readingTime CJK', () => {
it('should handle a CJK paragraph',
test('今天,我要说中文!(没错,现在这个库也完全支持中文了)', {
words: { total: 22 }
}))
it('should handle a CJK paragraph with Latin words',
test('你会说English吗?', {
words: { total: 5 }
}))
it('should handle a CJK paragraph with Latin punctuation',
test('科学文章中, 经常使用英语标点... (虽然这段话并不科学)', {
words: { total: 22 }
}))
it('should handle a CJK paragraph starting and terminating in Latin words',
test('JoshCena喜欢GitHub', {
words: { total: 4 }
}))
it('should handle a typical Korean paragraph',
test('이것은 한국어 단락입니다', {
words: { total: 11 }
}))
it('should handle a typical Japanese paragraph',
test('天気がいいから、散歩しましょう', {
words: { total: 14 }
}))
it('should treat Katakana as one word',
test('メガナイトありませんか?', {
words: { total: 7 }
}))
})