const shorten = (text, length = 10, ellipsisCount = 3) => { if (!(typeof text === "string" || text instanceof String)) { console.error(`expecting a string, ${typeof text} provided`) return "" } if (isNaN(length) || isNaN(ellipsisCount)) { console.error("length and ellipsisCount must be valid numbers") return } if (text.length <= length) { return text } const ellipsis = Array.from(Array(ellipsisCount)).map(() => ".").join("") return `${text.substr(0, length)}${ellipsis}` } shorten("I am some text", 4, 2) // I am..
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter