glob-parent 0.1.0

Extract the non-magic parent path from a glob: foo/bar/*.js -> foo/bar. A faithful port of the glob-parent npm package. Zero dependencies, no_std.
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented4 out of 5 items with examples
  • Size
  • Source code size: 34.14 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 158.06 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 6s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/glob-parent
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

glob-parent

Crates.io Documentation CI License

Extract the non-magic parent path from a globfoo/bar/*.jsfoo/bar, path/**/xpath. The directory a file watcher should actually watch. A faithful Rust port of the glob-parent npm package (used by gulp, chokidar, and many others), bundling ports of is-glob and is-extglob. Zero dependencies and #![no_std].

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.

[dependencies]
glob-parent = "0.1"

API

Item Purpose
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 or MIT at your option.