Make negated pattern position-insensitive#30
Make negated pattern position-insensitive#30sindresorhus merged 11 commits intosindresorhus:mainfrom
Conversation
|
You also need to update |
| for (const pattern of patterns) { | ||
| if (pattern.test(input)) { | ||
| matches = !pattern.negated; | ||
| if (pattern.test(input) && !(matches = !pattern.negated)) { |
There was a problem hiding this comment.
I don't like having an assignment in a condition. Move it out into a separate if.
| 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. |
There was a problem hiding this comment.
You need to update index.d.ts too.
| 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']); |
There was a problem hiding this comment.
This change will need a lot more assertions.
|
Ok, Im almost there... just noticed that in the current For the @param patterns - String or array of string patterns. A leading Please confirm or suggest a better wording - then I'll submit my final version. |
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. |
|
Plz check & let me know. I did not bump the version. |
| } | ||
|
|
||
| if (matches) { | ||
| if (matches || (matches === undefined && !patterns.some(p => !p.negated))) { |
There was a problem hiding this comment.
| if (matches || (matches === undefined && !patterns.some(p => !p.negated))) { | |
| if (matches || (matches === undefined && !patterns.some(pattern => !pattern.negated))) { |
| 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. |
| 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. |
There was a problem hiding this comment.
Readme and index.d.ts should be in sync documentation-wise.
There was a problem hiding this comment.
"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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
There was a problem hiding this comment.
| @@ -37,6 +41,10 @@ test('matcher.isMatch()', t => { | |||
| t.true(matcher.isMatch('moo', ['*oo', '!f*'])); | |||
| t.true(matcher.isMatch('UNICORN', ['!*oo', 'UNI*'], {caseSensitive: true})); | |||
|
|
|||
There was a problem hiding this comment.
Move these to a separate test with a descriptive title of what they're testing. Same with the other assertions.
There was a problem hiding this comment.
Did so. New tests named "... negated patern placement".
| return regexp.negated ? !matches : matches; | ||
| }); | ||
| }); | ||
| return matching.length > 0; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good point. I modified the code. Now isMatch seems to be bit quicker compared to the original implementation, probably because not constructing Regxp repeatedly.
| return false; | ||
| } | ||
| module.exports.isMatch = (inputs, patterns, options) => { | ||
| const matching = module.exports(inputs, patterns, options, true); |
There was a problem hiding this comment.
This doesn't change anything as you're calling the incorrect method.
There was a problem hiding this comment.
Omg, made error on merge. Fixed now.
|
You still need to do #30 (comment). |
Maybe I was a bit hasty w this one, but it was blocking my long-dormant #25.
Fixes #29