Skip to content

Commit 4bcc13c

Browse files
committed
chore: update based on the PR suggestions
1 parent ed8ab9e commit 4bcc13c

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed
+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { whitespace } from '../characterClasses';
1+
import { any, digit, whitespace, word } from '../characterClasses';
22
import { buildPattern } from '../compiler';
33
import { one } from '../quantifiers';
44

@@ -7,3 +7,21 @@ test('"whitespace" character class', () => {
77
expect(buildPattern(one('ab'), whitespace())).toEqual(`ab\\s`);
88
expect(buildPattern(one('ab'), whitespace(), one('c'))).toEqual(`ab\\sc`);
99
});
10+
11+
test('"digit" character class', () => {
12+
expect(buildPattern(digit())).toEqual(`\\d`);
13+
expect(buildPattern(one('ab'), digit())).toEqual(`ab\\d`);
14+
expect(buildPattern(one('ab'), digit(), one('c'))).toEqual(`ab\\dc`);
15+
});
16+
17+
test('"word" character class', () => {
18+
expect(buildPattern(word())).toEqual(`\\w`);
19+
expect(buildPattern(one('ab'), word())).toEqual(`ab\\w`);
20+
expect(buildPattern(one('ab'), word(), one('c'))).toEqual(`ab\\wc`);
21+
});
22+
23+
test('"any" character class', () => {
24+
expect(buildPattern(any())).toEqual(`.`);
25+
expect(buildPattern(one('ab'), any())).toEqual(`ab.`);
26+
expect(buildPattern(one('ab'), any(), one('c'))).toEqual(`ab.c`);
27+
});

src/characterClasses.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
import type { Whitespace } from './types';
1+
import type { Any, Digit, Whitespace, Word } from './types';
22

33
export function whitespace(): Whitespace {
4-
return `\\s`;
4+
return { type: 'whitespace' };
55
}
6+
7+
export function digit(): Digit {
8+
return { type: 'digit' };
9+
}
10+
11+
export function word(): Word {
12+
return { type: 'word' };
13+
}
14+
15+
export function any(): Any {
16+
return { type: 'any' };
17+
}
18+
19+
export const compilers = {
20+
whitespace: '\\s',
21+
digit: '\\d',
22+
word: '\\w',
23+
any: '.',
24+
};

src/compiler.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RegexElement } from './types';
22
import { compilers as quantifiers } from './quantifiers';
3+
import { compilers as characterClasses } from './characterClasses';
34

45
/**
56
* Generate RegExp object for elements.
@@ -32,11 +33,15 @@ function compileSingle(elements: RegexElement): string {
3233
return elements;
3334
}
3435

35-
const elementCompiler = quantifiers[elements.type];
36-
if (!elementCompiler) {
37-
throw new Error(`Unknown elements type ${elements.type}`);
36+
if ('children' in elements) {
37+
const elementCompiler = quantifiers[elements.type];
38+
if (!elementCompiler) {
39+
throw new Error(`Unknown elements type ${elements.type}`);
40+
}
41+
42+
const children = compileList(elements.children);
43+
return elementCompiler(children);
3844
}
3945

40-
const children = compileList(elements.children);
41-
return elementCompiler(children);
46+
return characterClasses[elements.type];
4247
}

src/types.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
export type RegexElement = string | RegexCharacterClass | RegexQuantifier;
22

3-
export type RegexCharacterClass = Whitespace;
3+
export type RegexCharacterClass = Whitespace | Digit | Word | Any;
44

55
export type RegexQuantifier = One | OneOrMore | Optionally | ZeroOrMore;
66

77
// Character classes
8-
export type Whitespace = `\\s`;
8+
export type Whitespace = { type: 'whitespace' };
9+
10+
export type Digit = { type: 'digit' };
11+
12+
export type Word = { type: 'word' };
13+
14+
export type Any = { type: 'any' };
915

1016
// Quantifiers
1117
export type One = {

0 commit comments

Comments
 (0)