Last active
August 9, 2018 23:13
-
-
Save romines/2d04bbceba4acdd6189f5b882bf2405a 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
/* | |
Rules: | |
1. Any word that starts with a vowel, add "way" to the end of the word | |
2. Any word that starts with a consonant, move the first letter of the word to the end of the word, and then add "ay" after that | |
*/ | |
const vowels = ['a', 'e', 'i', 'o', 'u']; | |
const allLetters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; | |
module.exports = function pigLatinize (str) { | |
function latinify(str) { | |
const precedingPunctation = getPrecedingPunctation(str); | |
const trailingPunctation = getTrailingPunctuation(str); | |
const puncLess = str.split('').filter(letter => allLetters.includes(letter.toLowerCase())).join(''); | |
const firstLetterWasCapitolized = puncLess[0] === puncLess[0].toUpperCase(); | |
const isVowelStart = vowels.includes(puncLess[0].toLowerCase()); | |
const latinifiedWord = isVowelStart | |
? puncLess + 'way' | |
: puncLess.slice(1) + puncLess[0] + 'ay'; | |
const capitolized = firstLetterWasCapitolized | |
? latinifiedWord[0].toUpperCase() + latinifiedWord.slice(1, latinifiedWord.length).toLowerCase() | |
: latinifiedWord; | |
return precedingPunctation + stripPunctuation(capitolized) + trailingPunctation; | |
} | |
function latinifySegment(sentence) { | |
return sentence.split(' ').map(latinify).join(' '); | |
} | |
const dashedSegments = str.split('-'); | |
return dashedSegments.map(latinifySegment).join('-'); | |
}; | |
/** | |
* | |
* Helpers | |
* | |
*/ | |
function getPrecedingPunctation(str) { | |
const innerFindPunctuations = (str, puncsFound) => { | |
const letters = str.split(''); | |
for (let i = 0; i < letters.length; i++) { | |
const letter = letters[i]; | |
if (allLetters.includes(letter.toLowerCase())) { | |
return puncsFound; | |
} | |
else { | |
return innerFindPunctuations(str.slice(1), puncsFound + letter); | |
} | |
} | |
}; | |
return innerFindPunctuations(str, ''); | |
} | |
function getTrailingPunctuation(str) { | |
const flipped = str.split('').reverse().join(''); | |
const punctuation = getPrecedingPunctation(flipped); | |
return punctuation.split('').reverse().join(''); | |
} | |
function stripPunctuation(str) { | |
return str.split('') | |
.filter(letter => allLetters.includes(letter.toLowerCase())) | |
.join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment