Skip to content

Commit

Permalink
Merge pull request #18 from nhsevidence/GN-18-Fix-IE-autocomplete
Browse files Browse the repository at this point in the history
GN-18 Fix autocomplete in IE9+
  • Loading branch information
lizaadebowale authored May 15, 2019
2 parents d78d03e + 4683826 commit 5b79532
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 5b79532

Please sign in to comment.