2.4. Truncate to a number of characters or words
Topics
This question is in
2. Strings and textCards, notifications, search results and table cells all show text in a fixed space: a title cut to 40 characters, a preview cut to 20 words, each ending with "…". The details decide whether it looks right: the ellipsis has to fit inside the limit, an emoji must not be cut in half into a broken symbol, and "Hello …" with a space before the ellipsis looks like a mistake.
Write two functions in truncate.js: truncateChars and truncateWords.
Signature
function truncateChars(text, maxLength, suffix = "…") // a string
function truncateWords(text, maxWords, suffix = "…") // a stringCharacters are counted as code points: "😀" is one character, though its length is 2. A word is a run of characters that are not whitespace.
Requirements
truncateCharsreturnstextunchanged when it has at mostmaxLengthcharacters.- Otherwise it keeps the first
maxLengthminus the suffix's length characters, removes any whitespace at the end of that part, and addssuffix. The result, suffix included, is never longer thanmaxLength. truncateCharsnever cuts an emoji in half, and counts an emoji as one character when deciding whether the text fits.- A custom suffix (
"..."," [+]", or"") is counted by its own length.maxLengthmay equal the suffix's length; then the result is just the suffix. truncateCharsthrows aRangeErrorwhenmaxLengthis not an integer, or is shorter than the suffix.truncateWordsreturnstextunchanged when it has at mostmaxWordswords.- Otherwise it keeps the text up to the end of word number
maxWords, with its original spacing and punctuation, and addssuffix. WithmaxWords0, a text that has words gives just the suffix. truncateWordsthrows aRangeErrorwhenmaxWordsis not an integer of 0 or more, and handles a text of 50,000 words quickly.
Examples
truncateChars("Frontend design", 9); // "Frontend…"
truncateChars("Hello world", 7); // "Hello…": no space before the ellipsis
truncateChars("abcdefghij", 6, "..."); // "abc..."
truncateChars("I 😀😀😀 it", 4); // "I 😀…"
truncateChars("Hello", 5); // "Hello"truncateWords("The quick brown fox jumps", 3); // "The quick brown…"
truncateWords("Hello, world. How are you?", 2); // "Hello, world.…"
truncateWords("one two", 5); // "one two"
truncateWords("a b c", -1); // throws RangeErrorEnvironment
- Plain JavaScript in a browser page; the tests call the globals
truncateCharsandtruncateWordsfromtruncate.js. textandsuffixare always strings.- Characters made of several code points (a skin-tone emoji, a flag) are not part of the tests.
- Any built-in may be used.
Hints
- What does
"😀😀".slice(0, 3)return, and how can you slice by code point instead? - A regular expression match knows where in the text it was found. Which property tells you?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it