glob-parent
    Overview
    Documentation
    Tutorials
    Chat
    Insights
    Code
    Dependencies
    Contributors
    Jobs

glob-parent

Extract the non-magic parent path from a glob string.

5.0.0  •  Updated 1 years ago  •  by gulpjs  •  ISC License

glob-parent

NPM version Downloads Azure Pipelines Build Status Travis Build Status AppVeyor Build Status Coveralls Status Gitter chat

Extract the non-magic parent path from a glob string.

Usage

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

globParent('path/to/*.js'); // 'path/to'
globParent('/root/path/to/*.js'); // '/root/path/to'
globParent('/*.js'); // '/'
globParent('*.js'); // '.'
globParent('**/*.js'); // '.'
globParent('path/{to,from}'); // 'path'
globParent('path/!(to|from)'); // 'path'
globParent('path/?(to|from)'); // 'path'
globParent('path/+(to|from)'); // 'path'
globParent('path/*(to|from)'); // 'path'
globParent('path/@(to|from)'); // 'path'
globParent('path/**/*'); // 'path'

// if provided a non-glob path, returns the nearest dir
globParent('path/foo/bar.js'); // 'path/foo'
globParent('path/foo/'); // 'path/foo'
globParent('path/foo'); // 'path' (see issue #3 for details)

API

globParent(maybeGlobString)

Takes a string and returns the part of the path before the glob begins. Be aware of Escaping rules and Limitations below.

Escaping

The following characters have special significance in glob patterns and must be escaped if you want them to be treated as regular path characters:

  • ? (question mark) unless used as a path segment alone
  • * (asterisk)
  • | (pipe)
  • ( (opening parenthesis)
  • ) (closing parenthesis)
  • { (opening curly brace)
  • } (closing curly brace)
  • [ (opening bracket)
  • ] (closing bracket)

Example

globParent('foo/[bar]/') // 'foo'
globParent('foo/\\[bar]/') // 'foo/[bar]'

Limitations

Braces & Brackets

This library attempts a quick and imperfect method of determining which path parts have glob magic without fully parsing/lexing the pattern. There are some advanced use cases that can trip it up, such as nested braces where the outer pair is escaped and the inner one contains a path separator. If you find yourself in the unlikely circumstance of being affected by this or need to ensure higher-fidelity glob handling in your library, it is recommended that you pre-process your input with expand-braces and/or expand-brackets.

Windows

Backslashes are not valid path separators for globs. If a path with backslashes is provided anyway, for simple cases, glob-parent will replace the path separator for you and return the non-glob parent path (now with forward-slashes, which are still valid as Windows path separators).

This cannot be used in conjunction with escape characters.

// BAD
globParent('C:\\Program Files \\(x86\\)\\*.ext') // 'C:/Program Files /(x86/)'

// GOOD
globParent('C:/Program Files\\(x86\\)/*.ext') // 'C:/Program Files (x86)'

If you are using escape characters for a pattern without path parts (i.e. relative to cwd), prefix with ./ to avoid confusing glob-parent.

// BAD
globParent('foo \\[bar]') // 'foo '
globParent('foo \\[bar]*') // 'foo '

// GOOD
globParent('./foo \\[bar]') // 'foo [bar]'
globParent('./foo \\[bar]*') // '.'

License

ISC

Popularity

Maintenance

Development

Last ver 1 year ago
Created 5 years ago
Last commit 6 months ago
1 month between commits

Technology

Node version: 10.15.1
7.3K unpacked

Compliance

ISC License
OSI Approved
0 vulnerabilities

Contributors

7 contributors
Elan Shanker
45 commits, 7 merges, 3 PRs
Works at Akamai
Blaine Bublitz
Maintainer, 7 commits, 2 merges, 2 PRs
Works at Paying the maintainers Tidelift
jonschlinkert
Maintainer, 6 commits, 2 PRs
Works at sellside
doowb
Maintainer, 2 commits
Works at Sellside, Inc.
Paul Miller
2 commits, 3 PRs
Works at hy-dev
wtgtybhertgeghgtwtg
1 commits, 1 PRs

Tags

glob
parent
strip
path
dirname
directory
base
wildcard
Openbase helps developers choose among and use millions of open-source packages, so they can build amazing products faster.
FacebookTwitterLinkedIn
© 2020 Devstore, Inc.