Skip to content

Commit

Permalink
GN-18 Fix autocomplete in IE9+
Browse files Browse the repository at this point in the history
  • Loading branch information
ediblecode committed May 14, 2019
1 parent d78d03e commit 4683826
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions src/Header/Search/Autocomplete/suggester/term-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,25 @@ export function findTerms(queryTokens, suggestionTokens) {
});
});

const firstMatchedSuggestionToken = unusedSuggestionTokens.find(function(
suggestionToken
) {
return suggestionToken.str.indexOf(queryToken.str) === 0;
});
// We use a for loop here because Array.prototype.find isn't supported in IE9
// And we didn't want to pollute the global scope with a polyfill
let firstMatchedSuggestionToken = null;
for (let i = 0; i < unusedSuggestionTokens.length; i++) {
const suggestionToken = unusedSuggestionTokens[i];
if (suggestionToken.str.indexOf(queryToken.str) === 0) {
firstMatchedSuggestionToken = suggestionToken;
break;
}
}

termMatches.push({
suggestionToken: firstMatchedSuggestionToken,
queryToken: queryToken
});
}

// const termMatches = queryTokens.map(function(queryToken) {
// const firstMatchedSuggestionToken = suggestionTokens.find(function(
// suggestionToken
// ) {
// return suggestionToken.str.indexOf(queryToken.str) === 0;
// });
// return {
// suggestionToken: firstMatchedSuggestionToken,
// queryToken: queryToken
// };
// });

const deDupedTermMatches = termMatches;

// const deDupedTermMatches = termMatches.filter(function(termMatch, index) {
// const prevMatches = termMatches.slice(0, index);

// const tokenHasAlreadyBeenUsed = prevMatches.some(function(prevMatch) {
// return prevMatch.suggestionToken === termMatch.suggestionToken;
// });

// return !tokenHasAlreadyBeenUsed;
// });

// Only consider term matches if we match *every* token in the query
return deDupedTermMatches.every(function(match) {
return !!match.suggestionToken;
Expand Down

0 comments on commit 4683826

Please sign in to comment.