Created
November 19, 2021 19:12
-
-
Save KrunchMuffin/18db1f6474aac7600865fb30f0c385f6 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
const nameInverter = function(name) { | |
const honorifics = ['MR.', 'MRS.', 'MS.', 'DR.']; | |
switch (name) { | |
case '': | |
return ''; | |
case undefined: | |
throw new Error('Name is undefined'); | |
} | |
let newName = name.trim(); | |
// i know this is lame, but it's fast | |
if (newName.toUpperCase() === 'MR.' || newName.toUpperCase() === 'MRS.' || newName.toUpperCase() === 'DR.' || newName.toUpperCase() === 'MS.') return ''; | |
const nameArray = newName.split(' '); | |
if (nameArray.length === 2) { | |
if (nameArray.some(item => honorifics.includes(item.toUpperCase()))) { | |
return newName; | |
} else { | |
return nameArray[1] + ', ' + nameArray[0]; | |
} | |
} | |
if (nameArray.length === 3) return nameArray[0] + ' ' + nameArray[2] + ', ' + nameArray[1]; | |
return newName; | |
}; | |
module.exports = nameInverter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment