Skip to content

Commit 686fdcd

Browse files
refactor: further renaming (#45)
1 parent 871002e commit 686fdcd

30 files changed

+198
-188
lines changed

GUIDELINES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
## Implementation guidelines
1010

11-
1. When the user passes the text to any regex component, it should be treated as an exact string to match and not as a regex string. We might provide an escape hatch for providing raw regex string through, but the user should use it explicitly.
11+
1. When the user passes the text to any regex construct, it should be treated as an exact string to match and not as a regex string. We might provide an escape hatch for providing raw regex string through, but the user should use it explicitly.

README.md

+25-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const hexDigit = charClass(
1919
charRange('0', '9'),
2020
);
2121

22-
const hexColor = buildRegex(
22+
const hexColor = buildRegExp(
2323
startOfString,
2424
optionally('#'),
2525
capture(
@@ -47,32 +47,35 @@ yarn add ts-regex-builder
4747
## Basic usage
4848

4949
```js
50-
import { buildRegex, capture, oneOrMore } from 'ts-regex-builder';
50+
import { buildRegExp, capture, oneOrMore } from 'ts-regex-builder';
5151

5252
// /Hello (\w+)/
53-
const regex = buildRegex(['Hello ', capture(oneOrMore(word))]);
53+
const regex = buildRegExp(['Hello ', capture(oneOrMore(word))]);
5454
```
5555

5656
## Regex domain-specific language
5757

58-
TS Regex Builder allows you to build complex regular expressions using domain-specific language of regex components.
58+
TS Regex Builder allows you to build complex regular expressions using domain-specific language.
5959

6060
Terminology:
6161

62-
- regex component (e.g., `capture()`, `oneOrMore()`, `word`) - function or object representing a regex construct
63-
- regex element (`RegexElement`) - object returned by regex components
64-
- regex sequence (`RegexSequence`) - single regex element or string (`RegexElement | string`) or array of such elements and strings (`Array<RegexElement | string>`)
62+
- regex construct (`RegexConstruct`) - common name for all regex constructs like character classes, quantifiers, and anchors.
6563

66-
Most of the regex components accept a regex sequence. Examples of sequences:
64+
- regex element (`RegexElement`) - fundamental building block of a regular expression, defined as either a regex construct or a string.
6765

68-
- single string: `'Hello World'` (note: all characters will be automatically escaped in the resulting regex)
69-
- single element: `capture('abc')`
70-
- array of elements and strings: `['$', oneOrMore(digit)]`
66+
- regex sequence (`RegexSequence`) - a sequence of regex elements forming a regular expression. For developer convenience it also accepts a single element instead of array.
7167

72-
Regex components can be composed into a complex tree:
68+
Most of the regex constructs accept a regex sequence as their argument.
69+
70+
Examples of sequences:
71+
- array of elements: `['USD', oneOrMore(digit)]`
72+
- single construct: `capture('abc')`
73+
- single string: `'Hello'`
74+
75+
Regex constructs can be composed into a tree:
7376

7477
```ts
75-
const currencyAmount = buildRegex([
78+
const currencyAmount = buildRegExp([
7679
choiceOf(
7780
'$',
7881
'',
@@ -87,14 +90,14 @@ const currencyAmount = buildRegex([
8790

8891
### Regex Builders
8992

90-
| Regex Component | Regex Pattern | Description |
91-
| --------------------------------------- | ------------- | ----------------------------------- |
92-
| `buildRegex(...)` | `/.../` | Create `RegExp` instance |
93-
| `buildRegex(..., { ignoreCase: true })` | `/.../i` | Create `RegExp` instance with flags |
93+
| Builder | Regex Pattern | Description |
94+
| ---------------------------------------- | ------------- | ----------------------------------- |
95+
| `buildRegExp(...)` | `/.../` | Create `RegExp` instance |
96+
| `buildRegExp(..., { ignoreCase: true })` | `/.../i` | Create `RegExp` instance with flags |
9497

95-
### Components
98+
### Regex Constructs
9699

97-
| Regex Component | Regex Pattern | Notes |
100+
| Regex Construct | Regex Pattern | Notes |
98101
| ------------------- | ------------- | ------------------------------- |
99102
| `capture(...)` | `(...)` | Create a capture group |
100103
| `choiceOf(x, y, z)` | `x\|y\|z` | Match one of provided sequences |
@@ -106,7 +109,7 @@ Notes:
106109

107110
### Quantifiers
108111

109-
| Regex Component | Regex Pattern | Description |
112+
| Regex Construct | Regex Pattern | Description |
110113
| -------------------------------- | ------------- | ------------------------------------------------- |
111114
| `zeroOrMore(x)` | `x*` | Zero or more occurence of a pattern |
112115
| `oneOrMore(x)` | `x+` | One or more occurence of a pattern |
@@ -119,7 +122,7 @@ All quantifiers accept sequence of elements
119122

120123
### Character classes
121124

122-
| Regex Component | Regex Pattern | Description |
125+
| Regex Construct | Regex Pattern | Description |
123126
| --------------------- | ------------- | ------------------------------------------- |
124127
| `any` | `.` | Any character |
125128
| `word` | `\w` | Word characters |
@@ -140,7 +143,7 @@ Notes:
140143

141144
### Anchors
142145

143-
| Regex Component | Regex Pattern | Description |
146+
| Regex Construct | Regex Pattern | Description |
144147
| --------------- | ------------- | ---------------------------------------------------------------- |
145148
| `startOfString` | `^` | Match start of the string (or start of a line in multiline mode) |
146149
| `endOfString` | `$` | Match end of the string (or end of a line in multiline mode) |

docs/API.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
## Builder
44

5-
### `buildRegex()` function
5+
### `buildRegExp()` function
66

77
```ts
8-
function buildRegex(sequence: RegexSequence): RegExp;
8+
function buildRegExp(sequence: RegexSequence): RegExp;
99

10-
function buildRegex(
10+
function buildRegExp(
11+
sequence: RegexSequence,
1112
flags: {
1213
global?: boolean;
1314
ignoreCase?: boolean;
1415
multiline?: boolean;
1516
hasIndices?: boolean;
1617
sticky?: boolean;
1718
},
18-
sequence: RegexSequence
1919
): RegExp;
2020
```
2121

22-
## Components
22+
## Constructs
2323

24-
### `capture()` component
24+
### `capture()`
2525

2626
Captures, also known as capturing groups, are used to extract and store parts of the matched string for later use.
2727

@@ -31,7 +31,7 @@ function capture(
3131
): Capture
3232
```
3333

34-
### `choiceOf()` component
34+
### `choiceOf()`
3535

3636
```ts
3737
function choiceOf(
@@ -45,31 +45,31 @@ Example: `choiceOf("color", "colour")` matches either `color` or `colour` patter
4545

4646
## Quantifiers
4747

48-
### `zeroOrMore()` component
48+
### `zeroOrMore()`
4949

5050
```ts
5151
function zeroOrMore(
5252
sequence: RegexSequence,
5353
): ZeroOrMore
5454
```
5555

56-
### `oneOrMore()` component
56+
### `oneOrMore()`
5757

5858
```ts
5959
function oneOrMore(
6060
sequence: RegexSequence,
6161
): OneOrMore
6262
```
6363

64-
### `optionally()` component
64+
### `optionally()`
6565

6666
```ts
6767
function optionally(
6868
sequence: RegexSequence,
6969
): Optionally
7070
```
7171

72-
### `repeat()` component
72+
### `repeat()`
7373

7474
```ts
7575
function repeat(
@@ -96,7 +96,7 @@ const whitespace: CharacterClass;
9696
* `digit` matches any digit.
9797
* `whitespace` matches any whitespace character (spaces, tabs, line breaks).
9898

99-
### `anyOf()` component
99+
### `anyOf()`
100100

101101
```ts
102102
function anyOf(
@@ -108,7 +108,7 @@ The `anyOf` class matches any character present in the `character` string.
108108

109109
Example: `anyOf('aeiou')` will match either `a`, `e`, `i` `o` or `u` characters.
110110

111-
### `characterRange()` component
111+
### `characterRange()`
112112

113113
```ts
114114
function characterRange(
@@ -124,29 +124,29 @@ Examples:
124124
* `characterRange('A', 'Z')` will match all uppercase characters from `a` to `z`.
125125
* `characterRange('0', '9')` will match all digit characters from `0` to `9`.
126126

127-
### `characterClass()` component
127+
### `characterClass()`
128128

129129
```ts
130130
function characterClass(
131131
...elements: CharacterClass[],
132132
): CharacterClass
133133
```
134134

135-
The `characterClass` component creates a new character class that includes all passed character classes.
135+
The `characterClass` construct creates a new character class that includes all passed character classes.
136136

137137
Example:
138138
* `characterClass(characterRange('a', 'f'), digit)` will match all lowercase hex digits (`0` to `9` and `a` to `f`).
139139
* `characterClass(characterRange('a', 'z'), digit, anyOf("._-"))` will match any digit, lowercase latin lettet from `a` to `z`, and either of `.`, `_`, and `-` characters.
140140

141-
### `inverted()` component
141+
### `inverted()`
142142

143143
```ts
144144
function inverted(
145145
element: CharacterClass,
146146
): CharacterClass
147147
```
148148

149-
The `inverted` component creates a new character class that matches any character that is not present in the passed character class.
149+
The `inverted` construct creates a new character class that matches any character that is not present in the passed character class.
150150

151151
Examples:
152152
* `inverted(digit)` matches any character that is not a digit

docs/Examples.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const octet = choiceOf(
1313
);
1414

1515
// Match
16-
const regex = buildRegex([
16+
const regex = buildRegExp([
1717
startOfString, //
1818
repeat([octet, '.'], { count: 3 }),
1919
octet,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"quoteProps": "consistent",
122122
"singleQuote": true,
123123
"tabWidth": 2,
124-
"trailingComma": "es5",
124+
"trailingComma": "all",
125125
"useTabs": false
126126
}
127127
],

src/__tests__/builder.test.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import { buildRegex } from '../builders';
1+
import { buildRegExp } from '../builders';
22

33
test('`regexBuilder` flags', () => {
4-
expect(buildRegex('a').flags).toBe('');
5-
expect(buildRegex('a', {}).flags).toBe('');
4+
expect(buildRegExp('a').flags).toBe('');
5+
expect(buildRegExp('a', {}).flags).toBe('');
66

7-
expect(buildRegex('a', { global: true }).flags).toBe('g');
8-
expect(buildRegex('a', { global: false }).flags).toBe('');
7+
expect(buildRegExp('a', { global: true }).flags).toBe('g');
8+
expect(buildRegExp('a', { global: false }).flags).toBe('');
99

10-
expect(buildRegex('a', { ignoreCase: true }).flags).toBe('i');
11-
expect(buildRegex('a', { ignoreCase: false }).flags).toBe('');
10+
expect(buildRegExp('a', { ignoreCase: true }).flags).toBe('i');
11+
expect(buildRegExp('a', { ignoreCase: false }).flags).toBe('');
1212

13-
expect(buildRegex('a', { multiline: true }).flags).toBe('m');
14-
expect(buildRegex('a', { multiline: false }).flags).toBe('');
13+
expect(buildRegExp('a', { multiline: true }).flags).toBe('m');
14+
expect(buildRegExp('a', { multiline: false }).flags).toBe('');
1515

16-
expect(buildRegex('a', { hasIndices: true }).flags).toBe('d');
17-
expect(buildRegex('a', { hasIndices: false }).flags).toBe('');
16+
expect(buildRegExp('a', { hasIndices: true }).flags).toBe('d');
17+
expect(buildRegExp('a', { hasIndices: false }).flags).toBe('');
1818

19-
expect(buildRegex('a', { sticky: true }).flags).toBe('y');
20-
expect(buildRegex('a', { sticky: false }).flags).toBe('');
19+
expect(buildRegExp('a', { sticky: true }).flags).toBe('y');
20+
expect(buildRegExp('a', { sticky: false }).flags).toBe('');
2121

2222
expect(
23-
buildRegex('a', {
23+
buildRegExp('a', {
2424
global: true, //
2525
ignoreCase: true,
2626
multiline: false,
27-
}).flags
27+
}).flags,
2828
).toBe('gi');
2929
});

src/__tests__/examples.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
buildRegex,
2+
buildRegExp,
33
charRange,
44
choiceOf,
55
digit,
@@ -14,10 +14,10 @@ test('example: IPv4 address validator', () => {
1414
[charRange('1', '9'), digit],
1515
['1', repeat(digit, { count: 2 })],
1616
['2', charRange('0', '4'), digit],
17-
['25', charRange('0', '5')]
17+
['25', charRange('0', '5')],
1818
);
1919

20-
const regex = buildRegex([
20+
const regex = buildRegExp([
2121
startOfString, //
2222
repeat([octet, '.'], { count: 3 }),
2323
octet,
@@ -38,6 +38,6 @@ test('example: IPv4 address validator', () => {
3838
expect(regex).not.toMatchString('255.255.255.256');
3939

4040
expect(regex).toHavePattern(
41-
/^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/
41+
/^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/,
4242
);
4343
});

src/builders.ts

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
import type { RegexSequence } from './types';
1+
import type { RegexFlags, RegexSequence } from './types';
22
import { encodeSequence } from './encoder/encoder';
3-
import { asNodeArray } from './utils/nodes';
4-
5-
export interface RegexFlags {
6-
/** Global search. */
7-
global?: boolean;
8-
9-
/** Case-insensitive search. */
10-
ignoreCase?: boolean;
11-
12-
/** Allows ^ and $ to match newline characters. */
13-
multiline?: boolean;
14-
15-
/** Generate indices for substring matches. */
16-
hasIndices?: boolean;
17-
18-
/** Perform a "sticky" search that matches starting at the current position in the target string. */
19-
sticky?: boolean;
20-
}
3+
import { ensureArray } from './utils/elements';
214

225
/**
236
* Generate RegExp object from elements with optional flags.
@@ -26,8 +9,8 @@ export interface RegexFlags {
269
* @param flags RegExp flags object
2710
* @returns RegExp object
2811
*/
29-
export function buildRegex(sequence: RegexSequence, flags?: RegexFlags): RegExp {
30-
const pattern = encodeSequence(asNodeArray(sequence)).pattern;
12+
export function buildRegExp(sequence: RegexSequence, flags?: RegexFlags): RegExp {
13+
const pattern = encodeSequence(ensureArray(sequence)).pattern;
3114
const flagsString = encodeFlags(flags ?? {});
3215
return new RegExp(pattern, flagsString);
3316
}
@@ -38,7 +21,7 @@ export function buildRegex(sequence: RegexSequence, flags?: RegexFlags): RegExp
3821
* @returns regex pattern string
3922
*/
4023
export function buildPattern(sequence: RegexSequence): string {
41-
return encodeSequence(asNodeArray(sequence)).pattern;
24+
return encodeSequence(ensureArray(sequence)).pattern;
4225
}
4326

4427
function encodeFlags(flags: RegexFlags): string {

0 commit comments

Comments
 (0)