Skip to content

Instantly share code, notes, and snippets.

@KrunchMuffin
Created November 19, 2021 19:12
Show Gist options
  • Save KrunchMuffin/18db1f6474aac7600865fb30f0c385f6 to your computer and use it in GitHub Desktop.
Save KrunchMuffin/18db1f6474aac7600865fb30f0c385f6 to your computer and use it in GitHub Desktop.
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