-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
truncate.spec.ts
648 lines (530 loc) · 20 KB
/
truncate.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
import truncate from '../src/truncate'
import cheerio from 'cheerio'
describe('Truncate html', () => {
describe('should works well when false params are given', () => {
it('should NOT truncate a string if no string provided', () => {
// @ts-ignore
expect(truncate(null)).toBe(null)
})
it('should NOT truncate a string if NO length provided', () => {
const html = 'string'
expect(truncate(html)).toBe(html)
})
it('should NOT truncate a string if NO length provided $', () => {
const html = cheerio.load('string')
expect(truncate(html)).toBe(html)
})
it('should NOT truncate a string if length is less than or equal to zero', () => {
const html = 'string'
expect(truncate(html, 0)).toBe(html)
})
it('should NOT truncate a string if length is less than or equal to zero $', () => {
const html = 'string'
const $ = cheerio.load(html)
expect(truncate($, 0)).toBe($)
})
})
describe('truncate with options.length', () => {
it('should truncate a string', () => {
const test = '123456789'
const expected = '12345...'
expect(truncate(test, 5)).toBe(expected)
})
it('should truncate a string $', () => {
const test = '123456789'
const expected = '12345...'
const $ = cheerio.load(test, null, false)
expect(truncate($, 5)).toBe(expected)
})
it('should truncate a string with tags', () => {
const test = '<p>123456789</p>'
const expected = '<p>123456...</p>'
expect(truncate(test, { length: 6 })).toBe(expected)
})
it('should kepp all the string if length logger than the origin string', () => {
const test = '<p>123456789</p>'
const expected = '<p>123456789</p>'
expect(truncate(test, { length: 100 })).toBe(expected)
})
it('should truncate a string with characters outside of tags', () => {
const test = '<p>12345</p>6789'
const expected = '<p>12345</p>678...'
expect(truncate(test, 8)).toBe(expected)
})
it('should works well when truncate at tag boundary', () => {
const test = 'Hello <b>world</b>'
const expected = 'Hello ...'
expect(truncate(test, 6)).toBe(expected)
})
it('should works well when truncate at tag boundary-2', () => {
const test = 'Hello <b>world</b>'
const expected = 'Hello <b>world</b>'
expect(truncate(test, 11)).toBe(expected)
})
it('should truncate a string two sets of tags', () => {
const test = '<p>12345</p><p>6789</p>'
const expected = '<p>12345</p><p>67...</p>'
expect(truncate(test, 7)).toBe(expected)
})
it('should truncate a string two sets of tags $', () => {
const test = cheerio.load('<p>12345</p><p>6789</p>', null, false)
const expected = '<p>12345</p><p>67...</p>'
expect(truncate(test, 7)).toBe(expected)
})
it('should keep empty tag', () => {
const test =
'<span></span><p>12345</p><p>6789</p><span> reset text </span>'
const expected = '<span></span><p>12345</p><p>67...</p>'
expect(truncate(test, 7)).toBe(expected)
})
it('should keep empty tag $', () => {
const test =
cheerio.load('<span></span><p>12345</p><p>6789</p><span> reset text </span>', null, false)
const expected = '<span></span><p>12345</p><p>67...</p>'
expect(truncate(test, 7)).toBe(expected)
})
it('should remove comment', () => {
const test = '<span></span><!-- comment --><p>12345</p><p>6789</p>'
const expected = '<span></span><p>12345</p><p>67...</p>'
expect(truncate(test, 7)).toBe(expected)
})
it('should remove comment in tag', () => {
const test = '<span></span><p><!-- comment -->12345</p><p>6789</p>'
const expected = '<span></span><p>12345</p><p>67...</p>'
expect(truncate(test, 7)).toBe(expected)
})
describe('works with options.reserveLastWord', () => {
it('should reserve the last word', () => {
const test = '<p>12345</p><p>6789</p>'
const expected = '<p>12345</p><p>6789</p>'
expect(
truncate(test, 7, {
reserveLastWord: true
})
).toBe(expected)
})
it('should reserve the last word(i18n)', () => {
const test = '<p>internationalization</p>'
const expected = '<p>internationalization</p>'
expect(
truncate(test, 7, {
reserveLastWord: 20 // exceed 20 letters
})
).toBe(expected)
})
it('should cut at the last word(i18n)', () => {
const test = '<p>internationalization</p>'
const expected = '<p>internationalizat...</p>'
expect(
truncate(test, 7, {
reserveLastWord: true // exceed 10 letters
})
).toBe(expected)
})
it('should reserve the last word if only one word', () => {
const test = '<p>internationalization</p>'
const expected = '<p>internationalizat...</p>'
expect(
truncate(test, 7, {
reserveLastWord: -1 // exceed 10 letters
})
).toBe(expected)
})
it('should cut the only word if trimTheOnlyWord true', () => {
const test = '<p>internationalization</p>'
const expected = '<p>interna...</p>'
expect(
truncate(test, 7, {
reserveLastWord: -1, // exceed 10 letters
trimTheOnlyWord: true
})
).toBe(expected)
})
it('should reserve the last word if only one word $', () => {
const test = cheerio.load('<p>internationalization</p>', null, false)
const expected = '<p>internationalizat...</p>'
expect(
truncate(test, 7, {
reserveLastWord: -1 // exceed 10 letters
})
).toBe(expected)
})
it('should reserve the last word if at the boundary', () => {
const test = '<p>Hello world from earth</p>'
const expected = '<p>Hello world...</p>'
expect(
truncate(test, 11, {
reserveLastWord: -1 // exceed 10 letters
})
).toBe(expected)
})
it('should reserve the last word if at the boundary even trimTheOnlyWord is true ', () => {
const test = '<p>Hello world from earth</p>'
const expected = '<p>Hello world...</p>'
expect(
truncate(test, 11, {
reserveLastWord: -1, // exceed 10 letters
trimTheOnlyWord: true
})
).toBe(expected)
})
// from issue #24
it('should cut correctly if string\'s length is equal to the truncation length', () => {
const test = '<p>a b c d ef</p>'
const expected = '<p>a b c d ef</p>'
expect(
truncate(test, 10, {
reserveLastWord: -1 // exceed 10 letters
})
).toBe(expected)
})
it('should remove the last word if more than one(i18n, reserveLastWord negative)', () => {
const test = '<p>hello internationalization</p>'
const expected = '<p>hello ...</p>'
expect(
truncate(test, 7, {
reserveLastWord: -1 // exceed 10 letters
})
).toBe(expected)
})
})
})
describe('with self-close tags', () => {
it('should truncate a string with an image tag', () => {
const html = '<p><img src="abc.png">This is a string</p> for test.'
const expected = '<p><img src="abc.png">This is a ...</p>'
expect(truncate(html, 10)).toBe(expected)
})
it('should truncate a string with an image and br tags', () => {
const html = '<p><img src="abc.png">This <br>is a string</p> for test.'
const expected = '<p><img src="abc.png">This <br>is a ...</p>'
expect(truncate(html, 10)).toBe(expected)
})
})
describe('with options.stripTags', () => {
it('should works well with plain text', () => {
const html = 'This is a string for test.'
const expected = 'This is a ...'
const options = {
stripTags: true
}
expect(truncate(html, 10, options)).toBe(expected)
})
it('should remove all tags', () => {
const html =
'<p><img src="abc.png">This <hr>is a string</p><br> for test.'
const expected = 'This is a ...'
const options = {
stripTags: true
}
expect(truncate(html, 10, options)).toBe(expected)
})
})
describe('with options.byWords', () => {
it('should truncate by words', () => {
const html = '<p><img src="abc.png">This is a string do</p> for test.'
const expected = '<p><img src="abc.png">This is a string...</p>'
const options = {
byWords: true
}
expect(truncate(html, 4, options)).toBe(expected)
})
it('should reverse the whole string when if length is bigger', () => {
const html = '<p><img src="abc.png">This is a string do</p> for test.'
const expected = '<p><img src="abc.png">This is a string do</p> for test.'
const options = {
byWords: true
}
expect(truncate(html, 10, options)).toBe(expected)
})
it('should works well when truncate at tag boundary', () => {
const test = 'Hello <b>world</b>'
const expected = 'Hello...'
const options = {
byWords: true
}
expect(truncate(test, 1, options)).toBe(expected)
})
it('should works well when truncate at tag boundary', () => {
const test = 'Hello <b>world</b>'
const expected = 'Hello <b>world</b>'
const options = {
byWords: true
}
expect(truncate(test, 2, options)).toBe(expected)
})
describe('works with options.reserveLastWord', () => {
it('should ignore reserveLastWord when byWords is on(length bigger)', () => {
const html = '<p><img src="abc.png">This is a string do</p> for test.'
const expected =
'<p><img src="abc.png">This is a string do</p> for test.'
const options = {
byWords: true,
reserveLastWord: true
}
expect(truncate(html, 10, options)).toBe(expected)
})
it('should ignore reserveLastWord when byWords is on(length smaller)', () => {
const html = '<p><img src="abc.png">This is a string do</p> for test.'
const expected = '<p><img src="abc.png">This is a...</p>'
const options = {
byWords: true,
reserveLastWord: true
}
expect(truncate(html, 3, options)).toBe(expected)
})
})
})
describe('with options.whitespaces', () => {
it('should trim whitespaces', () => {
const html =
'<p> <img src="abc.png">This is a string</p> for test.'
const expected = '<p> <img src="abc.png">This is a...</p>'
const options = {
keepWhitespaces: false
}
expect(truncate(html, 10, options)).toBe(expected)
})
it('should preserve whitespaces', () => {
const html =
'<p> <img src="abc.png">This is a string</p> for test.'
const expected = '<p> <img src="abc.png">This is a...</p>'
const options = {
keepWhitespaces: true
}
expect(truncate(html, 10, options)).toBe(expected)
})
it('should preserve last whitespace at boundary', () => {
const html =
'<p>Hello image. <img src="abc.png">This is a string</p> for test.'
const expected = '<p>Hello image. <img src="abc.png">This is ...</p>'
const options = {
keepWhitespaces: true
}
expect(truncate(html, 21, options)).toBe(expected)
})
it('should count continuous whitespaces as one', () => {
const html =
'<p>Hello image. <img src="abc.png">This is a string</p> for test.'
const expected = '<p>Hello image. <img src="abc.png">This is...</p>'
const options = {
keepWhitespaces: true
}
expect(truncate(html, 20, options)).toBe(expected)
})
})
describe('combine length and options', () => {
it('should works with length and options separate', () => {
const html = '<p><img src="abc.png">This is a string</p> for test.'
const expected = 'This is a ...'
const options = {
stripTags: true
}
expect(truncate(html, 10, options)).toBe(expected)
})
it('should allow length argument to be combined into the options object', () => {
const html = '<p><img src="abc.png">This is a string</p> for test.'
const expected = 'This is a ...'
const options = {
length: 10,
stripTags: true
}
expect(truncate(html, options)).toBe(expected)
})
})
describe('with options.ellipsis', () => {
it('should insert a custom ellipsis sign', () => {
const html = '<p><img src="abc.png">This is a string</p> for test.'
const expected = '<p><img src="abc.png">This is a ~</p>'
const options = {
length: 10,
ellipsis: '~'
}
expect(truncate(html, options)).toBe(expected)
})
it('should not insert a custom ellipsis sign', () => {
const html = '<p><img src="abc.png">This is a string</p> for test.'
const expected = '<p><img src="abc.png">This is a string</p> for test.'
const options = {
length: 50,
ellipsis: '~'
}
expect(truncate(html, options)).toBe(expected)
})
describe('last character in html tag', () => {
const testString = '123456<div>7</div><div>89</div>12'
it('should add ellipsis before a tag', () => {
const expected = '123456...'
expect(truncate(testString, 6)).toBe(expected)
})
it('should add ellipsis in a tag with one character', () => {
const expected = '123456<div>7...</div>'
expect(truncate(testString, 7)).toBe(expected)
})
it('should add ellipsis within tag', () => {
const expected = '123456<div>7</div><div>8...</div>'
expect(truncate(testString, 8)).toBe(expected)
})
it('should add ellipsis in a tag with multiple characters', () => {
const expected = '123456<div>7</div><div>89...</div>'
expect(truncate(testString, 9)).toBe(expected)
})
it('should add ellipsis after a character after closing tag', () => {
const expected = '123456<div>7</div><div>89</div>1...'
expect(truncate(testString, 10)).toBe(expected)
})
it('should add ellipsis in a nested tag ', () => {
const test = '123456<div>7</div><div><b>89</b></div>12'
const expected = '123456<div>7</div><div><b>89...</b></div>'
expect(truncate(test, 9)).toBe(expected)
})
})
})
describe('with options.excludes', () => {
it('should exclude elements by selector', () => {
const html = '<p><img src="abc.png">This is a string</p> for test.'
const expected = '<p>This is a ...</p>'
const options = {
length: 10,
excludes: 'img'
}
expect(truncate(html, options)).toBe(expected)
})
it('should exclude multiple elements by selector', () => {
const html =
'<p><img src="abc.png">This is a string</p><div class="something-unwanted"> unwanted string inserted ( ´•̥̥̥ω•̥̥̥` )</div> for test.'
const expected = '<p>This is a string</p> for...'
const options = {
length: 20,
excludes: ['img', '.something-unwanted']
}
expect(truncate(html, options)).toBe(expected)
})
})
describe('with options.decodeEntities', () => {
it('should handle encoded characters', () => {
const html = '<p> test for <p> encoded string</p>'
const expected = '<p> test for <p> encode...</p>'
const options = {
length: 20,
decodeEntities: true
}
expect(truncate(html, options)).toBe(expected)
})
// it('should leave encoded characters as is', () => {
// const html = '<p> test for <p> encoded string</p>'
// const expected = '<p> test for <p...</p>'
// const options = {
// length: 20,
// decodeEntities: false // this is the default value
// }
// expect(truncate(html, options)).toBe(expected)
// })
it('should leave encoded characters as is', () => {
const html = '<p> test for <p> encoded string</p>'
const expected = '<p> test for <p> encode...</p>'
const options = {
length: 20,
decodeEntities: false // this is the default value
}
expect(truncate(html, options)).toBe(expected)
})
it('should works with CJK when decodeEntities is true', () => {
const html = '<p> test for <p>@ 中文 string</p>'
const expected = '<p> test for <p>@ 中文 st...</p>'
const options = {
length: 20,
decodeEntities: true
}
expect(truncate(html, options)).toBe(expected)
})
it('should convert CJK from encoded with decodeEntities to false', () => {
const html = '<p> test for <p> 中文 string</p>'
const expected = '<p> test for <p> 中文 str...</p>'
const options = {
length: 20,
decodeEntities: true
}
expect(truncate(html, options)).toBe(expected)
})
it('should convert CJK from encoded with decodeEntities to false', () => {
const html = '<p> test for <p>@ 中文 string</p>'
const expected = '<p> test for <p>@ 中文 st...</p>'
const options = {
length: 20,
decodeEntities: false
}
expect(truncate(html, options)).toBe(expected)
})
})
describe('with truncate.setup', () => {
afterEach(function () {
truncate.setup({
byWords: false,
stripTags: false,
ellipsis: '...',
// @ts-ignore
length: null,
decodeEntities: false,
keepWhitespaces: false,
excludes: '',
reserveLastWord: false
})
})
it('should works well if setup with empty', () => {
// @ts-ignore
truncate.setup()
const test = 'hello from earth'
const expected = 'hello from e...'
expect(truncate(test, 12)).toBe(expected)
})
it('should use default length', () => {
truncate.setup({ length: 5 })
const test = '123456789'
const expected = '12345...'
expect(truncate(test)).toBe(expected)
})
it('should use default byWords settings', () => {
truncate.setup({ byWords: true })
const test = 'hello from earth'
const expected = 'hello from...'
expect(truncate(test, 2)).toBe(expected)
})
it('should use default reserveLastWord settings', () => {
truncate.setup({ reserveLastWord: true })
const test = 'hello from earth'
const expected = 'hello from earth'
expect(truncate(test, 12)).toBe(expected)
})
})
describe('should correcty handle text with emoji characters', () => {
it('emojis with character length more than 1', () => {
const test = '💩💩💩💩💩'
const expected = '💩💩💩...'
expect(truncate(test, 3)).toEqual(expected)
})
it('emojis with text', () => {
const test = 'Hello there, how are you?? 👍👍👍'
const expected = 'Hello there, how are you?? 👍👍...'
expect(truncate(test, 29)).toEqual(expected)
})
it('emojis with reservedLastWord setting', () => {
truncate.setup({ reserveLastWord: true })
const test = 'Hello there 😎😎😎'
const expected = 'Hello there 😎...'
expect(truncate(test, 13)).toEqual(expected)
})
it('emojis with byWords setting, with keepWhitespaces false', () => {
truncate.setup({ byWords: true, keepWhitespaces: false })
const test = 'Hello there, how are you?? 😎😎😎'
const expected = 'Hello there, how are you?? 😎😎😎'
expect(truncate(test, 20)).toEqual(expected)
})
it('emojis with keepWhitespaces setting', () => {
truncate.setup({ keepWhitespaces: true })
const test = 'Hello there 💩 💩 💩'
const expected = 'Hello there 💩 💩 💩'
expect(truncate(test, 20)).toEqual(expected)
})
})
})