Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a5a948
commit 5ed34b1
Showing
6 changed files
with
131 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
declare namespace matcher { | ||
interface Options { | ||
/** | ||
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. | ||
@default false | ||
*/ | ||
readonly caseSensitive?: boolean; | ||
} | ||
} | ||
|
||
declare const matcher: { | ||
/** | ||
Simple [wildcard](https://en.wikipedia.org/wiki/Wildcard_character) matching. | ||
@param inputs - Strings to match. | ||
@param patterns - Use `*` to match zero or more characters. A pattern starting with `!` will be negated. | ||
@returns `inputs` filtered based on the `patterns`. | ||
@example | ||
``` | ||
import matcher = require('matcher'); | ||
matcher(['foo', 'bar', 'moo'], ['*oo', '!foo']); | ||
//=> ['moo'] | ||
matcher(['foo', 'bar', 'moo'], ['!*oo']); | ||
//=> ['bar'] | ||
``` | ||
*/ | ||
(inputs: readonly string[], patterns: readonly string[], options?: matcher.Options): string[]; | ||
|
||
/** | ||
@param input - String to match. | ||
@param pattern - Use `*` to match zero or more characters. A pattern starting with `!` will be negated. | ||
@returns Whether the `input` matches the `pattern`. | ||
@example | ||
``` | ||
import matcher = require('matcher'); | ||
matcher.isMatch('unicorn', 'uni*'); | ||
//=> true | ||
matcher.isMatch('unicorn', '*corn'); | ||
//=> true | ||
matcher.isMatch('unicorn', 'un*rn'); | ||
//=> true | ||
matcher.isMatch('rainbow', '!unicorn'); | ||
//=> true | ||
matcher.isMatch('foo bar baz', 'foo b* b*'); | ||
//=> true | ||
matcher.isMatch('unicorn', 'uni\\*'); | ||
//=> false | ||
matcher.isMatch('UNICORN', 'UNI*', {caseSensitive: true}); | ||
//=> true | ||
matcher.isMatch('UNICORN', 'unicorn', {caseSensitive: true}); | ||
//=> false | ||
``` | ||
*/ | ||
isMatch(input: string, pattern: string, options?: matcher.Options): boolean; | ||
}; | ||
|
||
export = matcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {expectType} from 'tsd'; | ||
import matcher = require('.'); | ||
|
||
const options: matcher.Options = {}; | ||
|
||
expectType<string[]>(matcher(['foo', 'bar', 'moo'], ['*oo', '!foo'])); | ||
expectType<string[]>(matcher(['foo', 'bar', 'moo'], ['!*oo'])); | ||
expectType<string[]>( | ||
matcher(['foo', 'bar', 'moo'], ['!*oo'], {caseSensitive: true}) | ||
); | ||
|
||
expectType<boolean>(matcher.isMatch('unicorn', 'uni*')); | ||
expectType<boolean>(matcher.isMatch('UNICORN', 'UNI*', {caseSensitive: true})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,7 @@ String to match. | |
|
||
#### options | ||
|
||
Type: `Object` | ||
Type: `object` | ||
|
||
##### caseSensitive | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
import test from 'ava'; | ||
import m from '.'; | ||
import matcher from '.'; | ||
|
||
test('matcher()', t => { | ||
t.deepEqual(m(['foo', 'bar'], ['foo']), ['foo']); | ||
t.deepEqual(m(['foo', 'bar'], ['bar']), ['bar']); | ||
t.deepEqual(m(['foo', 'bar'], ['fo*', 'ba*', '!bar']), ['foo']); | ||
t.deepEqual(m(['foo', 'bar', 'moo'], ['!*o']), ['bar']); | ||
t.deepEqual(m(['moo', 'MOO'], ['*oo'], {caseSensitive: true}), ['moo']); | ||
t.deepEqual(m(['moo', 'MOO'], ['*oo'], {caseSensitive: false}), ['moo', 'MOO']); | ||
t.notThrows(() => m([], [])); | ||
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', 'moo'], ['!*o']), ['bar']); | ||
t.deepEqual(matcher(['moo', 'MOO'], ['*oo'], {caseSensitive: true}), ['moo']); | ||
t.deepEqual(matcher(['moo', 'MOO'], ['*oo'], {caseSensitive: false}), ['moo', 'MOO']); | ||
t.notThrows(() => matcher([], [])); | ||
}); | ||
|
||
test('matcher.isMatch()', t => { | ||
t.true(m.isMatch('unicorn', 'unicorn')); | ||
t.true(m.isMatch('MOO', 'MOO')); | ||
t.true(m.isMatch('unicorn', 'uni*')); | ||
t.true(m.isMatch('UNICORN', 'unicorn', {caseSensitive: false})); | ||
t.true(m.isMatch('unicorn', '*corn')); | ||
t.true(m.isMatch('unicorn', 'un*rn')); | ||
t.true(m.isMatch('foo unicorn bar', '*unicorn*')); | ||
t.true(m.isMatch('unicorn', '*')); | ||
t.true(m.isMatch('UNICORN', 'UNI*', {caseSensitive: true})); | ||
t.false(m.isMatch('UNICORN', 'unicorn', {caseSensitive: true})); | ||
t.false(m.isMatch('unicorn', '')); | ||
t.false(m.isMatch('unicorn', '!unicorn')); | ||
t.false(m.isMatch('unicorn', '!uni*')); | ||
t.false(m.isMatch('unicorn', 'uni\\*')); | ||
t.true(m.isMatch('unicorn', '!tricorn')); | ||
t.true(m.isMatch('unicorn', '!tri*')); | ||
t.true(matcher.isMatch('unicorn', 'unicorn')); | ||
t.true(matcher.isMatch('MOO', 'MOO')); | ||
t.true(matcher.isMatch('unicorn', 'uni*')); | ||
t.true(matcher.isMatch('UNICORN', 'unicorn', {caseSensitive: false})); | ||
t.true(matcher.isMatch('unicorn', '*corn')); | ||
t.true(matcher.isMatch('unicorn', 'un*rn')); | ||
t.true(matcher.isMatch('foo unicorn bar', '*unicorn*')); | ||
t.true(matcher.isMatch('unicorn', '*')); | ||
t.true(matcher.isMatch('UNICORN', 'UNI*', {caseSensitive: true})); | ||
t.false(matcher.isMatch('UNICORN', 'unicorn', {caseSensitive: true})); | ||
t.false(matcher.isMatch('unicorn', '')); | ||
t.false(matcher.isMatch('unicorn', '!unicorn')); | ||
t.false(matcher.isMatch('unicorn', '!uni*')); | ||
t.false(matcher.isMatch('unicorn', 'uni\\*')); | ||
t.true(matcher.isMatch('unicorn', '!tricorn')); | ||
t.true(matcher.isMatch('unicorn', '!tri*')); | ||
}); |