Truncation looks like one line of code and is not. The limit has to include the ellipsis or the result overflows anyway; a string already short enough must come back untouched; and cutting at a word boundary has to cope with there being no boundary to cut at.
The module under test
| 1 | const ELLIPSIS = "\u2026"; | ||
| 2 | |||
| 3 | export function truncate(text, limit) { | ||
| 4 | if (typeof text !== "string") { | ||
| 5 | return ""; | ||
| 6 | } | ||
| 7 | if (text.length <= limit) { | ||
| 8 | return text; | ||
| 9 | } | ||
| 10 | const room = limit - ELLIPSIS.length; | ||
| 11 | if (room <= 0) { | ||
| 12 | return ELLIPSIS; | ||
| 13 | } | ||
| 14 | const cut = text.slice(0, room); | ||
| 15 | const lastSpace = cut.lastIndexOf(" "); | ||
| 16 | if (lastSpace > 0) { | ||
| 17 | return cut.slice(0, lastSpace) + ELLIPSIS; | ||
| 18 | } | ||
| 19 | return cut + ELLIPSIS; | ||
| 20 | } | ||
| 21 |
Your tests
Mutants
0%
Mutation score
- Survived
- 0
- Killed
- 0 / 0
- Line coverage
- —
- Tests
- 0
- Assertion width
- —
- Cycle
- —
survivedkilledtimed outequivalentnot run