├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test ├── globule.js └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | custom: https://sindresorhus.com/donate 4 | tidelift: npm/multimatch 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import {type MinimatchOptions} from 'minimatch'; 2 | 3 | export type Options = Readonly; 4 | 5 | /** 6 | Extends [`minimatch.match()`](https://github.com/isaacs/minimatch#minimatchmatchlist-pattern-options) with support for multiple patterns. 7 | 8 | @param paths - The paths to match against. 9 | @param patterns - Globbing patterns to use. For example: `['*', '!cake']`. See supported [`minimatch` patterns](https://github.com/isaacs/minimatch#usage). 10 | @returns The matching paths in the order of input paths. 11 | 12 | @example 13 | ``` 14 | import multimatch from 'multimatch'; 15 | 16 | multimatch(['unicorn', 'cake', 'rainbows'], ['*', '!cake']); 17 | //=> ['unicorn', 'rainbows'] 18 | ``` 19 | */ 20 | export default function multimatch( 21 | paths: string | readonly string[], 22 | patterns: string | readonly string[], 23 | options?: Options 24 | ): string[]; 25 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {minimatch} from 'minimatch'; 2 | import arrayUnion from 'array-union'; 3 | import arrayDiffer from 'array-differ'; 4 | 5 | export default function multimatch(list, patterns, options = {}) { 6 | list = [list].flat(); 7 | patterns = [patterns].flat(); 8 | 9 | if (list.length === 0 || patterns.length === 0) { 10 | return []; 11 | } 12 | 13 | let result = []; 14 | for (const item of list) { 15 | for (let pattern of patterns) { 16 | let process = arrayUnion; 17 | 18 | if (pattern[0] === '!') { 19 | pattern = pattern.slice(1); 20 | process = arrayDiffer; 21 | } 22 | 23 | result = process(result, minimatch.match([item], pattern, options)); 24 | } 25 | } 26 | 27 | return result; 28 | } 29 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import multimatch, {type Options} from './index.js'; 2 | 3 | const options: Options = {}; 4 | 5 | multimatch(['unicorn', 'cake', 'rainbows'], '!cake'); 6 | multimatch(['unicorn', 'cake', 'rainbows'], ['*', '!cake']); 7 | multimatch(['unicorn', 'cake', 'rainbows'], ['*', '!cake'], { 8 | debug: true, 9 | }); 10 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "multimatch", 3 | "version": "7.0.0", 4 | "description": "Extends `minimatch.match()` with support for multiple patterns", 5 | "license": "MIT", 6 | "repository": "sindresorhus/multimatch", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "engines": { 19 | "node": ">=18" 20 | }, 21 | "scripts": { 22 | "test": "xo && ava && tsd" 23 | }, 24 | "files": [ 25 | "index.js", 26 | "index.d.ts" 27 | ], 28 | "keywords": [ 29 | "expand", 30 | "find", 31 | "glob", 32 | "globbing", 33 | "globs", 34 | "match", 35 | "matcher", 36 | "minimatch", 37 | "pattern", 38 | "patterns", 39 | "wildcard" 40 | ], 41 | "dependencies": { 42 | "array-differ": "^4.0.0", 43 | "array-union": "^3.0.1", 44 | "minimatch": "^9.0.3" 45 | }, 46 | "devDependencies": { 47 | "ava": "^5.3.1", 48 | "tsd": "^0.29.0", 49 | "xo": "^0.56.0" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # multimatch 2 | 3 | > Extends [`minimatch.match()`](https://github.com/isaacs/minimatch#minimatchmatchlist-pattern-options) with support for multiple patterns 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install multimatch 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import multimatch from 'multimatch'; 15 | 16 | multimatch(['unicorn', 'cake', 'rainbows'], ['*', '!cake']); 17 | //=> ['unicorn', 'rainbows'] 18 | ``` 19 | 20 | See the [tests](test) for more usage examples and expected matches. 21 | 22 | ## API 23 | 24 | ### multimatch(paths, patterns, options?) 25 | 26 | Returns an array of matching paths in the order of input paths. 27 | 28 | #### paths 29 | 30 | Type: `string | string[]` 31 | 32 | The paths to match against. 33 | 34 | #### patterns 35 | 36 | Type: `string | string[]` 37 | 38 | Globbing patterns to use. For example: `['*', '!cake']`. See supported [`minimatch` patterns](https://github.com/isaacs/minimatch#usage). 39 | 40 | - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) 41 | - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) 42 | 43 | #### options 44 | 45 | Type: `object` 46 | 47 | See the [`minimatch` options](https://github.com/isaacs/minimatch#options). 48 | 49 | ## How multiple patterns work 50 | 51 | Positive patterns (e.g. `foo` or `*`) add to the results, while negative patterns (e.g. `!foo`) subtract from the results. 52 | 53 | Therefore a lone negation (e.g. `['!foo']`) will never match anything. Use `['*', '!foo']` instead. 54 | 55 | ## Globbing patterns 56 | 57 | Just a quick overview. 58 | 59 | - `*` matches any number of characters, but not `/` 60 | - `?` matches a single character, but not `/` 61 | - `**` matches any number of characters, including `/`, as long as it's the only thing in a path part 62 | - `{}` allows for a comma-separated list of "or" expressions 63 | - `!` at the beginning of a pattern will negate the match 64 | 65 | ## Related 66 | 67 | - [globby](https://github.com/sindresorhus/globby) - Match against the filesystem instead of a list 68 | - [matcher](https://github.com/sindresorhus/matcher) - Simple wildcard matching 69 | -------------------------------------------------------------------------------- /test/globule.js: -------------------------------------------------------------------------------- 1 | // Tests from [globule](https://github.com/cowboy/node-globule) 2 | import test from 'ava'; 3 | import multimatch from '../index.js'; 4 | 5 | test('Should return empty set if a required argument is missing or an empty set.', t => { 6 | t.deepEqual(multimatch('', 'foo.js'), []); 7 | t.deepEqual(multimatch('*.js', ''), []); 8 | t.deepEqual(multimatch([], 'foo.js'), []); 9 | t.deepEqual(multimatch('*.js', []), []); 10 | t.deepEqual(multimatch('', ['foo.js']), []); 11 | t.deepEqual(multimatch(['*.js'], ''), []); 12 | }); 13 | 14 | test('basic matching should match correctly', t => { 15 | t.deepEqual(multimatch('foo.js', '*.js'), ['foo.js']); 16 | t.deepEqual(multimatch(['foo.js'], '*.js'), ['foo.js']); 17 | t.deepEqual(multimatch(['foo.js', 'bar.css'], '*.js'), ['foo.js']); 18 | t.deepEqual(multimatch('foo.js', ['*.js', '*.css']), ['foo.js']); 19 | t.deepEqual(multimatch(['foo.js'], ['*.js', '*.css']), ['foo.js']); 20 | t.deepEqual(multimatch(['foo.js', 'bar.css'], ['*.js', '*.css']), ['foo.js', 'bar.css']); 21 | }); 22 | 23 | test('should fail to match', t => { 24 | t.deepEqual(multimatch('foo.css', '*.js'), []); 25 | t.deepEqual(multimatch(['foo.css', 'bar.css'], '*.js'), []); 26 | }); 27 | 28 | test('should return a uniqued set', t => { 29 | t.deepEqual(multimatch(['foo.js', 'foo.js'], '*.js'), ['foo.js']); 30 | t.deepEqual(multimatch(['foo.js', 'foo.js'], ['*.js', '*.*']), ['foo.js']); 31 | }); 32 | 33 | test('solitary exclusion should match nothing', t => { 34 | t.deepEqual(multimatch(['foo.js', 'bar.js'], ['!*.js']), []); 35 | }); 36 | 37 | test('exclusion should cancel match', t => { 38 | t.deepEqual(multimatch(['foo.js', 'bar.js'], ['*.js', '!*.js']), []); 39 | }); 40 | 41 | test('partial exclusion should partially cancel match', t => { 42 | t.deepEqual(multimatch(['foo.js', 'bar.js', 'baz.js'], ['*.js', '!f*.js']), ['bar.js', 'baz.js']); 43 | }); 44 | 45 | test('inclusion / exclusion order matters', t => { 46 | t.deepEqual(multimatch(['foo.js', 'bar.js', 'baz.js'], ['*.js', '!*.js', 'b*.js']), ['bar.js', 'baz.js']); 47 | t.deepEqual(multimatch(['foo.js', 'bar.js', 'baz.js'], ['*.js', '!f*.js', '*.js']), ['foo.js', 'bar.js', 'baz.js']); 48 | }); 49 | 50 | test('should matchBase (minimatch) when specified.', t => { 51 | t.deepEqual(multimatch(['foo.js', 'bar', 'baz/xyz.js'], '*.js', {matchBase: true}), ['foo.js', 'baz/xyz.js']); 52 | }); 53 | 54 | test('should not matchBase (minimatch) by default.', t => { 55 | t.deepEqual(multimatch(['foo.js', 'bar', 'baz/xyz.js'], '*.js'), ['foo.js']); 56 | }); 57 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import multimatch from '../index.js'; 3 | 4 | const fixtures = ['vendor/js/foo.js', 'vendor/js/bar.js', 'vendor/js/baz.js']; 5 | 6 | test('match on multiple patterns', t => { 7 | t.deepEqual(multimatch(['unicorn', 'cake', 'rainbows'], ['*', '!cake']), ['unicorn', 'rainbows']); 8 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo']), ['foo']); 9 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo']), []); 10 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', 'bar']), ['foo', 'bar']); 11 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!bar']), ['foo']); 12 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo', 'bar']), ['bar']); 13 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo', '!bar']), []); 14 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*{o,r}', 'foo']), ['foo']); 15 | }); 16 | 17 | test('return an array of matches', t => { 18 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], 'foo'), ['foo']); 19 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['f*']), ['foo']); 20 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['f*', 'bar']), ['foo', 'bar']); 21 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', 'bar']), ['foo', 'bar']); 22 | }); 23 | 24 | test('return matches in the order the list were defined', t => { 25 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['bar', 'f*']), ['foo', 'bar']); 26 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['f*', '*z']), ['foo', 'baz']); 27 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*z', 'f*']), ['foo', 'baz']); 28 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*z', '*r', 'f*']), ['foo', 'bar']); 29 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*a*', '!f*']), ['bar', 'baz']); 30 | }); 31 | 32 | test('return an array with negations omitted', t => { 33 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], '!foo'), []); 34 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo']), []); 35 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo', '!bar']), []); 36 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*z']), []); 37 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*z', '!*a*']), []); 38 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*']), []); 39 | t.deepEqual(multimatch(fixtures, ['!**/*z.js']), []); 40 | t.deepEqual(multimatch(fixtures, ['!**/*z.js', '**/foo.js']), ['vendor/js/foo.js']); 41 | t.deepEqual(multimatch(fixtures, ['!**/*z.js', '!**/*a*.js']), []); 42 | }); 43 | 44 | test('return an empty array when no matches are found', t => { 45 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['quux']), []); 46 | t.deepEqual(multimatch(fixtures, ['!**/*.js']), []); 47 | }); 48 | 49 | test('patterns be order sensitive', t => { 50 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*a*', '*z']), ['baz']); 51 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*z', '!*a*']), []); 52 | t.deepEqual(multimatch(['foo', 'foam', 'for', 'forum'], ['!*m', 'f*']), ['foo', 'foam', 'for', 'forum']); 53 | t.deepEqual(multimatch(['foo', 'foam', 'for', 'forum'], ['f*', '!*m']), ['foo', 'for']); 54 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*{o,r}', 'foo']), ['foo']); 55 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!*{o,r}']), []); 56 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo', 'bar']), ['bar']); 57 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!bar']), ['foo']); 58 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['bar', '!foo', 'foo']), ['foo', 'bar']); 59 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!foo', 'bar']), ['bar']); 60 | }); 61 | 62 | test('override negations and re-include explicitly defined patterns', t => { 63 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*']), []); 64 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*a*']), []); 65 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['bar', '!*a*']), []); 66 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*a*', 'bar']), ['bar']); 67 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*a*', '*']), ['foo', 'bar', 'baz']); 68 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*a*', '*z']), ['baz']); 69 | }); 70 | 71 | test('misc', t => { 72 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!foo']), ['bar', 'baz']); 73 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!foo', 'bar']), ['bar', 'baz']); 74 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!foo']), ['bar', 'baz']); 75 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo', '*']), ['foo', 'bar', 'baz']); 76 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!foo', '!bar']), ['baz']); 77 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*{o,r}', '*']), ['foo', 'bar', 'baz']); 78 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!*{o,r}']), ['baz']); 79 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!*{o,r}', '*']), ['foo', 'bar', 'baz']); 80 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!*{o,r}', 'foo']), ['foo', 'baz']); 81 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*{o,r}', '*', 'foo']), ['foo', 'bar', 'baz']); 82 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!*{o,r}']), []); 83 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!*{o,r}', 'foo']), ['foo']); 84 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!*{o,r}', 'foo']), ['foo']); 85 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!*{o,r}']), ['baz']); 86 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], 'foo'), ['foo']); 87 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo']), []); 88 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['*', '!foo']), ['bar', 'baz']); 89 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', 'bar']), ['foo', 'bar']); 90 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['foo', '!bar']), ['foo']); 91 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo', 'bar']), ['bar']); 92 | t.deepEqual(multimatch(['foo', 'bar', 'baz'], ['!foo', '!bar']), []); 93 | t.deepEqual(multimatch(['foo', 'one', 'two', 'four', 'do', 'once', 'only'], ['once', '!o*', 'once']), ['once']); 94 | t.deepEqual(multimatch(['foo', 'one', 'two', 'four', 'do', 'once', 'only'], ['*', '!o*', 'once']), ['foo', 'two', 'four', 'do', 'once']); 95 | }); 96 | --------------------------------------------------------------------------------