Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/matcher
Simple wildcard matching
https://github.com/sindresorhus/matcher
Last synced: 6 days ago
JSON representation
Simple wildcard matching
- Host: GitHub
- URL: https://github.com/sindresorhus/matcher
- Owner: sindresorhus
- License: mit
- Created: 2016-02-05T06:58:04.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2021-10-03T09:29:45.000Z (about 3 years ago)
- Last Synced: 2024-10-29T15:46:31.266Z (about 1 month ago)
- Language: JavaScript
- Size: 58.6 KB
- Stars: 538
- Watchers: 6
- Forks: 30
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs-cn - matcher - 简单通配符匹配. (包 / 文本)
- awesome-nodejs - matcher - Simple wildcard matching. ![](https://img.shields.io/github/stars/sindresorhus/matcher.svg?style=social&label=Star) (Repository / RegExp/Glob)
- awesome-nodejs-cn - matcher - **star:538** 简单的通配符匹配 (包 / 文本)
- awesome-nodejs - matcher - Simple wildcard matching. (Packages / Text)
- awesome-nodejs - matcher - Simple wildcard matching - ★ 411 (Text)
- awesome-node - matcher - Simple wildcard matching. (Packages / Text)
- awesome-nodejs-cn - matcher - 简单通配符匹配. (目录 / 文本处理)
README
# matcher
> Simple [wildcard](https://en.wikipedia.org/wiki/Wildcard_character) matching
Useful when you want to accept loose string input and regexes/globs are too convoluted.
## Install
```sh
npm install matcher
```## Usage
```js
import {matcher, isMatch} from 'matcher';matcher(['foo', 'bar', 'moo'], ['*oo', '!foo']);
//=> ['moo']matcher(['foo', 'bar', 'moo'], ['!*oo']);
//=> ['bar']matcher('moo', ['']);
//=> []matcher('moo', []);
//=> []matcher([''], ['']);
//=> ['']isMatch('unicorn', 'uni*');
//=> trueisMatch('unicorn', '*corn');
//=> trueisMatch('unicorn', 'un*rn');
//=> trueisMatch('rainbow', '!unicorn');
//=> trueisMatch('foo bar baz', 'foo b* b*');
//=> trueisMatch('unicorn', 'uni\\*');
//=> falseisMatch(['foo', 'bar'], 'f*');
//=> trueisMatch(['foo', 'bar'], ['a*', 'b*']);
//=> trueisMatch('unicorn', ['']);
//=> falseisMatch('unicorn', []);
//=> falseisMatch([], 'bar');
//=> falseisMatch([], []);
//=> falseisMatch('', '');
//=> true
```## API
It matches even across newlines. For example, `foo*r` will match `foo\nbar`.
### matcher(inputs, patterns, options?)
Accepts a string or an array of strings for both `inputs` and `patterns`.
Returns an array of `inputs` filtered based on the `patterns`.
### isMatch(inputs, patterns, options?)
Accepts a string or an array of strings for both `inputs` and `patterns`.
Returns a `boolean` of whether any of given `inputs` matches all the `patterns`.
#### inputs
Type: `string | string[]`
The string or array of strings to match.
#### options
Type: `object`
##### caseSensitive
Type: `boolean`\
Default: `false`Treat uppercase and lowercase characters as being the same.
Ensure you use this correctly. For example, files and directories should be matched case-insensitively, while most often, object keys should be matched case-sensitively.
```js
import {isMatch} from 'matcher';isMatch('UNICORN', 'UNI*', {caseSensitive: true});
//=> trueisMatch('UNICORN', 'unicorn', {caseSensitive: true});
//=> falseisMatch('unicorn', ['tri*', 'UNI*'], {caseSensitive: true});
//=> false
```##### allPatterns
Type: `boolean`\
Default: `false`Require all negated patterns to not match and any normal patterns to match at least once. Otherwise, it will be a no-match condition.
```js
import {matcher} from 'matcher';// Find text strings containing both "edge" and "tiger" in arbitrary order, but not "stunt".
const demo = (strings) => matcher(strings, ['*edge*', '*tiger*', '!*stunt*'], {allPatterns: true});demo(['Hey, tiger!', 'tiger has edge over hyenas', 'pushing a tiger over the edge is a stunt']);
//=> ['tiger has edge over hyenas']
``````js
import {matcher} from 'matcher';matcher(['foo', 'for', 'bar'], ['f*', 'b*', '!x*'], {allPatterns: true});
//=> ['foo', 'for', 'bar']matcher(['foo', 'for', 'bar'], ['f*'], {allPatterns: true});
//=> []
```#### patterns
Type: `string | string[]`
Use `*` to match zero or more characters.
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.
## Benchmark
```sh
npm run bench
```## Related
- [matcher-cli](https://github.com/sindresorhus/matcher-cli) - CLI for this module
- [multimatch](https://github.com/sindresorhus/multimatch) - Extends `minimatch.match()` with support for multiple patterns---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.