Created
June 27, 2019 05:41
-
-
Save nvlled/ab14deac5351e9beacfc5e4b94d6e04a to your computer and use it in GitHub Desktop.
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
// examples: | |
// makePermissionDescription'oneTwoThree') == 'one two three' | |
// makePermissionDescription('hasFooBarXYZ') == 'foo bar XYZ' | |
// makePermissionDescription('isFOOBar') == 'is FOO bar' | |
// Note: output is meaningful only for inputs that consists only of letters | |
const splitByUpperCase = keyName => { | |
const words = []; | |
let desc = keyName.replace(/^can/, ''); | |
let loops = 4096; | |
while (loops-- > 0) { | |
const m = desc.slice(1).match(/[A-Z]+/); | |
if (!m || m[0].length == desc.length - 1) { | |
if (!(desc[1] || '').match(/[A-Z]/)) { | |
desc = desc.toLowerCase(); | |
} | |
words.push(desc); | |
break; | |
} | |
const i = m.index == 0 ? m[0].length : m.index + 1; | |
let word = desc.slice(0, i); | |
if (!(word[1] || '').match(/[A-Z]/)) { | |
word = word.toLowerCase(); | |
} | |
words.push(word); | |
desc = desc.slice(i); | |
} | |
return words.join(' '); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment