From 46838263842d3ec9e92b632381826d154544d6ad Mon Sep 17 00:00:00 2001 From: Ian Routledge Date: Tue, 14 May 2019 16:47:49 +0100 Subject: [PATCH] GN-18 Fix autocomplete in IE9+ --- .../Autocomplete/suggester/term-finder.js | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/src/Header/Search/Autocomplete/suggester/term-finder.js b/src/Header/Search/Autocomplete/suggester/term-finder.js index 0781f125..d92f5d28 100644 --- a/src/Header/Search/Autocomplete/suggester/term-finder.js +++ b/src/Header/Search/Autocomplete/suggester/term-finder.js @@ -12,11 +12,16 @@ 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, @@ -24,30 +29,8 @@ export function findTerms(queryTokens, suggestionTokens) { }); } - // 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;