Skip to content
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
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},

"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2018,
"sourceType": "module"
},

"rules": {
Expand Down
2 changes: 1 addition & 1 deletion bench/first-match-picomatch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const pm = require('..');
const { default: pm } = require('..');

console.time('picomatch');
console.log(pm.makeRe('**/*').test('foo/bar/baz/qux.js'));
Expand Down
2 changes: 1 addition & 1 deletion bench/glob-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Suite } = require('benchmark');
const { red } = require('ansi-colors');
const argv = require('minimist')(process.argv.slice(2));
const parent = require('glob-parent');
const scan = require('../lib/scan');
const { default: scan } = require('../lib/scan');

/**
* Setup
Expand Down
2 changes: 1 addition & 1 deletion bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Suite } = require('benchmark');
const { red } = require('ansi-colors');
const minimist = require('minimist');
const mm = require('minimatch');
const pm = require('..');
const { default: pm } = require('..');

const argv = minimist(process.argv.slice(2));

Expand Down
2 changes: 1 addition & 1 deletion bench/load-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

console.log('# Load time');
console.time('picomatch');
exports.pm = require('..');
exports.pm = require('..').default;
console.timeEnd('picomatch');
console.time('minimatch');
exports.mm = require('minimatch');
Expand Down
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use strict';
import pico from './lib/picomatch.js';
import { isWindows } from './lib/utils.js';

const pico = require('./lib/picomatch');
const utils = require('./lib/utils');

function picomatch(glob, options, returnState = false) {
export default function picomatch(glob, options, returnState = false) {
// default to os.platform()
if (options && (options.windows === null || options.windows === undefined)) {
// don't mutate the original options object
options = { ...options, windows: utils.isWindows() };
options = { ...options, windows: isWindows() };
}

return pico(glob, options, returnState);
}

Object.assign(picomatch, pico);
module.exports = picomatch;
4 changes: 1 addition & 3 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const WIN_SLASH = '\\\\/';
const WIN_NO_SLASH = `[^${WIN_SLASH}]`;

Expand Down Expand Up @@ -85,7 +83,7 @@ const POSIX_REGEX_SOURCE = {
xdigit: 'A-Fa-f0-9'
};

module.exports = {
export default {
MAX_LENGTH: 1024 * 64,
POSIX_REGEX_SOURCE,

Expand Down
32 changes: 14 additions & 18 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const constants = require('./constants');
const utils = require('./utils');
import constants from './constants.js';
import { escapeRegex, removePrefix, wrapOutput, hasRegexChars, escapeLast } from './utils.js';

/**
* Constants
Expand Down Expand Up @@ -31,7 +29,7 @@ const expandRange = (args, options) => {
/* eslint-disable-next-line no-new */
new RegExp(value);
} catch (ex) {
return args.map(v => utils.escapeRegex(v)).join('..');
return args.map(v => escapeRegex(v)).join('..');
}

return value;
Expand All @@ -52,7 +50,7 @@ const syntaxError = (type, char) => {
* @return {Object}
*/

const parse = (input, options) => {
export default function parse(input, options) {
if (typeof input !== 'string') {
throw new TypeError('Expected a string');
}
Expand Down Expand Up @@ -126,7 +124,7 @@ const parse = (input, options) => {
tokens
};

input = utils.removePrefix(input, state);
input = removePrefix(input, state);
len = input.length;

const extglobs = [];
Expand Down Expand Up @@ -319,7 +317,7 @@ const parse = (input, options) => {
return state;
}

state.output = utils.wrapOutput(output, state, options);
state.output = wrapOutput(output, state, options);
return state;
}

Expand Down Expand Up @@ -432,7 +430,7 @@ const parse = (input, options) => {
*/

if (state.quotes === 1 && value !== '"') {
value = utils.escapeRegex(value);
value = escapeRegex(value);
prev.value += value;
append({ value });
continue;
Expand Down Expand Up @@ -522,11 +520,11 @@ const parse = (input, options) => {

// when literal brackets are explicitly disabled
// assume we should match with a regex character class
if (opts.literalBrackets === false || utils.hasRegexChars(prevValue)) {
if (opts.literalBrackets === false || hasRegexChars(prevValue)) {
continue;
}

const escaped = utils.escapeRegex(prev.value);
const escaped = escapeRegex(prev.value);
state.output = state.output.slice(0, -prev.value.length);

// when literal brackets are explicitly enabled
Expand Down Expand Up @@ -954,19 +952,19 @@ const parse = (input, options) => {

while (state.brackets > 0) {
if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ']'));
state.output = utils.escapeLast(state.output, '[');
state.output = escapeLast(state.output, '[');
decrement('brackets');
}

while (state.parens > 0) {
if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ')'));
state.output = utils.escapeLast(state.output, '(');
state.output = escapeLast(state.output, '(');
decrement('parens');
}

while (state.braces > 0) {
if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', '}'));
state.output = utils.escapeLast(state.output, '{');
state.output = escapeLast(state.output, '{');
decrement('braces');
}

Expand All @@ -988,7 +986,7 @@ const parse = (input, options) => {
}

return state;
};
}

/**
* Fast paths for creating regular expressions for common glob patterns.
Expand Down Expand Up @@ -1072,7 +1070,7 @@ parse.fastpaths = (input, options) => {
}
};

const output = utils.removePrefix(input, state);
const output = removePrefix(input, state);
let source = create(output);

if (source && opts.strictSlashes !== true) {
Expand All @@ -1081,5 +1079,3 @@ parse.fastpaths = (input, options) => {

return source;
};

module.exports = parse;
40 changes: 16 additions & 24 deletions lib/picomatch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

const scan = require('./scan');
const parse = require('./parse');
const utils = require('./utils');
const constants = require('./constants');
import scan from './scan.js';
import parse from './parse.js';
import { toPosixSlashes, basename } from './utils.js';
import constants from './constants.js';
const isObject = val => val && typeof val === 'object' && !Array.isArray(val);

/**
Expand All @@ -14,7 +12,7 @@ const isObject = val => val && typeof val === 'object' && !Array.isArray(val);
* returns an object with additional information.
*
* ```js
* const picomatch = require('picomatch');
* import picomatch from 'picomatch';
* // picomatch(glob[, options]);
*
* const isMatch = picomatch('*.!(*a)');
Expand All @@ -28,7 +26,7 @@ const isObject = val => val && typeof val === 'object' && !Array.isArray(val);
* @api public
*/

const picomatch = (glob, options, returnState = false) => {
export default function picomatch(glob, options, returnState = false) {
if (Array.isArray(glob)) {
const fns = glob.map(input => picomatch(input, options, returnState));
const arrayMatcher = str => {
Expand Down Expand Up @@ -94,14 +92,14 @@ const picomatch = (glob, options, returnState = false) => {
}

return matcher;
};
}

/**
* Test `input` with the given `regex`. This is used by the main
* `picomatch()` function to test the input string.
*
* ```js
* const picomatch = require('picomatch');
* import picomatch from 'picomatch';
* // picomatch.test(input, regex[, options]);
*
* console.log(picomatch.test('foo/bar', /^(?:([^/]*?)\/([^/]*?))$/));
Expand All @@ -123,7 +121,7 @@ picomatch.test = (input, regex, options, { glob, posix } = {}) => {
}

const opts = options || {};
const format = opts.format || (posix ? utils.toPosixSlashes : null);
const format = opts.format || (posix ? toPosixSlashes : null);
let match = input === glob;
let output = (match && format) ? format(input) : input;

Expand All @@ -147,7 +145,7 @@ picomatch.test = (input, regex, options, { glob, posix } = {}) => {
* Match the basename of a filepath.
*
* ```js
* const picomatch = require('picomatch');
* import picomatch from 'picomatch';
* // picomatch.matchBase(input, glob[, options]);
* console.log(picomatch.matchBase('foo/bar.js', '*.js'); // true
* ```
Expand All @@ -159,14 +157,14 @@ picomatch.test = (input, regex, options, { glob, posix } = {}) => {

picomatch.matchBase = (input, glob, options) => {
const regex = glob instanceof RegExp ? glob : picomatch.makeRe(glob, options);
return regex.test(utils.basename(input));
return regex.test(basename(input));
};

/**
* Returns true if **any** of the given glob `patterns` match the specified `string`.
*
* ```js
* const picomatch = require('picomatch');
* import picomatch from 'picomatch';
* // picomatch.isMatch(string, patterns[, options]);
*
* console.log(picomatch.isMatch('a.a', ['b.*', '*.a'])); //=> true
Expand All @@ -186,7 +184,7 @@ picomatch.isMatch = (str, patterns, options) => picomatch(patterns, options)(str
* expression.
*
* ```js
* const picomatch = require('picomatch');
* import picomatch from 'picomatch';
* const result = picomatch.parse(pattern[, options]);
* ```
* @param {String} `pattern`
Expand All @@ -204,7 +202,7 @@ picomatch.parse = (pattern, options) => {
* Scan a glob pattern to separate the pattern into segments.
*
* ```js
* const picomatch = require('picomatch');
* import picomatch from 'picomatch';
* // picomatch.scan(input[, options]);
*
* const result = picomatch.scan('!./foo/*.js');
Expand Down Expand Up @@ -267,7 +265,7 @@ picomatch.compileRe = (state, options, returnOutput = false, returnState = false
* Create a regular expression from a parsed glob pattern.
*
* ```js
* const picomatch = require('picomatch');
* import picomatch from 'picomatch';
* const state = picomatch.parse('*.js');
* // picomatch.compileRe(state[, options]);
*
Expand Down Expand Up @@ -304,7 +302,7 @@ picomatch.makeRe = (input, options = {}, returnOutput = false, returnState = fal
* Create a regular expression from the given regex source string.
*
* ```js
* const picomatch = require('picomatch');
* import picomatch from 'picomatch';
* // picomatch.toRegex(source[, options]);
*
* const { output } = picomatch.parse('*.js');
Expand Down Expand Up @@ -333,9 +331,3 @@ picomatch.toRegex = (source, options) => {
*/

picomatch.constants = constants;

/**
* Expose "picomatch"
*/

module.exports = picomatch;
19 changes: 8 additions & 11 deletions lib/scan.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const utils = require('./utils');
import { removeBackslashes } from './utils.js';
import constants from './constants.js';
const {
CHAR_ASTERISK, /* * */
CHAR_AT, /* @ */
Expand All @@ -17,7 +16,7 @@ const {
CHAR_RIGHT_CURLY_BRACE, /* } */
CHAR_RIGHT_PARENTHESES, /* ) */
CHAR_RIGHT_SQUARE_BRACKET /* ] */
} = require('./constants');
} = constants;

const isPathSeparator = code => {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
Expand All @@ -36,7 +35,7 @@ const depth = token => {
* with `!(`) and `negatedExtglob` (true if the path starts with `!(`).
*
* ```js
* const pm = require('picomatch');
* import pm from 'picomatch';
* console.log(pm.scan('foo/bar/*.js'));
* { isGlob: true, input: 'foo/bar/*.js', base: 'foo/bar', glob: '*.js' }
* ```
Expand All @@ -46,7 +45,7 @@ const depth = token => {
* @api public
*/

const scan = (input, options) => {
export default function scan(input, options) {
const opts = options || {};

const length = input.length - 1;
Expand Down Expand Up @@ -317,10 +316,10 @@ const scan = (input, options) => {
}

if (opts.unescape === true) {
if (glob) glob = utils.removeBackslashes(glob);
if (glob) glob = removeBackslashes(glob);

if (base && backslashes === true) {
base = utils.removeBackslashes(base);
base = removeBackslashes(base);
}
}

Expand Down Expand Up @@ -386,6 +385,4 @@ const scan = (input, options) => {
}

return state;
};

module.exports = scan;
}
Loading