🎯 micromatch
A minimal utility for matching strings against glob patterns. Supports standard wildcards (*, **, ?) and character ranges ([a-z]). Can also negate patterns using !.
Import
1import { micromatch } from "hbh-nodes";
escapeRegex(str)
Escapes special regex characters in a string.
Parameters:
str(string) – Input string.
Returns: Escaped string safe for regex.
Example:
1const escaped = micromatch.escapeRegex("file?.txt"); // "file\?.txt"
globToRegex(pattern)
Converts a glob pattern to a JavaScript RegExp.
Parameters:
pattern(string) – Glob pattern (e.g.,"*.js"or"src/**/index.js").
Returns:
RegExp – Equivalent regex for matching.
Example:
1const regex = micromatch.globToRegex("src/**/*.js");2console.log(regex.test("src/app/index.js")); // true
Edge Cases:
*matches any sequence except/.**matches any sequence including/.?matches any single character except/.- Character ranges like
[a-z]are preserved.
micromatch(list, pattern)
Filters a list of strings based on a glob pattern. Supports negation with a leading !.
Parameters:
list(string[]) – Array of strings to match.pattern(string) – Glob pattern.
Returns: Array of matched strings.
Example:
1const files = ["index.js", "style.css", "app.js"];2console.log(micromatch.micromatch(files, "*.js")); // ["index.js", "app.js"]3console.log(micromatch.micromatch(files, "!*.js")); // ["style.css"]
matchGlob(pattern, str)
Tests if a single string matches a glob pattern.
Parameters:
pattern(string) – Glob pattern.str(string) – String to test.
Returns:
boolean – true if the string matches the pattern.
Example:
1console.log(micromatch.matchGlob("*.js", "index.js")); // true2console.log(micromatch.matchGlob("*.js", "style.css")); // false