Skip to content

Commit

Permalink
🐛 Fix exact match with edge punctuation
Browse files Browse the repository at this point in the history
  • Loading branch information
richardfrost committed Mar 28, 2019
1 parent a3f16c5 commit 7de69bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/script/lib/word.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default class Word {
private static readonly _edgePunctuationRegExp = /(^[,.'"!?%$]|[,.'"!?%$]$)/;
private static readonly _escapeRegExp = /[\/\\^$*+?.()|[\]{}]/g;
private static readonly _unicodeRegex = /[^\u0000-\u00ff]/;
private static readonly _unicodeWordBoundary = '[\\s.,\'"+!?|-]';
Expand All @@ -21,6 +22,8 @@ export default class Word {
// Work around for lack of word boundary support for unicode characters
// /(^|[\s.,'"+!?|-]+)(word)([\s.,'"+!?|-]+|$)/giu
return new RegExp('(^|' + Word._unicodeWordBoundary + '+)(' + Word.processPhrase(str, matchRepeated) + ')(' + Word._unicodeWordBoundary + '+|$)', 'giu');
} else if (str.match(Word._edgePunctuationRegExp)) { // Begin or end with punctuation (not \w))
return new RegExp('(^|\\s)(' + Word.processPhrase(str, matchRepeated) + ')(\\s|$)', 'giu');
} else {
return new RegExp('\\b' + Word.processPhrase(str, matchRepeated) + '\\b', 'gi');
}
Expand Down
23 changes: 23 additions & 0 deletions test/lib/filter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ describe('Filter', function() {
expect(filter.replaceText('I love having good examples as an Exxaammppllee.')).to.equal('I love having good examples as an [Demo].');
});

it('Should filter an exact word ending with punctuation', function() {
let filter = new Filter;
filter.cfg = new Config(
{
filterMethod: 1,
globalMatchMethod: 3,
substitutionMark: false,
preserveCase: true,
words: {
'this!': { matchMethod: 0, repeat: false, sub: 'that!' },
'!bang': { matchMethod: 0, repeat: true, sub: '!poof' },
'!another!': { matchMethod: 0, repeat: false, sub: '$znother#' }
},
}
);
filter.init();
expect(filter.replaceText('I love This! Do you?')).to.equal('I love That! Do you?');
expect(filter.replaceText('I love this!')).to.equal('I love that!');
expect(filter.replaceText('Go out with a !baangg')).to.equal('Go out with a !poof');
expect(filter.replaceText('Go out with a !Bang!')).to.equal('Go out with a !Bang!');
expect(filter.replaceText('!ANOTHER! so cool!')).to.equal('$ZNOTHER# so cool!');
});

it('Should filter an partial word with substitions not marked and preserveCase', function() {
let filter = new Filter;
filter.cfg = new Config({ words: Object.assign({}, testWords), filterMethod: 1, globalMatchMethod: 3, substitutionMark: false, preserveCase: true });
Expand Down

0 comments on commit 7de69bf

Please sign in to comment.