Skip to content

Commit 871002e

Browse files
refactor: rename types (#43)
1 parent c8a3f36 commit 871002e

File tree

10 files changed

+211
-32
lines changed

10 files changed

+211
-32
lines changed

docs/API.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# API
2+
3+
## Builder
4+
5+
### `buildRegex()` function
6+
7+
```ts
8+
function buildRegex(sequence: RegexSequence): RegExp;
9+
10+
function buildRegex(
11+
flags: {
12+
global?: boolean;
13+
ignoreCase?: boolean;
14+
multiline?: boolean;
15+
hasIndices?: boolean;
16+
sticky?: boolean;
17+
},
18+
sequence: RegexSequence
19+
): RegExp;
20+
```
21+
22+
## Components
23+
24+
### `capture()` component
25+
26+
Captures, also known as capturing groups, are used to extract and store parts of the matched string for later use.
27+
28+
```ts
29+
function capture(
30+
sequence: RegexSequence
31+
): Capture
32+
```
33+
34+
### `choiceOf()` component
35+
36+
```ts
37+
function choiceOf(
38+
...alternatives: RegexSequence[]
39+
): ChoiceOf {
40+
```
41+
42+
The `choiceOf` (alternation) construct is used to match one out of several possible sequences. It functions similarly to a logical OR operator in programming. It can match simple string options as well as complex patterns.
43+
44+
Example: `choiceOf("color", "colour")` matches either `color` or `colour` pattern.
45+
46+
## Quantifiers
47+
48+
### `zeroOrMore()` component
49+
50+
```ts
51+
function zeroOrMore(
52+
sequence: RegexSequence,
53+
): ZeroOrMore
54+
```
55+
56+
### `oneOrMore()` component
57+
58+
```ts
59+
function oneOrMore(
60+
sequence: RegexSequence,
61+
): OneOrMore
62+
```
63+
64+
### `optionally()` component
65+
66+
```ts
67+
function optionally(
68+
sequence: RegexSequence,
69+
): Optionally
70+
```
71+
72+
### `repeat()` component
73+
74+
```ts
75+
function repeat(
76+
options: { count: number } | { min: number; max?: number },
77+
sequence: RegexSequence,
78+
): Repeat
79+
```
80+
81+
## Character classes
82+
83+
Character classes are a set of characters that match any one of the characters in the set.
84+
85+
### Common character classess
86+
87+
```ts
88+
const any: CharacterClass;
89+
const word: CharacterClass;
90+
const digit: CharacterClass;
91+
const whitespace: CharacterClass;
92+
```
93+
94+
* `any` matches any character except newline characters.
95+
* `word` matches any word character (alphanumeric & underscore).
96+
* `digit` matches any digit.
97+
* `whitespace` matches any whitespace character (spaces, tabs, line breaks).
98+
99+
### `anyOf()` component
100+
101+
```ts
102+
function anyOf(
103+
characters: string,
104+
): CharacterClass
105+
```
106+
107+
The `anyOf` class matches any character present in the `character` string.
108+
109+
Example: `anyOf('aeiou')` will match either `a`, `e`, `i` `o` or `u` characters.
110+
111+
### `characterRange()` component
112+
113+
```ts
114+
function characterRange(
115+
start: string,
116+
end: string,
117+
): CharacterClass
118+
```
119+
120+
The `characterRange` class matches any character present in the range from `start` to `end` (inclusive).
121+
122+
Examples:
123+
* `characterRange('a', 'z')` will match all lowercase characters from `a` to `z`.
124+
* `characterRange('A', 'Z')` will match all uppercase characters from `a` to `z`.
125+
* `characterRange('0', '9')` will match all digit characters from `0` to `9`.
126+
127+
### `characterClass()` component
128+
129+
```ts
130+
function characterClass(
131+
...elements: CharacterClass[],
132+
): CharacterClass
133+
```
134+
135+
The `characterClass` component creates a new character class that includes all passed character classes.
136+
137+
Example:
138+
* `characterClass(characterRange('a', 'f'), digit)` will match all lowercase hex digits (`0` to `9` and `a` to `f`).
139+
* `characterClass(characterRange('a', 'z'), digit, anyOf("._-"))` will match any digit, lowercase latin lettet from `a` to `z`, and either of `.`, `_`, and `-` characters.
140+
141+
### `inverted()` component
142+
143+
```ts
144+
function inverted(
145+
element: CharacterClass,
146+
): CharacterClass
147+
```
148+
149+
The `inverted` component creates a new character class that matches any character that is not present in the passed character class.
150+
151+
Examples:
152+
* `inverted(digit)` matches any character that is not a digit
153+
* `inverted(anyOf('aeiou'))` matches any character that is not a lowercase vowel.
154+
155+
156+
157+
## Anchors
158+
159+
Anchors are special characters or sequences that specify positions in the input string, rather than matching specific characters.
160+
161+
### Line start and end
162+
163+
```ts
164+
const startOfString: Anchor; // Regex: ^
165+
const endOfString: Anchor; // Regex: $
166+
```
167+
168+
The `startOfString` (regex: `^`) matches the start of a string (or line, if multiline mode is enabled).
169+
170+
The `endOfString` (regex: `$`) matches the end of a string (or line, if multiline mode is enabled).

src/components/anchors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { EncodeOutput } from '../encoder/types';
2-
import type { RegexElement } from '../types';
2+
import type { RegexEncodable } from '../types';
33

4-
export interface Anchor extends RegexElement {
4+
export interface Anchor extends RegexEncodable {
55
type: 'anchor';
66
symbol: string;
77
}

src/components/capture.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { encodeSequence } from '../encoder/encoder';
22
import type { EncodeOutput } from '../encoder/types';
33
import { asNodeArray } from '../utils/nodes';
4-
import type { RegexElement, RegexNode, RegexSequence } from '../types';
4+
import type { RegexElement, RegexEncodable, RegexSequence } from '../types';
55

6-
export interface Capture extends RegexElement {
6+
export interface Capture extends RegexEncodable {
77
type: 'capture';
8-
children: RegexNode[];
8+
children: RegexElement[];
99
}
1010

1111
export function capture(sequence: RegexSequence): Capture {

src/components/choice-of.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { encodeSequence } from '../encoder/encoder';
22
import type { EncodeOutput } from '../encoder/types';
33
import { asNodeArray } from '../utils/nodes';
4-
import type { RegexElement, RegexNode, RegexSequence } from '../types';
4+
import type { RegexElement, RegexEncodable, RegexSequence } from '../types';
55

6-
export interface ChoiceOf extends RegexElement {
6+
export interface ChoiceOf extends RegexEncodable {
77
type: 'choiceOf';
8-
alternatives: RegexNode[][];
8+
alternatives: RegexElement[][];
99
}
1010

1111
export function choiceOf(...alternatives: RegexSequence[]): ChoiceOf {

src/components/quantifiers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { encodeAtom } from '../encoder/encoder';
22
import type { EncodeOutput } from '../encoder/types';
33
import { asNodeArray } from '../utils/nodes';
4-
import type { RegexElement, RegexNode, RegexSequence } from '../types';
4+
import type { RegexElement, RegexEncodable, RegexSequence } from '../types';
55

6-
export interface OneOrMore extends RegexElement {
6+
export interface OneOrMore extends RegexEncodable {
77
type: 'oneOrMore';
8-
children: RegexNode[];
8+
children: RegexElement[];
99
}
1010

11-
export interface Optionally extends RegexElement {
11+
export interface Optionally extends RegexEncodable {
1212
type: 'optionally';
13-
children: RegexNode[];
13+
children: RegexElement[];
1414
}
1515

16-
export interface ZeroOrMore extends RegexElement {
16+
export interface ZeroOrMore extends RegexEncodable {
1717
type: 'zeroOrMore';
18-
children: RegexNode[];
18+
children: RegexElement[];
1919
}
2020

2121
export function oneOrMore(sequence: RegexSequence): OneOrMore {

src/components/repeat.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { encodeAtom } from '../encoder/encoder';
22
import type { EncodeOutput } from '../encoder/types';
33
import { asNodeArray } from '../utils/nodes';
4-
import type { RegexElement, RegexNode, RegexSequence } from '../types';
4+
import type { RegexElement, RegexEncodable, RegexSequence } from '../types';
55

6-
export interface Repeat extends RegexElement {
6+
export interface Repeat extends RegexEncodable {
77
type: 'repeat';
88
options: RepeatOptions;
9-
children: RegexNode[];
9+
children: RegexElement[];
1010
}
1111

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

src/encoder/encoder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import type { RegexNode } from '../types';
1+
import type { RegexElement } from '../types';
22
import { escapeText } from '../utils/text';
33
import type { EncodeOutput } from './types';
44

5-
export function encodeSequence(nodes: RegexNode[]): EncodeOutput {
5+
export function encodeSequence(nodes: RegexElement[]): EncodeOutput {
66
const encodedNodes = nodes.map((n) => encodeNode(n));
77
return concatSequence(encodedNodes);
88
}
99

10-
export function encodeAtom(nodes: RegexNode[]): EncodeOutput {
10+
export function encodeAtom(nodes: RegexElement[]): EncodeOutput {
1111
return asAtom(encodeSequence(nodes));
1212
}
1313

14-
function encodeNode(node: RegexNode): EncodeOutput {
14+
function encodeNode(node: RegexElement): EncodeOutput {
1515
if (typeof node === 'string') {
1616
return encodeText(node);
1717
}

src/types.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import type { EncodeOutput } from './encoder/types';
22

3-
export type RegexSequence = RegexNode | RegexNode[];
3+
/**
4+
* Sequence of `RegexElements` that can be encoded into a regular expression.
5+
*/
6+
export type RegexSequence = RegexElement[] | RegexElement;
47

5-
export type RegexNode = RegexElement | string;
8+
/**
9+
* Represents a result of calling a regex component (`RegexEncodable`) or a string to be matched literally.
10+
*/
11+
export type RegexElement = RegexEncodable | string;
612

7-
export interface RegexElement {
13+
/**
14+
* Represents result of calling a regex componen.
15+
*/
16+
export interface RegexEncodable {
817
type: string;
918
encode(): EncodeOutput;
1019
}

src/utils/nodes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { RegexNode, RegexSequence } from '../types';
1+
import type { RegexElement, RegexSequence } from '../types';
22

3-
export function asNodeArray(sequence: RegexSequence): RegexNode[] {
3+
export function asNodeArray(sequence: RegexSequence): RegexElement[] {
44
return Array.isArray(sequence) ? sequence : [sequence];
55
}

test-utils/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { buildRegex } from '../src/builders';
2-
import type { RegexElement, RegexNode } from '../src/types';
2+
import type { RegexElement, RegexEncodable } from '../src/types';
33

4-
export function isRegexNode(node: unknown): node is RegexNode {
5-
return typeof node === 'string' || isRegexElement(node);
4+
export function isRegexElement(node: unknown): node is RegexElement {
5+
return typeof node === 'string' || isRegexEncodable(node);
66
}
77

8-
export function isRegexElement(element: unknown): element is RegexElement {
8+
export function isRegexEncodable(element: unknown): element is RegexEncodable {
99
return (
1010
typeof element === 'object' &&
1111
element !== null &&
@@ -14,7 +14,7 @@ export function isRegexElement(element: unknown): element is RegexElement {
1414
);
1515
}
1616

17-
export function asRegExp(regex: RegExp | RegexNode | RegexNode[]) {
17+
export function asRegExp(regex: RegExp | RegexElement | RegexElement[]) {
1818
if (regex instanceof RegExp) {
1919
return regex;
2020
}

0 commit comments

Comments
 (0)