Comparing version 3.0.0 to 4.0.0
@@ -16,28 +16,6 @@ declare namespace matcher { | ||
/** | ||
Simple [wildcard](https://en.wikipedia.org/wiki/Wildcard_character) matching. | ||
It matches even across newlines. For example, `foo*r` will match `foo\nbar`. | ||
@param inputs - Strings to match. | ||
@param patterns - Use `*` to match zero or more characters. A pattern starting with `!` will be negated. | ||
@returns The `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[]; | ||
/** | ||
It matches even across newlines. For example, `foo*r` will match `foo\nbar`. | ||
@param input - String or array of strings to match. | ||
@param pattern - String or array of string patterns. Use `*` to match zero or more characters. A pattern starting with `!` will be negated. | ||
@param inputs - String or array of strings to match. | ||
@param patterns - String or array of string patterns. Use `*` to match zero or more characters. A pattern starting with `!` will be negated. | ||
@returns Whether any given `input` matches every given `pattern`. | ||
@@ -81,7 +59,53 @@ | ||
//=> false | ||
matcher.isMatch('unicorn', ['']); | ||
//=> false | ||
matcher.isMatch('unicorn', []); | ||
//=> false | ||
matcher.isMatch([], 'bar'); | ||
//=> false | ||
matcher.isMatch([], []); | ||
//=> false | ||
matcher.isMatch([''], ['']); | ||
//=> true | ||
``` | ||
*/ | ||
isMatch: (input: string | readonly string[], pattern: string | readonly string[], options?: matcher.Options) => boolean; | ||
isMatch: (inputs: string | readonly string[], patterns: string | readonly string[], options?: matcher.Options) => boolean; | ||
/** | ||
Simple [wildcard](https://en.wikipedia.org/wiki/Wildcard_character) matching. | ||
It matches even across newlines. For example, `foo*r` will match `foo\nbar`. | ||
@param inputs - String or array of strings to match. | ||
@param patterns - String or array of string patterns. Use `*` to match zero or more characters. A pattern starting with `!` will be negated. | ||
@returns The `inputs` filtered based on the `patterns`. | ||
@example | ||
``` | ||
import matcher = require('matcher'); | ||
matcher(['foo', 'bar', 'moo'], ['*oo', '!foo']); | ||
//=> ['moo'] | ||
matcher(['foo', 'bar', 'moo'], ['!*oo']); | ||
//=> ['bar'] | ||
matcher('moo', ['']); | ||
//=> [] | ||
matcher('moo', []); | ||
//=> [] | ||
matcher([''], ['']); | ||
//=> [''] | ||
``` | ||
*/ | ||
(inputs: string | readonly string[], patterns: string | readonly string[], options?: matcher.Options): string[]; | ||
}; | ||
export = matcher; |
48
index.js
@@ -6,2 +6,29 @@ 'use strict'; | ||
function sanitizeArray(input, inputName) { | ||
if (!Array.isArray(input)) { | ||
switch (typeof input) { | ||
case 'string': | ||
input = [input]; | ||
break; | ||
case 'undefined': | ||
input = []; | ||
break; | ||
default: | ||
throw new TypeError(`Expected '${inputName}' to be a string or an array, but got a type of '${typeof input}'`); | ||
} | ||
} | ||
return input.filter(string => { | ||
if (typeof string !== 'string') { | ||
if (typeof string === 'undefined') { | ||
return false; | ||
} | ||
throw new TypeError(`Expected '${inputName}' to be an array of strings, but found a type of '${typeof string}' in the array`); | ||
} | ||
return true; | ||
}); | ||
} | ||
function makeRegexp(pattern, options) { | ||
@@ -35,8 +62,7 @@ options = { | ||
module.exports = (inputs, patterns, options) => { | ||
if (!(Array.isArray(inputs) && Array.isArray(patterns))) { | ||
throw new TypeError(`Expected two arrays, got ${typeof inputs} ${typeof patterns}`); | ||
} | ||
inputs = sanitizeArray(inputs, 'inputs'); | ||
patterns = sanitizeArray(patterns, 'patterns'); | ||
if (patterns.length === 0) { | ||
return inputs; | ||
return []; | ||
} | ||
@@ -68,8 +94,12 @@ | ||
module.exports.isMatch = (input, pattern, options) => { | ||
const inputArray = Array.isArray(input) ? input : [input]; | ||
const patternArray = Array.isArray(pattern) ? pattern : [pattern]; | ||
module.exports.isMatch = (inputs, patterns, options) => { | ||
inputs = sanitizeArray(inputs, 'inputs'); | ||
patterns = sanitizeArray(patterns, 'patterns'); | ||
return inputArray.some(input => { | ||
return patternArray.every(pattern => { | ||
if (patterns.length === 0) { | ||
return false; | ||
} | ||
return inputs.some(input => { | ||
return patterns.every(pattern => { | ||
const regexp = makeRegexp(pattern, options); | ||
@@ -76,0 +106,0 @@ const matches = regexp.test(input); |
{ | ||
"name": "matcher", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Simple wildcard matching", | ||
"license": "MIT", | ||
"repository": "sindresorhus/matcher", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
@@ -46,10 +47,5 @@ "name": "Sindre Sorhus", | ||
"matcha": "^0.7.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.30.0" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"@typescript-eslint/member-ordering": "off" | ||
} | ||
"tsd": "^0.14.0", | ||
"xo": "^0.38.2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# matcher [![Build Status](https://travis-ci.com/sindresorhus/matcher.svg?branch=master)](https://travis-ci.com/sindresorhus/matcher) | ||
# matcher | ||
@@ -24,2 +24,11 @@ > Simple [wildcard](https://en.wikipedia.org/wiki/Wildcard_character) matching | ||
matcher('moo', ['']); | ||
//=> [] | ||
matcher('moo', []); | ||
//=> [] | ||
matcher([''], ['']); | ||
//=> [''] | ||
matcher.isMatch('unicorn', 'uni*'); | ||
@@ -57,2 +66,17 @@ //=> true | ||
//=> false | ||
matcher.isMatch('unicorn', ['']); | ||
//=> false | ||
matcher.isMatch('unicorn', []); | ||
//=> false | ||
matcher.isMatch([], 'bar'); | ||
//=> false | ||
matcher.isMatch([], []); | ||
//=> false | ||
matcher.isMatch('', ''); | ||
//=> true | ||
``` | ||
@@ -66,3 +90,3 @@ | ||
Accepts an array of `input`'s and `pattern`'s. | ||
Accepts a string or an array of strings for both `inputs` and `patterns`. | ||
@@ -73,3 +97,3 @@ Returns an array of `inputs` filtered based on the `patterns`. | ||
Accepts either a string or array of strings for both `input` and `pattern`. | ||
Accepts a string or an array of strings for both `inputs` and `patterns`. | ||
@@ -76,0 +100,0 @@ Returns a `boolean` of whether any given `input` matches every given `pattern`. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9956
160
145