Skip to content

Instantly share code, notes, and snippets.

@cplpearce
Created August 11, 2020 18:53
Show Gist options
  • Save cplpearce/b82090d7476c383ebde07b38f8abba78 to your computer and use it in GitHub Desktop.
Save cplpearce/b82090d7476c383ebde07b38f8abba78 to your computer and use it in GitHub Desktop.
Kata 13 - Case Maker II
const makeCase = function (input, scheme) {
(typeof scheme !== 'object') && (scheme = [scheme]);
let arrStr = [input];
for (let sch of scheme){
let i = 0;
let cap = false;
for (let c of arrStr.join('')) {
if (sch === 'camel') {
c === ' ' ? cap = true : cap === true ? (arrStr[i] = c.toUpperCase()) && (cap = false) : arrStr[i] = c;
i++;
} else if (sch === 'pascal') {
i === 0 ? arrStr[i] = c.toUpperCase() : c === ' ' ? capitalize = true : capitalize === true ? (arrStr[i] = c.toUpperCase()) && (capitalize = false) : arrStr[i] = c;
i++;
} else if (sch === 'snake') {
c === ' ' ? arrStr[i] = '_' : arrStr[i] = c;
i++;
} else if (sch === 'kebab') {
c === ' ' ? arrStr[i] = '-' : arrStr[i] = c;
i++;
} else if (sch === 'title') {
cap === true ? ((arrStr[i] = c.toUpperCase()), (cap = false)) : c === ' ' ? ((cap = true) && (arrStr[i] = c)) : i === 0 ? (arrStr[i] = c.toUpperCase()) && (i++) : arrStr[i] = c;
i++;
} else if (sch === 'vowel') {
['a', 'e', 'i', 'o', 'u'].includes(c) ? arrStr[i] = c.toUpperCase() : arrStr[i] = c;
i++;
} else if (sch === 'consonant') {
!['a', 'e', 'i', 'o', 'u'].includes(c) ? arrStr[i] = c.toUpperCase() : arrStr[i] = c;
i++;
} else if (sch === 'upper') {
arrStr = arrStr.map(c => c.toUpperCase())
} else {
arrStr = arrStr.map(c => c.toLowerCase())
}
}
}
return arrStr.join('');
};
console.log(makeCase("this is a string", "camel")); // thisIsAString
console.log(makeCase("this is a string", "pascal")); // ThisIsAString
console.log(makeCase("this is a string", "snake")); // this_is_a_string
console.log(makeCase("this is a string", "kebab")); // this-is-a-string
console.log(makeCase("this is a string", "title")); // This Is A String
console.log(makeCase("this is a string", "vowel")); // thIs Is A strIng
console.log(makeCase("this is a string", "consonant")); // THiS iS a STRiNG
console.log(makeCase("this is a string", "upper")); // THIS IS A STRING
console.log(makeCase("this is a string", "lower")); // this is a string
console.log(makeCase("this is a string", ["lower", "snake", "vowel"])); // thIs_Is_A_strIng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment