Skip to content

Instantly share code, notes, and snippets.

@nvlled
Created June 27, 2019 05:41
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 nvlled/ab14deac5351e9beacfc5e4b94d6e04a to your computer and use it in GitHub Desktop.
Save nvlled/ab14deac5351e9beacfc5e4b94d6e04a to your computer and use it in GitHub Desktop.
// 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