Skip to content

Commit 1eb5f66

Browse files
committed
refactor: code review changes
1 parent 4d30597 commit 1eb5f66

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ buck-out/
3232
lib/
3333

3434
/.idea
35+
coverage/

src/__tests__/compiler.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test('regex constructor', () => {
2929
expect(buildRegex('a').test('b')).toBeFalsy();
3030
});
3131

32-
test('buildPattern escapes special characters', () => {
32+
test('"buildPattern" escapes special characters', () => {
3333
expect(buildPattern('.')).toBe('\\.');
3434
expect(buildPattern('*')).toBe('\\*');
3535
expect(buildPattern('+')).toBe('\\+');
@@ -44,4 +44,8 @@ test('buildPattern escapes special characters', () => {
4444
expect(buildPattern('\\')).toBe('\\\\');
4545

4646
expect(buildPattern('*.*')).toBe('\\*\\.\\*');
47+
48+
expect(buildPattern(oneOrMore('.*'), zeroOrMore('[]{}'))).toBe(
49+
'(?:\\.\\*)+(?:\\[\\]\\{\\})*'
50+
);
4751
});

src/compiler.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RegexElement } from './types';
22
import { characterClasses, isCharacterClass } from './character-classes';
33
import { baseQuantifiers, isBaseQuantifier } from './quantifiers/base';
44
import { compileRepeat } from './quantifiers/repeat';
5+
import { escapeText } from './utils';
56

67
/**
78
* Generate RegExp object for elements.
@@ -52,8 +53,3 @@ function compileSingle(element: RegexElement): string {
5253
// @ts-expect-error User passed incorrect type
5354
throw new Error(`Unknown elements type ${element.type}`);
5455
}
55-
56-
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping
57-
function escapeText(text: string) {
58-
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
59-
}

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
export function wrapGroup(regex: string): string {
88
return regex.length === 1 ? regex : `(?:${regex})`;
99
}
10+
11+
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping
12+
export function escapeText(text: string) {
13+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
14+
}

0 commit comments

Comments
 (0)