How to use the escapePath function from fast-glob

Find comprehensive JavaScript fast-glob.escapePath code examples handpicked from public code repositorys.

fast-glob.escapePath is a function that escapes special characters in a file path, making it safe to use in a regular expression pattern.

149
150
151
152
153
154
155
156
157
158
		? path.join(globCWD, entry)
		: path.normalize(entry);

	if (fs.existsSync(absolutePath)) {
		// This path points to a file. Return an escaped path to avoid globbing
		return fastGlob.escapePath(normalizePath(entry));
	}

	return entry;
});
fork icon966
star icon0
watch icon91

+ 8 other calls in file

361
362
363
364
365
366
367
368
369
370

case "file":
  compilation.fileDependencies.add(absoluteFrom);
  logger.debug(`added '${absoluteFrom}' as a file dependency`);
  pattern.context = path.dirname(absoluteFrom);
  glob = fastGlob.escapePath(normalizePath(path.resolve(absoluteFrom)));

  if (typeof globOptions.dot === "undefined") {
    globOptions.dot = true;
  }
fork icon0
star icon0
watch icon1

+ 4 other calls in file

How does fast-glob.escapePath work?

fast-glob.escapePath is a function that takes a file path as input and escapes any special characters in the path, making it safe to use in a regular expression pattern. When searching for files using a pattern, such as *.js, the pattern is typically converted into a regular expression that can be matched against file paths. However, if the file path contains special characters that have special meaning in regular expressions, such as . or *, the regular expression may not match the intended files. To avoid this problem, fast-glob.escapePath can be used to escape any special characters in the file path, ensuring that the resulting regular expression matches only the intended files. The function replaces each special character with its escaped form, which is a backslash followed by the character itself. For example, if the input file path is path/to/*.js, fast-glob.escapePath would escape the * character, resulting in the escaped path path/to/\*.js. This escaped path can then be used in a regular expression pattern to match only the .js files in the path/to/ directory. Overall, fast-glob.escapePath provides a simple and efficient way to escape special characters in a file path, ensuring that it can be safely used in a regular expression pattern.

Ai Example

1
2
3
4
5
6
7
8
9
const fg = require("fast-glob");
const { escapePath } = require("fast-glob/out/utils/patterns");

const inputPath = "path/to/*.js";
const escapedPath = escapePath(inputPath);

const files = fg.sync(escapedPath);

console.log(files);

In this example, we first import the fast-glob library, as well as the escapePath function from the patterns utility module. We then define an input file path path/to/*.js that contains a special character, *. We pass the input file path to escapePath, which escapes the * character and returns the escaped file path path/to/\*.js. Finally, we pass the escaped file path to fast-glob.sync, which searches for files that match the pattern and returns an array of matching file paths. We log the result of this search to the console. By escaping the special character in the file path using fast-glob.escapePath, we ensure that the resulting regular expression pattern matches only the intended files.