Skip to content

Commit

Permalink
Improve type matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Apr 5, 2019
1 parent 02257b9 commit cb143fe
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ const normalizePath = require('normalize-path');
const {sep} = require('path'); // required for tests.

/**
* @typedef {String|RegExp|{(string:String): Boolean}} AnymatchPattern
* @typedef {(string:String) => Boolean} AnymatchStrBoolFn
* @typedef {String|RegExp|AnymatchStrBoolFn} AnymatchPattern
* @typedef {AnymatchPattern|Array<AnymatchPattern>} AnymatchMatcher
*/

const BANG = '!';
const arrify = (item) => Array.isArray(item) ? item : [item];

/**
*
* @param {AnymatchPattern} matcher
* @returns {AnymatchStrBoolFn}
*/
const createPattern = (matcher) => (string) => {
if (typeof matcher === 'function') {
return matcher(string);
Expand All @@ -31,12 +37,12 @@ const createPattern = (matcher) => (string) => {
* @param {Boolean=} returnIndex
* @returns {Boolean|Number|Function}
*/
const anymatch = (matchers, testString, returnIndex=false) => {
const anymatch = (matchers, testString, returnIndex = false) => {
if (matchers == null) {
throw new TypeError('anymatch: specify first argument');
}
if (testString == null) {
return (testString, ri=false) => {
return (testString, ri = false) => {
const returnIndex = typeof ri === 'boolean' ? ri : false;
return anymatch(matchers, testString, returnIndex);
}
Expand All @@ -61,7 +67,7 @@ const anymatch = (matchers, testString, returnIndex=false) => {
}

const patterns = arrified.map(createPattern);
for (let index=0; index < patterns.length; index++) {
for (let index = 0; index < patterns.length; index++) {
const pattern = patterns[index];
if (pattern(unixified)) {
return returnIndex ? index : true;
Expand Down

0 comments on commit cb143fe

Please sign in to comment.