Skip to content

Commit

Permalink
fix(pattern.ts): enable use of regex flags for pattern in ruleset fil…
Browse files Browse the repository at this point in the history
…es (#251)

* fix(pattern.ts): enable use of regex flags for pattern in ruleset files

#242

* Update src/functions/pattern.ts

Co-Authored-By: Jakub Rożek <P0lip@users.noreply.github.com>

* Update src/functions/pattern.ts

Co-Authored-By: Jakub Rożek <P0lip@users.noreply.github.com>

* test(pattern.test.ts): testing exception on invalid flag in string regex

#242

* test(pattern.ts): use a different assertion
  • Loading branch information
arno-di-loreto authored and P0lip committed Jun 14, 2019
1 parent 73b9022 commit e951347
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
67 changes: 67 additions & 0 deletions src/functions/__tests__/pattern.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { pattern } from '../pattern';

function runPattern(targetVal: any, options?: any) {
return pattern(
targetVal,
options,
{
given: ['$'],
},
{
given: null,
original: null,
},
);
}

describe('pattern', () => {
describe('given a true regex', () => {
test('should return empty array when given value matches the given match regex', () => {
expect(runPattern('abc', { match: /[abc]+/ })).toEqual([]);
});

test('should return a message when given value does not match the given notMatch regex', () => {
expect(runPattern('def', { match: /[abc]+/ })).toEqual([{ message: "must match the pattern '/[abc]+/'" }]);
});

test('should return empty array when given value does not match the given notMatch regex', () => {
expect(runPattern('dEf', { notMatch: /[abc]+/i })).toEqual([]);
});

test('should return a message when given value does match the given notMatch regex', () => {
expect(runPattern('aBc', { notMatch: /[abc]+/i })).toEqual([
{ message: "must not match the pattern '/[abc]+/i'" },
]);
});
});

describe('given a string regex', () => {
test('should return empty array when given value matches the given match string regex without slashes', () => {
expect(runPattern('abc', { match: '[abc]+' })).toEqual([]);
});

test('should return empty array when given value matches the given match string regex with slashes', () => {
expect(runPattern('abc', { match: '/[abc]+/' })).toEqual([]);
});

test('should return empty array when given value matches the given match string regex with slashes and modifier', () => {
expect(runPattern('aBc', { match: '/[abc]+/im' })).toEqual([]);
});

test('should throw an exception when given string regex contains invalid flags', () => {
expect(() => runPattern('aBc', { match: '/[abc]+/invalid' })).toThrow(
"Invalid flags supplied to RegExp constructor 'invalid'",
);
});

test('should return empty array when given value does not match the given notMatch string regex with slashes and modifier', () => {
expect(runPattern('def', { notMatch: '/[abc]+/i' })).toEqual([]);
});
});

describe('given match and notMatch regexes', () => {
test('should return empty array when given value match the given match and does not match the given notMatch regexes', () => {
expect(runPattern('def', { match: /[def]+/, notMatch: /[abc]+/ })).toEqual([]);
});
});
});
26 changes: 22 additions & 4 deletions src/functions/pattern.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { IFunction, IFunctionResult, IRulePatternOptions } from '../types';

function test(value: string, regex: RegExp | string) {
let re;
if (typeof regex === 'string') {
// regex in a string like {"match": "/[a-b]+/im"} or {"match": "[a-b]+"} in a json ruleset
// the available flags are "gimsuy" as described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
const splitRegex = /^\/(.+)\/([a-z]*)$/.exec(regex);
if (splitRegex) {
// with slashes like /[a-b]+/ and possibly with flags like /[a-b]+/im
re = new RegExp(splitRegex[1], splitRegex[2]);
} else {
// without slashes like [a-b]+
re = new RegExp(regex);
}
} else {
// true regex
re = new RegExp(regex);
}
return re.test(value);
}

export const pattern: IFunction<IRulePatternOptions> = (targetVal, opts) => {
const results: IFunctionResult[] = [];

Expand All @@ -8,17 +28,15 @@ export const pattern: IFunction<IRulePatternOptions> = (targetVal, opts) => {
const { match, notMatch } = opts;

if (match) {
const re = new RegExp(match);
if (re.test(targetVal) !== true) {
if (test(targetVal, match) !== true) {
results.push({
message: `must match the pattern '${match}'`,
});
}
}

if (notMatch) {
const re = new RegExp(notMatch);
if (re.test(targetVal) === true) {
if (test(targetVal, notMatch) === true) {
results.push({
message: `must not match the pattern '${notMatch}'`,
});
Expand Down

0 comments on commit e951347

Please sign in to comment.