Skip to content

Commit

Permalink
chore: simplify regex flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Sep 23, 2024
1 parent d218336 commit 2a33b15
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a33b15

Please sign in to comment.