Skip to content

Instantly share code, notes, and snippets.

@cezary
Created April 30, 2018 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cezary/9c9fd186e58681881d0c7409a27bb816 to your computer and use it in GitHub Desktop.
Save cezary/9c9fd186e58681881d0c7409a27bb816 to your computer and use it in GitHub Desktop.
const capitalize = word => word[0].toUpperCase() + word.slice(1).toLowerCase()
const abbreviate = (str, length=2) => {
str = str
.split(' ')
.map(word => word.toUpperCase() === word ? capitalize(word) : word)
.join(' ');
const ret = [];
str.replace(/([A-Z])/g, (match, p1) => {
if (ret.length >= 2) return;
ret.push(p1);
});
return ret.join('');
}
const testStrings = ['Test', 'The Test', 'Test Test', 'TestTest', 'TEST', 'CUR Media', 'CUR MEDIA', 'Clockwise.MD', 'Fresh8 Gaming', 'Numbers Personal Finance AG', 'Q CTRL Pty']
console.log(testStrings.map(abbreviate));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment