How to use the GLOBSTAR function from minimatch

Find comprehensive JavaScript minimatch.GLOBSTAR code examples handpicked from public code repositorys.

minimatch.GLOBSTAR is a constant that represents the pattern ** in minimatch, which matches zero or more directories and subdirectories.

353
354
355
356
357
358
359
360
361
362

//if ignored, skip _processing
if (childrenIgnored(this, read))
  return cb()

var isGlobStar = remain[0] === minimatch.GLOBSTAR
if (isGlobStar)
  this._processGlobStar(prefix, read, abs, remain, index, inGlobStar, cb)
else
  this._processReaddir(prefix, read, abs, remain, index, inGlobStar, cb)
fork icon0
star icon1
watch icon1

How does minimatch.GLOBSTAR work?

minimatch.GLOBSTAR is a constant in the minimatch library that is used to represent the ** pattern in glob matching. This pattern matches zero or more directories and subdirectories in a file path. For example, if you have the file path src/js/main.js, the pattern src/**/* would match all files and directories within the src directory and its subdirectories, including src/js/main.js. The minimatch.GLOBSTAR constant is used in conjunction with other glob patterns to create more complex matching patterns. For example, the pattern src/**/*.js would match all JavaScript files within the src directory and its subdirectories. minimatch.GLOBSTAR works by using a recursive algorithm to match directory and subdirectory names in a file path. When it encounters the ** pattern, it matches all directories and subdirectories at that level of the file path, and then recursively matches all directories and subdirectories within those directories. Overall, minimatch.GLOBSTAR is a powerful tool for creating complex glob patterns that can match files and directories in a flexible and customizable way.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const minimatch = require("minimatch");

const files = [
  "src/app.js",
  "src/utils/helpers.js",
  "src/components/header.js",
  "src/components/footer.js",
];

const jsFiles = files.filter((file) => {
  return minimatch(file, "src/**/*.js");
});

console.log(jsFiles);
// Output: ['src/app.js', 'src/utils/helpers.js', 'src/components/header.js', 'src/components/footer.js']

In this example, we're using minimatch.GLOBSTAR to match all JavaScript files within the src directory and its subdirectories. We start by defining an array of file paths that we want to filter, and then use the Array.filter method to create a new array of files that match the src/**/*.js pattern. This pattern matches all files with a .js extension within the src directory and its subdirectories. Finally, we log the resulting array of JavaScript files to the console. Overall, this example demonstrates how minimatch.GLOBSTAR can be used to match files and directories in a flexible and powerful way.