Commit 9838a3ac authored by Ludan Stoecklé's avatar Ludan Stoecklé

simple future

parent 4c8b58ea
export type EnglishTense = 'SIMPLE_PRESENT' | 'PRESENT' | 'PAST' | 'FUTURE';
export type EnglishTense = 'SIMPLE_PAST' | 'PAST' | 'SIMPLE_PRESENT' | 'PRESENT' | 'SIMPLE_FUTURE' | 'FUTURE';
const tenses = ['SIMPLE_PAST', 'PAST', 'SIMPLE_PRESENT', 'PRESENT', 'SIMPLE_FUTURE', 'FUTURE'];
export type Numbers = 'S' | 'P';
const modals = ['can', 'could', 'may', 'might', 'must', 'shall', 'should', 'will', 'would'];
export interface VerbsInfo {
[key: string]: string[][];
}
export interface ExtraParams {
GOING_TO?: boolean;
WILL?: boolean;
}
function getIrregularPreterit(verbsInfo: VerbsInfo, verb: string): string {
if (verbsInfo && verbsInfo[verb] && verbsInfo[verb].length != 0) {
const verbInfo = verbsInfo[verb];
if (verbInfo.length > 1) {
console.log(`WARNING: multiple conjugations for ${verb}: ${JSON.stringify(verbInfo)}`);
}
// console.log(verbInfo);
return verbInfo[0][0];
} else {
return null;
}
}
function getSimplePast(verbsInfo: VerbsInfo, verb: string, number: Numbers): string {
let irregular: string;
if (verb === 'be') {
if (number === 'P') {
return 'were';
} else {
return 'was';
}
} else if (verb === 'do') {
return 'did';
} else if (verb === 'have') {
return 'had';
} else if ((irregular = getIrregularPreterit(verbsInfo, verb))) {
// is irregular, and we have info
return irregular;
} else {
return verb + 'ed';
}
}
function getSimplePresent(verb: string, number: Numbers): string {
if (number === 'P') {
if (verb === 'be') {
......@@ -36,13 +79,38 @@ function getSimplePresent(verb: string, number: Numbers): string {
}
}
export function getConjugation(verb: string, tense: EnglishTense, number: Numbers): string {
function getSimpleFuture(verb: string, number: Numbers, extraParams: ExtraParams): string {
if (extraParams && extraParams.GOING_TO) {
if (number === 'P') {
return 'are going to ' + verb;
} else {
return 'is going to ' + verb;
}
} else {
// default is will
return 'will ' + verb;
}
}
export function getConjugation(
verbsInfo: VerbsInfo,
verb: string,
tense: EnglishTense,
number: Numbers,
extraParams: ExtraParams,
): string {
if (!verb) {
const err = new Error();
err.name = 'TypeError';
err.message = 'verb must not be null';
throw err;
}
if (tenses.indexOf(tense) == -1) {
const err = new Error();
err.name = 'TypeError';
err.message = `invalid tense: ${tense}`;
throw err;
}
if (number != 'S' && number != 'P') {
const err = new Error();
err.name = 'TypeError';
......@@ -50,9 +118,15 @@ export function getConjugation(verb: string, tense: EnglishTense, number: Number
throw err;
}
if (tense === 'PRESENT' || tense === 'SIMPLE_PRESENT') {
return getSimplePresent(verb, number);
switch (tense) {
case 'PAST':
case 'SIMPLE_PAST':
return getSimplePast(verbsInfo, verb, number);
case 'PRESENT':
case 'SIMPLE_PRESENT':
return getSimplePresent(verb, number);
case 'FUTURE':
case 'SIMPLE_FUTURE':
return getSimpleFuture(verb, number, extraParams);
}
return null;
}
const assert = require('assert');
const EnglishVerbs = require('../dist/index.js');
const EnglishVerbsIrregular = require('english-verbs-irregular');
const testCases = {
// SIMPLE
......@@ -22,6 +23,32 @@ const testCases = {
['can', 'S', 'can'],
],
SIMPLE_PRESENT: [['snow', 'S', 'snows']],
PAST: [
// regular
['listen', 'S', 'listened'],
['listen', 'P', 'listened'],
// irregular
['come', 'S', 'came'],
['come', 'P', 'came'],
['go', 'S', 'went'],
['wake', 'S', 'woke'],
// be have do
['be', 'S', 'was'],
['be', 'P', 'were'],
['have', 'S', 'had'],
['have', 'P', 'had'],
['do', 'S', 'did'],
['do', 'P', 'did'],
],
SIMPLE_PAST: [['see', 'P', 'saw']],
SIMPLE_FUTURE: [
['sleep', 'S', 'will sleep'],
['sleep', 'P', 'will sleep'],
['sleep', 'S', 'is going to sleep', { GOING_TO: true }],
['sleep', 'P', 'are going to sleep', { GOING_TO: true }],
['sleep', 'S', 'will sleep', { WILL: true }],
],
FUTURE: [['rest', 'S', 'will rest']],
};
describe('english-verbs', function() {
......@@ -36,12 +63,39 @@ describe('english-verbs', function() {
const verb = testCase[0];
const number = testCase[1];
const expected = testCase[2];
it(`${verb} ${tense} ${number} => ${expected}`, function() {
assert.equal(EnglishVerbs.getConjugation(verb, tense, number), expected);
let extraParams;
if (tense === 'FUTURE' || tense === 'SIMPLE_FUTURE') {
// has extra params sometimes
if (testCase.length > 3) {
extraParams = testCase[3];
}
}
let verbsList;
if (tense === 'PAST' || tense === 'SIMPLE_PAST') {
verbsList = EnglishVerbsIrregular;
}
it(`${verb} ${tense} ${number} ${
extraParams ? JSON.stringify(extraParams) : ''
} => ${expected}`, function() {
assert.equal(EnglishVerbs.getConjugation(verbsList, verb, tense, number, extraParams), expected);
});
}
});
}
});
describe('edge cases', function() {
it(`null verb`, function() {
assert.throws(() => EnglishVerbs.getConjugation(null, null, 'PRESENT', 'S'), /verb/);
});
it(`invalid number`, function() {
assert.throws(() => EnglishVerbs.getConjugation(null, 'eat', 'PRESENT', 'X'), /number/);
});
it(`invalid tense`, function() {
assert.throws(() => EnglishVerbs.getConjugation(null, 'eat', 'PLUS_QUE_BLABLA', 'S'), /tense/);
});
});
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment