Skip to content

Instantly share code, notes, and snippets.

@Delni
Created September 8, 2020 20:13
Show Gist options
  • Save Delni/1a6ad9850bc7ab320e4941b78316d9a8 to your computer and use it in GitHub Desktop.
Save Delni/1a6ad9850bc7ab320e4941b78316d9a8 to your computer and use it in GitHub Desktop.
const isVowel = (c: string) => 'aeiouy'.includes(c)
const yay = (word: string) => word + (isVowel(word[word.length - 1]) ? 'y' : '') + 'ay'
const isUpperString = (c: string) => c === c.toUpperCase()
const hasUpperCharacter = (word: string) => word.split('').find(isUpperString)
const restablishCase = (word: string) => hasUpperCharacter(word)
? word[0].toUpperCase() + word.substring(1).toLowerCase()
: word;
const toPigLatinWord = (word: string) => {
const index = word.split('').findIndex(isVowel)
return restablishCase(yay(word.substring(index) + word.substring(0, index)))
}
const toPigLatinSentence = (sentence: string) => sentence.replace(/\w+/g, toPigLatinWord)
console.log(toPigLatinSentence('Come to the dark side, we have cookies (and keebs!)'))
// Omecay otay ethay arkday idesay, eway avehay ookiescay (anday eebskay!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment