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

Deconstruct string matched with .startsWith, .endsWith, regex #220

Open
titouancreach opened this issue Jan 29, 2024 · 2 comments
Open

Deconstruct string matched with .startsWith, .endsWith, regex #220

titouancreach opened this issue Jan 29, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@titouancreach
Copy link

titouancreach commented Jan 29, 2024

Is your feature request related to a problem? Please describe.
I'm always frustrated when: I need to split the string manually after it matches .startsWith or .endsWith or regex (matching group).

For example I want to normalize phone number from a string
So, if it start with a "0" or "0033" I want to replace this number with "+33" (country code), if it starts with "+33", it's Ok.

match(phoneNumber)
  .with(P.string.startsWith("0033").select(), (matched) => {
    return "+33" + matched.slice(4);
  })
  ...

same with a regex:

match(phoneNumber)
  .with(P.string.regex("^0033([0-9]+)").select(), (matched) => {
    return "+33" + matched.slice(4); // if I want the group matched here, I need to manually re-execute the regex
  })
  ...

Describe the solution you'd like
I would like solution to match strings more like some other functionnal programming langage, example Elixir

iex> "he" <> rest = "hello"
"hello"
iex> rest
"llo"

Describe alternatives you've considered

Maybe select could be surcharged and select with a regex would give you "matches" (to be honest, I don't have a good idea of how it should be done)

match(phoneNumber)
  .with(P.string.startsWith("0033").select({ tail: true }), (matched, { tail }) => {
    return `+33${tail}`);
  })
  ...

or

match(phoneNumber)
  .with(P.string.startsWith("0033", 'tail').select(), (matched, { tail }) => {
    return `+33${tail}`);
  })
  ...
@titouancreach titouancreach added the enhancement New feature or request label Jan 29, 2024
@titouancreach titouancreach changed the title Deconstruct string matched with .startWith, .endWith, (sorry wip) Deconstruct string matched with .startWith, .endWith, regex Jan 29, 2024
@titouancreach titouancreach changed the title (sorry wip) Deconstruct string matched with .startWith, .endWith, regex Deconstruct string matched with .startWith, .endsWith, regex Jan 29, 2024
@titouancreach titouancreach changed the title Deconstruct string matched with .startWith, .endsWith, regex Deconstruct string matched with .startsWith, .endsWith, regex Jan 29, 2024
@cyberixae
Copy link

I made a feature request #227 about template literal matching support. Since your feature request is also related to strings, I thought that might be interesting to you. I also included a workaround that you might find useful, even though it might not be exactly what you are looking for.

@titouancreach
Copy link
Author

@cyberixae Yes, it would solve 80% of my use cases

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