diff --git a/src/replace-words.js b/src/replace-words.js index b9b6990..4173b94 100644 --- a/src/replace-words.js +++ b/src/replace-words.js @@ -1,6 +1,6 @@ -const verbsHelper = require('english-verbs-helper'); -const verbsIrregular = require('english-verbs-irregular/dist/verbs.json'); -const verbsGerunds = require('english-verbs-gerunds/dist/gerunds.json'); +const verbsHelper = require("english-verbs-helper"); +const verbsIrregular = require("english-verbs-irregular/dist/verbs.json"); +const verbsGerunds = require("english-verbs-gerunds/dist/gerunds.json"); const verbsData = verbsHelper.mergeVerbsData(verbsIrregular, verbsGerunds); /* Replaces verbs, point-of-view, name, pronouns, and "also" terms */ @@ -50,19 +50,8 @@ function replaceName(options) { /* Replaces all verb keys */ function replaceVerbs(options) { - const isPlural = getPronouns(options.preset, options.other).plurality == "plural"; - if (isPlural) { - walk(document.body, /([Vv])r([Nn])\/([\w\s]+)\/([\w\s]+)\//, options, pluralPrnVerbMethod); - } else { - walk(document.body, /([Vv])r[Nn]\/([\w\s]+)\/([\w\s]+)\//, options, prnVerbMethod); - } - - const isPluralThird = isPlural && options.pov == "third"; - if (isPluralThird) { - walk(document.body, /([Vv])r([Bb])\/([\w\s]+)\/([\w\s]+)\//, options, pluralThirdVerbMethod); - } else { - walk(document.body, /([Vv])r[Bb]\/([\w\s]+)\/([\w\s]+)\//, options, verbMethod); - } + const searchTerm = /(v)r([nb])\/([\w\s]+)\/(?:((?:(?:simple|progressive|perfect|perfect[ _]progressive|participle(?![ _]future))[ _])*(?:past|present|future))\/)*/i; + walk(document.body, searchTerm, options, verbMethod); } /* Replaces all pronoun keys */ @@ -237,14 +226,14 @@ function replaceAlso(options) { /* Replaces exception statements by POV */ function replaceExceptions(options) { - const searchTerm = /[Ee]xc\/([^\/]*)\/([^\/]*)\//; + const searchTerm = /exc\/([^\/]*)\/([^\/]*)\/ig/; walk(document.body, searchTerm, options, exceptionMethod); } /* Replaces cut statements with cuts */ function replaceCuts() { - const searchTerm = /[Cc]ut\/([^\/]+)\/(-?[\d]+)\//; + const searchTerm = /cut\/([^\/]+)\/(-?[\d]+)\/ig/; walk(document.body, searchTerm, null, cutMethod); } @@ -288,133 +277,49 @@ function directMethod(node, searchTerm, replaceValue) { } /* Replaces all verbs, not used for third-person plural */ -/* TODO Is the "options" variable here readable? It's not replaceValue and that seems bad */ function verbMethod(node, searchTerm, options) { - let match = node.nodeValue.match(searchTerm); + const match = node.nodeValue.match(searchTerm); if (match == null) { return; } const isCapital = /V/.test(match[1]); - const verb = match[2]; - let tense = getTense(match[3]); - const isTenseProvided = tense != null; - if (!isTenseProvided) { + const category = match[2]; + const verb = match[3]; + let tense = match[4]; + + if (tense == null) { tense = "PAST"; + } else { + tense = tense.toUpperCase().replaceAll(" ", "_"); } - let replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, getPovIndex(options)); + let povIndex; + switch (category) { + case "B": /* B and b vary by POV, capitalized to indicate a named subject */ + if (options.pov == "third") { + povIndex = getPovIndex("third-singular", null); + } else { + povIndex = getPovIndex(options.pov, options); + } + case "b": + povIndex = getPovIndex(options.pov, options); + case "N": /* N and n are always third-person */ + povIndex = getPovIndex("third-singular", null); + case "n": + povIndex = getPovIndex("third", options); + } + + let replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, povIndex); if (isCapital) { replaceValue = capitalize(replaceValue); } - if (isTenseProvided) { - node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); - } else { - const searchTermShortVerb = /([Vv])r[Bb]\/([\w\s]+)\// - node.nodeValue = node.nodeValue.replace(searchTermShortVerb, replaceValue); - } - + node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); verbMethod(node, searchTerm, options); } -/* Replaces all verbs, used only for third-person plural */ -function pluralThirdVerbMethod(node, searchTerm, options) { - let match = node.nodeValue.match(searchTerm); - if (match == null) { return; } - const isCapital = /V/.test(match[1]); - const isNamedActor = /B/.test(match[2]); - const verb = match[3]; - let tense = getTense(match[4]); - const isTenseProvided = tense != null; - if (!isTenseProvided) { - tense = "PAST"; - } - - let replaceValue; - if (isNamedActor) { - replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, 2); - } else { - replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, getPovIndex(options)); - } - - if (isCapital) { - replaceValue = capitalize(replaceValue); - } - - if (isTenseProvided) { - node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); - } else { - const searchTermShortVerb = /([Vv])r([Bb])\/([\w\s]+)\// - node.nodeValue = node.nodeValue.replace(searchTermShortVerb, replaceValue); - } - - pluralThirdVerbMethod(node, searchTerm, options); -} - -/* Replaces verbs by pronoun rather than POV, not used for plural */ -function prnVerbMethod(node, searchTerm, options) { - let match = node.nodeValue.match(searchTerm); - if (match == null) { return; } - const isCapital = /V/.test(match[1]); - const verb = match[2]; - let tense = getTense(match[3]); - const isTenseProvided = tense != null; - if (!isTenseProvided) { - tense = "PAST"; - } - - let replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, 2); - - if (isCapital) { - replaceValue = capitalize(replaceValue); - } - - if (isTenseProvided) { - node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); - } else { - const searchTermShortVerb = /([Vv])r[Bb]\/([\w\s]+)\// - node.nodeValue = node.nodeValue.replace(searchTermShortVerb, replaceValue); - } - - prnVerbMethod(node, searchTerm, options); -} - -/* Replaces verbs by pronoun rather than POV, used only for plural */ -function pluralPrnVerbMethod(node, searchTerm, options) { - let match = node.nodeValue.match(searchTerm); - if (match == null) { return; } - const isCapital = /V/.test(match[1]); - const isNamedActor = /N/.test(match[2]); - const verb = match[3]; - let tense = getTense(match[4]); - const isTenseProvided = tense != null; - if (!isTenseProvided) { - tense = "PAST"; - } - - let replaceValue; - if (isNamedActor) { - replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, 2); - } else { - replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, 5); - } - - if (isCapital) { - replaceValue = capitalize(replaceValue); - } - - if (isTenseProvided) { - node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); - } else { - const searchTermShortVerb = /([Vv])r([Bb])\/([\w\s]+)\// - node.nodeValue = node.nodeValue.replace(searchTermShortVerb, replaceValue); - } - - pluralPrnVerbMethod(node, searchTerm, options); -} - /* Allows for custom words to be cut off */ function cutMethod(node, searchTerm, unused) { - let match = node.nodeValue.match(searchTerm); + const match = node.nodeValue.match(searchTerm); if (match == null) { return; } const target = match[1]; const amount = match[2]; @@ -438,7 +343,7 @@ function cutMethod(node, searchTerm, unused) { /* Allows for custom words to be cut off */ function exceptionMethod(node, searchTerm, options) { - let match = node.nodeValue.match(searchTerm); + const match = node.nodeValue.match(searchTerm); if (match == null) { return; } let replaceValue; @@ -458,53 +363,21 @@ function capitalize(word) { } /* Determines the index for point-of-view to be used when conjugating verbs */ -function getPovIndex(options) { - switch (options.pov) { +function getPovIndex(pov, options) { + switch (pov) { case "first": return 0; case "second": return 1; + case "third-singular": + return 2; case "third": if (getPronouns(options.preset, options.other).plurality == "singular") { return 2; } else { /* Plurality is plural, "they go" */ return 5; } - } -} - -/* Gets the verb tense from a regexp match */ -function getTense(match) { - const tense = match.toUpperCase().replaceAll(" ", "_"); - const tenses = [ - // SIMPLE - 'SIMPLE_PAST', - 'PAST', - 'SIMPLE_PRESENT', - 'PRESENT', - 'SIMPLE_FUTURE', - 'FUTURE', - // PROGRESSIVE - 'PROGRESSIVE_PAST', - 'PROGRESSIVE_PRESENT', - 'PROGRESSIVE_FUTURE', - // PERFECT - 'PERFECT_PAST', - 'PERFECT_PRESENT', - 'PERFECT_FUTURE', - // PERFECT PROGRESSIVE - 'PERFECT_PROGRESSIVE_PAST', - 'PERFECT_PROGRESSIVE_PRESENT', - 'PERFECT_PROGRESSIVE_FUTURE', - // PARTICIPLE - 'PARTICIPLE_PRESENT', - 'PARTICIPLE_PAST', - ]; - - if (tenses.indexOf(tense) == -1) { - return null; - } else { - return tense; + return getThirdIndex(options); } }