Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected a conditional expression and instead saw an assignment

Tags:

javascript

I have the following function in my javascript code:

  addParam(url, param, value) {
    var a = document.createElement('a'), regex = /(?:\?|&|&)+([^=]+)(?:=([^&]*))*/g;
    var match, str = []; a.href = url; param = encodeURIComponent(param);

    while(match = regex.exec(a.search)) {
      if(param != match[1]) {
        str.push(match[1] + (match[2] ? '=' + match[2] : ''));
      }
    }

    str.push(param + (value ? '=' + encodeURIComponent(value) : ''));
    a.search = str.join('&');
    return a.href;
  }

eslint returns me following error:

  306:15  error  Expected a conditional expression and instead saw an assignment  no-cond-assign

The problem is this while statement:

while(match = regex.exec(a.search)) {
  if(param != match[1]) {
    str.push(match[1] + (match[2] ? '=' + match[2] : ''));
  }
}

How can I rewrite this to fix that?

like image 777
Mateusz Urbański Avatar asked Oct 23 '17 06:10

Mateusz Urbański


1 Answers

You want to do:

while((match = regex.exec(a.search))) {
  if(param != match[1]) {
    str.push(match[1] + (match[2] ? '=' + match[2] : ''));
  }
}

you need that extra set of parentheses

like image 180
Alexander Mills Avatar answered Nov 19 '22 01:11

Alexander Mills