Bug 1371554 - Change .indexOf(...) == 0 to the more expressive .startsWith(...). r=mattn. draft
authorJonathan Guillotte-Blouin <jguillotteblouin@mozilla.com>
Tue, 06 Jun 2017 13:35:34 -0700
changeset 591585 5c354a54a0fc
parent 591584 bb004a70937f
child 632553 eb61b770d93f
push id63098
push userbmo:jguillotteblouin@mozilla.com
push dateFri, 09 Jun 2017 07:56:46 +0000
reviewersmattn
bugs1371554
milestone55.0a1
Bug 1371554 - Change .indexOf(...) == 0 to the more expressive .startsWith(...). r=mattn. MozReview-Commit-ID: Jr3ALp6v2rM
toolkit/components/satchel/nsFormAutoComplete.js
--- a/toolkit/components/satchel/nsFormAutoComplete.js
+++ b/toolkit/components/satchel/nsFormAutoComplete.js
@@ -530,23 +530,29 @@ FormAutoComplete.prototype = {
    * searchTokens -- array of tokens of the search string
    *
    * Returns: an int
    */
   _calculateScore(entry, aSearchString, searchTokens) {
     let boundaryCalc = 0;
     // for each word, calculate word boundary weights
     for (let token of searchTokens) {
-      boundaryCalc += (entry.textLowerCase.indexOf(token) == 0);
-      boundaryCalc += (entry.textLowerCase.includes(" " + token));
+      if (entry.textLowerCase.startsWith(token)) {
+        boundaryCalc++;
+      }
+      if (entry.textLowerCase.includes(" " + token)) {
+        boundaryCalc++;
+      }
     }
     boundaryCalc = boundaryCalc * this._boundaryWeight;
     // now add more weight if we have a traditional prefix match and
     // multiply boundary bonuses by boundary weight
-    boundaryCalc += this._prefixWeight * (entry.textLowerCase.indexOf(aSearchString) == 0);
+    if (entry.textLowerCase.startsWith(aSearchString)) {
+      boundaryCalc += this._prefixWeight;
+    }
     entry.totalScore = Math.round(entry.frecency * Math.max(1, boundaryCalc));
   }
 
 }; // end of FormAutoComplete implementation
 
 // nsIAutoCompleteResult implementation
 function FormAutoCompleteResult(client,
                                 entries,