Last active
December 5, 2022 19:26
-
-
Save lloydjatkinson/1d7ede0f078be1289b9b54305c196c59 to your computer and use it in GitHub Desktop.
Trim text and prefix ellipses when the text length is greater than the maximum
This file contains hidden or 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 trimText = (input = '', maximumLength = 80) => { | |
const exceedsMaximum = input.length >= maximumLength; | |
return { | |
exceedsMaximum, | |
text: exceedsMaximum ? `${ input.substr(0, input.lastIndexOf(' ', maximumLength)) }...` : input, | |
} | |
}; | |
export default trimText; | |
// trimText('Hello world! Lorem ipsum.', 17) => { exceedsMaximum: true, text: "Hello world!..." } | |
// trimText('Hello world! Lorem ipsum.', 18) => { exceedsMaximum: true, text: "Hello world! Lorem... "} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment