Skip to content

Instantly share code, notes, and snippets.

@Elbone
Last active December 8, 2020 06:03
Show Gist options
  • Save Elbone/66a495ceaef207a30e3621db90e62e07 to your computer and use it in GitHub Desktop.
Save Elbone/66a495ceaef207a30e3621db90e62e07 to your computer and use it in GitHub Desktop.
Truncate the middle of a string
const truncate = function(fullStr, strLen, separator) {
if (fullStr.length <= strLen) return fullStr;
separator = separator || '…';
const sepLen = separator.length;
const charsToShow = strLen - sepLen;
const frontChars = Math.ceil(charsToShow/2);
const backChars = Math.floor(charsToShow/2);
return [
fullStr.substr(0, frontChars),
separator,
fullStr.substr(fullStr.length - backChars)
].join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment