Last active
December 8, 2020 06:03
-
-
Save Elbone/66a495ceaef207a30e3621db90e62e07 to your computer and use it in GitHub Desktop.
Truncate the middle of a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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