Skip to content
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

feat: type-safe String.match #68

Merged
merged 3 commits into from
Sep 7, 2022
Merged

feat: type-safe String.match #68

merged 3 commits into from
Sep 7, 2022

Conversation

didavid61202
Copy link
Collaborator

Updates

  1. Add new generic CapturedGroupsArr to Input type for tracking captured grouped regex pattern

  2. Add GetCapturedGroupsArr and MapToCapturedGroupsArr utility types

  3. Update MagicRegExpMatchArray type and match function type for String to return typed array with corresponding type per each index.

  4. Add StringCaptureBy generic type for a clean way to express what regex pattern each match is captured by.

This PR should also resolve #24

This PR also can be enhanced by resolving all possible match strings as a union type when using generic StringCaptureBy,
ex. StringCaptureBy<'(?<group>foo|da?o)(?:bar|baz)\\k<group>'> is infer as 'foobazfoo' | 'foobarfoo' | 'dobazdo' | 'dobardo' | 'daobazdao' | 'daobardao' 🚀

Usage

const pattern = anyOf(
      exactly('foo|?').grouped(),
      exactly('bar').and(maybe('baz').grouped()).groupedAs('groupName'),
      exactly('boo').times(2).grouped(),
      anyOf(
        exactly('a').times(3),
        exactly('b').and(maybe('c|d?')).times.any(),
        exactly('1')
          .and(maybe(exactly('2').and(maybe('3')).and('2')))
          .and('1')
      ).grouped()
    ).grouped()

    const match = 'booboo'.match(createRegExp(pattern))

    if (!match) return expect(match).toBeTruthy()
    expectTypeOf(match.length).toEqualTypeOf<7>()
    expectTypeOf(match[0]).toEqualTypeOf<string | undefined>()
    expectTypeOf(match[1]).toEqualTypeOf<
      | StringCaptureBy<'((foo\\|\\?)|(?<groupName>bar(baz)?)|(boo){2}|(a{3}|(?:b(?:c\\|d\\?)?)*|1(?:23?2)?1))'>
      | undefined
    >()
    //@ts-expect-error
    match[1] = 'match result array marked as readonly'
    let typedVar: string | undefined
    // eslint-disable-next-line @typescript-eslint/no-unused-vars, prefer-const
    typedVar = match[1] // can be assign to typed variable
    expectTypeOf(match[2]).toEqualTypeOf<StringCaptureBy<'(foo\\|\\?)'> | undefined>()
    expectTypeOf(match[2]?.concat(match[3] || '')).toEqualTypeOf<string | undefined>()
    expectTypeOf(match[3]).toEqualTypeOf<StringCaptureBy<'(?<groupName>bar(baz)?)'> | undefined>()
    expectTypeOf(match[4]).toEqualTypeOf<StringCaptureBy<'(baz)'> | undefined>()
    expectTypeOf(match[5]).toEqualTypeOf<StringCaptureBy<'(boo)'> | undefined>()
    expectTypeOf(match[6]).toEqualTypeOf<
      StringCaptureBy<'(a{3}|(?:b(?:c\\|d\\?)?)*|1(?:23?2)?1)'> | undefined
    >()
    expectTypeOf(match[7]).toEqualTypeOf<never>()

Todos

  • add/update types
  • add/update tests

@vercel
Copy link

vercel bot commented Sep 1, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
magic-regexp ✅ Ready (Inspect) Visit Preview Sep 7, 2022 at 0:50AM (UTC)

@codecov-commenter
Copy link

codecov-commenter commented Sep 1, 2022

Codecov Report

Merging #68 (9bfb86a) into main (4afe083) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main       #68   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines          398       454   +56     
  Branches        82        82           
=========================================
+ Hits           398       454   +56     
Impacted Files Coverage Δ
src/core/inputs.ts 100.00% <100.00%> (ø)
src/core/internal.ts 100.00% <100.00%> (ø)
src/core/types/magic-regexp.ts 100.00% <100.00%> (ø)
src/index.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

Beautiful 😍

@danielroe danielroe changed the title feat: type safe match array access with refer hint feat: type-safe String.match Sep 7, 2022
@danielroe danielroe merged commit 1a2d5d4 into main Sep 7, 2022
@danielroe danielroe deleted the feat/type-safe-anon-group branch September 7, 2022 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

improvements to anonymous groups
3 participants