Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: escape special characters #20

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ buck-out/
lib/

/.idea
coverage/
30 changes: 29 additions & 1 deletion src/__tests__/compiler.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { buildPattern, buildRegex } from '../compiler';
import { oneOrMore, optionally, one, zeroOrMore } from '../quantifiers/base';
import { one, oneOrMore, optionally, zeroOrMore } from '../quantifiers/base';
import { repeat } from '../quantifiers/repeat';

test('basic quantifies', () => {
Expand Down Expand Up @@ -28,3 +28,31 @@ test('regex constructor', () => {
expect(buildRegex('a').test('a')).toBeTruthy();
expect(buildRegex('a').test('b')).toBeFalsy();
});

test('"buildPattern" escapes special characters', () => {
expect(buildPattern('.')).toBe('\\.');
expect(buildPattern('*')).toBe('\\*');
expect(buildPattern('+')).toBe('\\+');
expect(buildPattern('?')).toBe('\\?');
expect(buildPattern('^')).toBe('\\^');
expect(buildPattern('$')).toBe('\\$');
expect(buildPattern('{')).toBe('\\{');
expect(buildPattern('}')).toBe('\\}');
expect(buildPattern('|')).toBe('\\|');
expect(buildPattern('[')).toBe('\\[');
expect(buildPattern(']')).toBe('\\]');
expect(buildPattern('\\')).toBe('\\\\');

expect(buildPattern('*.*')).toBe('\\*\\.\\*');

expect(buildPattern(oneOrMore('.*'), zeroOrMore('[]{}'))).toBe(
'(?:\\.\\*)+(?:\\[\\]\\{\\})*'
);
});

test('buildRegex throws error on unknown element', () => {
expect(() =>
// @ts-expect-error intentionally passing incorrect object
buildRegex({ type: 'unknown' })
).toThrowErrorMatchingInlineSnapshot(`"Unknown elements type unknown"`);
});
7 changes: 4 additions & 3 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RegexElement } from './types';
import { characterClasses, isCharacterClass } from './character-classes';
import { baseQuantifiers, isBaseQuantifier } from './quantifiers/base';
import { compileRepeat } from './quantifiers/repeat';
import { escapeText } from './utils';

/**
* Generate RegExp object for elements.
Expand Down Expand Up @@ -31,20 +32,20 @@ function compileList(elements: RegexElement[]): string {

function compileSingle(element: RegexElement): string {
if (typeof element === 'string') {
return element;
return escapeText(element);
}

if (isCharacterClass(element)) {
return characterClasses[element.type];
}

const compiledChildren = compileList(element.children);

if (element.type === 'repeat') {
const compiledChildren = compileList(element.children);
return compileRepeat(element.config, compiledChildren);
}

if (isBaseQuantifier(element)) {
const compiledChildren = compileList(element.children);
const compiler = baseQuantifiers[element.type];
return compiler(compiledChildren);
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
export function wrapGroup(regex: string): string {
return regex.length === 1 ? regex : `(?:${regex})`;
}

// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping
export function escapeText(text: string) {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}