Remove regex replace strategy selector

This commit is contained in:
Cohee 2024-01-24 16:10:50 +02:00
parent 4823bcf4ff
commit b4e7ee05e6
3 changed files with 2 additions and 109 deletions

View File

@ -76,7 +76,7 @@
</div> </div>
<div class="flex-container"> <div class="flex-container">
<div class="wi-enter-footer-text flex-container flexFlowColumn flexNoGap alignitemsstart"> <div class="flex1 wi-enter-footer-text flex-container flexFlowColumn flexNoGap alignitemsstart">
<small>Affects</small> <small>Affects</small>
<div> <div>
<label class="checkbox flex-container"> <label class="checkbox flex-container">
@ -97,7 +97,7 @@
</label> </label>
</div> </div>
</div> </div>
<div class="wi-enter-footer-text flex-container flexFlowColumn flexNoGap alignitemsstart"> <div class="flex1 wi-enter-footer-text flex-container flexFlowColumn flexNoGap alignitemsstart">
<small>Other Options</small> <small>Other Options</small>
<label class="checkbox flex-container"> <label class="checkbox flex-container">
<input type="checkbox" name="disabled" /> <input type="checkbox" name="disabled" />
@ -120,13 +120,6 @@
<span data-i18n="Substitute Regex">Substitute Regex (?)</span> <span data-i18n="Substitute Regex">Substitute Regex (?)</span>
</label> </label>
</div> </div>
<div class="flex-container flexFlowColumn alignitemsstart">
<small>Replacement Strategy</small>
<select name="replace_strategy_select" class="margin0">
<option value="0">Replace</option>
<option value="1">Overlay (currently broken)</option>
</select>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -19,14 +19,6 @@ const regex_placement = {
SLASH_COMMAND: 3, SLASH_COMMAND: 3,
}; };
/**
* @enum {number} How the regex script should replace the matched string
*/
const regex_replace_strategy = {
REPLACE: 0,
OVERLAY: 1,
};
/** /**
* Instantiates a regular expression from a string. * Instantiates a regular expression from a string.
* @param {string} input The input string. * @param {string} input The input string.
@ -153,86 +145,3 @@ function filterString(rawString, trimStrings, { characterOverride } = {}) {
return finalString; return finalString;
} }
/**
* Substitutes regex-specific and normal parameters
* @param {string} rawString
* @param {string} regexMatch
* @param {RegexSubstituteParams} params The parameters to use for the regex substitution
* @returns {string} The substituted string
* @typedef {{characterOverride?: string, replaceStrategy?: number}} RegexSubstituteParams The parameters to use for the regex substitution
*/
function substituteRegexParams(rawString, regexMatch, { characterOverride, replaceStrategy } = {}) {
let finalString = rawString;
finalString = substituteParams(finalString, undefined, characterOverride);
let overlaidMatch = regexMatch;
// TODO: Maybe move the for loops into a separate function?
if (replaceStrategy === regex_replace_strategy.OVERLAY) {
const splitReplace = finalString.split('{{match}}');
// There's a prefix
if (splitReplace[0]) {
// Fetch the prefix
const splicedPrefix = spliceSymbols(splitReplace[0], false);
// Sequentially remove all occurrences of prefix from start of split
const splitMatch = overlaidMatch.split(splicedPrefix);
let sliceNum = 0;
for (let index = 0; index < splitMatch.length; index++) {
if (splitMatch[index].length === 0) {
sliceNum++;
} else {
break;
}
}
overlaidMatch = splitMatch.slice(sliceNum, splitMatch.length).join(splicedPrefix);
}
// There's a suffix
if (splitReplace[1]) {
// Fetch the suffix
const splicedSuffix = spliceSymbols(splitReplace[1], true);
// Sequential removal of all suffix occurrences from end of split
const splitMatch = overlaidMatch.split(splicedSuffix);
let sliceNum = 0;
for (let index = splitMatch.length - 1; index >= 0; index--) {
if (splitMatch[index].length === 0) {
sliceNum++;
} else {
break;
}
}
overlaidMatch = splitMatch.slice(0, splitMatch.length - sliceNum).join(splicedSuffix);
}
}
// Only one match is replaced. This is by design
finalString = finalString.replace('{{match}}', overlaidMatch) || finalString.replace('{{match}}', regexMatch);
return finalString;
}
/**
* Splices common sentence symbols and whitespace from the beginning and end of a string.
* Using a for loop due to sequential ordering.
* @param {string} rawString The raw string to splice
* @param {boolean} isSuffix String is a suffix
* @returns {string} The spliced string
*/
function spliceSymbols(rawString, isSuffix) {
let offset = 0;
for (const ch of isSuffix ? rawString.split('').reverse() : rawString) {
if (ch.match(/[^\w.,?'!]/)) {
offset++;
} else {
break;
}
}
return isSuffix ? rawString.substring(0, rawString.length - offset) : rawString.substring(offset);
}

View File

@ -141,9 +141,6 @@ async function onRegexEditorOpenClick(existingId) {
editorHtml editorHtml
.find('input[name="substitute_regex"]') .find('input[name="substitute_regex"]')
.prop('checked', existingScript.substituteRegex ?? false); .prop('checked', existingScript.substituteRegex ?? false);
editorHtml
.find('select[name="replace_strategy_select"]')
.val(existingScript.replaceStrategy ?? 0);
existingScript.placement.forEach((element) => { existingScript.placement.forEach((element) => {
editorHtml editorHtml
@ -181,7 +178,6 @@ async function onRegexEditorOpenClick(existingId) {
replaceString: editorHtml.find('.regex_replace_string').val(), replaceString: editorHtml.find('.regex_replace_string').val(),
trimStrings: String(editorHtml.find('.regex_trim_strings').val()).split('\n').filter((e) => e.length !== 0) || [], trimStrings: String(editorHtml.find('.regex_trim_strings').val()).split('\n').filter((e) => e.length !== 0) || [],
substituteRegex: editorHtml.find('input[name="substitute_regex"]').prop('checked'), substituteRegex: editorHtml.find('input[name="substitute_regex"]').prop('checked'),
replaceStrategy: Number(editorHtml.find('select[name="replace_strategy_select"]').find(':selected').val()) ?? 0,
}; };
const rawTestString = String(editorHtml.find('#regex_test_input').val()); const rawTestString = String(editorHtml.find('#regex_test_input').val());
const result = runRegexScript(testScript, rawTestString); const result = runRegexScript(testScript, rawTestString);
@ -224,11 +220,6 @@ async function onRegexEditorOpenClick(existingId) {
editorHtml editorHtml
.find('input[name="substitute_regex"]') .find('input[name="substitute_regex"]')
.prop('checked'), .prop('checked'),
replaceStrategy:
parseInt(editorHtml
.find('select[name="replace_strategy_select"]')
.find(':selected')
.val()) ?? 0,
}; };
saveRegexScript(newRegexScript, existingScriptIndex); saveRegexScript(newRegexScript, existingScriptIndex);