-
Notifications
You must be signed in to change notification settings - Fork 3
Feature: allow type-only processing of capture groups #18
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
Conversation
Having a type-safe function is amazing and all, but sometimes all you need is just the types.
|
I'll give this a look at the weekends. Thank you very much for your effort already! |
|
So, I just gave this a look and I really like the idea of exporting the
All of these are just ideas. I'm happy with the PR as is and can merge it already. Just leave a comment on what you think about these points and I can implement the changes accordingly. |
|
Thanks for the detailed feedback and suggestions! You raise good points.
Whenever libraries forward captures to the user from the regex, it is usually done for the sake of convenience: the regular expression acts as a makeshift parser of sorts, and the user defines the tokens that need to be extracted from capture groups to use in some other context, as the original input string is discarded. Therefore, the following pattern emerges in many libraries' code, where the first match is omitted altogether: //...
const [/* omitted */, ...matches] = regex.exec(str);
//...
return matches;You're right, however, that this behavior is inconsistent with runtime behavior of the regex itself.
Indeed, I initially duplicated the definition from
Absolutely, good point once again. I'm preparing the changes as I write this response, so the update should follow shortly. |
Add ParseNamedCaptures for parity Add Tails to expoted types to address the common use-case of omitting the input string from captures
|
Oh wow, thanks for taking care of everything on your own — awesome job! Everything looks really well done, and I really appreciate the extra documentation and examples you added. I'll check it out now and aim to publish this version today. If you come across any issues or think anything could be revisited for performance or benchmarking reasons, feel free to let me know. For now I'm more than happy with the initial state of this new feature. |
Having a type-safe function is amazing and all, but sometimes all you need is just the types.
This PR exports the
Parsetype, while also introducing a separateParseCapturesin case the user needs only the regular (non-named) captures.Motivation
Many libraries would benefit from being able to type-parse regexes from user-defined string types without actually doing all of the runtime work that
typedRegExpis doing.Also, a lot of libraries that use regular expressions in some way do not support named captures, so parsing their syntax is just wasted work in this case, hence the new
ParseCapturestype to simplify DX.This PR allows to smoothly integrate
ts-regexpinto other libraries because of these improvements.