glob-parent

中文文档
6.0.2发布于 2021/09/30ISCCommonJS近 30 天下载 12.9亿

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

原始使用文档
文档目录8 节
  1. Usage
  2. API
  3. globParent(maybeGlobString, [options])
  4. Escaping
  5. Limitations
  6. Braces & Brackets
  7. Windows
  8. License

glob-parent

NPM version Downloads Build Status Coveralls Status

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, [options])

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

options

{
  // Disables the automatic conversion of slashes for Windows
  flipBackslashes: true;
}

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

版本与发布记录

glob-parent 共发布 16 个版本,首次发布于 2015/01/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.0.2latest2021/09/305 年前
6.0.12021/07/205 年前
6.0.02021/05/045 年前
5.1.22021/03/075 年前
5.1.12020/03/226 年前
5.1.02019/09/227 年前

关于 glob-parent 的常见问题

glob-parent 是什么?

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

如何安装 glob-parent?

在项目目录运行 npm install glob-parent,或使用 pnpm add / yarn add / bun add。

glob-parent 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/glob-parent 是否存在。

glob-parent 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

glob-parent 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10.13.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

glob-parent 可以商用吗?

glob-parent 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

glob-parent 的下载量有多少?

根据 npm 官方下载统计,glob-parent 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,294,574,823 次,日均约 43,152,494 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

glob-parent 还在维护吗?

最新版本 6.0.2 发布于 2021/09/30(5 年前);至今共发布 16 个版本,其中过去 12 个月发布了 0 个;首次发布于 2015/01/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。