Skip to content

Commit

Permalink
🔀 Merge pull request #538 from FrostCo/fix_remove_special_characters
Browse files Browse the repository at this point in the history
Fix special characters for remove filter mode
  • Loading branch information
richardfrost authored Jan 10, 2024
2 parents 5802211 + 221d155 commit b530da1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/script/lib/word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Word {
unicode: boolean;
value: string;

private static readonly _edgePunctuationRegExp = /(^[,.'"!?%$+]|[,.'"!?%$+]$)/;
private static readonly _edgePunctuationRegExp = /(^[,.'"`!@#$%^&*?:;+~_=()[\]{}<>|\\/-]|[,.'"`!@#$%^&*?:;+~_=()[\]{}<>|\\/-]$)/;
private static readonly _escapeRegExp = /[\/\\^$*+?.()|[\]{}]/g;
private static readonly _unicodeRegExp = /[^\u0000-\u00ff]/;
private static readonly _unicodeWordBoundary = '[\\s.,\'"+!?|-]';
Expand Down
72 changes: 72 additions & 0 deletions test/spec/lib/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ describe('Filter', () => {
filter.init();
expect(filter.replaceText('You deserve an A+')).to.equal('You deserve an __');
});

it('Ending with tilde', () => {
const filter = new Filter;
filter.cfg = new Config({
censorCharacter: '_',
filterMethod: Constants.FILTER_METHODS.CENSOR,
preserveFirst: false,
words: {
'waves~': { matchMethod: Constants.MATCH_METHODS.EXACT },
},
});
filter.init();
expect(filter.replaceText('Look at those waves~')).to.equal('Look at those ______');
});
});

describe('Partial', () => {
Expand Down Expand Up @@ -186,6 +200,22 @@ describe('Filter', () => {
expect(filter.replaceText('I love allthis! Do you?')).to.equal('I love all_____ Do you?');
expect(filter.replaceText('I love this! Do you?')).to.equal('I love _____ Do you?');
});

it('Ending with brackets and braces', () => {
const filter = new Filter;
filter.cfg = new Config({
censorCharacter: '_',
filterMethod: Constants.FILTER_METHODS.CENSOR,
preserveFirst: false,
words:{
'{cool}': { matchMethod: Constants.MATCH_METHODS.PARTIAL },
'[tag]': { matchMethod: Constants.MATCH_METHODS.PARTIAL },
},
});
filter.init();
expect(filter.replaceText('Check the [tag] for the sale')).to.equal('Check the _____ for the sale');
expect(filter.replaceText('You are so {cool}!')).to.equal('You are so ______!');
});
});

describe('Whole', () => {
Expand Down Expand Up @@ -832,6 +862,19 @@ describe('Filter', () => {
expect(filter.replaceText('I love this!')).to.equal('I love');
});

it('Ending with semicolon', () => {
const filter = new Filter;
filter.cfg = new Config({
filterMethod: Constants.FILTER_METHODS.REMOVE,
words: {
'and more;': { matchMethod: Constants.MATCH_METHODS.EXACT, repeat: Constants.FALSE },
},
});
filter.init();
expect(filter.replaceText('and more;')).to.equal('');
expect(filter.replaceText('you can have it all, and more;')).to.equal('you can have it all,');
});

it('With separators', () => {
const filter = new Filter;
filter.cfg = new Config({
Expand Down Expand Up @@ -874,6 +917,35 @@ describe('Filter', () => {
expect(filter.replaceText('I love allthis! Do you?')).to.equal('I love Do you?');
expect(filter.replaceText('I love this! Do you?')).to.equal('I love Do you?');
});

it('Ending with colon', () => {
const filter = new Filter;
filter.cfg = new Config({
filterMethod: Constants.FILTER_METHODS.REMOVE,
words: {
'app. rate:': { matchMethod: Constants.MATCH_METHODS.PARTIAL, repeat: Constants.FALSE },
},
});
filter.init();
expect(filter.replaceText('app. rate:')).to.equal('');
expect(filter.replaceText('app. rate: 5.93%')).to.equal('5.93%');
});

it('Ending with special characters', () => {
const filter = new Filter;
filter.cfg = new Config({
filterMethod: Constants.FILTER_METHODS.REMOVE,
words: {
'check-mate/': { matchMethod: Constants.MATCH_METHODS.PARTIAL, repeat: Constants.FALSE },
'cheese()': { matchMethod: Constants.MATCH_METHODS.PARTIAL, repeat: Constants.FALSE },
'{cake': { matchMethod: Constants.MATCH_METHODS.PARTIAL, repeat: Constants.FALSE },
},
});
filter.init();
expect(filter.replaceText('Surprise! Check-mate/')).to.equal('Surprise!');
expect(filter.replaceText('Say cheese()')).to.equal('Say');
expect(filter.replaceText('Do you want any {cake')).to.equal('Do you want any');
});
});

it('Should filter a RegExp', () => {
Expand Down
5 changes: 5 additions & 0 deletions test/spec/lib/word.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ describe('Word', function() {
expect(word.regExp).to.eql(/(^|\s)([\w-]*word![\w-]*)(\s|$)/gi);
});

it('should build RegExp with ending colon', function() {
const word = new Word('app. rate:', { matchMethod: Constants.MATCH_METHODS.PARTIAL }, Object.assign(Config._defaults, { filterMethod: Constants.FILTER_METHODS.REMOVE }));
expect(word.regExp).to.eql(/(^|\s)([\w-]*app\. rate:[\w-]*)(\s|$)/gi);
});

// Work around for lack of word boundary support for unicode characters
describe('Unicode', function() {
it('should build RegExp', function() {
Expand Down

0 comments on commit b530da1

Please sign in to comment.