Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize #52

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.DS_Store
npm-debug.log
coverage
.nyc_output
10 changes: 10 additions & 0 deletions cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function anymatch(matchers: AnymatchMatcher, testString: string | string[], options?: PicomatchOptions | returnIndexOptions | boolean): boolean | number | anymatchFn;
export default anymatch;
export type PicomatchOptions = import("picomatch").PicomatchOptions;
export type AnymatchFn = (testString: string) => boolean;
export type AnymatchPattern = string | RegExp | AnymatchFn;
export type AnymatchMatcher = AnymatchPattern | AnymatchPattern[];
export type returnIndexOptions = {
returnIndex?: boolean;
};
export type anymatchFn = (testString: string, ri: boolean) => anymatchFn | boolean | number;
75 changes: 75 additions & 0 deletions cjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cjs/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function anymatch(matchers: AnymatchMatcher, testString: string | string[], options?: PicomatchOptions | returnIndexOptions | boolean): boolean | number | anymatchFn;
export default anymatch;
export type PicomatchOptions = import("picomatch").PicomatchOptions;
export type AnymatchFn = (testString: string) => boolean;
export type AnymatchPattern = string | RegExp | AnymatchFn;
export type AnymatchMatcher = AnymatchPattern | AnymatchPattern[];
export type returnIndexOptions = {
returnIndex?: boolean;
};
export type anymatchFn = (testString: string, ri: boolean) => anymatchFn | boolean | number;
69 changes: 69 additions & 0 deletions esm/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions esm/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const {anymatch} = require('./cjs')
module.exports = anymatch
20 changes: 0 additions & 20 deletions index.d.ts

This file was deleted.

61 changes: 42 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
'use strict';

Object.defineProperty(exports, "__esModule", { value: true });
import picomatch from 'picomatch';
import normalizePath from 'normalize-path';

const picomatch = require('picomatch');
const normalizePath = require('normalize-path');

/**
* @typedef {import("picomatch").PicomatchOptions} PicomatchOptions
* @typedef {(testString: string) => boolean} AnymatchFn
* @typedef {string|RegExp|AnymatchFn} AnymatchPattern
* @typedef {AnymatchPattern|AnymatchPattern[]} AnymatchMatcher
* @typedef {{returnIndex?: boolean}} returnIndexOptions
* @typedef {(testString: string, ri: boolean) => anymatchFn|boolean|number} anymatchFn
*/

const BANG = '!';

/**
* @type {returnIndexOptions}
*/
const DEFAULT_OPTIONS = {returnIndex: false};
/**
* @template T
* @param {T|T[]} item
* @returns {T[]}
*/
const arrify = (item) => Array.isArray(item) ? item : [item];


/**
* @param {AnymatchPattern} matcher
* @param {object} options
* @param {AnymatchPattern} matcher
* @param {object} options
* @returns {AnymatchFn}
*/
const createPattern = (matcher, options) => {
Expand All @@ -30,14 +43,14 @@ const createPattern = (matcher, options) => {
if (matcher instanceof RegExp) {
return (string) => matcher.test(string);
}
return (string) => false;
return () => false;
};

/**
* @param {Array<Function>} patterns
* @param {Array<Function>} negPatterns
* @param {String|Array} args
* @param {Boolean} returnIndex
* @param {Function[]} patterns
* @param {Function[]} negPatterns
* @param {string|string[]} args
* @param {boolean} returnIndex
* @returns {boolean|number}
*/
const matchPatterns = (patterns, negPatterns, args, returnIndex) => {
Expand Down Expand Up @@ -67,23 +80,34 @@ const matchPatterns = (patterns, negPatterns, args, returnIndex) => {
return returnIndex ? -1 : false;
};


/**
* @param {AnymatchMatcher} matchers
* @param {Array|string} testString
* @param {object} options
* @returns {boolean|number|Function}
* @param {AnymatchMatcher} matchers
* @param {string|string[]} testString
* @param {PicomatchOptions | returnIndexOptions | boolean} options
* @returns {boolean|number|anymatchFn}
*/
const anymatch = (matchers, testString, options = DEFAULT_OPTIONS) => {
export const anymatch = (matchers, testString, options = DEFAULT_OPTIONS) => {
if (matchers == null) {
throw new TypeError('anymatch: specify first argument');
}
/**
* @type {PicomatchOptions & returnIndexOptions}
*/
const opts = typeof options === 'boolean' ? {returnIndex: options} : options;
/**
* @type {boolean}
*/
const returnIndex = opts.returnIndex || false;

// Early cache for matchers.
const mtchers = arrify(matchers);
const negatedGlobs = mtchers
.filter(item => typeof item === 'string' && item.charAt(0) === BANG)
/**
* @type {string[]}
*/
const stringBangedMatchers = /**@type {string[]} */ (mtchers
.filter(item => typeof item === 'string' && item.charAt(0) === BANG))
const negatedGlobs = stringBangedMatchers
.map(item => item.slice(1))
.map(item => picomatch(item, opts));
const patterns = mtchers
Expand All @@ -100,5 +124,4 @@ const anymatch = (matchers, testString, options = DEFAULT_OPTIONS) => {
return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
};

anymatch.default = anymatch;
module.exports = anymatch;
export default anymatch
3 changes: 3 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {anymatch} from './esm'
export * from './esm'
export default anymatch
Loading