Skip to content

Commit bb18bb1

Browse files
authored
🔀 Merge pull request #392 from richardfrost/fix_marking_regex_backreference
Fix marking regex backreference
2 parents 2c982f8 + 3621ec1 commit bb18bb1

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

‎src/script/lib/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export default class Config {
9797
if (Object.keys(this.words).includes(str)) {
9898
return false; // Already exists
9999
} else {
100-
options.sub = options.case ? options.sub.trim() : options.sub.trim().toLowerCase();
100+
options.sub = options.case == Constants.TRUE ? options.sub.trim() : options.sub.trim().toLowerCase();
101101
this.words[str] = options;
102102
return true;
103103
}

‎src/script/lib/filter.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,27 @@ export default class Filter {
140140
const { word, string, match, matchStartIndex, captureGroups, internalCaptureGroups } = this.matchData(wordlist, index, originalMatch, args);
141141
if (this.checkWhitelist(match, string, matchStartIndex, word)) { return match; } // Check for whitelisted match
142142
if (statsType) { this.foundMatch(word, statsType); }
143+
let sub = word.sub || this.cfg.defaultSubstitution;
143144

144145
// Support backreferences for REGEX match method (only checks for 1 capture group)
145146
if (word.matchMethod == Constants.MATCH_METHODS.REGEX && captureGroups.length && word.sub.includes('\\1')) {
146-
let sub = word.sub;
147-
captureGroups.forEach((captureGroup, i) => { sub = sub.replace(`\\${i + 1}`, captureGroup); });
147+
// Use captureGroup.toLowerCase() when not preserving case
148+
captureGroups.forEach((captureGroup, i) => {
149+
sub = sub.replace(`\\${i + 1}`, this.cfg.preserveCase ? captureGroup : captureGroup.toLowerCase());
150+
});
151+
152+
// Only return if something was substituted
148153
if (sub !== word.sub) {
149-
// Only return if something was substituted
154+
if (this.cfg.substitutionMark) {
155+
sub = '[' + sub + ']';
156+
}
157+
150158
return sub;
151159
}
152160
}
153161

154-
// Filter
155-
let sub = word.sub || this.cfg.defaultSubstitution;
156-
157162
// Make substitution match case of original match
158-
if (!word.case && this.cfg.preserveCase) {
163+
if (word.case == Constants.FALSE && this.cfg.preserveCase) {
159164
if (Word.allUpperCase(match)) {
160165
sub = sub.toUpperCase();
161166
} else if (Word.eachWordCapitalized(match)) {

‎src/script/lib/helper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ export function formatNumber(number: number): string {
3434
} else if (length <= 6) { // 1,000 - 999,999
3535
const n = (number/1000).toPrecision();
3636
const index = n.indexOf('.');
37-
return ((index >= -1 && index <= 1) ? n.substr(0, 3) : n.substr(0, index)) + 'k';
37+
return ((index >= -1 && index <= 1) ? n.slice(0, 3) : n.slice(0, index)) + 'k';
3838
} else if (length <= 9) { // 1,000,000 - 999,999,999
3939
const n = (number/1000000).toPrecision();
4040
const index = n.indexOf('.');
41-
return ((index >= -1 && index <= 1) ? n.substr(0, 3) : n.substr(0, index)) + 'M';
41+
return ((index >= -1 && index <= 1) ? n.slice(0, 3) : n.slice(0, index)) + 'M';
4242
} else { // >= 1,000,000,000
4343
return '1G+';
4444
}
@@ -212,7 +212,7 @@ export function removeFromArray(array: string[], element: string) {
212212
}
213213

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

218218
export function stringArray(data: string | string[]): string[] {

‎src/script/lib/word.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class Word {
3838
}
3939

4040
static capitalizeFirst(string: string): string {
41-
return string.charAt(0).toUpperCase() + string.substr(1);
41+
return string.charAt(0).toUpperCase() + string.slice(1);
4242
}
4343

4444
static containsDoubleByte(str): boolean {

‎test/lib/filter.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,26 @@ describe('Filter', () => {
396396
expect(filter.replaceText('Try not to pee John off.')).to.equal('Try not to tick John off.');
397397
expect(filter.replaceText('Try not to scare John off.')).to.equal('Try not to spook John off.');
398398
});
399+
400+
it('Should support substitutionMark with backreferences in replace', () => {
401+
const filter = new Filter;
402+
filter.cfg = new Config({ filterMethod: Constants.FILTER_METHODS.SUBSTITUTE, substitutionMark: Constants.TRUE, words: {} });
403+
filter.cfg.words['(a)'] = { matchMethod: Constants.MATCH_METHODS.REGEX, repeat: Constants.FALSE, sub: 'you typed: \\1' };
404+
filter.cfg.words['(b)'] = { matchMethod: Constants.MATCH_METHODS.REGEX, repeat: Constants.FALSE, sub: 'you typed: \\1' };
405+
filter.init();
406+
expect(filter.replaceText('a')).to.equal('[you typed: a]');
407+
expect(filter.replaceText('B')).to.equal('[you typed: B]');
408+
});
409+
410+
it('Should not preserveCase when false using backreferences in replace', () => {
411+
const filter = new Filter;
412+
filter.cfg = new Config({ filterMethod: Constants.FILTER_METHODS.SUBSTITUTE, preserveCase: false, words: {} });
413+
filter.cfg.words['(a)'] = { matchMethod: Constants.MATCH_METHODS.REGEX, repeat: Constants.FALSE, sub: 'you typed: \\1' };
414+
filter.cfg.words['(b)'] = { matchMethod: Constants.MATCH_METHODS.REGEX, repeat: Constants.FALSE, sub: 'you typed: \\1' };
415+
filter.init();
416+
expect(filter.replaceText('a')).to.equal('you typed: a');
417+
expect(filter.replaceText('B')).to.equal('you typed: b');
418+
});
399419
});
400420

401421
describe('whitelist', () => {

0 commit comments

Comments
 (0)