From 94b04337bedfca82fb80eba21103b65993fd6256 Mon Sep 17 00:00:00 2001 From: Jean Date: Thu, 26 Jun 2025 18:55:35 -0700 Subject: [PATCH] Adding capitalization feature, resolves #30 --- src/replace-words.js | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/replace-words.js b/src/replace-words.js index d922cd3..fae509e 100644 --- a/src/replace-words.js +++ b/src/replace-words.js @@ -40,6 +40,7 @@ function replaceAll() { if (isAllSet) { replaceExceptions(options); replaceCuts(); + replaceCapitals(); } }); } @@ -226,13 +227,18 @@ function replaceExceptions(options) { walk(document.body, searchTerm, options, exceptionMethod); } - /* Replaces cut statements with cuts */ function replaceCuts() { const searchTerm = /cut\/([^\/]+)\/(-?[\d]+)\//i; walk(document.body, searchTerm, null, cutMethod); } +/* Replaces cut statements with cuts */ +function replaceCapitals() { + const searchTerm = /(cap|Cap|CAP)\/([^\/]+)\//i; + walk(document.body, searchTerm, null, capitalMethod); +} + /* Turns a term into regexp format and uses that to replace it */ function escapeAndReplace(searchTerm, replaceValue, caseSensitive) { let searchTermEscaped = searchTerm.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); @@ -331,6 +337,22 @@ function verbMethod(node, searchTerm, options) { verbMethod(node, searchTerm, options); } +/* Allows for first, second, and third-person POV to read differently */ +function exceptionMethod(node, searchTerm, options) { + const match = node.nodeValue.match(searchTerm); + if (match == null) { return; } + + let replaceValue; + if (options.pov == "third") { + replaceValue = match[2]; + } else { + replaceValue = match[1]; + } + + node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); + exceptionMethod(node, searchTerm, options); +} + /* Allows for custom words to be cut off */ function cutMethod(node, searchTerm, unused) { const match = node.nodeValue.match(searchTerm); @@ -355,20 +377,28 @@ function cutMethod(node, searchTerm, unused) { cutMethod(node, searchTerm, unused); } -/* Allows for custom words to be cut off */ -function exceptionMethod(node, searchTerm, options) { +/* Allows for custom words to be capitalized */ +function capitalMethod(node, searchTerm, unused) { const match = node.nodeValue.match(searchTerm); if (match == null) { return; } + const style = match[1]; + const target = match[2]; + const isLowerCase = /c/.test(style.slice(0,1)); // cap let replaceValue; - if (options.pov == "third") { - replaceValue = match[2]; + if (isLowerCase) { + replaceValue = target.toLowerCase(); } else { - replaceValue = match[1]; + const isStartCase = /a/.test(style.slice(1,2)); // Cap + if (isStartCase) { + replaceValue = capitalize(target); + } else { // CAP + replaceValue = target.toUpperCase(); + } } node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); - exceptionMethod(node, searchTerm, options); + capitalMethod(node, searchTerm, unused); } /* Returns the input word, capitalized */