From 781350d089140387f0083bcd45ad4aa481bed284 Mon Sep 17 00:00:00 2001 From: Jean Date: Sun, 20 Jul 2025 14:44:29 -0700 Subject: [PATCH] Adding equals feature, fixes #40 --- src/replace-words.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/replace-words.js b/src/replace-words.js index 9945ec0..f09829a 100644 --- a/src/replace-words.js +++ b/src/replace-words.js @@ -40,6 +40,7 @@ function replaceAll() { if (isAllSet) { replaceExceptions(options); + replaceEquals(); replaceCuts(); replaceCapitals(); replaceMirror(); @@ -230,6 +231,12 @@ function replaceExceptions(options) { walk(document.body, searchTerm, options, exceptionMethod); } +/* Replaces statements by whether they match requested terms */ +function replaceEquals() { + const searchTerm = /eql\/([\w\s]+)\/([\w\s]+)\/([^\/]*)\/(?:([^\/]*)\/)?/i; + walk(document.body, searchTerm, null, equalsMethod); +} + /* Replaces cut statements with cuts */ function replaceCuts() { const searchTerm = /cut\/([^\/]+)\/(off|only) (first|last) ([1-9][0-9]*)\//i; @@ -375,13 +382,27 @@ function exceptionMethod(node, searchTerm, options) { } } - - - node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); exceptionMethod(node, searchTerm, options); } +/* Allows for custom answers to custom words */ +function equalsMethod(node, searchTerm, unused) { + const match = node.nodeValue.match(searchTerm); + if (match == null) { return; } + let replaceValue; + + const isEqual = match[1] === match[2]; + if (isEqual) { + replaceValue = match[3]; + } else { + replaceValue = match[4]; + } + + node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue); + exceptionMethod(node, searchTerm, unused); +} + /* Allows for custom words to be truncated */ function cutMethod(node, searchTerm, unused) { const match = node.nodeValue.match(searchTerm);