Skip to content

refactor: remove one component #34

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 2 commits into from
Dec 26, 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
14 changes: 1 addition & 13 deletions src/components/__tests__/quantifiers.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { buildRegex } from '../../builders';
import { digit } from '../character-class';
import { one, oneOrMore, optionally, zeroOrMore } from '../quantifiers';
import { oneOrMore, optionally, zeroOrMore } from '../quantifiers';

test('`oneOrMore` quantifier', () => {
expect(oneOrMore('a')).toHavePattern('a+');
expect(oneOrMore('ab')).toHavePattern('(?:ab)+');
});

test('`one` quantifier', () => {
expect(one('a')).toHavePattern('a');
expect(one('ab')).toHavePattern('ab');
});

test('`optionally` quantifier', () => {
expect(optionally('a')).toHavePattern('a?');
expect(optionally('ab')).toHavePattern('(?:ab)?');
Expand All @@ -28,12 +23,6 @@ test('`oneOrMore` does not generate capture when grouping', () => {
expect(groups).toEqual(['aa']);
});

test('`one` does not generate capture when grouping', () => {
const regex = buildRegex(one('aa'));
const groups = [...'aa'.match(regex)!];
expect(groups).toEqual(['aa']);
});

test('`optionally` does not generate capture when grouping', () => {
const regex = buildRegex(optionally('aa'));
const groups = [...'aa'.match(regex)!];
Expand All @@ -47,7 +36,6 @@ test('`zeroOrMore` does not generate capture when grouping', () => {
});

test('base quantifiers optimize grouping for atoms', () => {
expect(one(digit)).toHavePattern('\\d');
expect(oneOrMore(digit)).toHavePattern('\\d+');
expect(optionally(digit)).toHavePattern('\\d?');
expect(zeroOrMore(digit)).toHavePattern('\\d*');
Expand Down
19 changes: 1 addition & 18 deletions src/components/quantifiers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { encodeAtom, encodeSequence } from '../encoder/encoder';
import { encodeAtom } from '../encoder/encoder';
import type { EncodeOutput } from '../encoder/types';
import { asNodeArray } from '../utils/nodes';
import type { RegexElement, RegexNode } from '../types';

export interface One extends RegexElement {
type: 'one';
children: RegexNode[];
}

export interface OneOrMore extends RegexElement {
type: 'oneOrMore';
children: RegexNode[];
Expand All @@ -23,14 +18,6 @@ export interface ZeroOrMore extends RegexElement {
children: RegexNode[];
}

export function one(nodes: RegexNode | RegexNode[]): One {
return {
type: 'one',
children: asNodeArray(nodes),
encode: encodeOne,
};
}

export function oneOrMore(nodes: RegexNode | RegexNode[]): OneOrMore {
return {
type: 'oneOrMore',
Expand All @@ -55,10 +42,6 @@ export function zeroOrMore(nodes: RegexNode | RegexNode[]): ZeroOrMore {
};
}

function encodeOne(this: One) {
return encodeSequence(this.children);
}

function encodeOneOrMore(this: OneOrMore): EncodeOutput {
return {
precedence: 'sequence',
Expand Down
3 changes: 1 addition & 2 deletions src/encoder/__tests__/encoder.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { buildPattern, buildRegex } from '../../builders';
import {
one,
oneOrMore,
optionally,
zeroOrMore,
Expand All @@ -26,7 +25,7 @@ test('basic quantifies', () => {

expect([optionally('a'), 'b']).toHavePattern('a?b');

expect([optionally('a'), 'b', one('d')]).toHavePattern('a?bd');
expect([optionally('a'), 'b', oneOrMore('d')]).toHavePattern('a?bd+');
});

test('regex constructor', () => {
Expand Down
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ export type * from './types';

export { buildPattern, buildRegex } from './builders';

export { startOfString, endOfString } from './components/anchors';
export { capture } from './components/capture';
export {
any,
anyOf,
digit,
whitespace,
word,
anyOf,
characterRange,
characterClass,
inverted,
} from './components/character-class';
export { choiceOf } from './components/choice-of';
export {
one,
oneOrMore,
optionally,
zeroOrMore,
} from './components/quantifiers';
export { oneOrMore, optionally, zeroOrMore } from './components/quantifiers';
export { repeat } from './components/repeat';