Created
April 30, 2018 16:32
-
-
Save cezary/9c9fd186e58681881d0c7409a27bb816 to your computer and use it in GitHub Desktop.
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 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