Skip to content

Make negated pattern position-insensitive#30

Merged
sindresorhus merged 11 commits intosindresorhus:mainfrom
valango:ticket_29
May 12, 2021
Merged

Make negated pattern position-insensitive#30
sindresorhus merged 11 commits intosindresorhus:mainfrom
valango:ticket_29

Conversation

@valango
Copy link
Copy Markdown
Contributor

@valango valango commented Mar 8, 2021

Maybe I was a bit hasty w this one, but it was blocking my long-dormant #25.

Fixes #29

@sindresorhus
Copy link
Copy Markdown
Owner

You also need to update .isMatch.

Comment thread index.js Outdated
for (const pattern of patterns) {
if (pattern.test(input)) {
matches = !pattern.negated;
if (pattern.test(input) && !(matches = !pattern.negated)) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like having an assignment in a condition. Move it out into a separate if.

Comment thread readme.md Outdated
Use `*` to match zero or more characters. A pattern starting with `!` will be negated.
Use `*` to match zero or more characters.

A leading `!` negates the pattern - any matching inputs will be excluded from the results.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to update index.d.ts too.

Comment thread test.js Outdated
t.deepEqual(matcher(['foo', 'bar'], ['foo']), ['foo']);
t.deepEqual(matcher(['foo', 'bar'], ['bar']), ['bar']);
t.deepEqual(matcher(['foo', 'bar'], ['fo*', 'ba*', '!bar']), ['foo']);
t.deepEqual(matcher(['foo', 'bar'], ['fo*', '!bar', 'ba*']), ['foo']);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will need a lot more assertions.

@valango
Copy link
Copy Markdown
Contributor Author

valango commented Mar 10, 2021

Ok, Im almost there... just noticed that in the current index.d.ts, the @returns declarations are switched - I'll correct that, too.

For the matcher(), I'd add a horribly long wording - provided you want both .ts and .md exactly equivalent:

@param patterns - String or array of string patterns. A leading ! negates the pattern. An input string will be omitted, if it does not match any non-negated patterns present, or if it matches a negated pattern, or if no pattern is present.

Please confirm or suggest a better wording - then I'll submit my final version.

@sindresorhus
Copy link
Copy Markdown
Owner

String or array of string patterns.

This part feels moot in the TS definition (but not the readme) as it's already made clear by the types.

Otherwise, it looks ok.

@valango
Copy link
Copy Markdown
Contributor Author

valango commented Mar 28, 2021

Plz check & let me know. I did not bump the version.

Comment thread index.js Outdated
Comment thread index.js Outdated
}

if (matches) {
if (matches || (matches === undefined && !patterns.some(p => !p.negated))) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (matches || (matches === undefined && !patterns.some(p => !p.negated))) {
if (matches || (matches === undefined && !patterns.some(pattern => !pattern.negated))) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

Comment thread readme.md Outdated
A leading `!` negates the pattern.

An input string will be omitted, if it does not match any non-negated patterns present,
or if it matches a negated pattern, or if no pattern is present.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't hard-wrap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected.

Comment thread readme.md
Use `*` to match zero or more characters. A pattern starting with `!` will be negated.
Use `*` to match zero or more characters.

A leading `!` negates the pattern.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readme and index.d.ts should be in sync documentation-wise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"This part feels moot in the TS definition (but not the readme) as it's already made clear by the types."

Did I get you wrong or have you changed your mind?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let me rephrase: index.d.ts and readme should be in sync, except for cases where the readme just points out the types as that's already clear by the types.

However, that's not what I'm pointing out here. You have removed some stuff from index.d.ts that is preserved here and not updated other parts, like https://github.com/sindresorhus/matcher/pull/30/files#diff-7aa4473ede4abd9ec099e87fec67fd57afafaf39e05d493ab4533acc38547eb8R83

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I restored the missing stuff and made the contents as coherent as I could via ee30306.
I also found:

  • 'inputs' and 'patterns' were randomly used in both plural and singular form. Fixed that.
  • '@param options' is missing in index.d.ts - I left it for you to decide.

Copy link
Copy Markdown
Contributor Author

@valango valango May 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I restored the missing stuff and made the contents as coherent as I could via ee30306.
Also found:

  • 'inputs' and 'patterns' were randomly used in both plural and singular form. Fixed that.
  • '@param options' is missing in index.d.ts - I left it for you to decide.

Comment thread test.js
@@ -37,6 +41,10 @@ test('matcher.isMatch()', t => {
t.true(matcher.isMatch('moo', ['*oo', '!f*']));
t.true(matcher.isMatch('UNICORN', ['!*oo', 'UNI*'], {caseSensitive: true}));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these to a separate test with a descriptive title of what they're testing. Same with the other assertions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did so. New tests named "... negated patern placement".

Comment thread index.js
return regexp.negated ? !matches : matches;
});
});
return matching.length > 0;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this new logic here is that it's inefficient. The old way, it short-circuited when it found a match. Now it will do everything and then check for a match.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I modified the code. Now isMatch seems to be bit quicker compared to the original implementation, probably because not constructing Regxp repeatedly.

Comment thread index.js Outdated
return false;
}
module.exports.isMatch = (inputs, patterns, options) => {
const matching = module.exports(inputs, patterns, options, true);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't change anything as you're calling the incorrect method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omg, made error on merge. Fixed now.

@sindresorhus
Copy link
Copy Markdown
Owner

You still need to do #30 (comment).

@sindresorhus sindresorhus changed the title Negated rule made position-insensitive (#29) Make negated pattern position-insensitive May 12, 2021
@sindresorhus sindresorhus merged commit 43d778c into sindresorhus:main May 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Negated rule is position - sensitive

2 participants