1

I am using coffeescript with grunt and am trying to only include JS files file pattern "*.min.js". Somehow my test is failing though and all files get included. So my if statement always returns true. How do I make this work?

getJsDependencies = ->
  js_dependencies_path = path.join __dirname, "js", "dep"
  paths = []
  for js_file in fs.readdirSync(js_dependencies_path)
    file_path = path.join __dirname, "js", "dep", js_file
    console.log js_file
    if js_file.indexOf ".min.js", 0 > 0
      paths.push file_path
  paths  

I tried all kinds of combinations of js_file.indexOf but I am not having any luck with having only .min.js files included. In fact, I want to exclude them but I am stuck with string matching not the logic.

Help is appreciated!

Tim Hysniu
  • 1,446
  • 13
  • 24

1 Answers1

2

If we add the implicit function calling parentheses to

if js_file.indexOf ".min.js", 0 > 0

we get:

if js_file.indexOf(".min.js", 0 > 0)

so you're really passing false to indexOf as its second argument and that doesn't make a lot of sense.

I think you want to say this:

if js_file.indexOf(".min.js", 0) > 0

Or since the second argument to String.prototype.indexOf is zero anyway:

if js_file.indexOf('.min.js') > 0

but even here you need the parentheses when calling indexOf to ensure that you're comparing the indexOf return value with zero.

mu is too short
  • 426,620
  • 70
  • 833
  • 800