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: simplify regex flags #103

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 14 additions & 11 deletions src/builders.ts
Original file line number Diff line number Diff line change
@@ -25,18 +25,21 @@ export function buildPattern(sequence: RegexSequence): string {
return encode(sequence).pattern;
}

function encodeFlags(flags: RegexFlags): string {
let result = '';

if (flags.global) result += 'g';
if (flags.ignoreCase) result += 'i';
if (flags.multiline) result += 'm';
if (flags.hasIndices) result += 'd';
if (flags.dotAll) result += 's';
if (flags.sticky) result += 'y';
if (flags.unicode) result += 'u';
const flagsMap: Record<keyof RegexFlags, string> = {
global: 'g',
ignoreCase: 'i',
multiline: 'm',
hasIndices: 'd',
dotAll: 's',
sticky: 'y',
unicode: 'u',
};

return result;
function encodeFlags(flags: RegexFlags): string {
return Object.entries(flags)
.filter(([, value]) => value)
.map(([key]) => flagsMap[key as keyof RegexFlags])
.join('');
}

// Matches unicode mode patterns: \u{...}, \p{...}, \P{...}, but avoids valid \\u{...}, etc