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

add "match" function #6

Open
gajus opened this issue Jan 18, 2017 · 4 comments
Open

add "match" function #6

gajus opened this issue Jan 18, 2017 · 4 comments

Comments

@gajus
Copy link
Owner

gajus commented Jan 18, 2017

Used to extract 1 match from the result.

This function function could be invoked either from the select, e.g.

::attribute(href)::match("/salles/(.+)")

or from the declarative API manifest, e.g.

nid: {
  selector: '::attribute(href)',
  match: '\/salles\/(.+)'
}

An implementation:

const match = (inputRule: RegExp, subject: string): string => {
  if (inputRule.ignoreCase) {
    throw new Error('"rule" parameter value must be an instance of a case sensitive RegExp.');
  }

  const rule = new RegExp(inputRule.source, 'gm');
  const matches = [];

  let match;

    // eslint-disable-next-line
    while (match = rule.exec(subject)) {
      matches.push(match[1]);
    }

  if (!matches.length) {
    throw new Error('Did not match.');
  }

  if (matches.length !== 1) {
    throw new Error('Matched more than one group.');
  }

  return matches[0];
};
@gajus gajus changed the title add extractMatch function add "match" function Jan 18, 2017
@rla
Copy link
Collaborator

rla commented Jan 18, 2017

I could see ::matchAll(regex) be useful too in the case when multiple matching parts from the element/attribute/property text needs to be extracted. Maybe this goes into a separate issue?

@gajus
Copy link
Owner Author

gajus commented Jan 18, 2017

Maybe this goes into a separate issue?

Here is fine. :-)

I could see ::matchAll(regex) be useful too in the case when multiple matching parts from the element/attribute/property text needs to be extracted.

Do you have any example of when such would be the case?

In all cases that I have observed in practise, when multiple matches need to be made, then there is an additional selector that can be used, e.g. span. Though, you have explicitly said attribute values. Therefore, I'd be curious to know of a practical example.

My problem with matchAll is that it isn't strict, i.e. it can match 0, 1, or 100 results. As such, it would make sense to simply extract the text value and process it imperatively.

I could be overlooking a use case, though.

@rla
Copy link
Collaborator

rla commented Jan 18, 2017

@gajus

Do you have any example of when such would be the case?

I propose to label feature proposals lacking an use case with the label needs-use-case. This helps to focus. I admit I tend to shoot sometime not-well-thought-out ideas.

@gajus
Copy link
Owner Author

gajus commented Jan 18, 2017

Raised an issue to track the proposal.

#7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants