Skip to content
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
21 changes: 21 additions & 0 deletions src/__tests__/capture.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { buildPattern } from '..';
import { capture } from '../capture';
import { oneOrMore } from '../quantifiers/base';
import { execRegex } from '../test-utils';

test('"capture" base cases', () => {
expect(buildPattern(capture('a'))).toBe('(a)');
expect(buildPattern(capture('abc'))).toBe('(abc)');
expect(buildPattern(capture(oneOrMore('abc')))).toBe('((?:abc)+)');
expect(buildPattern(oneOrMore(capture('abc')))).toBe('(abc)+');
});

test('"capture" captures group', () => {
expect(execRegex('ab', [capture('b')])).toEqual(['b', 'b']);
expect(execRegex('ab', ['a', capture('b')])).toEqual(['ab', 'b']);
expect(execRegex('abc', ['a', capture('b'), capture('c')])).toEqual([
'abc',
'b',
'c',
]);
});
16 changes: 16 additions & 0 deletions src/capture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Capture, RegexElement } from './types';
import { EncoderPriority, type EncoderNode } from './types-internal';

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

export function encodeCapture(node: EncoderNode): EncoderNode {
return {
pattern: `(${node.pattern})`,
priority: EncoderPriority.Atom,
};
}
5 changes: 5 additions & 0 deletions src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from './quantifiers/base';
import { encodeRepeat } from './quantifiers/repeat';
import { concatNodes, escapeText } from './utils';
import { encodeCapture } from './capture';

export function encodeSequence(elements: RegexElement[]): EncoderNode {
return concatNodes(elements.map((c) => encodeElement(c)));
Expand Down Expand Up @@ -48,6 +49,10 @@ export function encodeElement(element: RegexElement): EncoderNode {
return encodeZeroOrMore(encodeSequence(element.children));
}

if (element.type === 'capture') {
return encodeCapture(encodeSequence(element.children));
}

// @ts-expect-error User passed incorrect type
throw new Error(`Unknown elements type ${element.type}`);
}
Expand Down
12 changes: 12 additions & 0 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { buildRegex } from '.';
import type { RegexElement } from './types';

export function execRegex(text: string, elements: RegexElement[]) {
const regex = buildRegex(...elements);
return [...regex.exec(text)!];
}

export function execRegexFull(text: string, elements: RegexElement[]) {
const regex = buildRegex(...elements);
return regex.exec(text)!;
}
13 changes: 12 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export type RegexElement = string | ChoiceOf | CharacterClass | Quantifier;
export type RegexElement =
| string
| CharacterClass
| ChoiceOf
| Quantifier
| Capture;

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

Expand Down Expand Up @@ -41,3 +46,9 @@ export type Repeat = {
};

export type RepeatConfig = { count: number } | { min: number; max?: number };

// Captures
export type Capture = {
type: 'capture';
children: RegexElement[];
};