-
-
Save daraul/50df89e9245b7a1fcf155b23b41a35ba to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var exceptions = { | |
"are": "were", | |
"eat": "ate", | |
"go": "went", | |
"have": "had", | |
"inherit": "inherited", | |
"is": "was", | |
"run": "ran", | |
"sit": "sat", | |
"visit": "visited", | |
"make": "made", | |
"pay": "paid" | |
} | |
// grammatically predictable rules | |
function getPastTense(verb) { | |
if (exceptions[verb]) { | |
return exceptions[verb]; | |
} | |
if ((/e$/i).test(verb)) { | |
return verb + 'd'; | |
} | |
if ((/[aeiou]c/i).test(verb)) { | |
return verb + 'ked'; | |
} | |
// for american english only | |
if ((/el$/i).test(verb)) { | |
return verb + 'ed'; | |
} | |
if ((/[aeio][aeiou][dlmnprst]$/).test(verb)) { | |
return verb + 'ed'; | |
} | |
if ((/[aeiou][bdglmnprst]$/i).test(verb)) { | |
return verb.replace(/(.+[aeiou])([bdglmnprst])/, '$1$2$2ed'); | |
} | |
return verb + 'ed'; | |
} | |
// tests | |
var tests = { | |
"bake": "baked", | |
"smile": "smiled", | |
"free": "freed", | |
"dye": "dyed", | |
"tiptoe": "tiptoed", | |
"travel": "traveled", | |
"model": "modeled", | |
"distil": "distilled", | |
"equal": "equalled", | |
"admit": "admitted", | |
"commit": "committed", | |
"refer": "referred", | |
"inherit": "inherited", | |
"visit": "visited", | |
"stop": "stopped", | |
"tap": "tapped", | |
"sob": "sobbed", | |
"treat": "treated", | |
"wheel": "wheeled", | |
"pour": "poured", | |
"picnic": "picnicked", | |
"mimic": "mimicked", | |
"traffic": "trafficked", | |
"pay": "paid", | |
"make": "made" | |
}; | |
for (verb in tests) { | |
var past = getPastTense(verb); | |
console.log('Expect "' + verb + '" to be "' + tests[verb] + '": ' + (past === tests[verb] ? 'PASS' : 'FAIL (' + past + ')') ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment