-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Avoid creating multiple Minimatch instances for the same negative glob #33
Conversation
(improved a comment & squashed) |
Changed commit message. |
This optimizes use cases such as `['a', 'b', '!b']`, where a new Minimatch instance for `'!b'` would be created for each previous positive glob. Now the Minimatch instances for negative globs are created beforehand, thus positive globs can use the same negative matcher instances. That is, for `['a', 'b', '!b']` both `'a'` and `'b'` globs will use the same Minimatch instance for `'!b'` instead of instantiating their own. Should also note that this change, technically, would instantiate Minimatch instances that would never be used when considering leading negative globs (e.g. `['!b', 'b']`), but that is a complete misuse and IIRC Minimatch produces their underlying regex lazily, so this shouldn't be a problem. (imported from gulpjs/glob-stream#33)
Avoid creating multiple Minimatch instances for the same negative glob
Nice catch - thanks! |
No problem, though, I believe it lost a bit of readability, seeing as negatives' |
- Better naming for negative matchers ( gulpjs#33 (comment) ) - Throw on invalid glob argument ( gulpjs#34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does. - Applied glob type validation in the `isNegative` function, as all globs go through it before stream creation and that function is already testing their types.
- Better naming for negative matchers ( gulpjs#33 (comment) ) - Throw on invalid glob argument ( gulpjs#34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Better naming for negative matchers ( gulpjs#33 (comment) ) - Throw on invalid glob argument ( gulpjs#34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Better naming for negative matchers ( gulpjs#33 (comment) ) - Throw on invalid glob argument ( gulpjs#34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
Avoid creating multiple Minimatch instances for the same negative glob
- Better naming for negative matchers ( #33 (comment) ) - Throw on invalid glob argument ( #34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
Avoid creating multiple Minimatch instances for the same negative glob
- Better naming for negative matchers ( #33 (comment) ) - Throw on invalid glob argument ( #34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Better naming for negative matchers ( #33 (comment) ) - Throw on invalid glob argument ( #34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Better naming for negative matchers ( #33 (comment) ) - Throw on invalid glob argument ( #34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Better naming for negative matchers ( #33 (comment) ) - Throw on invalid glob argument ( #34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Better naming for negative matchers ( #33 (comment) ) - Throw on invalid glob argument ( #34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Better naming for negative matchers ( #33 (comment) ) - Throw on invalid glob argument ( #34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
- Better naming for negative matchers ( #33 (comment) ) - Throw on invalid glob argument ( #34 (comment) ) - Removed the `if (!Array.isArray(globs)) return gs.createStream(globs, [], opt);` because this optimization is already covered a couple lines below at `if (positives.length === 1) return streamFromPositive(positives[0]);`, and to not duplicate the validation code. Previously, passing a string with a negative glob did not throw a "Missing positive glob" error, now it does.
This optimizes use cases such as
['a', 'b', '!b']
, where a new Minimatch instance for'!b'
would be created for each previous positive glob.Now the Minimatch instances for negative globs are created beforehand, thus positive globs can use the same negative matcher instances. That is, for
['a', 'b', '!b']
both'a'
and'b'
globs will use the same Minimatch instance for'!b'
instead of instantiating their own.Also, as far as I can see,
gs.createStream
is internal (no docs or tests), so I took the liberty of adapting it a bit as I needed the completeopt
object to pass to the Minimatch constructor ings.create
.Should also note that this change, technically, would instantiate Minimatch instances that would never be used when considering leading negative globs (e.g.
['!b', 'b']
), but that is a complete misuse and IIRC Minimatch produces their underlying regex lazily, so this shouldn't be a problem.