Skip to content

feat: choiceOf component #10

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 5 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ buck-out/
lib/

/.idea
coverage/
coverage/
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
},
"eslintIgnore": [
"node_modules/",
"lib/"
"lib/",
"coverage/"
],
"prettier": {
"quoteProps": "consistent",
Expand Down
5 changes: 5 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { RegexElement } from './types';
import { characterClasses, isCharacterClass } from './character-classes';
import { compileChoiceOf } from './components/choiceOf';
import { baseQuantifiers, isBaseQuantifier } from './quantifiers/base';
import { compileRepeat } from './quantifiers/repeat';
import { escapeText } from './utils';
Expand Down Expand Up @@ -39,6 +40,10 @@ function compileSingle(element: RegexElement): string {
return characterClasses[element.type];
}

if (element.type === 'choiceOf') {
return compileChoiceOf(element, compileSingle);
}

if (element.type === 'repeat') {
const compiledChildren = compileList(element.children);
return compileRepeat(element.config, compiledChildren);
Expand Down
23 changes: 23 additions & 0 deletions src/components/__tests__/choiceOf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { buildPattern } from '../../compiler';
import { oneOrMore, zeroOrMore } from '../../quantifiers/base';
import { repeat } from '../../quantifiers/repeat';
import { choiceOf } from '../choiceOf';

test('"choiceOf" using basic strings', () => {
expect(buildPattern(choiceOf('a'))).toEqual('a');
expect(buildPattern(choiceOf('a', 'b'))).toEqual('(?:a|b)');
expect(buildPattern(choiceOf('a', 'b', 'c'))).toEqual('(?:a|b|c)');

expect(buildPattern(choiceOf('aaa', 'bbb'))).toEqual('(?:aaa|bbb)');
});

test('"choiceOf" using nested regex', () => {
expect(buildPattern(choiceOf(oneOrMore('a'), zeroOrMore('b')))).toBe(
'(?:a+|b*)'
);
expect(
buildPattern(
choiceOf(repeat({ min: 1, max: 3 }, 'a'), repeat({ count: 5 }, 'bx'))
)
).toBe('(?:a{1,3}|(?:bx){5})');
});
18 changes: 18 additions & 0 deletions src/components/choiceOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { ChoiceOf, RegexElement } from '../types';
import type { CompileSingle } from '../types-internal';
import { wrapGroup } from '../utils';

export function choiceOf(...children: RegexElement[]): ChoiceOf {
return {
type: 'choiceOf',
children,
};
}

export function compileChoiceOf(
element: ChoiceOf,
compileSingle: CompileSingle
): string {
const compiledChildren = element.children.map(compileSingle);
return wrapGroup(compiledChildren.join('|'));
}
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type * from './types';

export { any, digit, whitespace, word } from './character-classes';
export { buildRegex, buildPattern } from './compiler';
export { one, oneOrMore, optionally, zeroOrMore } from './quantifiers/base';
export { repeat } from './quantifiers/repeat';
export { choiceOf } from './components/choiceOf';
5 changes: 0 additions & 5 deletions src/index.tsx

This file was deleted.

3 changes: 3 additions & 0 deletions src/types-internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { RegexElement } from './types';

export type CompileSingle = (element: RegexElement) => string;
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type RegexElement = string | CharacterClass | Quantifier;
export type RegexElement = string | ChoiceOf | CharacterClass | Quantifier;

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

Expand All @@ -10,6 +10,12 @@ export type Digit = { type: 'digit' };
export type Word = { type: 'word' };
export type Any = { type: 'any' };

// Components
export type ChoiceOf = {
type: 'choiceOf';
children: RegexElement[];
};

// Quantifiers
export type One = {
type: 'one';
Expand Down