Skip to content
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
2 changes: 1 addition & 1 deletion src/core/types/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type EscapeChar<T extends string> = Escape<T, '\\' | '^' | '-' | ']'>
export type StripEscapes<T extends string> = T extends `${infer A}\\${infer B}` ? `${A}${B}` : T

// prettier-ignore
type ExactEscapeChar = '.' | '*' | '+' | '?' | '^' | '$' | '{' | '}' | '(' | ')' | '|' | '[' | ']' | '/'
export type ExactEscapeChar = '.' | '*' | '+' | '?' | '^' | '$' | '{' | '}' | '(' | ')' | '|' | '[' | ']' | '/'

export type GetValue<T extends InputSource> = T extends string
? Escape<T, ExactEscapeChar>
Expand Down
36 changes: 23 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ import { Input, exactly } from './core/inputs'
import type { Join } from './core/types/join'
import type { MagicRegExp, MagicRegExpMatchArray } from './core/types/magic-regexp'

export const createRegExp = <
Value extends string,
NamedGroups extends string = never,
CapturedGroupsArr extends (string | undefined)[] = [],
Flags extends Flag[] = never[]
>(
raw: Input<Value, NamedGroups, CapturedGroupsArr> | Value,
flags?: [...Flags] | string | Set<Flag>
) =>
new RegExp(exactly(raw).toString(), [...(flags || '')].join('')) as MagicRegExp<
`/${Value}/${Join<Flags, '', ''>}`,
NamedGroups,
CapturedGroupsArr,
import type { Escape, ExactEscapeChar } from './core/types/escape'

export const createRegExp: {
/** Create Magic RegExp from Input helper */
<
Value extends string,
NamedGroups extends string = never,
CapturedGroupsArr extends (string | undefined)[] = [],
Flags extends Flag[] = never[]
>(
raw: Input<Value, NamedGroups, CapturedGroupsArr>,
flags?: [...Flags] | string | Set<Flag>
): MagicRegExp<`/${Value}/${Join<Flags, '', ''>}`, NamedGroups, CapturedGroupsArr, Flags[number]>
/** Create Magic RegExp from string, string will be sanitized */
<Value extends string, Flags extends Flag[] = never[]>(
raw: Value,
flags?: [...Flags] | string | Set<Flag>
): MagicRegExp<
`/${Escape<Value, ExactEscapeChar>}/${Join<Flags, '', ''>}`,
never,
[],
Flags[number]
>
} = (raw: any, flags?: any) =>
new RegExp(exactly(raw).toString(), [...(flags || '')].join('')) as any

export * from './core/flags'
export * from './core/inputs'
Expand Down
12 changes: 11 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ describe('magic-regexp', () => {
})
it('collects flag type', () => {
const re = createRegExp('.', [global, multiline])
expectTypeOf(re).toEqualTypeOf<MagicRegExp<'/./gm', never, [], 'g' | 'm'>>()
expectTypeOf(re).toEqualTypeOf<MagicRegExp<'/\\./gm', never, [], 'g' | 'm'>>()
})
it('sanitize string input', () => {
const escapeChars = '.*+?^${}()[]/'
const re = createRegExp(escapeChars)
expect(String(re)).toMatchInlineSnapshot(
'"/\\\\.\\\\*\\\\+\\\\?\\\\^\\\\$\\\\{\\\\}\\\\(\\\\)\\\\[\\\\]\\\\//"'
)
expectTypeOf(re).toEqualTypeOf<
MagicRegExp<'/\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\[\\]\\//', never, []>
>()
})
})

Expand Down