Skip to content

feat: support core regex flags #26

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

Merged
merged 2 commits into from
Dec 18, 2023
Merged
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
25 changes: 25 additions & 0 deletions src/__tests__/builder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { buildRegex } from '../builders';

test('"regexBuilder" flags', () => {
expect(buildRegex('a').flags).toBe('');
expect(buildRegex({}, 'a').flags).toBe('');

expect(buildRegex({ global: true }, 'a').flags).toBe('g');
expect(buildRegex({ global: false }, 'a').flags).toBe('');

expect(buildRegex({ ignoreCase: true }, 'a').flags).toBe('i');
expect(buildRegex({ ignoreCase: false }, 'a').flags).toBe('');

expect(buildRegex({ multiline: true }, 'a').flags).toBe('m');
expect(buildRegex({ multiline: false }, 'a').flags).toBe('');

expect(buildRegex({ hasIndices: true }, 'a').flags).toBe('d');
expect(buildRegex({ hasIndices: false }, 'a').flags).toBe('');

expect(buildRegex({ sticky: true }, 'a').flags).toBe('y');
expect(buildRegex({ sticky: false }, 'a').flags).toBe('');

expect(
buildRegex({ global: true, ignoreCase: true, multiline: false }, 'a').flags
).toBe('gi');
});
54 changes: 51 additions & 3 deletions src/builders.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import type { RegexElement } from './components/types';
import { encodeSequence } from './encoder/encoder';
import { isRegexElement } from './utils';

export interface RegexFlags {
/** Global search. */
global?: boolean;
/** Case-insensitive search. */
ignoreCase?: boolean;
/** Allows ^ and $ to match newline characters. */
multiline?: boolean;
/** Generate indices for substring matches. */
hasIndices?: boolean;
/** Perform a "sticky" search that matches starting at the current position in the target string. */
sticky?: boolean;
}

/**
* Generate RegExp object for elements.
*
* @param elements
* @returns
*/
export function buildRegex(...elements: Array<RegexElement | string>): RegExp {
const pattern = encodeSequence(elements).pattern;
return new RegExp(pattern);
export function buildRegex(...elements: Array<RegexElement | string>): RegExp;
export function buildRegex(
flags: RegexFlags,
...elements: Array<RegexElement | string>
): RegExp;
export function buildRegex(
first: RegexFlags | RegexElement | string,
...rest: Array<RegexElement | string>
): RegExp {
if (typeof first === 'string' || isRegexElement(first)) {
return buildRegex({}, first, ...rest);
}

const pattern = encodeSequence(rest).pattern;
const flags = encodeFlags(first);
return new RegExp(pattern, flags);
}

/**
Expand All @@ -22,3 +49,24 @@ export function buildPattern(
): string {
return encodeSequence(elements).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.sticky) {
result += 'y';
}

return result;
}
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { RegexElement } from './components/types';
import { type EncoderNode, EncoderPrecedence } from './encoder/types';

/**
Expand Down Expand Up @@ -29,6 +30,10 @@ export function concatNodes(nodes: EncoderNode[]): EncoderNode {
};
}

export function isRegexElement(element: unknown): element is RegexElement {
return typeof element === 'object' && element !== null && 'type' in element;
}

// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping
export function escapeText(text: string) {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
Expand Down