MCPcopy
hub / github.com/mcnamee/react-native-starter-kit / truncate

Function truncate

src/lib/string.js:20–37  ·  view source on GitHub ↗
(inputContent, numWords)

Source from the content-addressed store, hash-verified

18 * @return {str}
19 */
20export const truncate = (inputContent, numWords) => {
21 if (!inputContent) {
22 return '';
23 }
24 const limit = !numWords ? 100 : numWords;
25
26 // Trim whitespace
27 let content = inputContent.trim();
28
29 // Convert the content into an array of words
30 const contentArr = content.split(' ');
31
32 // Remove any words above the limit
33 content = contentArr.slice(0, limit);
34
35 // Convert the array of words back into a string
36 return `${content.join(' ')}${contentArr.length > limit ? '…' : ''}`;
37};
38
39/**
40 * Strip any HTML from a string

Callers 1

string.test.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected