Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: bundle size optimizations #100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/constructs/anchors.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { EncodedRegex } from '../types';

export const startOfString: EncodedRegex = {
precedence: 'atom',
type: 'atom',
pattern: '^',
};

export const endOfString: EncodedRegex = {
precedence: 'atom',
type: 'atom',
pattern: '$',
};

export const wordBoundary: EncodedRegex = {
precedence: 'atom',
type: 'atom',
pattern: '\\b',
};

export const nonWordBoundary: EncodedRegex = {
precedence: 'atom',
type: 'atom',
pattern: '\\B',
};

Expand Down
6 changes: 3 additions & 3 deletions src/constructs/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export function capture(sequence: RegexSequence, options?: CaptureOptions): Enco
const name = options?.name;
if (name) {
return {
precedence: 'atom',
type: 'atom',
pattern: `(?<${name}>${encode(sequence).pattern})`,
};
}

return {
precedence: 'atom',
type: 'atom',
pattern: `(${encode(sequence).pattern})`,
};
}
Expand All @@ -43,7 +43,7 @@ export function capture(sequence: RegexSequence, options?: CaptureOptions): Enco
*/
export function ref(name: string): Reference {
return {
precedence: 'atom',
type: 'atom',
pattern: `\\k<${name}>`,
name,
};
Expand Down
2 changes: 1 addition & 1 deletion src/constructs/char-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function encodeCharClass(
if (pattern === '[^-]') pattern = '[\\^-]';

return {
precedence: 'atom',
type: 'atom',
pattern,
};
}
14 changes: 7 additions & 7 deletions src/constructs/char-escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@ import type { CharacterEscape, EncodedRegex } from '../types';
* Specifically this one is NOT a character escape.
*/
export const any: EncodedRegex = {
precedence: 'atom',
type: 'atom',
pattern: '.',
};

export const digit: CharacterEscape = {
precedence: 'atom',
type: 'atom',
pattern: '\\d',
chars: ['\\d'],
};

export const nonDigit: CharacterEscape = {
precedence: 'atom',
type: 'atom',
pattern: '\\D',
chars: ['\\D'],
};

export const word: CharacterEscape = {
precedence: 'atom',
type: 'atom',
pattern: '\\w',
chars: ['\\w'],
};

export const nonWord: CharacterEscape = {
precedence: 'atom',
type: 'atom',
pattern: '\\W',
chars: ['\\W'],
};

export const whitespace: CharacterEscape = {
precedence: 'atom',
type: 'atom',
pattern: '\\s',
chars: ['\\s'],
};

export const nonWhitespace: CharacterEscape = {
precedence: 'atom',
type: 'atom',
pattern: '\\S',
chars: ['\\S'],
};
Expand Down
2 changes: 1 addition & 1 deletion src/constructs/choice-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function choiceOf(...alternatives: RegexSequence[]): EncodedRegex {
}

return {
precedence: 'disjunction',
type: 'disjunction',
pattern: encodedAlternatives.map((n) => n.pattern).join('|'),
};
}
2 changes: 1 addition & 1 deletion src/constructs/lookahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
*/
export function lookahead(sequence: RegexSequence): EncodedRegex {
return {
precedence: 'atom',
type: 'atom',
pattern: `(?=${encode(sequence).pattern})`,
};
}
2 changes: 1 addition & 1 deletion src/constructs/lookbehind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
*/
export function lookbehind(sequence: RegexSequence): EncodedRegex {
return {
precedence: 'atom',
type: 'atom',
pattern: `(?<=${encode(sequence).pattern})`,
};
}
2 changes: 1 addition & 1 deletion src/constructs/negative-lookahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
*/
export function negativeLookahead(sequence: RegexSequence): EncodedRegex {
return {
precedence: 'atom',
type: 'atom',
pattern: `(?!${encode(sequence).pattern})`,
};
}
2 changes: 1 addition & 1 deletion src/constructs/negative-lookbehind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
*/
export function negativeLookbehind(sequence: RegexSequence): EncodedRegex {
return {
precedence: 'atom',
type: 'atom',
pattern: `(?<!${encode(sequence).pattern})`,
};
}
6 changes: 3 additions & 3 deletions src/constructs/quantifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ export interface QuantifierOptions {
export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
precedence: 'sequence',
type: 'sequence',
pattern: `${encodeAtomic(elements)}*${options?.greedy === false ? '?' : ''}`,
};
}

export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
precedence: 'sequence',
type: 'sequence',
pattern: `${encodeAtomic(elements)}+${options?.greedy === false ? '?' : ''}`,
};
}

export function optional(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
precedence: 'sequence',
type: 'sequence',
pattern: `${encodeAtomic(elements)}?${options?.greedy === false ? '?' : ''}`,
};
}
4 changes: 2 additions & 2 deletions src/constructs/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export function repeat(sequence: RegexSequence, options: RepeatOptions): Encoded

if (typeof options === 'number') {
return {
precedence: 'sequence',
type: 'sequence',
pattern: `${encodeAtomic(elements)}{${options}}`,
};
}

return {
precedence: 'sequence',
type: 'sequence',
pattern: `${encodeAtomic(elements)}{${options.min},${options?.max ?? ''}}${
options.greedy === false ? '?' : ''
}`,
Expand Down
4 changes: 2 additions & 2 deletions src/constructs/unicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function unicodeChar(codePoint: number): CharacterEscape {
: `\\u{${codePoint.toString(16)}}`; // 1-6 digit hex (requires unicode-aware mode)

return {
precedence: 'atom',
type: 'atom',
pattern: escape,
chars: [escape],
};
Expand All @@ -50,7 +50,7 @@ export function unicodeProperty(property: string, value?: string): CharacterEsca
const escape = `\\p{${property}${value ? `=${value}` : ''}}`;

return {
precedence: 'atom',
type: 'atom',
pattern: escape,
chars: [escape],
};
Expand Down
12 changes: 5 additions & 7 deletions src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ export function encode(sequence: RegexSequence): EncodedRegex {
}

return {
precedence: 'sequence',
pattern: encoded
.map((n) => (n.precedence === 'disjunction' ? encodeAtomic(n) : n.pattern))
.join(''),
type: 'sequence',
pattern: encoded.map((n) => (n.type === 'disjunction' ? encodeAtomic(n) : n.pattern)).join(''),
};
}

export function encodeAtomic(sequence: RegexSequence): string {
const encoded = encode(sequence);
return encoded.precedence === 'atom' ? encoded.pattern : `(?:${encoded.pattern})`;
return encoded.type === 'atom' ? encoded.pattern : `(?:${encoded.pattern})`;
}

function encodeElement(element: RegexElement): EncodedRegex {
Expand Down Expand Up @@ -51,7 +49,7 @@ function encodeText(text: string): EncodedRegex {

return {
// Optimize for single character case
precedence: text.length === 1 ? 'atom' : 'sequence',
type: text.length === 1 ? 'atom' : 'sequence',
pattern: escapeText(text),
};
}
Expand All @@ -61,7 +59,7 @@ function encodeRegExp(regexp: RegExp): EncodedRegex {

return {
// Encode at safe precedence
precedence: isAtomicPattern(pattern) ? 'atom' : 'disjunction',
type: isAtomicPattern(pattern) ? 'atom' : 'disjunction',
pattern,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export type RegexConstruct = EncodedRegex | LazyEncodableRegex;
* Encoded regex pattern with information about its type (atom, sequence)
*/
export interface EncodedRegex {
precedence: EncodePrecedence;
type: RegexType;
pattern: string;
}

export type EncodePrecedence = 'atom' | 'sequence' | 'disjunction';
export type RegexType = 'atom' | 'sequence' | 'disjunction';

export interface CharacterEscape extends EncodedRegex {
// `CharacterClass` compatibility
Expand Down
Loading