Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for OR in gulp JavaScript variable

This should be easy. I have this path variable declared in a gulpfile.js:

paths = {
    img: [
    'src/patternlab/**/**/*.jpg',
    'src/patternlab/**/**/*.png'
  ]
};

I'd like to use a simple OR switch for the file extension, along the lines of:

'src/patternlab/**/**/*.(jpg|png)'

But I can't find the right syntax! Have tried numerous permutations, looked through MDN etc…

like image 410
Jake Rayson Avatar asked May 13 '14 10:05

Jake Rayson


People also ask

What is Gulp used for JS?

Gulp is a task runner that uses Node. js as a platform. Gulp purely uses the JavaScript code and helps to run front-end tasks and large-scale web applications. It builds system automated tasks like CSS and HTML minification, concatenating library files, and compiling the SASS files.

What was used by the gulp file js or JSON?

Gulp is built upon Node. js and it already has a strong community that builds various plugins for performing various tasks. There are currenlty more than 3000+ plugins available here which makes almost every possible tasks we can think of can be automated. The gulp tasks are writted in javascript file called gulpfile.


2 Answers

The patterns used by gulp are those promoted by npm's glob package which doesn't use a precise regex pattern matching syntax, I imagine because these things start to look horrific when matching against paths.

the documentation regarding minimatch (used by glob) cover your described case:

'/**/*.+(jpg|png|tiff)'
like image 89
Benjamin Conlan Avatar answered Oct 29 '22 18:10

Benjamin Conlan


have you tried this :

^src\/patternlab\/.{2}\/.{2}\/.\.(jpg|png)$

the regex you have mentioned will also match files which have file names like some.png something else.

try this in your console:

var newarr = []
var paths = {
    img: [
    'src/patternlab/**/**/*.jpg',
    'src/patternlab/**/**/*.png',
    'src/patternlab/**/**/*.tiff',
    'src/patternlab/**/**/*.jpg'
  ]
};
for(var i=0;i<paths.img.length;i++){
  if (/^src\/patternlab\/.{2}\/.{2}\/.\.(jpg|png)$/.test(paths.img[i])){
    newarr.push(paths.img[i]);
  }
}

console.log(newarr);
like image 35
aelor Avatar answered Oct 29 '22 18:10

aelor