Skip to content

Commit

Permalink
Merge pull request #5 from xflagstudio/add_regexp_pattern
Browse files Browse the repository at this point in the history
Add regexp pattern
  • Loading branch information
hashijun authored Aug 30, 2018
2 parents cfc26bf + 9c98950 commit 026ba62
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ So you must set up JSON file to configure your protection settings. Here is an e
],
"targetWords": {
"common": ["hoge", "huga"],
"aaa.zendesk.com": ["piyo"],
"aaa.zendesk.com": ["piyo", "huga-\\d+"],
"bbb.zendesk.com": ["moge"]
}
}
Expand All @@ -38,7 +38,7 @@ So you must set up JSON file to configure your protection settings. Here is an e
`zendesk-incident-protector.user.js` requires configuration with two attributes.

* `hosts` : target hosts to protect.
* `targetWords` : target words to trigger alert. Words will be selected from `common` attribute and matched Zendesk host.
* `targetWords` : target words to trigger alert. Words will be selected from `common` attribute and matched Zendesk host. You can also set regexp pattern. (In example, `huga-123` will trigger alert.)

Be sure to minify JSON before uploading.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zendesk-incident-protector",
"version": "1.0.0",
"version": "1.0.1",
"description": "Prevent replying to customer with specific NG keywords",
"main": "zendesk-incident-protector.user.js",
"scripts": {
Expand Down
24 changes: 16 additions & 8 deletions test/zendesk-incident-protector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('ValidatorManager', () => {

describe('addValidator', () => {
it('adds button id into idsWithValidator', () => {
const targetWords = ['test', 'memo', '(aaa|xxx)'];
const targetWords = ['test', 'memo', '(aaa|xxx)', 'HOGE-\\d+', '\\(bbb\\|yyy\\)'];
const buttonViewId = expectedButtonViewId;
const locale = 'ja';

Expand All @@ -106,7 +106,7 @@ describe('ValidatorManager', () => {

context('after adding validator', () => {
beforeEach(() => {
const targetWords = ['test', 'memo', '(aaa|xxx)'];
const targetWords = ['test', 'memo', '(aaa|xxx)', 'HOGE-\\d+', '\\(bbb\\|yyy\\)'];
const buttonViewId = expectedButtonViewId;

validatorManager.addValidator(targetWords, buttonViewId);
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('NGWordManager', () => {
],
'targetWords': {
'common': ['test', 'memo'],
'aaa.zendesk.com': ['(aaa|xxx)'],
'aaa.zendesk.com': ['(aaa|xxx)', 'HOGE-\\d+', '\\(bbb\\|yyy\\)'],
'bbb.zendesk.com': ['bbb'],
'ccc.zendesk.com': ['ccc']
}
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('NGWordManager', () => {
context('target words at host is defined', () => {
it('returns target words defined on common and host', () => {
const host = 'aaa.zendesk.com';
const expected = ['test', 'memo', '(aaa|xxx)'];
const expected = ['test', 'memo', '(aaa|xxx)', 'HOGE-\\d+', '\\(bbb\\|yyy\\)'];

ngWordManager.toTargetWords(host).should.eql(expected);
});
Expand Down Expand Up @@ -297,7 +297,7 @@ describe('NGWordManager', () => {

describe('NGWordValidator', () => {
const targetDOM = ValidatorManager.UI_CONSTANTS.selector.buttonArea;
const targetWords = ['test', 'memo', '(aaa|xxx)']
const targetWords = ['test', 'memo', '(aaa|xxx)', 'HOGE-\\d+', '\\(bbb\\|yyy\\)'];
const locale = 'ja';

let ngWordValidator;
Expand Down Expand Up @@ -334,14 +334,22 @@ describe('NGWordValidator', () => {
// text with word in common target words
const text1 = 'test hogehoge';
// text with word in target words of aaa.zendesk.com
const text2 = '(aaa|xxx) hogehoge';
const text2 = 'aaa hogehoge';
const text3 = 'xxx hogehoge';
const text4 = 'HOGE-1234 hogehoge';
const text5 = '(bbb|yyy) hogehoge';
// text without target words
const text3 = 'aaa hogehoge';
const text6 = 'HOGE- hogehoge';
const text7 = 'bbb hogehoge';

it('judges target words', () => {
ngWordValidator.isIncludeTargetWord(text1).should.equal(true);
ngWordValidator.isIncludeTargetWord(text2).should.equal(true);
ngWordValidator.isIncludeTargetWord(text3).should.equal(false);
ngWordValidator.isIncludeTargetWord(text3).should.equal(true);
ngWordValidator.isIncludeTargetWord(text4).should.equal(true);
ngWordValidator.isIncludeTargetWord(text5).should.equal(true);
ngWordValidator.isIncludeTargetWord(text6).should.equal(false);
ngWordValidator.isIncludeTargetWord(text7).should.equal(false);
});
});

Expand Down
9 changes: 7 additions & 2 deletions zendesk-incident-protector.user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name Zendesk Incident Protector
// @version 1.0.0
// @version 1.0.1
// @description Prevent replying to customer with specific NG keywords
// @author XFLAG Studio CRE Team
// @include https://*.zendesk.com/*
Expand Down Expand Up @@ -238,7 +238,12 @@
}

isIncludeTargetWord(text) {
return this.targetWords.some(word => text.includes(word));
let isMatch = (pattern, text) => {
const regexp = new RegExp(pattern);
return regexp.test(text);
};

return this.targetWords.some(word => isMatch(word, text));
}

createConfirmText(text) {
Expand Down

0 comments on commit 026ba62

Please sign in to comment.