Search code examples
javascriptarraysnode.jsglobminimatch

What is the simplest way to prefix all strings in an array with a "!" (exclamation mark) in javascript?


I have two variables I use for when I want to either include or exclude system files (using minimatch glob syntax, where the ! stands for exclusion).

var systemFiles = ['node_modules/**/*', '.git/**/*'];
var ignoredSystemFiles = ['!node_modules/**/*', '!.git/**/*'];

But this feels a bit redundant. Is there a simple way to turn the systemFiles array into the ignoredSystemFiles array (i.e.: prefix all items in systemFiles with a !)?

I know !systemFiles does not work, but something as compact as this would be awesome, as it would allow me to eliminate the ignoredSystemFiles variable.


Solution

  • You can use this

    var systemFiles = ['node_modules/**/*', '.git/**/*'];
    var ignoredSystemFiles = systemFiles.map(function(el) { return '!' + el } );