Skip to content

feat: anyOf component #21

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

Merged
merged 6 commits into from
Dec 12, 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
26 changes: 0 additions & 26 deletions src/character-classes.ts

This file was deleted.

23 changes: 23 additions & 0 deletions src/character-classes/__tests__/any-of.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { buildPattern as p } from '../../compiler';
import { oneOrMore } from '../../quantifiers/base';
import { anyOf } from '../any-of';

test('"anyOf" base cases', () => {
expect(p(anyOf(''))).toBe('');
expect(p(anyOf('a'))).toBe('a');
expect(p(anyOf('abc'))).toBe('[abc]');
});

test('"anyOf" in context', () => {
expect(p('x', anyOf('a'), 'x')).toBe('xax');
expect(p('x', anyOf('abc'), 'x')).toBe('x[abc]x');
expect(p('x', oneOrMore(anyOf('abc')), 'x')).toBe('x(?:[abc])+x');
});

test('"anyOf" escapes special characters', () => {
expect(p(anyOf('abc-+.'))).toBe('[-abc\\+\\.]');
});

test('"anyOf" moves hyphen to the first position', () => {
expect(p(anyOf('a-bc'))).toBe('[-abc]');
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { any, digit, whitespace, word } from '../character-classes';
import { buildPattern } from '../compiler';
import { one } from '../quantifiers/base';
import { any, digit, whitespace, word } from '../base';
import { buildPattern } from '../../compiler';
import { one } from '../../quantifiers/base';

test('"whitespace" character class', () => {
expect(buildPattern(whitespace)).toEqual(`\\s`);
Expand Down
9 changes: 9 additions & 0 deletions src/character-classes/any-of.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CharacterClass } from '../types';
import { escapeText } from '../utils';

export function anyOf(characters: string): CharacterClass {
return {
type: 'characterClass',
characters: characters.split('').map(escapeText),
};
}
21 changes: 21 additions & 0 deletions src/character-classes/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { CharacterClass } from '../types';

export const whitespace: CharacterClass = {
type: 'characterClass',
characters: ['\\s'],
};

export const digit: CharacterClass = {
type: 'characterClass',
characters: ['\\d'],
};

export const word: CharacterClass = {
type: 'characterClass',
characters: ['\\w'],
};

export const any: CharacterClass = {
type: 'characterClass',
characters: ['.'],
};
24 changes: 24 additions & 0 deletions src/character-classes/compiler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { CharacterClass } from '../types';

export function compileCharacterClass({ characters }: CharacterClass): string {
if (characters.length === 0) {
return '';
}

if (characters.length === 1) {
return characters[0]!;
}

return `[${escapeHyphen(characters).join('')}]`;
}

// If passed characters includes hyphen (`-`) it need to be moved to
// first (or last) place in order to treat it as hyphen character and not a range.
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes#types
function escapeHyphen(characters: string[]) {
if (characters.includes('-')) {
return ['-', ...characters.filter((c) => c !== '-')];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this function also call .join() and return a string?

Copy link
Member Author

@mdjastrzebski mdjastrzebski Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, it is parsing it's input for RegexElement. Join will be done when we will compile it. We want to keep these as array of strings, as character class may hold e.g. \\d. \\u1234 which are two+ chars, but one conceptual char.

}

return characters;
}
6 changes: 3 additions & 3 deletions src/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RegexElement } from './types';
import { characterClasses, isCharacterClass } from './character-classes';
import { compileChoiceOf } from './components/choiceOf';
import { compileCharacterClass } from './character-classes/compiler';
import { baseQuantifiers, isBaseQuantifier } from './quantifiers/base';
import { compileRepeat } from './quantifiers/repeat';
import { escapeText } from './utils';
Expand Down Expand Up @@ -36,8 +36,8 @@ function compileSingle(element: RegexElement): string {
return escapeText(element);
}

if (isCharacterClass(element)) {
return characterClasses[element.type];
if (element.type === 'characterClass') {
return compileCharacterClass(element);
}

if (element.type === 'choiceOf') {
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export type * from './types';

export { any, digit, whitespace, word } from './character-classes';
export { buildRegex, buildPattern } from './compiler';

export { any, digit, whitespace, word } from './character-classes/base';
export { anyOf } from './character-classes/any-of';
export { one, oneOrMore, optionally, zeroOrMore } from './quantifiers/base';
export { repeat } from './quantifiers/repeat';
export { choiceOf } from './components/choiceOf';
5 changes: 5 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type * from './types';

export { whitespace } from './character-classes/base';
export { buildRegex, buildPattern } from './compiler';
export { oneOrMore, optionally } from './quantifiers/base';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { one, oneOrMore, optionally, zeroOrMore } from '../quantifiers/base';
import { buildPattern, buildRegex } from '../compiler';
import { one, oneOrMore, optionally, zeroOrMore } from '../base';
import { buildPattern, buildRegex } from '../../compiler';

test('"oneOrMore" quantifier', () => {
expect(buildPattern(oneOrMore('a'))).toEqual('a+');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buildPattern } from '../compiler';
import { zeroOrMore, oneOrMore } from '../quantifiers/base';
import { repeat } from '../quantifiers/repeat';
import { buildPattern } from '../../compiler';
import { zeroOrMore, oneOrMore } from '../base';
import { repeat } from '../repeat';

test('"repeat" quantifier', () => {
expect(buildPattern('a', repeat({ min: 1, max: 5 }, 'b'))).toEqual('ab{1,5}');
Expand Down
12 changes: 6 additions & 6 deletions src/quantifiers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import type {
} from '../types';
import { wrapGroup } from '../utils';

export function oneOrMore(...children: RegexElement[]): OneOrMore {
export function one(...children: RegexElement[]): One {
return {
type: 'oneOrMore',
type: 'one',
children,
};
}

export function optionally(...children: RegexElement[]): Optionally {
export function oneOrMore(...children: RegexElement[]): OneOrMore {
return {
type: 'optionally',
type: 'oneOrMore',
children,
};
}

export function one(...children: RegexElement[]): One {
export function optionally(...children: RegexElement[]): Optionally {
return {
type: 'one',
type: 'optionally',
children,
};
}
Expand Down
11 changes: 4 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
export type RegexElement = string | ChoiceOf | CharacterClass | Quantifier;

export type CharacterClass = Whitespace | Digit | Word | Any;

export type Quantifier = One | OneOrMore | Optionally | ZeroOrMore | Repeat;

// Character classes
export type Whitespace = { type: 'whitespace' };
export type Digit = { type: 'digit' };
export type Word = { type: 'word' };
export type Any = { type: 'any' };
export type CharacterClass = {
type: 'characterClass';
characters: string[];
};

// Components
export type ChoiceOf = {
Expand Down