Extract the non-magic parent path from a glob string.
npx @tessl/cli install tessl/npm-glob-parent@6.0.0Glob Parent is a utility library for extracting the non-magic parent path from glob strings. It intelligently parses glob patterns to identify the static directory portion before any wildcard characters, handling complex patterns including braces, brackets, and various glob operators while maintaining cross-platform compatibility.
npm install glob-parentconst globParent = require('glob-parent');For ES modules:
import globParent from 'glob-parent';const globParent = require('glob-parent');
// Extract parent paths from globs
globParent('path/to/*.js'); // 'path/to'
globParent('/root/path/to/*.js'); // '/root/path/to'
globParent('*.js'); // '.'
globParent('**/*.js'); // '.'
// Handle complex glob patterns
globParent('path/{to,from}'); // 'path'
globParent('path/[a-z]'); // 'path'
globParent('path/!(foo|bar)'); // 'path'
// Non-glob paths return nearest directory
globParent('path/foo/bar.js'); // 'path/foo'
globParent('path/foo/'); // 'path/foo'Extracts the non-magic parent path from a glob string, returning the static directory portion before any glob patterns begin.
/**
* Extract the non-magic parent path from a glob string
* @param {string} str - The glob pattern or path string to analyze
* @param {Object} [opts] - Configuration options
* @param {boolean} [opts.flipBackslashes=true] - Controls automatic conversion of backslashes to forward slashes on Windows
* @returns {string} The parent directory path before any glob magic begins
*/
function globParent(str, opts);The function supports various glob patterns:
*, **, ?{a,b}, {to,from}[a-z], [bar]!(pattern), ?(pattern), +(pattern), *(pattern), @(pattern)(a|b)Usage Examples:
const globParent = require('glob-parent');
// Basic wildcard patterns
globParent('path/to/*.js'); // 'path/to'
globParent('/root/**/*.js'); // '/root'
globParent('**/*.js'); // '.'
// Brace patterns
globParent('path/{to,from}'); // 'path'
globParent('/{a,b}'); // '/'
// Bracket patterns
globParent('path/[a-z]'); // 'path'
globParent('[a-z]'); // '.'
// Extended glob patterns
globParent('path/!(foo|bar)'); // 'path'
globParent('path/?(to|from)'); // 'path'
globParent('path/+(to|from)'); // 'path'
globParent('path/*(to|from)'); // 'path'
globParent('path/@(to|from)'); // 'path'
// Parentheses patterns
globParent('path/(a|b)'); // 'path'
globParent('(a|b)'); // '.'
// Non-glob paths
globParent('path/foo/bar.js'); // 'path/foo'
globParent('path/foo/'); // 'path/foo'
globParent('path/foo'); // 'path'The function handles cross-platform path separators automatically:
flipBackslashes: false to disable automatic conversion// Windows paths (automatic conversion)
globParent('path\\to\\*.js'); // 'path/to'
globParent('C:\\Program Files\\*.exe'); // 'C:/Program Files'
// Disable backslash conversion
globParent('path\\to\\*.js', { flipBackslashes: false }); // 'path\\to'Special glob characters can be escaped with backslashes to treat them as literal path characters:
// Escaped characters are treated as literals
globParent('foo/\\[bar]/'); // 'foo/[bar]'
globParent('foo/\\*bar'); // 'foo/*bar'
globParent('./foo\\ \\[bar]'); // 'foo [bar]'
// Without escaping, they're treated as glob patterns
globParent('foo/[bar]/'); // 'foo'
globParent('foo/*bar'); // 'foo'/**
* Configuration options for glob-parent
*/
interface GlobParentOptions {
/** Controls automatic conversion of backslashes to forward slashes on Windows (default: true) */
flipBackslashes?: boolean;
}