diff --git a/src/index.ts b/src/index.ts index f3490973..a57fb2f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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>(regexp: R): MagicRegExpMatchArray | null + match>>( + regexp: R + ): MagicRegExpMatchArray | null + match>(regexp: R): string[] | null + + /** @deprecated String.matchAll requires global flag to be set. */ + matchAll>(regexp: R): never + /** @deprecated String.matchAll requires global flag to be set. */ + matchAll>>(regexp: R): never matchAll>( regexp: R ): IterableIterator> + + /** @deprecated String.replaceAll requires global flag to be set. */ + replaceAll>( + searchValue: R, + replaceValue: string | ((substring: string, ...args: any[]) => string) + ): never + /** @deprecated String.replaceAll requires global flag to be set. */ + replaceAll>>( + searchValue: R, + replaceValue: string | ((substring: string, ...args: any[]) => string) + ): never } } diff --git a/test/augments.test.ts b/test/augments.test.ts index 3b0bb47b..1ca5e92b 100644 --- a/test/augments.test.ts +++ b/test/augments.test.ts @@ -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() + expectTypeOf(result).toEqualTypeOf() }) 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() + expectTypeOf('test'.matchAll(createRegExp(char.as('foo'), ['m']))).toEqualTypeOf() }) 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() + count++ + expect([...'test'].includes(result?.groups.foo || '')).toBeTruthy() + expectTypeOf(result).toEqualTypeOf< + MagicRegExpMatchArray.)/g', 'foo', 'g'>> + >() } + expect(count).toBe(4) + }) + it.todo('.replaceAll non-global', () => { + // should be deprecated + expectTypeOf('test'.replaceAll(createRegExp(char.as('foo')), '')).toEqualTypeOf() + expectTypeOf('test'.replaceAll(createRegExp(char.as('foo'), ['m']), '')).toEqualTypeOf() }) })