or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Glob Parent

Glob 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.

Package Information

  • Package Name: glob-parent
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install glob-parent

Core Imports

const globParent = require('glob-parent');

For ES modules:

import globParent from 'glob-parent';

Basic Usage

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'

Capabilities

Glob Parent Extraction

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:

  • Wildcards: *, **, ?
  • Braces: {a,b}, {to,from}
  • Brackets: [a-z], [bar]
  • Extended globs: !(pattern), ?(pattern), +(pattern), *(pattern), @(pattern)
  • Parentheses: (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'

Cross-Platform Behavior

The function handles cross-platform path separators automatically:

  • Windows: Backslashes are converted to forward slashes by default
  • POSIX: Forward slashes are used natively
  • Configuration: Set 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'

Escape Characters

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'

Types

/**
 * Configuration options for glob-parent
 */
interface GlobParentOptions {
  /** Controls automatic conversion of backslashes to forward slashes on Windows (default: true) */
  flipBackslashes?: boolean;
}