Skip to content

Instantly share code, notes, and snippets.

@letsgetrandy
Created October 30, 2014 19:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save letsgetrandy/1e05a68ea74ba6736eb5 to your computer and use it in GitHub Desktop.
Save letsgetrandy/1e05a68ea74ba6736eb5 to your computer and use it in GitHub Desktop.
// language exceptions
var exceptions = {
"are": "were",
"eat": "ate",
"go": "went",
"have": "had",
"inherit": "inherited",
"is": "was",
"run": "ran",
"sit": "sat",
"visit": "visited"
}
// 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"
};
for (verb in tests) {
var past = getPastTense(verb);
console.log('Expect "' + verb + '" to be "' + tests[verb] + '": ' + (past === tests[verb] ? 'PASS' : 'FAIL (' + past + ')') );
}
@chadknight-wf
Copy link

@letsgetrandy great gist. I think you have a minor bug on one of your cases, though. L22, (/[aeiou]c/i) should be (/[aeiou]c$/i)

testing with "connect" -> "connectked"

thanks for putting this function out here!

@ts-23
Copy link

ts-23 commented Jul 29, 2019

@chadknight-wf, thanks for the heads up but unfortunately /[aeiou]c$/i also does not work because now "edited" becomes "editted". I don't know the solution but just added to the exceptions map. Thanks for the gist, small, lightweight and good enough for a small selection of words.

@pmstss
Copy link

pmstss commented Sep 11, 2019

@letsgetrandy Great, but 'open' -> 'openned', 'listen' -> 'listenned', etc. Had to remove 'n' from /(.+[aeiou])([bdglmnprst])/ - after that works fine for my limited set of verbs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment