Created
October 17, 2019 17:55
-
-
Save adrifmonte/89645064fb557a917def018da8262b85 to your computer and use it in GitHub Desktop.
shorten constant to fit maximum length accepted by sentry
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
// 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