Skip to content

Commit

Permalink
resolves sapegin#15 allow punctuation marks
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jan 4, 2021
1 parent 3d1d56b commit b171651
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ function filterDict(rules, excludedWords) {
*/
function getRegExp(word) {
const wordPattern = escapeRegExp(word).replace(/'/g, "['’‘]");
return new RegExp(`(?:^|[^-\\w])(${wordPattern}(?= |\\. |\\.$|$))`, 'ig');
const punctuations = ['\\.', ',', ':', ';', '\\?', '!'];
const punctuationsRegExp = punctuations.map(
punctuation => `${punctuation} |${punctuation}$`
);
return new RegExp(
`(?:^|[^-\\w])(${wordPattern}(?= |$|${punctuationsRegExp.join('|')}))`,
'ig'
);
}

/**
Expand Down
42 changes: 40 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ describe('getRegExp', () => {
expect(result).toBeFalsy();
});

it('should not match a pattern at the beginning of a string', () => {
it('should match a pattern at the beginning of a string', () => {
const result = getRegExp(word).exec('java bar');
expect(result).toBeTruthy();
expect(result[1]).toBe('java');
});

it('should not match a pattern at the end of a string', () => {
it('should match a pattern at the end of a string', () => {
const result = getRegExp(word).exec('foo java');
expect(result).toBeTruthy();
expect(result[1]).toBe('java');
Expand Down Expand Up @@ -53,6 +53,44 @@ describe('getRegExp', () => {
expect(result[1]).toBe('java');
});

it('should match a pattern followed by a punctuation mark', () => {
const regexp = getRegExp(word);
const result1 = 'java! Awesome!'.match(regexp);
expect(result1).toBeTruthy();
expect(result1[0]).toBe('java');
const result2 = 'java? never heard of'.match(regexp);
expect(result2).toBeTruthy();
expect(result2[0]).toBe('java');
const result3 = 'java, a programming language'.match(regexp);
expect(result3).toBeTruthy();
expect(result3[0]).toBe('java');
const result4 = 'java; Python'.match(regexp);
expect(result4).toBeTruthy();
expect(result4[0]).toBe('java');
const result5 = 'java: free'.match(regexp);
expect(result5).toBeTruthy();
expect(result5[0]).toBe('java');
});

it('should match a pattern ending with a punctuation mark', () => {
const regexp = getRegExp(word);
const result1 = 'java!'.match(regexp);
expect(result1).toBeTruthy();
expect(result1[0]).toBe('java');
const result2 = 'java?'.match(regexp);
expect(result2).toBeTruthy();
expect(result2[0]).toBe('java');
const result3 = 'java,'.match(regexp);
expect(result3).toBeTruthy();
expect(result3[0]).toBe('java');
const result4 = 'java;'.match(regexp);
expect(result4).toBeTruthy();
expect(result4[0]).toBe('java');
const result5 = 'java:'.match(regexp);
expect(result5).toBeTruthy();
expect(result5[0]).toBe('java');
});

it('should not match a pattern in as a part of a file name', () => {
const result = getRegExp(word).exec('java.md');
expect(result).toBeFalsy();
Expand Down

0 comments on commit b171651

Please sign in to comment.