# glob-parent
[](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`.
## License
Licensed under either of [Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at
your option.