Skip to content

Instantly share code, notes, and snippets.

@IrhaAli
Created October 13, 2022 15:38
Show Gist options
  • Save IrhaAli/f7771ba5b2fe115bc6f5ffa1fe348daf to your computer and use it in GitHub Desktop.
Save IrhaAli/f7771ba5b2fe115bc6f5ffa1fe348daf to your computer and use it in GitHub Desktop.
Returns the string converted to the different styles specified
//helper functions
const camel = function(input){
//split the words
let output = input.split(' ');
//capitalize the words
for (let i = 1; i < output.length; i++){
output[i] = output[i][0].toUpperCase() + output[i].substring(1);
}
//join the words
return output.join('');
}
const pascal = function(input){
return input[0].toUpperCase() + camel(input).substring(1);
}
const snake = function(input){
return input.replaceAll(' ', '_');
}
const kebab = function(input){
return input.replaceAll(' ', '-');
}
const title = function(input){
//split the words
let output = input.split(' ');
//capitalize the words
for (let i = 0; i < output.length; i++){
output[i] = output[i][0].toUpperCase() + output[i].substring(1);
}
//join the words
return output.join(' ');
}
const vowel = function(input){
let vowels = ['a','e','i','o','u'];
//split the words
let output = input.split('');
//capitalize the vowels
for (let i = 0; i < output.length; i++){
if (vowels.includes(output[i])){
output[i] = output[i].toUpperCase();
}
}
//join the words
return output.join('');
}
const consonant = function(input){
let vowels = ['a','e','i','o','u'];
//split the words
let output = input.split('');
//capitalize the consonants
for (let i = 0; i < output.length; i++){
if (!(vowels.includes(output[i]))){
output[i] = output[i].toUpperCase();
}
}
//join the words
return output.join('');
}
const upper = function(input){
return input.toUpperCase();
}
const lower = function(input){
return input.toLowerCase();
}
//main function
const makeCase = function(input, style) {
/*casing styles:camel, pascal (camel & first letter cap),snake (_ for spaces), kebab (- for spaces), title (each word cap),
vowel (vowel cap), consonant (const cap),
upper (cap each letter), lower (uncap each letter)
*/
if (!(Array.isArray(style))){
style = [style];
}
let modifiedString = input;
if (style.includes('camel')){
modifiedString = camel(modifiedString);
}if (style.includes('pascal')){
modifiedString = pascal(modifiedString);
}if (style.includes('snake')){
modifiedString = snake(modifiedString);
}if (style.includes('kebab')){
modifiedString = kebab(modifiedString);
}if (style.includes('title')){
modifiedString = title(modifiedString);
}if (style.includes('vowel')){
modifiedString = vowel(modifiedString);
}if (style.includes('consonant')){
modifiedString = consonant(modifiedString);
}if (style.includes('upper')){
modifiedString = upper(modifiedString);
}if (style.includes('lower')){
modifiedString = lower(modifiedString);
}
return modifiedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment