Skip to content

Instantly share code, notes, and snippets.

@adrifmonte
Created October 17, 2019 17:55
Show Gist options
  • Save adrifmonte/89645064fb557a917def018da8262b85 to your computer and use it in GitHub Desktop.
Save adrifmonte/89645064fb557a917def018da8262b85 to your computer and use it in GitHub Desktop.
shorten constant to fit maximum length accepted by sentry
// shorten constant to fit maximum length accepted by sentry
const shortenConstant = (constantCaseString, maxlength = 25, separator = '_') => {
// no need to shorten
if (!constantCaseString || constantCaseString.length < maxlength) {
return constantCaseString;
}
// abbreviate each word to fit the max length
const splittedConstantCaseTokens = constantCaseString.split(separator);
if (splittedConstantCaseTokens.length > 1) {
const maxWordLength = maxlength / splittedConstantCaseTokens.length;
return splittedConstantCaseTokens
.map(constantCaseToken => constantCaseToken.substring(0, maxWordLength))
.join(separator);
}
// no way to abbreviate, single word is too long
// just return the initial part
return constantCaseString.substr(0, maxlength);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment