'use strict';

function jsHelper(...args) {
  return args.reduce((result, path, i) => {
    if (i) result += '\n';

    if (Array.isArray(path)) {
      return result + Reflect.apply(jsHelper, this, path);
    }
    if (!path.includes('?') && !path.endsWith('.js')) path += '.js';
    return `${result}<script src="${this.url_for(path)}"></script>`;
  }, '');
}

module.exports = jsHelper;

@@ -12,99 +12,278 @@
 ';%0A%0A
-function jsHelper(...args) %7B%0A  return args.reduce((result, path, i) =%3E %7B%0A    if (i)
+const flatten = function(arr, result = %5B%5D) %7B%0A  for (const i in arr) %7B%0A    const value = arr%5Bi%5D;%0A    if (Array.isArray(value)) %7B%0A      flatten(value, result);%0A    %7D else %7B%0A      result.push(value);%0A    %7D%0A  %7D%0A  return result;%0A%7D;%0A%0Afunction jsHelper(...args) %7B%0A  let
  result 
 += '
@@ -282,17 +282,16 @@
 ult 
-+
 = '%5Cn';%0A
 %0A   
@@ -286,25 +286,44 @@
 = '%5Cn';%0A
-%0A
   
+let items = args;%0A%0A
   if (
+!
 Array.is
@@ -328,20 +328,20 @@
 isArray(
-path
+args
 )) %7B%0A   
@@ -345,100 +345,195 @@
     
-  return result + Reflect.apply(jsHelper, this, path);%0A    %7D%0A    if (!path.includes('?') && 
+items = %5Bargs%5D;%0A  %7D%0A%0A  items = flatten(items);%0A%0A  items.forEach(item =%3E %7B%0A    // Old syntax%0A    if (typeof item === 'string' %7C%7C item instanceof String) %7B%0A      let path = item;%0A      if (
 !pat
@@ -550,16 +550,26 @@
 ('.js'))
+ %7B%0A       
  path +=
@@ -584,25 +584,29 @@
     
-return %60$%7B
+  %7D%0A      
 result
-%7D
+ += %60
 %3Cscr
@@ -650,19 +650,525 @@
 ipt%3E
+%5Cn
 %60;%0A  
+ 
-%7D, '')
+ %7D else %7B%0A      // New syntax%0A      let tmpResult = '%3Cscript';%0A      for (const attribute in item) %7B%0A        if (attribute === 'src') %7B%0A          item%5Battribute%5D = this.url_for(item%5Battribute%5D);%0A          if (!item%5Battribute%5D.endsWith('.js')) item%5Battribute%5D += '.js';%0A        %7D%0A%0A        if (item%5Battribute%5D === true) tmpResult += ' ' + attribute;%0A        else tmpResult += %60 $%7Battribute%7D=%22$%7Bitem%5Battribute%5D%7D%22%60;%0A      %7D%0A      tmpResult += '%3E%3C/script%3E%5Cn';%0A      result += tmpResult;%0A    %7D%0A  %7D);%0A  return result
 ;%0A%7D%0A

add support for custom attribute