Skip to content

Commit

Permalink
feat(types): respect global flag for .match and .matchAll types (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 24, 2022
1 parent 545d725 commit 2211a83
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,29 @@ export * from './core/types/magic-regexp'
// Add additional overload to global String object types to allow for typed capturing groups
declare global {
interface String {
match<R extends MagicRegExp<string, string, string>>(regexp: R): MagicRegExpMatchArray<R> | null
match<R extends MagicRegExp<string, string, Exclude<Flag, 'g'>>>(
regexp: R
): MagicRegExpMatchArray<R> | null
match<R extends MagicRegExp<string, string, 'g'>>(regexp: R): string[] | null

/** @deprecated String.matchAll requires global flag to be set. */
matchAll<R extends MagicRegExp<string, string, never>>(regexp: R): never
/** @deprecated String.matchAll requires global flag to be set. */
matchAll<R extends MagicRegExp<string, string, Exclude<Flag, 'g'>>>(regexp: R): never

matchAll<R extends MagicRegExp<string, string, string>>(
regexp: R
): IterableIterator<MagicRegExpMatchArray<R>>

/** @deprecated String.replaceAll requires global flag to be set. */
replaceAll<R extends MagicRegExp<string, string, never>>(
searchValue: R,
replaceValue: string | ((substring: string, ...args: any[]) => string)
): never
/** @deprecated String.replaceAll requires global flag to be set. */
replaceAll<R extends MagicRegExp<string, string, Exclude<Flag, 'g'>>>(
searchValue: R,
replaceValue: string | ((substring: string, ...args: any[]) => string)
): never
}
}
25 changes: 17 additions & 8 deletions test/augments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,30 @@ describe('String', () => {
it('.match global', () => {
const result = 'test'.match(createRegExp(char.as('foo'), [global]))
expect(Array.isArray(result)).toBeTruthy()
// @ts-expect-error
expect(result?.groups).toBeUndefined()
// TODO: https://github.com/danielroe/magic-regexp/issues/26
// expectTypeOf(result).toEqualTypeOf<null | string[]>()
expectTypeOf(result).toEqualTypeOf<null | string[]>()
})
it.todo('.matchAll non-global', () => {
// TODO: @ts-expect-error
'test'.matchAll(createRegExp(char.as('foo')))
// should be deprecated
expectTypeOf('test'.matchAll(createRegExp(char.as('foo')))).toEqualTypeOf<never>()
expectTypeOf('test'.matchAll(createRegExp(char.as('foo'), ['m']))).toEqualTypeOf<never>()
})
it('.matchAll global', () => {
const results = 'test'.matchAll(createRegExp(char.as('foo'), [global]))
expect(Array.isArray([...results])).toBeTruthy()
let count = 0
for (const result of results) {
expect(result?.groups).toBeUndefined()
// TODO: https://github.com/danielroe/magic-regexp/issues/26
// expectTypeOf(result).toEqualTypeOf<null | string[]>()
count++
expect([...'test'].includes(result?.groups.foo || '')).toBeTruthy()
expectTypeOf(result).toEqualTypeOf<
MagicRegExpMatchArray<MagicRegExp<'/(?<foo>.)/g', 'foo', 'g'>>
>()
}
expect(count).toBe(4)
})
it.todo('.replaceAll non-global', () => {
// should be deprecated
expectTypeOf('test'.replaceAll(createRegExp(char.as('foo')), '')).toEqualTypeOf<never>()
expectTypeOf('test'.replaceAll(createRegExp(char.as('foo'), ['m']), '')).toEqualTypeOf<never>()
})
})

1 comment on commit 2211a83

@vercel
Copy link

@vercel vercel bot commented on 2211a83 Jul 24, 2022

Choose a reason for hiding this comment

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

Please # to comment.