Skip to content

Commit

Permalink
feat: hoist regexps and strings for performance gains
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr authored and phated committed Jan 27, 2021
1 parent 2a551dd commit 4a80667
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
'use strict';

var isglob = require('is-glob');
var isGlob = require('is-glob');
var pathDirname = require('path-dirname');
var isWin32 = require('os').platform() === 'win32';

var slash = '/';
var backslash = /\\/g;
var enclosure = /[\{\[].*[\/]*.*[\}\]]$/;
var globby = /(^|[^\\])([\{\[]|\([^\)]+$)/;
var escaped = /\\([\*\?\|\[\]\(\)\{\}])/g;

module.exports = function globParent(str) {
// flip windows path separators
if (isWin32 && str.indexOf('/') < 0) {
str = str.split('\\').join('/');
if (isWin32 && str.indexOf(slash) < 0) {
str = str.replace(backslash, slash);
}

// special case for strings ending in enclosure containing path separator
if (/[\{\[].*[\/]*.*[\}\]]$/.test(str)) {
str += '/';
if (enclosure.test(str)) {
str += slash;
}

// preserves full path in case of trailing path separator
Expand All @@ -21,8 +27,8 @@ module.exports = function globParent(str) {
// remove path parts that are globby
do {
str = pathDirname.posix(str);
} while (isglob(str) || /(^|[^\\])([\{\[]|\([^\)]+$)/.test(str));
} while (isGlob(str) || globby.test(str));

// remove escape chars and return result
return str.replace(/\\([\*\?\|\[\]\(\)\{\}])/g, '$1');
return str.replace(escaped, '$1');
};

0 comments on commit 4a80667

Please sign in to comment.