10

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

Alan Souza
  • 7,475
  • 10
  • 46
  • 68
  • hey, I'm not familiar with 2 asterisks in a row in a glob expression, in your sample what is the difference between `*` and `**`? – chiliNUT Dec 13 '16 at 01:00
  • I think I understand your question, I just don't understand what the meaning of 2 stars in a row is in a glob expression – chiliNUT Dec 13 '16 at 01:03
  • 1
    Your edit doesn' bring a response to the (good) question from @chiliNUT! So, again: what is `**` supposed to mean? – cFreed Dec 13 '16 at 01:05
  • 2
    Sorry again. It means it will match any subdirectories levels in the path. I'm not an expert in the glob pattern myself, so check this out: http://stackoverflow.com/questions/8532929/two-asterisks-in-file-path – Alan Souza Dec 13 '16 at 01:08
  • 1
    cool, I just learned something new :). So, my idea would be to convert the glob to a regex, which javascript can then use to validate path names. here's an npm that can do that: https://www.npmjs.com/package/glob-to-regexp – chiliNUT Dec 13 '16 at 01:17
  • Yeah i just got to the same conclusion. would you mind posting as an answer so I can accept it? It would be nice if node-glob supported it by default (I will deal this as a feature request in the project itself) – Alan Souza Dec 13 '16 at 01:18

2 Answers2

12

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/

chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • 2
    well I'm using node-glob myself. but it does not have the "match" support I'm looking for. – Alan Souza Dec 13 '16 at 01:25
  • off the tip of my head (haven't tested) it seems like you could do something like replace a single star with `([^./]*)` and double star with `([^/]+/*)+` and just call that the regex – chiliNUT Dec 13 '16 at 01:40
  • @AlanSouza also found `minimatch` though it still might have the same limitations as `node-glob` since its by the same guy – chiliNUT Dec 16 '16 at 18:37
7

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
AJ Richardson
  • 6,610
  • 1
  • 49
  • 59