Skip to content

Commit

Permalink
🔀 Merge pull request #392 from richardfrost/fix_marking_regex_backref…
Browse files Browse the repository at this point in the history
…erence

Fix marking regex backreference
  • Loading branch information
richardfrost authored Mar 10, 2022
2 parents 2c982f8 + 3621ec1 commit bb18bb1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/script/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class Config {
if (Object.keys(this.words).includes(str)) {
return false; // Already exists
} else {
options.sub = options.case ? options.sub.trim() : options.sub.trim().toLowerCase();
options.sub = options.case == Constants.TRUE ? options.sub.trim() : options.sub.trim().toLowerCase();
this.words[str] = options;
return true;
}
Expand Down
19 changes: 12 additions & 7 deletions src/script/lib/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,27 @@ export default class Filter {
const { word, string, match, matchStartIndex, captureGroups, internalCaptureGroups } = this.matchData(wordlist, index, originalMatch, args);
if (this.checkWhitelist(match, string, matchStartIndex, word)) { return match; } // Check for whitelisted match
if (statsType) { this.foundMatch(word, statsType); }
let sub = word.sub || this.cfg.defaultSubstitution;

// Support backreferences for REGEX match method (only checks for 1 capture group)
if (word.matchMethod == Constants.MATCH_METHODS.REGEX && captureGroups.length && word.sub.includes('\\1')) {
let sub = word.sub;
captureGroups.forEach((captureGroup, i) => { sub = sub.replace(`\\${i + 1}`, captureGroup); });
// Use captureGroup.toLowerCase() when not preserving case
captureGroups.forEach((captureGroup, i) => {
sub = sub.replace(`\\${i + 1}`, this.cfg.preserveCase ? captureGroup : captureGroup.toLowerCase());
});

// Only return if something was substituted
if (sub !== word.sub) {
// Only return if something was substituted
if (this.cfg.substitutionMark) {
sub = '[' + sub + ']';
}

return sub;
}
}

// Filter
let sub = word.sub || this.cfg.defaultSubstitution;

// Make substitution match case of original match
if (!word.case && this.cfg.preserveCase) {
if (word.case == Constants.FALSE && this.cfg.preserveCase) {
if (Word.allUpperCase(match)) {
sub = sub.toUpperCase();
} else if (Word.eachWordCapitalized(match)) {
Expand Down
6 changes: 3 additions & 3 deletions src/script/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export function formatNumber(number: number): string {
} else if (length <= 6) { // 1,000 - 999,999
const n = (number/1000).toPrecision();
const index = n.indexOf('.');
return ((index >= -1 && index <= 1) ? n.substr(0, 3) : n.substr(0, index)) + 'k';
return ((index >= -1 && index <= 1) ? n.slice(0, 3) : n.slice(0, index)) + 'k';
} else if (length <= 9) { // 1,000,000 - 999,999,999
const n = (number/1000000).toPrecision();
const index = n.indexOf('.');
return ((index >= -1 && index <= 1) ? n.substr(0, 3) : n.substr(0, index)) + 'M';
return ((index >= -1 && index <= 1) ? n.slice(0, 3) : n.slice(0, index)) + 'M';
} else { // >= 1,000,000,000
return '1G+';
}
Expand Down Expand Up @@ -212,7 +212,7 @@ export function removeFromArray(array: string[], element: string) {
}

export function secondsToHMS(seconds: number): string {
return new Date(seconds * 1000).toISOString().substr(11, 12);
return new Date(seconds * 1000).toISOString().slice(11, (11 + 12));
}

export function stringArray(data: string | string[]): string[] {
Expand Down
2 changes: 1 addition & 1 deletion src/script/lib/word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class Word {
}

static capitalizeFirst(string: string): string {
return string.charAt(0).toUpperCase() + string.substr(1);
return string.charAt(0).toUpperCase() + string.slice(1);
}

static containsDoubleByte(str): boolean {
Expand Down
20 changes: 20 additions & 0 deletions test/lib/filter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,26 @@ describe('Filter', () => {
expect(filter.replaceText('Try not to pee John off.')).to.equal('Try not to tick John off.');
expect(filter.replaceText('Try not to scare John off.')).to.equal('Try not to spook John off.');
});

it('Should support substitutionMark with backreferences in replace', () => {
const filter = new Filter;
filter.cfg = new Config({ filterMethod: Constants.FILTER_METHODS.SUBSTITUTE, substitutionMark: Constants.TRUE, words: {} });
filter.cfg.words['(a)'] = { matchMethod: Constants.MATCH_METHODS.REGEX, repeat: Constants.FALSE, sub: 'you typed: \\1' };
filter.cfg.words['(b)'] = { matchMethod: Constants.MATCH_METHODS.REGEX, repeat: Constants.FALSE, sub: 'you typed: \\1' };
filter.init();
expect(filter.replaceText('a')).to.equal('[you typed: a]');
expect(filter.replaceText('B')).to.equal('[you typed: B]');
});

it('Should not preserveCase when false using backreferences in replace', () => {
const filter = new Filter;
filter.cfg = new Config({ filterMethod: Constants.FILTER_METHODS.SUBSTITUTE, preserveCase: false, words: {} });
filter.cfg.words['(a)'] = { matchMethod: Constants.MATCH_METHODS.REGEX, repeat: Constants.FALSE, sub: 'you typed: \\1' };
filter.cfg.words['(b)'] = { matchMethod: Constants.MATCH_METHODS.REGEX, repeat: Constants.FALSE, sub: 'you typed: \\1' };
filter.init();
expect(filter.replaceText('a')).to.equal('you typed: a');
expect(filter.replaceText('B')).to.equal('you typed: b');
});
});

describe('whitelist', () => {
Expand Down

0 comments on commit bb18bb1

Please sign in to comment.