Skip to content

feat: base anchors #32

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 1 commit into from
Dec 25, 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
20 changes: 20 additions & 0 deletions src/components/__tests__/anchors.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { endOfString, startOfString } from '../anchors';
import { oneOrMore } from '../quantifiers';

test('`startOfString` basic cases', () => {
expect([startOfString]).toHavePattern('^');
expect([startOfString, 'a', 'b']).toHavePattern('^ab');
});

test('`startOfString` regex tests', () => {
expect([startOfString, oneOrMore('a')]).toMatchGroups('a aa aaa', ['a']);
});

test('`endOfString` basic cases', () => {
expect([endOfString]).toHavePattern('$');
expect(['a', 'b', endOfString]).toHavePattern('ab$');
});

test('`endOfString` regex tests', () => {
expect([oneOrMore('a'), endOfString]).toMatchGroups('a aa aaa', ['aaa']);
});
19 changes: 19 additions & 0 deletions src/components/anchors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type EncoderNode, EncoderPrecedence } from '../encoder/types';
import type { Anchor } from './types';

export const startOfString: Anchor = {
type: 'anchor',
symbol: '^',
};

export const endOfString: Anchor = {
type: 'anchor',
symbol: '$',
};

export function encodeAnchor(anchor: Anchor): EncoderNode {
return {
precedence: EncoderPrecedence.Sequence,
pattern: anchor.symbol,
};
}
24 changes: 15 additions & 9 deletions src/components/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
export type RegexElement =
| string
| Capture
| CharacterClass
| Anchor
| ChoiceOf
| Quantifier;
| Quantifier
| Capture;

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

export type CharacterClass = {
type: 'characterClass';
characters: string[];
ranges: CharacterRange[];
isInverted: boolean;
};

/**
* Character range from start to end (inclusive).
*/
Expand All @@ -23,6 +17,18 @@ export type CharacterRange = {
};

// Components
export type CharacterClass = {
type: 'characterClass';
characters: string[];
ranges: CharacterRange[];
isInverted: boolean;
};

export type Anchor = {
type: 'anchor';
symbol: string;
};

export type ChoiceOf = {
type: 'choiceOf';
children: RegexElement[][];
Expand Down
6 changes: 6 additions & 0 deletions src/encoder/encoder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { RegexElement } from '../components/types';
import { encodeAnchor } from '../components/anchors';
import { encodeCapture } from '../components/capture';
import { encodeCharacterClass } from '../components/character-class';
import { encodeChoiceOf } from '../components/choice-of';
Expand All @@ -10,6 +11,7 @@ import {
} from '../components/quantifiers';
import { encodeRepeat } from '../components/repeat';
import { escapeText } from '../utils/text';

import { type EncoderNode, EncoderPrecedence } from './types';
import { concatNodes } from './utils';

Expand All @@ -26,6 +28,10 @@ export function encodeElement(element: RegexElement): EncoderNode {
return encodeCharacterClass(element);
}

if (element.type === 'anchor') {
return encodeAnchor(element);
}

if (element.type === 'choiceOf') {
return encodeChoiceOf(element, encodeSequence);
}
Expand Down