# glob-parent
[](#contributors-)
[](https://crates.io/crates/glob-parent)
[](https://docs.rs/glob-parent)
[](https://github.com/trananhtung/glob-parent/actions/workflows/ci.yml)
[](#license)
**Extract the non-magic parent path from a glob** — `foo/bar/*.js` → `foo/bar`,
`path/**/x` → `path`. The directory a file watcher should actually watch. A faithful
Rust port of the [`glob-parent`](https://www.npmjs.com/package/glob-parent) npm
package (used by gulp, chokidar, and many others), bundling ports of `is-glob` and
`is-extglob`. Zero dependencies and `#![no_std]`.
```rust
use glob_parent::glob_parent;
assert_eq!(glob_parent("path/to/*.js"), "path/to");
assert_eq!(glob_parent("path/*/file.js"), "path");
assert_eq!(glob_parent("path/**/*.js"), "path");
assert_eq!(glob_parent("/path/to/file.js"), "/path/to");
assert_eq!(glob_parent("*.js"), ".");
```
## Why glob-parent?
To watch the files a glob matches, you first need the deepest real directory it
lives under — everything before the first wildcard, brace set, bracket class, or
extglob. Getting that right means understanding glob syntax (including escapes and
enclosures that span path separators), which is exactly what the canonical JS
module does. This ports it faithfully.
```toml
[dependencies]
glob-parent = "0.1"
```
## API
| `glob_parent(glob)` | The non-magic parent path |
| `glob_parent_with_options(glob, flip_backslashes)` | Control the Windows backslash flip |
| `is_glob(s)` | Whether `s` looks like a glob (the `is-glob` strict check) |
| `is_extglob(s)` | Whether `s` contains an extglob (`@(…)`, `!(…)`, …) |
## Behavior
- Returns the path up to (but not including) the first segment containing glob
magic; a pattern with no parent yields `"."`.
- Understands `*`, `?`, `**`, brace sets `{a,b}`, bracket classes `[a-z]`,
extglobs `@(…)`/`!(…)`/`+(…)`/`?(…)`/`*(…)`, and backslash escapes.
- A trailing path separator is preserved as a directory.
- On Windows, backslashes are flipped to forward slashes (when the input has no
forward slash); elsewhere backslashes are escapes. Control this with
`glob_parent_with_options`.
## Contributors ✨
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/trananhtung"><img src="https://avatars.githubusercontent.com/u/30992229?v=4?s=100" width="100px;" alt="Tung Tran"/><br /><sub><b>Tung Tran</b></sub></a><br /><a href="https://github.com/trananhtung/glob-parent/commits?author=trananhtung" title="Code">💻</a> <a href="#maintenance-trananhtung" title="Maintenance">🚧</a></td>
</tr>
</tbody>
</table>
## License
Licensed under either of [Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at
your option.