Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does String match glob pattern

I have an array of paths, let's say:

/Users/alansouza/workspace/project/src/js/components/chart/Graph.js

Also I have an entry in a configuration file with additional attributes for this path in a wildcard(glob) format, as in:

{
  '**/*.js': {
    runBabel: true
  }
}

Question: Is there an easy way to validate if the path matches the wildcard expression?

For example:

const matches = Glob.matches(
  '**/*.js',
  '/Users/alansouza/workspace/project/src/js/components/chart/Graph.js'
);

if (matches) {
  //do something
}

FYI: I'm using https://github.com/isaacs/node-glob

like image 353
Alan Souza Avatar asked Dec 13 '16 00:12

Alan Souza


2 Answers

You could convert the glob to to a regex with node glob-to-regex and then validate path strings against the regex.
https://www.npmjs.com/package/glob-to-regexp

it looks like node glob is another option.
https://github.com/isaacs/node-glob

And node has its own glob implementation
https://github.com/isaacs/minimatch

My first thought was to see if phpjs had implemented a glob function but it looks like they haven't :(
http://locutus.io/php/

like image 107
2 revs Avatar answered Oct 05 '22 16:10

2 revs


As @chiliNUT theorized in a comment, minimatch has this behaviour:

minimatch("file.js", "**/*.js") // true/false
minimatch.match(["file1.js", "file2.js"], "**/*.js") // array of matches
like image 40
AJ Richardson Avatar answered Oct 05 '22 15:10

AJ Richardson