Skip to content
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

refactor: reorganize files #25

Merged
merged 7 commits into from
Dec 16, 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: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"del-cli": "^5.0.0",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^28.1.1",
"prettier": "^2.0.5",
Expand Down Expand Up @@ -103,6 +104,9 @@
"@react-native",
"prettier"
],
"plugins": [
"import"
],
"rules": {
"prettier/prettier": [
"error",
Expand All @@ -113,7 +117,15 @@
"trailingComma": "es5",
"useTabs": false
}
]
],
"sort-imports": [
"error",
{
"ignoreCase": true,
"ignoreDeclarationSort": true
}
],
"import/order": "error"
}
},
"eslintIgnore": [
Expand Down
24 changes: 24 additions & 0 deletions src/builders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { RegexElement } from './components/types';
import { encodeSequence } from './encoder/encoder';

/**
* Generate RegExp object for elements.
*
* @param elements
* @returns
*/
export function buildRegex(...elements: Array<RegexElement | string>): RegExp {
const pattern = encodeSequence(elements).pattern;
return new RegExp(pattern);
}

/**
* Generate regex pattern for elements.
* @param elements
* @returns
*/
export function buildPattern(
...elements: Array<RegexElement | string>
): string {
return encodeSequence(elements).pattern;
}
28 changes: 0 additions & 28 deletions src/character-classes/__tests__/any-of.test.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/character-classes/__tests__/base.test.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/character-classes/__tests__/encoder.test.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/character-classes/any-of.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/character-classes/base.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/character-classes/encoder.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildPattern } from '..';
import { buildPattern } from '../../builders';
import { capture } from '../capture';
import { oneOrMore } from '../quantifiers/base';
import { execRegex } from '../test-utils';
import { oneOrMore } from '../quantifiers';
import { execRegex } from '../../test-utils';

test('"capture" base cases', () => {
expect(buildPattern(capture('a'))).toBe('(a)');
Expand Down
78 changes: 78 additions & 0 deletions src/components/__tests__/character-class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { buildPattern } from '../../builders';
import { one, oneOrMore } from '../quantifiers';
import {
any,
anyOf,
digit,
encodeCharacterClass,
whitespace,
word,
} from '../character-class';

test('"whitespace" character class', () => {
expect(buildPattern(whitespace)).toEqual(`\\s`);

expect(buildPattern(one('ab'), whitespace)).toEqual(`ab\\s`);

expect(buildPattern(one('ab'), whitespace, one('c'))).toEqual(`ab\\sc`);
});

test('"digit" character class', () => {
expect(buildPattern(digit)).toEqual(`\\d`);

expect(buildPattern(one('ab'), digit)).toEqual(`ab\\d`);

expect(buildPattern(one('ab'), digit, one('c'))).toEqual(`ab\\dc`);
});

test('"word" character class', () => {
expect(buildPattern(word)).toEqual(`\\w`);

expect(buildPattern(one('ab'), word)).toEqual(`ab\\w`);

expect(buildPattern(one('ab'), word, one('c'))).toEqual(`ab\\wc`);
});

test('"any" character class', () => {
expect(buildPattern(any)).toEqual(`.`);

expect(buildPattern(one('ab'), any)).toEqual(`ab.`);

expect(buildPattern(one('ab'), any, one('c'))).toEqual(`ab.c`);
});

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

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

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

test('"anyOf" moves hyphen to the first position', () => {
expect(buildPattern(anyOf('a-bc'))).toBe('[-abc]');
});

test('`anyOf` throws on empty text', () => {
expect(() => anyOf('')).toThrowErrorMatchingInlineSnapshot(
`"\`anyOf\` should received at least one character"`
);
});

test('buildPattern throws on empty text', () => {
expect(() =>
encodeCharacterClass({
type: 'characterClass',
characters: [],
})
).toThrowErrorMatchingInlineSnapshot(
`"Character class should contain at least one character"`
);
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildPattern } from '../..';
import { oneOrMore, zeroOrMore } from '../../quantifiers/base';
import { repeat } from '../../quantifiers/repeat';
import { choiceOf } from '../choiceOf';
import { buildPattern } from '../../builders';
import { oneOrMore, zeroOrMore } from '../quantifiers';
import { repeat } from '../repeat';
import { choiceOf } from '../choice-of';

test('"choiceOf" using basic strings', () => {
expect(buildPattern(choiceOf('a'))).toEqual('a');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buildPattern, buildRegex } from '../..';
import { digit } from '../../character-classes/base';
import { one, oneOrMore, optionally, zeroOrMore } from '../base';
import { buildPattern, buildRegex } from '../../builders';
import { digit } from '../character-class';
import { one, oneOrMore, optionally, zeroOrMore } from '../quantifiers';

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 '../..';
import { digit } from '../../character-classes/base';
import { zeroOrMore, oneOrMore } from '../base';
import { buildPattern } from '../../builders';
import { digit } from '../character-class';
import { oneOrMore, zeroOrMore } from '../quantifiers';
import { repeat } from '../repeat';

test('"repeat" quantifier', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/capture.ts → src/components/capture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type EncoderNode, EncoderPrecedence } from '../encoder/types';
import type { Capture, RegexElement } from './types';
import { EncoderPriority, type EncoderNode } from './types-internal';

export function capture(...children: RegexElement[]): Capture {
export function capture(...children: Array<RegexElement | string>): Capture {
return {
type: 'capture',
children,
Expand All @@ -10,7 +10,7 @@ export function capture(...children: RegexElement[]): Capture {

export function encodeCapture(node: EncoderNode): EncoderNode {
return {
precedence: EncoderPrecedence.Atom,
pattern: `(${node.pattern})`,
priority: EncoderPriority.Atom,
};
}
Loading