Giter Club home page Giter Club logo

Comments (3)

michaelschufi avatar michaelschufi commented on June 1, 2024 1

Hi @didavid61202

Thank you for the fast response.

The first part, I completely agree with. Since groups will always be undefined if the regexp doesn't match.

Regarding the second case, you hit the nail on the head. I expect the value part of the type to be string | undefined if and only if I mark at least one group as optional. Otherwise all the groups will always have a value, correct?

Edit: In the meantime I've started to write an enhancement request issue regarding the type resolution for the groups. It would be awesome if the type of each group could be inferred based on the anyOf parameters.

from magic-regexp.

didavid61202 avatar didavid61202 commented on June 1, 2024 1

Hi @michaelschufi,

Yes, you are right. If all the groups are not optional, the match result should either be null or matched array with all groups containing value. I think same goes for capturing groups too.

I've benn working on a new type inferrencing for a while, I've put more detail at #235, hopefully will resolve this and many other cases 👍

from magic-regexp.

didavid61202 avatar didavid61202 commented on June 1, 2024

Hi @michaelschufi,

I think the behavior you're observing is actually working as intended.

We didn't touch any runtime logic (helpers are transform to pure RegExp), so it behave exactly as native RegExp at runtime. The issue here is that /(?<opponent>A|B|C) (?<self>X|Y|Z)/ required both groups opponent and self to be matched, you can actually try with native RegExp:

console.log('B '.match(/(?<opponent>A|B|C) (?<self>X|Y|Z)/)?.groups); 
// return `undefined`

maybe what you want/expect is making the self part optional, you can try:

const regex = createRegExp(
  anyOf('A', 'B', 'C')
    .groupedAs('opponent')
    .and(' ')
    .and(anyOf('X', 'Y', 'Z').groupedAs('self').optionally())
);

this resolve to /(?<opponent>A|B|C) (?<self>X|Y|Z)?/, this will still return undefined if matching 'B ', as the self groups is (or inside) a optional quantifier:

console.log('B '.match(/(?<opponent>A|B|C) (?<self>X|Y|Z)?/)?.groups) 
// return `{ opponent: 'B', self: undefined }`

this is why the type of groups is Record<"opponent" | "self", string | undefined>, as we can still match a certain RegExp but not all of the groups having captured values.

Please let me know if you have any further questions or concerns. Thank you!

from magic-regexp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.