Skip to content

Commit 53ecc2f

Browse files
refactor: simplify exact repeat quantifier (#46)
1 parent 686fdcd commit 53ecc2f

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const hexColor = buildRegExp(
2424
optionally('#'),
2525
capture(
2626
choiceOf(
27-
repeat(hexDigit, { count: 6 }), // #rrggbb
28-
repeat(hexDigit, { count: 3 }), // #rgb
27+
repeat(hexDigit, 6), // #rrggbb
28+
repeat(hexDigit, 3), // #rgb
2929
),
3030
),
3131
endOfString,
@@ -79,11 +79,11 @@ const currencyAmount = buildRegExp([
7979
choiceOf(
8080
'$',
8181
'',
82-
repeat({ count: 3 }, charRange('A', 'Z')), // ISO currency code
82+
repeat(charRange('A', 'Z'), 3), // ISO currency code
8383
),
8484
capture(
8585
oneOrMore(digit), // Integer part
86-
optionally(['.', repeat({ count: 2 }, digit)]), // Fractional part
86+
optionally(['.', repeat(digit, 2)]), // Fractional part
8787
),
8888
]);
8989
```
@@ -114,7 +114,7 @@ Notes:
114114
| `zeroOrMore(x)` | `x*` | Zero or more occurence of a pattern |
115115
| `oneOrMore(x)` | `x+` | One or more occurence of a pattern |
116116
| `optionally(x)` | `x?` | Zero or one occurence of a pattern |
117-
| `repeat(x, { count: n })` | `x{n}` | Pattern repeats exact number of times |
117+
| `repeat(x, n)` | `x{n}` | Pattern repeats exact number of times |
118118
| `repeat(x, { min: n, })` | `x{n,}` | Pattern repeats at least given number of times |
119119
| `repeat(x, { min: n, max: n2 })` | `x{n1,n2}` | Pattern repeats between n1 and n2 number of times |
120120

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function optionally(
7373

7474
```ts
7575
function repeat(
76-
options: { count: number } | { min: number; max?: number },
76+
options: number | { min: number; max?: number },
7777
sequence: RegexSequence,
7878
): Repeat
7979
```

docs/Examples.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
const octet = choiceOf(
88
[digit],
99
[charRange('1', '9'), digit],
10-
['1', repeat({ count: 2 }, digit)],
10+
['1', repeat(digit, 2)],
1111
['2', charRange('0', '4'), digit],
1212
['25', charRange('0', '5')]
1313
);
1414

1515
// Match
1616
const regex = buildRegExp([
1717
startOfString, //
18-
repeat([octet, '.'], { count: 3 }),
18+
repeat([octet, '.'], 3),
1919
octet,
2020
endOfString,
2121
]);

src/__tests__/examples.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ test('example: IPv4 address validator', () => {
1212
const octet = choiceOf(
1313
[digit],
1414
[charRange('1', '9'), digit],
15-
['1', repeat(digit, { count: 2 })],
15+
['1', repeat(digit, 2)],
1616
['2', charRange('0', '4'), digit],
1717
['25', charRange('0', '5')],
1818
);
1919

2020
const regex = buildRegExp([
2121
startOfString, //
22-
repeat([octet, '.'], { count: 3 }),
22+
repeat([octet, '.'], 3),
2323
octet,
2424
endOfString,
2525
]);

src/constructs/__tests__/choice-of.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test('`choiceOf` with sequence options', () => {
2929

3030
test('`choiceOf` using nested regex', () => {
3131
expect(choiceOf(oneOrMore('a'), zeroOrMore('b'))).toHavePattern(/a+|b*/);
32-
expect(choiceOf(repeat('a', { min: 1, max: 3 }), repeat('bx', { count: 5 }))).toHavePattern(
32+
expect(choiceOf(repeat('a', { min: 1, max: 3 }), repeat('bx', 5))).toHavePattern(
3333
/a{1,3}|(?:bx){5}/,
3434
);
3535
});

src/constructs/__tests__/repeat.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import { repeat } from '../repeat';
55
test('`repeat` quantifier', () => {
66
expect(['a', repeat('b', { min: 1, max: 5 })]).toHavePattern(/ab{1,5}/);
77
expect(['a', repeat('b', { min: 1 })]).toHavePattern(/ab{1,}/);
8-
expect(['a', repeat('b', { count: 1 })]).toHavePattern(/ab{1}/);
8+
expect(['a', repeat('b', 1)]).toHavePattern(/ab{1}/);
99

10-
expect(['a', repeat(['a', zeroOrMore('b')], { count: 1 })]).toHavePattern(/a(?:ab*){1}/);
11-
expect(repeat(['text', ' ', oneOrMore('d')], { count: 5 })).toHavePattern(/(?:text d+){5}/);
10+
expect(['a', repeat(['a', zeroOrMore('b')], 1)]).toHavePattern(/a(?:ab*){1}/);
11+
expect(repeat(['text', ' ', oneOrMore('d')], 5)).toHavePattern(/(?:text d+){5}/);
1212
});
1313

1414
test('`repeat` optimizes grouping for atoms', () => {
15-
expect(repeat(digit, { count: 2 })).toHavePattern(/\d{2}/);
15+
expect(repeat(digit, 2)).toHavePattern(/\d{2}/);
1616
expect(repeat(digit, { min: 2 })).toHavePattern(/\d{2,}/);
1717
expect(repeat(digit, { min: 1, max: 5 })).toHavePattern(/\d{1,5}/);
1818
});
1919

2020
test('`repeat` throws on no children', () => {
21-
expect(() => repeat([], { count: 1 })).toThrowErrorMatchingInlineSnapshot(
21+
expect(() => repeat([], 1)).toThrowErrorMatchingInlineSnapshot(
2222
`"\`repeat\` should receive at least one element"`,
2323
);
2424
});

src/constructs/repeat.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface Repeat extends RegexConstruct {
99
children: RegexElement[];
1010
}
1111

12-
export type RepeatOptions = { count: number } | { min: number; max?: number };
12+
export type RepeatOptions = number | { min: number; max?: number };
1313

1414
export function repeat(sequence: RegexSequence, options: RepeatOptions): Repeat {
1515
const children = ensureArray(sequence);
@@ -29,10 +29,10 @@ export function repeat(sequence: RegexSequence, options: RepeatOptions): Repeat
2929
function encodeRepeat(this: Repeat): EncodeResult {
3030
const atomicNodes = encodeAtom(this.children);
3131

32-
if ('count' in this.options) {
32+
if (typeof this.options === 'number') {
3333
return {
3434
precedence: 'sequence',
35-
pattern: `${atomicNodes.pattern}{${this.options.count}}`,
35+
pattern: `${atomicNodes.pattern}{${this.options}}`,
3636
};
3737
}
3838

0 commit comments

Comments
 (0)