unjs / magic-regexp

A compiled-away, type-safe, readable RegExp alternative
https://regexp.dev
MIT License
3.82k stars 53 forks source link

feat: experimental type-level RegExp match #288

Closed didavid61202 closed 1 year ago

didavid61202 commented 1 year ago

Update

Add type-level-regexp as dependency, and create a new subpath export type-level-regexp for end-users to try out this experimental features by importing magic-regexp helpers from magic-regexp/type-level-regexp.

this experimental features utilize generic types import from type-level-regexp to enhance the current MagicRegExp regexp object and return fully typed matched result when passing it to literal string's match, matchAll and replace functions.

for detail example please see test/type-level-regexp.test.ts

Usage

original semver example

const regExp = createRegExp(
      exactly('bar')
        .or('baz')
        .groupedAs('g1')
        .and(exactly('qux').and(digit.times(2)).groupedAs('g2')),
      new Set([caseInsensitive])
    )

    const replaceFunctionResult = 'prefix_babAzqUx42_suffix'.replace(
    //     ^? 'prefix_ba_capture-1: bAz, offset: 9, groups: { g1: bAz, g2: qUx42 }_suffix'
      regExp,
      (match, c1, c2, offset, input, groups) => // all arguments are typed
        `_capture-1: ${c1}, offset: ${offset}, groups: { g1: ${groups.g1}, g2: ${groups.g2} }`
    )

Related issues

233

235

Tasks

vercel[bot] commented 1 year ago

Someone is attempting to deploy a commit to a Personal Account owned by @danielroe on Vercel.

@danielroe first needs to authorize it.

what-the-diff[bot] commented 1 year ago

PR Summary

codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (0ff5bcf) 100.00% compared to head (446ce19) 100.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #288 +/- ## ========================================== Coverage 100.00% 100.00% ========================================== Files 7 8 +1 Lines 533 671 +138 Branches 60 65 +5 ========================================== + Hits 533 671 +138 ``` | [Impacted Files](https://app.codecov.io/gh/danielroe/magic-regexp/pull/288?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Roe) | Coverage Δ | | |---|---|---| | [src/further-magic.ts](https://app.codecov.io/gh/danielroe/magic-regexp/pull/288?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Roe#diff-c3JjL2Z1cnRoZXItbWFnaWMudHM=) | `100.00% <100.00%> (ø)` | | | [src/index.ts](https://app.codecov.io/gh/danielroe/magic-regexp/pull/288?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Roe#diff-c3JjL2luZGV4LnRz) | `100.00% <100.00%> (ø)` | | | [src/transform.ts](https://app.codecov.io/gh/danielroe/magic-regexp/pull/288?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Roe#diff-c3JjL3RyYW5zZm9ybS50cw==) | `100.00% <100.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

didavid61202 commented 1 year ago

Hi @danielroe,

I've rebase and update the createRegExp function in the subpath export type-level-regexp to variadic functions.

Also update two small typo in docs and JSDoc.

please review and check if it needs more updates or fix when you have some free time 😊

vercel[bot] commented 1 year ago

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

Name Status Preview Comments Updated (UTC)
magic-regexp ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 5, 2023 9:45am
danielroe commented 1 year ago

Sorry for the long time to merge! This is fantastic and can't wait to start using it.