const stringTrim = (string, maxLength) => { if (!string) return string; if (maxLength < 1) return string; if (string.length <= maxLength) return string; if (maxLength === 1) return string.substring(0,1) + '...'; const midpoint = Math.ceil(string.length / 2); const toRemove = string.length - maxLength; const leftStrip = Math.ceil(toRemove/2); const rightStrip = toRemove - leftStrip; return `${string.substring(0, midpoint - leftStrip)}...${string.substring(midpoint + rightStrip)}` }
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