Skip to content

Instantly share code, notes, and snippets.

@lloydjatkinson
Last active December 5, 2022 19:26
Show Gist options
  • Save lloydjatkinson/1d7ede0f078be1289b9b54305c196c59 to your computer and use it in GitHub Desktop.
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
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