Skip to content

Commit f52b3b1

Browse files
committed
refactor: minify types
1 parent d218336 commit f52b3b1

15 files changed

+81
-81
lines changed

src/builders.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { encode } from './encoder';
99
* @returns RegExp object
1010
*/
1111
export function buildRegExp(sequence: RegexSequence, flags?: RegexFlags): RegExp {
12-
const pattern = encode(sequence).pattern;
12+
const pattern = encode(sequence).p;
1313
ensureUnicodeFlagIfNeeded(pattern, flags);
1414

1515
const flagsString = encodeFlags(flags ?? {});
@@ -22,7 +22,7 @@ export function buildRegExp(sequence: RegexSequence, flags?: RegexFlags): RegExp
2222
* @returns regex pattern string
2323
*/
2424
export function buildPattern(sequence: RegexSequence): string {
25-
return encode(sequence).pattern;
25+
return encode(sequence).p;
2626
}
2727

2828
function encodeFlags(flags: RegexFlags): string {

src/constructs/anchors.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ import type { EncodedRegex } from '../types';
44
* Start of string anchor. Matches the start of of string. In `multiline` mode, also matches immediately following a newline.
55
*/
66
export const startOfString: EncodedRegex = {
7-
precedence: 'atom',
8-
pattern: '^',
7+
k: 'atom',
8+
p: '^',
99
};
1010

1111
/**
1212
* End of string anchor. Matches the end of a string. In `multiline` mode, also matches immediately preceding a newline.
1313
*/
1414
export const endOfString: EncodedRegex = {
15-
precedence: 'atom',
16-
pattern: '$',
15+
k: 'atom',
16+
p: '$',
1717
};
1818

1919
/**
2020
* Word boundary anchor. Matches the position where one side is a word character (alphanumeric or underscore) and the other side is a non-word character (anything else).
2121
*/
2222
export const wordBoundary: EncodedRegex = {
23-
precedence: 'atom',
24-
pattern: '\\b',
23+
k: 'atom',
24+
p: '\\b',
2525
};
2626

2727
/**
2828
* Non-word boundary anchor. Matches the position where both sides are word characters.
2929
*/
3030
export const nonWordBoundary: EncodedRegex = {
31-
precedence: 'atom',
32-
pattern: '\\B',
31+
k: 'atom',
32+
p: '\\B',
3333
};
3434

3535
/**

src/constructs/capture.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ export function capture(sequence: RegexSequence, options?: CaptureOptions): Enco
2121
const name = options?.name;
2222
if (name) {
2323
return {
24-
precedence: 'atom',
25-
pattern: `(?<${name}>${encode(sequence).pattern})`,
24+
k: 'atom',
25+
p: `(?<${name}>${encode(sequence).p})`,
2626
};
2727
}
2828

2929
return {
30-
precedence: 'atom',
31-
pattern: `(${encode(sequence).pattern})`,
30+
k: 'atom',
31+
p: `(${encode(sequence).p})`,
3232
};
3333
}
3434

@@ -43,8 +43,8 @@ export function capture(sequence: RegexSequence, options?: CaptureOptions): Enco
4343
*/
4444
export function ref(name: string): Reference {
4545
return {
46-
precedence: 'atom',
47-
pattern: `\\k<${name}>`,
46+
k: 'atom',
47+
p: `\\k<${name}>`,
4848
name,
4949
};
5050
}

src/constructs/char-class.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function charClass(...elements: Array<CharacterClass | CharacterEscape>):
1313
}
1414

1515
return {
16-
elements: elements.map((c) => c.elements).flat(),
16+
e: elements.map((c) => c.e).flat(),
1717
encode: encodeCharClass,
1818
};
1919
}
@@ -35,7 +35,7 @@ export function charRange(start: string, end: string): CharacterClass {
3535
}
3636

3737
return {
38-
elements: [`${start}-${end}`],
38+
e: [`${start}-${end}`],
3939
encode: encodeCharClass,
4040
};
4141
}
@@ -50,7 +50,7 @@ export function anyOf(chars: string): CharacterClass {
5050
ensureText(chars);
5151

5252
return {
53-
elements: chars.split('').map(escapeChar),
53+
e: chars.split('').map(escapeChar),
5454
encode: encodeCharClass,
5555
};
5656
}
@@ -81,7 +81,7 @@ function encodeCharClass(
8181
isNegated?: boolean,
8282
): EncodedRegex {
8383
return {
84-
precedence: 'atom',
85-
pattern: `[${isNegated ? '^' : ''}${this.elements.join('')}]`,
84+
k: 'atom',
85+
p: `[${isNegated ? '^' : ''}${this.e.join('')}]`,
8686
};
8787
}

src/constructs/char-escape.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,62 @@ import type { CharacterEscape, EncodedRegex } from '../types';
55
* Specifically this one is NOT a character escape.
66
*/
77
export const any: EncodedRegex = {
8-
precedence: 'atom',
9-
pattern: '.',
8+
k: 'atom',
9+
p: '.',
1010
};
1111

1212
/**
1313
* Matches any digit (0-9).
1414
*/
1515
export const digit: CharacterEscape = {
16-
precedence: 'atom',
17-
pattern: '\\d',
18-
elements: ['\\d'],
16+
k: 'atom',
17+
p: '\\d',
18+
e: ['\\d'],
1919
};
2020

2121
/**
2222
* Matches any non-digit (0-9) character.
2323
*/
2424
export const nonDigit: CharacterEscape = {
25-
precedence: 'atom',
26-
pattern: '\\D',
27-
elements: ['\\D'],
25+
k: 'atom',
26+
p: '\\D',
27+
e: ['\\D'],
2828
};
2929

3030
/**
3131
* Matches any word character (alphanumeric or underscore).
3232
*/
3333
export const word: CharacterEscape = {
34-
precedence: 'atom',
35-
pattern: '\\w',
36-
elements: ['\\w'],
34+
k: 'atom',
35+
p: '\\w',
36+
e: ['\\w'],
3737
};
3838

3939
/**
4040
* Matches any non-word (alphanumeric or underscore) character.
4141
*/
4242
export const nonWord: CharacterEscape = {
43-
precedence: 'atom',
44-
pattern: '\\W',
45-
elements: ['\\W'],
43+
k: 'atom',
44+
p: '\\W',
45+
e: ['\\W'],
4646
};
4747

4848
/**
4949
* Matches any whitespace character (space, tab, newline, etc.).
5050
*/
5151
export const whitespace: CharacterEscape = {
52-
precedence: 'atom',
53-
pattern: '\\s',
54-
elements: ['\\s'],
52+
k: 'atom',
53+
p: '\\s',
54+
e: ['\\s'],
5555
};
5656

5757
/**
5858
* Matches any non-whitespace (space, tab, newline, etc.) character.
5959
*/
6060
export const nonWhitespace: CharacterEscape = {
61-
precedence: 'atom',
62-
pattern: '\\S',
63-
elements: ['\\S'],
61+
k: 'atom',
62+
p: '\\S',
63+
e: ['\\S'],
6464
};
6565

6666
/**

src/constructs/choice-of.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function choiceOf(...alternatives: RegexSequence[]): EncodedRegex {
1818
}
1919

2020
return {
21-
precedence: 'disjunction',
22-
pattern: encodedAlternatives.map((n) => n.pattern).join('|'),
21+
k: 'disjunction',
22+
p: encodedAlternatives.map((n) => n.p).join('|'),
2323
};
2424
}

src/constructs/lookahead.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
1717
*/
1818
export function lookahead(sequence: RegexSequence): EncodedRegex {
1919
return {
20-
precedence: 'atom',
21-
pattern: `(?=${encode(sequence).pattern})`,
20+
k: 'atom',
21+
p: `(?=${encode(sequence).p})`,
2222
};
2323
}

src/constructs/lookbehind.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
1717
*/
1818
export function lookbehind(sequence: RegexSequence): EncodedRegex {
1919
return {
20-
precedence: 'atom',
21-
pattern: `(?<=${encode(sequence).pattern})`,
20+
k: 'atom',
21+
p: `(?<=${encode(sequence).p})`,
2222
};
2323
}

src/constructs/negative-lookahead.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
1717
*/
1818
export function negativeLookahead(sequence: RegexSequence): EncodedRegex {
1919
return {
20-
precedence: 'atom',
21-
pattern: `(?!${encode(sequence).pattern})`,
20+
k: 'atom',
21+
p: `(?!${encode(sequence).p})`,
2222
};
2323
}

src/constructs/negative-lookbehind.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
1717
*/
1818
export function negativeLookbehind(sequence: RegexSequence): EncodedRegex {
1919
return {
20-
precedence: 'atom',
21-
pattern: `(?<!${encode(sequence).pattern})`,
20+
k: 'atom',
21+
p: `(?<!${encode(sequence).p})`,
2222
};
2323
}

src/constructs/quantifiers.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export interface QuantifierOptions {
1515
export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
1616
const elements = ensureElements(sequence);
1717
return {
18-
precedence: 'sequence',
19-
pattern: `${encodeAtomic(elements)}*${options?.greedy === false ? '?' : ''}`,
18+
k: 'sequence',
19+
p: `${encodeAtomic(elements)}*${options?.greedy === false ? '?' : ''}`,
2020
};
2121
}
2222

@@ -29,8 +29,8 @@ export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions)
2929
export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
3030
const elements = ensureElements(sequence);
3131
return {
32-
precedence: 'sequence',
33-
pattern: `${encodeAtomic(elements)}+${options?.greedy === false ? '?' : ''}`,
32+
k: 'sequence',
33+
p: `${encodeAtomic(elements)}+${options?.greedy === false ? '?' : ''}`,
3434
};
3535
}
3636

@@ -43,7 +43,7 @@ export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions):
4343
export function optional(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
4444
const elements = ensureElements(sequence);
4545
return {
46-
precedence: 'sequence',
47-
pattern: `${encodeAtomic(elements)}?${options?.greedy === false ? '?' : ''}`,
46+
k: 'sequence',
47+
p: `${encodeAtomic(elements)}?${options?.greedy === false ? '?' : ''}`,
4848
};
4949
}

src/constructs/repeat.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ export function repeat(sequence: RegexSequence, options: RepeatOptions): Encoded
2222

2323
if (typeof options === 'number') {
2424
return {
25-
precedence: 'sequence',
26-
pattern: `${encodeAtomic(elements)}{${options}}`,
25+
k: 'sequence',
26+
p: `${encodeAtomic(elements)}{${options}}`,
2727
};
2828
}
2929

3030
return {
31-
precedence: 'sequence',
32-
pattern: `${encodeAtomic(elements)}{${options.min},${options?.max ?? ''}}${
31+
k: 'sequence',
32+
p: `${encodeAtomic(elements)}{${options.min},${options?.max ?? ''}}${
3333
options.greedy === false ? '?' : ''
3434
}`,
3535
};

src/constructs/unicode.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export function unicodeChar(codePoint: number): CharacterEscape {
2323
: `\\u{${codePoint.toString(16)}}`; // 1-6 digit hex (requires unicode-aware mode)
2424

2525
return {
26-
precedence: 'atom',
27-
pattern: escape,
28-
elements: [escape],
26+
k: 'atom',
27+
p: escape,
28+
e: [escape],
2929
};
3030
}
3131

@@ -50,8 +50,8 @@ export function unicodeProperty(property: string, value?: string): CharacterEsca
5050
const escape = `\\p{${property}${value ? `=${value}` : ''}}`;
5151

5252
return {
53-
precedence: 'atom',
54-
pattern: escape,
55-
elements: [escape],
53+
k: 'atom',
54+
p: escape,
55+
e: [escape],
5656
};
5757
}

src/encoder.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ export function encode(sequence: RegexSequence): EncodedRegex {
1010
}
1111

1212
return {
13-
precedence: 'sequence',
14-
pattern: encoded
15-
.map((n) => (n.precedence === 'disjunction' ? encodeAtomic(n) : n.pattern))
13+
k: 'sequence',
14+
p: encoded
15+
.map((n) => (n.k === 'disjunction' ? encodeAtomic(n) : n.p))
1616
.join(''),
1717
};
1818
}
1919

2020
export function encodeAtomic(sequence: RegexSequence): string {
2121
const encoded = encode(sequence);
22-
return encoded.precedence === 'atom' ? encoded.pattern : `(?:${encoded.pattern})`;
22+
return encoded.k === 'atom' ? encoded.p : `(?:${encoded.p})`;
2323
}
2424

2525
function encodeElement(element: RegexElement): EncodedRegex {
@@ -33,7 +33,7 @@ function encodeElement(element: RegexElement): EncodedRegex {
3333

3434
if (typeof element === 'object') {
3535
// EncodedRegex
36-
if ('pattern' in element) {
36+
if ('p' in element) {
3737
return element;
3838
}
3939

@@ -51,18 +51,18 @@ function encodeText(text: string): EncodedRegex {
5151

5252
return {
5353
// Optimize for single character case
54-
precedence: text.length === 1 ? 'atom' : 'sequence',
55-
pattern: escapeText(text),
54+
k: text.length === 1 ? 'atom' : 'sequence',
55+
p: escapeText(text),
5656
};
5757
}
5858

5959
function encodeRegExp(regexp: RegExp): EncodedRegex {
60-
const pattern = regexp.source;
60+
const p = regexp.source;
6161

6262
return {
6363
// Encode at safe precedence
64-
precedence: isAtomicPattern(pattern) ? 'atom' : 'disjunction',
65-
pattern,
64+
k: isAtomicPattern(p) ? 'atom' : 'disjunction',
65+
p,
6666
};
6767
}
6868

0 commit comments

Comments
 (0)