Skip to content

[Feature]. Missing Constructor #74

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

Closed
5 tasks
PaulJPhilp opened this issue Mar 25, 2024 · 4 comments
Closed
5 tasks

[Feature]. Missing Constructor #74

PaulJPhilp opened this issue Mar 25, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@PaulJPhilp
Copy link
Contributor

Is your feature request related to a problem? Please describe.
When building complex RegExes, I often need to do something like:

const port = [portSeperator, portNumber];
type = const port: (string | Repeat)[]

OR

const hostname = [host, optional(repeat([period, host], { min: 1, max: 255 }))];
type = const hostname: (Repeat | Optional)[]

Then, I want to use those components in a builder:

const urlAuthority = [optional([userInfo, at]), hostname, optional(port)];
export const UrlAuthorityFinder = buildRegExp(urlAuthority, {
  ignoreCase: true,
  global: true,
});

This causes the following type error:
Type '(Repeat | Optional)[]' is not assignable to type 'RegexElement'.ts(2345)

To get around this I use a constructor [capture(hostname) or choiceOf(hostname)]. This works but is ugly.

Describe the solution you'd like
I'd like to see a constructor (combine?, compose?, noop?, group?, ...) that groups the elements into a RegexSequence.

const port = compose([portSeperator, portNumber]);

OR

const hostname = group([host, optional(repeat([period, host], { min: 1, max: 255 }))]);

Describe alternatives you've considered
It turns out that this is the use case I had for non-capture groups. Even this is ugly.

Checklist

  • Implementation
  • Tests
  • API docs
  • README docs (if relevant)
  • Example docs & tests (if relevant)

Additional context
Add any other context or screenshots about the feature request here.

@mdjastrzebski
Copy link
Member

Swift Regex Builder uses Regex(...) for that, so adapting it analogously to TS Regex Builder that would be:

const port = regex([portSeperator, portNumber]);

Alternatively, we've might allow for nesting RegexSequences as elements of RegexSequences (as arrays), so that for encoding, they would be flattened.

const urlAuthority: RegexSequence = [optional([userInfo, at]), choiceOf(hostname), optional(port)];
export const UrlAuthorityValidator = buildRegExp(
  [startOfString, urlAuthority, endOfString], // <-- Nested array happens here
  {
    ignoreCase: true,
  },
);

@PaulJPhilp
Copy link
Contributor Author

PaulJPhilp commented Mar 26, 2024 via email

@mdjastrzebski
Copy link
Member

Resolved by #77

@mdjastrzebski
Copy link
Member

Resolved in v1.4.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants