Skip to content

Commit

Permalink
fix: sanitize createRegExp return type (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
didavid61202 committed Jan 29, 2023
1 parent 0b61f78 commit 5264256
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
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

1 comment on commit 5264256

@vercel
Copy link

@vercel vercel bot commented on 5264256 Jan 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.