Skip to content

Commit 3c8c4fd

Browse files
refactor: rename optionally to optional (#48)
1 parent 0db6063 commit 3c8c4fd

File tree

9 files changed

+52
-52
lines changed

9 files changed

+52
-52
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const hexDigit = charClass(
2323

2424
const hexColor = buildRegExp(
2525
startOfString,
26-
optionally('#'),
26+
optional('#'),
2727
capture(
2828
choiceOf(
2929
repeat(hexDigit, 6), // #rrggbb
@@ -85,7 +85,7 @@ const currencyAmount = buildRegExp([
8585
),
8686
capture(
8787
oneOrMore(digit), // Integer part
88-
optionally(['.', repeat(digit, 2)]), // Fractional part
88+
optional(['.', repeat(digit, 2)]), // Fractional part
8989
),
9090
]);
9191
```
@@ -112,7 +112,7 @@ Comprehensive API document is available [here](./API.md).
112112
| -------------------------------- | ------------- | ------------------------------------------------- |
113113
| `zeroOrMore(x)` | `x*` | Zero or more occurence of a pattern |
114114
| `oneOrMore(x)` | `x+` | One or more occurence of a pattern |
115-
| `optionally(x)` | `x?` | Zero or one occurence of a pattern |
115+
| `optional(x)` | `x?` | Zero or one occurence of a pattern |
116116
| `repeat(x, n)` | `x{n}` | Pattern repeats exact number of times |
117117
| `repeat(x, { min: n, })` | `x{n,}` | Pattern repeats at least given number of times |
118118
| `repeat(x, { min: n, max: n2 })` | `x{n1,n2}` | Pattern repeats between n1 and n2 number of times |

docs/API.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,17 @@ Regex syntax: `x+`;
100100

101101
The `oneOrMore` quantifier matches one or more occurrences of given pattern, allowing a flexible number of repetitions of that element.
102102

103-
### `optionally()`
103+
### `optional()`
104104

105105
```ts
106-
function optionally(
106+
function optional(
107107
sequence: RegexSequence,
108108
): Optionally
109109
```
110110

111111
Regex syntax: `x?`;
112112

113-
The `optionally` quantifier matches zero or one occurrence of given pattern, making it optional.
113+
The `optional` quantifier matches zero or one occurrence of given pattern, making it optional.
114114

115115
### `repeat()`
116116

docs/Examples.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
## JavaScript number
44

55
```ts
6-
const optionalSign = optionally(anyOf('+-'));
7-
const exponent = [anyOf('eE'), optionalSign, oneOrMore(digit)];
6+
const sign = anyOf('+-');
7+
const exponent = [anyOf('eE'), optional(sign), oneOrMore(digit)];
88

99
const regex = buildRegExp([
10-
optionalSign,
10+
optional(sing),
1111
choiceOf(
12-
[oneOrMore(digit), optionally(['.', zeroOrMore(digit)])], // leading digit
12+
[oneOrMore(digit), optional(['.', zeroOrMore(digit)])], // leading digit
1313
['.', oneOrMore(digit)], // leading dot
1414
),
15-
optionally(exponent), // exponent
15+
optional(exponent), // exponent
1616
]);
1717
```
1818

src/__tests__/examples.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ import {
66
digit,
77
endOfString,
88
oneOrMore,
9-
optionally,
9+
optional,
1010
repeat,
1111
startOfString,
1212
zeroOrMore,
1313
} from '../index';
1414

1515
test('example: validate JavaScript number', () => {
16-
const optionalSign = optionally(anyOf('+-'));
17-
const exponent = [anyOf('eE'), optionalSign, oneOrMore(digit)];
16+
const sign = anyOf('+-');
17+
const exponent = [anyOf('eE'), optional(sign), oneOrMore(digit)];
1818

1919
const regex = buildRegExp([
2020
startOfString,
21-
optionalSign,
21+
optional(sign),
2222
choiceOf(
23-
[oneOrMore(digit), optionally(['.', zeroOrMore(digit)])], // leading digit
23+
[oneOrMore(digit), optional(['.', zeroOrMore(digit)])], // leading digit
2424
['.', oneOrMore(digit)], // leading dot
2525
),
26-
optionally(exponent), // exponent
26+
optional(exponent), // exponent
2727
endOfString,
2828
]);
2929

src/constructs/__tests__/character-class.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { oneOrMore, optionally, zeroOrMore } from '../quantifiers';
1+
import { oneOrMore, optional, zeroOrMore } from '../quantifiers';
22
import {
33
any,
44
anyOf,
@@ -76,7 +76,7 @@ test('`anyOf` base cases', () => {
7676

7777
test('`anyOf` with quantifiers', () => {
7878
expect(['x', oneOrMore(anyOf('abc')), 'x']).toHavePattern(/x[abc]+x/);
79-
expect(['x', optionally(anyOf('abc')), 'x']).toHavePattern(/x[abc]?x/);
79+
expect(['x', optional(anyOf('abc')), 'x']).toHavePattern(/x[abc]?x/);
8080
expect(['x', zeroOrMore(anyOf('abc')), 'x']).toHavePattern(/x[abc]*x/);
8181
});
8282

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { digit } from '../character-class';
2-
import { oneOrMore, optionally, zeroOrMore } from '../quantifiers';
2+
import { oneOrMore, optional, zeroOrMore } from '../quantifiers';
33

44
test('`oneOrMore` quantifier', () => {
55
expect(oneOrMore('a')).toHavePattern(/a+/);
66
expect(oneOrMore('ab')).toHavePattern(/(?:ab)+/);
77
});
88

9-
test('`optionally` quantifier', () => {
10-
expect(optionally('a')).toHavePattern(/a?/);
11-
expect(optionally('ab')).toHavePattern(/(?:ab)?/);
9+
test('`optional` quantifier', () => {
10+
expect(optional('a')).toHavePattern(/a?/);
11+
expect(optional('ab')).toHavePattern(/(?:ab)?/);
1212
});
1313

1414
test('`zeroOrMore` quantifier', () => {
@@ -20,8 +20,8 @@ test('`oneOrMore` does not generate capture when grouping', () => {
2020
expect(oneOrMore('aa')).toMatchGroups('aa', ['aa']);
2121
});
2222

23-
test('`optionally` does not generate capture when grouping', () => {
24-
expect(optionally('aa')).toMatchGroups('aa', ['aa']);
23+
test('`optional` does not generate capture when grouping', () => {
24+
expect(optional('aa')).toMatchGroups('aa', ['aa']);
2525
});
2626

2727
test('`zeroOrMore` does not generate capture when grouping', () => {
@@ -30,10 +30,10 @@ test('`zeroOrMore` does not generate capture when grouping', () => {
3030

3131
test('base quantifiers optimize grouping for atoms', () => {
3232
expect(oneOrMore(digit)).toHavePattern(/\d+/);
33-
expect(optionally(digit)).toHavePattern(/\d?/);
33+
expect(optional(digit)).toHavePattern(/\d?/);
3434
expect(zeroOrMore(digit)).toHavePattern(/\d*/);
3535

3636
expect(oneOrMore('a')).toHavePattern(/a+/);
37-
expect(optionally('a')).toHavePattern(/a?/);
37+
expect(optional('a')).toHavePattern(/a?/);
3838
expect(zeroOrMore('a')).toHavePattern(/a*/);
3939
});

src/constructs/quantifiers.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@ import type { EncodeResult } from '../encoder/types';
33
import { ensureArray } from '../utils/elements';
44
import type { RegexConstruct, RegexElement, RegexSequence } from '../types';
55

6+
export interface ZeroOrMore extends RegexConstruct {
7+
type: 'zeroOrMore';
8+
children: RegexElement[];
9+
}
10+
611
export interface OneOrMore extends RegexConstruct {
712
type: 'oneOrMore';
813
children: RegexElement[];
914
}
1015

11-
export interface Optionally extends RegexConstruct {
12-
type: 'optionally';
16+
export interface Optional extends RegexConstruct {
17+
type: 'optional';
1318
children: RegexElement[];
1419
}
1520

16-
export interface ZeroOrMore extends RegexConstruct {
17-
type: 'zeroOrMore';
18-
children: RegexElement[];
21+
export function zeroOrMore(sequence: RegexSequence): ZeroOrMore {
22+
return {
23+
type: 'zeroOrMore',
24+
children: ensureArray(sequence),
25+
encode: encodeZeroOrMore,
26+
};
1927
}
2028

2129
export function oneOrMore(sequence: RegexSequence): OneOrMore {
@@ -26,19 +34,18 @@ export function oneOrMore(sequence: RegexSequence): OneOrMore {
2634
};
2735
}
2836

29-
export function optionally(sequence: RegexSequence): Optionally {
37+
export function optional(sequence: RegexSequence): Optional {
3038
return {
31-
type: 'optionally',
39+
type: 'optional',
3240
children: ensureArray(sequence),
33-
encode: encodeOptionally,
41+
encode: encodeOptional,
3442
};
3543
}
3644

37-
export function zeroOrMore(sequence: RegexSequence): ZeroOrMore {
45+
function encodeZeroOrMore(this: ZeroOrMore): EncodeResult {
3846
return {
39-
type: 'zeroOrMore',
40-
children: ensureArray(sequence),
41-
encode: encodeZeroOrMore,
47+
precedence: 'sequence',
48+
pattern: `${encodeAtom(this.children).pattern}*`,
4249
};
4350
}
4451

@@ -49,16 +56,9 @@ function encodeOneOrMore(this: OneOrMore): EncodeResult {
4956
};
5057
}
5158

52-
function encodeOptionally(this: Optionally): EncodeResult {
59+
function encodeOptional(this: Optional): EncodeResult {
5360
return {
5461
precedence: 'sequence',
5562
pattern: `${encodeAtom(this.children).pattern}?`,
5663
};
5764
}
58-
59-
function encodeZeroOrMore(this: ZeroOrMore): EncodeResult {
60-
return {
61-
precedence: 'sequence',
62-
pattern: `${encodeAtom(this.children).pattern}*`,
63-
};
64-
}

src/encoder/__tests__/encoder.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { buildPattern, buildRegExp } from '../../builders';
2-
import { oneOrMore, optionally, zeroOrMore } from '../../constructs/quantifiers';
2+
import { oneOrMore, optional, zeroOrMore } from '../../constructs/quantifiers';
33
import { repeat } from '../../constructs/repeat';
44

55
test('basic quantifies', () => {
66
expect('a').toHavePattern(/a/);
77
expect(['a', 'b']).toHavePattern(/ab/);
88

99
expect(oneOrMore('a')).toHavePattern(/a+/);
10-
expect(optionally('a')).toHavePattern(/a?/);
10+
expect(optional('a')).toHavePattern(/a?/);
1111

1212
expect(['a', oneOrMore('b')]).toHavePattern(/ab+/);
1313
expect(['a', oneOrMore('bc')]).toHavePattern(/a(?:bc)+/);
@@ -19,9 +19,9 @@ test('basic quantifies', () => {
1919
expect(['a', zeroOrMore('bc')]).toHavePattern(/a(?:bc)*/);
2020
expect(['a', zeroOrMore('bc')]).toHavePattern(/a(?:bc)*/);
2121

22-
expect([optionally('a'), 'b']).toHavePattern(/a?b/);
22+
expect([optional('a'), 'b']).toHavePattern(/a?b/);
2323

24-
expect([optionally('a'), 'b', oneOrMore('d')]).toHavePattern(/a?bd+/);
24+
expect([optional('a'), 'b', oneOrMore('d')]).toHavePattern(/a?bd+/);
2525
});
2626

2727
test('`buildPattern` escapes special characters', () => {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ export {
1515
inverted,
1616
} from './constructs/character-class';
1717
export { choiceOf } from './constructs/choice-of';
18-
export { oneOrMore, optionally, zeroOrMore } from './constructs/quantifiers';
18+
export { oneOrMore, optional, zeroOrMore } from './constructs/quantifiers';
1919
export { repeat } from './constructs/repeat';

0 commit comments

Comments
 (0)