Skip to content

Commit

Permalink
Merge pull request #73 from silverwind/nodeps
Browse files Browse the repository at this point in the history
Remove `process` and `path` dependencies, document `windows` option
  • Loading branch information
jonschlinkert committed Oct 29, 2023
2 parents 13efdf0 + ee6ebcc commit 49d10c4
Show file tree
Hide file tree
Showing 19 changed files with 2,819 additions and 2,811 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Creates a matcher function from one or more glob patterns. The returned function

**Example**

By default, `picomatch` uses [`os.platform()`](https://nodejs.org/api/os.html#osplatform) to detect the operating system.

```js
const picomatch = require('picomatch');
// picomatch(glob[, options]);
Expand All @@ -128,6 +130,23 @@ console.log(isMatch('a.a')); //=> false
console.log(isMatch('a.b')); //=> true
```

**Example without node.js**

For environments without `node.js`, `picomatch/posix` provides you a dependency-free matcher, without automatic OS detection.

```js
const picomatch = require('picomatch/posix');
// the same API, defaulting to posix paths
const isMatch = picomatch('a/*');
console.log(isMatch('a\\b')); //=> false
console.log(isMatch('a/b')); //=> true

// you can still configure the matcher function to accept windows paths
const isMatch = picomatch('a/*', { options: windows });
console.log(isMatch('a\\b')); //=> true
console.log(isMatch('a/b')); //=> true
```

### [.test](lib/picomatch.js#L117)

Test `input` with the given `regex`. This is used by the main `picomatch()` function to test the input string.
Expand Down Expand Up @@ -339,6 +358,7 @@ The following options may be used with the main `picomatch()` function or any of
| `strictSlashes` | `boolean` | `undefined` | When true, picomatch won't match trailing slashes with single stars. |
| `unescape` | `boolean` | `undefined` | Remove backslashes preceding escaped characters in the glob pattern. By default, backslashes are retained. |
| `unixify` | `boolean` | `undefined` | Alias for `posixSlashes`, for backwards compatibility. |
| `windows` | `boolean` | `false` | Also accept backslashes as the path separator. |

picomatch has automatic detection for regex positive and negative lookbehinds. If the pattern contains a negative lookbehind, you must be using Node.js >= 8.10 or else picomatch will throw an error.

Expand Down Expand Up @@ -484,7 +504,7 @@ isMatch('baz');
| **Character** | **Description** |
| --- | --- |
| `*` | Matches any character zero or more times, excluding path separators. Does _not match_ path separators or hidden files or directories ("dotfiles"), unless explicitly enabled by setting the `dot` option to `true`. |
| `**` | Matches any character zero or more times, including path separators. Note that `**` will only match path separators (`/`, and `\\` on Windows) when they are the only characters in a path segment. Thus, `foo**/bar` is equivalent to `foo*/bar`, and `foo/a**b/bar` is equivalent to `foo/a*b/bar`, and _more than two_ consecutive stars in a glob path segment are regarded as _a single star_. Thus, `foo/***/bar` is equivalent to `foo/*/bar`. |
| `**` | Matches any character zero or more times, including path separators. Note that `**` will only match path separators (`/`, and `\\` with the `windows` option) when they are the only characters in a path segment. Thus, `foo**/bar` is equivalent to `foo*/bar`, and `foo/a**b/bar` is equivalent to `foo/a*b/bar`, and _more than two_ consecutive stars in a glob path segment are regarded as _a single star_. Thus, `foo/***/bar` is equivalent to `foo/*/bar`. |
| `?` | Matches any character excluding path separators one time. Does _not match_ path separators or leading dots. |
| `[abc]` | Matches any characters inside the brackets. For example, `[abc]` would match the characters `a`, `b` or `c`, and nothing else. |
Expand Down
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
'use strict';

module.exports = require('./lib/picomatch');
const os = require('os');
const pico = require('./lib/picomatch');

const isWindows = os.platform() === 'win32';

function picomatch(glob, options, returnState = false) {
// default to os.platform()
if (options.windows === null || options.windows === undefined) {
options.windows = isWindows;
}
return pico(glob, options, returnState);
}

module.exports = picomatch;
// public api
module.exports.test = pico.test;
module.exports.matchBase = pico.matchBase;
module.exports.isMatch = pico.isMatch;
module.exports.parse = pico.parse;
module.exports.scan = pico.scan;
module.exports.compileRe = pico.compileRe;
module.exports.toRegex = pico.toRegex;
// for tests
module.exports.makeRe = pico.makeRe;
10 changes: 5 additions & 5 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const path = require('path');
const WIN_SLASH = '\\\\/';
const WIN_NO_SLASH = `[^${WIN_SLASH}]`;

Expand All @@ -23,6 +22,7 @@ const NO_DOT_SLASH = `(?!${DOT_LITERAL}{0,1}${END_ANCHOR})`;
const NO_DOTS_SLASH = `(?!${DOTS_SLASH})`;
const QMARK_NO_DOT = `[^.${SLASH_LITERAL}]`;
const STAR = `${QMARK}*?`;
const SEP = '/';

const POSIX_CHARS = {
DOT_LITERAL,
Expand All @@ -39,7 +39,8 @@ const POSIX_CHARS = {
NO_DOTS_SLASH,
QMARK_NO_DOT,
STAR,
START_ANCHOR
START_ANCHOR,
SEP
};

/**
Expand All @@ -59,7 +60,8 @@ const WINDOWS_CHARS = {
NO_DOTS_SLASH: `(?!${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
QMARK_NO_DOT: `[^.${WIN_SLASH}]`,
START_ANCHOR: `(?:^|[${WIN_SLASH}])`,
END_ANCHOR: `(?:[${WIN_SLASH}]|$)`
END_ANCHOR: `(?:[${WIN_SLASH}]|$)`,
SEP: '\\'
};

/**
Expand Down Expand Up @@ -153,8 +155,6 @@ module.exports = {
CHAR_VERTICAL_LINE: 124, /* | */
CHAR_ZERO_WIDTH_NOBREAK_SPACE: 65279, /* \uFEFF */

SEP: path.sep,

/**
* Create EXTGLOB_CHARS
*/
Expand Down
6 changes: 2 additions & 4 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ const parse = (input, options) => {
const tokens = [bos];

const capture = opts.capture ? '' : '?:';
const win32 = utils.isWindows(options);

// create constants based on platform, for windows or posix
const PLATFORM_CHARS = constants.globChars(win32);
const PLATFORM_CHARS = constants.globChars(opts.windows);
const EXTGLOB_CHARS = constants.extglobChars(PLATFORM_CHARS);

const {
Expand Down Expand Up @@ -1010,7 +1009,6 @@ parse.fastpaths = (input, options) => {
}

input = REPLACEMENTS[input] || input;
const win32 = utils.isWindows(options);

// create constants based on platform, for windows or posix
const {
Expand All @@ -1023,7 +1021,7 @@ parse.fastpaths = (input, options) => {
NO_DOTS_SLASH,
STAR,
START_ANCHOR
} = constants.globChars(win32);
} = constants.globChars(opts.windows);

const nodot = opts.dot ? NO_DOTS : NO_DOT;
const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
Expand Down
7 changes: 3 additions & 4 deletions lib/picomatch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const path = require('path');
const scan = require('./scan');
const parse = require('./parse');
const utils = require('./utils');
Expand Down Expand Up @@ -49,7 +48,7 @@ const picomatch = (glob, options, returnState = false) => {
}

const opts = options || {};
const posix = utils.isWindows(options);
const posix = opts.windows;
const regex = isState
? picomatch.compileRe(glob, options)
: picomatch.makeRe(glob, options, false, true);
Expand Down Expand Up @@ -158,9 +157,9 @@ picomatch.test = (input, regex, options, { glob, posix } = {}) => {
* @api public
*/

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

/**
Expand Down
17 changes: 8 additions & 9 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const path = require('path');
const win32 = process.platform === 'win32';
const {
REGEX_BACKSLASH,
REGEX_REMOVE_BACKSLASH,
Expand Down Expand Up @@ -29,13 +27,6 @@ exports.supportsLookbehinds = () => {
return false;
};

exports.isWindows = options => {
if (options && typeof options.windows === 'boolean') {
return options.windows;
}
return win32 === true || path.sep === '\\';
};

exports.escapeLast = (input, char, lastIdx) => {
const idx = input.lastIndexOf(char, lastIdx);
if (idx === -1) return input;
Expand All @@ -62,3 +53,11 @@ exports.wrapOutput = (input, state = {}, options = {}) => {
}
return output;
};

exports.basename = (path, { windows } = {}) => {
if (windows) {
return path.replace(/[\\/]$/, '').replace(/.*[\\/]/, '');
} else {
return path.replace(/\/$/, '').replace(/.*\//, '');
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "MIT",
"files": [
"index.js",
"posix.js",
"lib"
],
"main": "index.js",
Expand Down
3 changes: 3 additions & 0 deletions posix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./lib/picomatch');
17 changes: 17 additions & 0 deletions test/api.posix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const assert = require('assert');
const picomatch = require('../posix');

describe('picomatch/posix', () => {
it('should use posix paths only by default', () => {
const match = picomatch('a/**');
assert(match('a/b'));
assert(!match('a\\b'));
});
it('should still be manually configurable to accept non-posix paths', () => {
const match = picomatch('a/**', { windows: true });
assert(match('a\\b'));
assert(match('a/b'));
});
});
Loading

0 comments on commit 49d10c4

Please sign in to comment.