import uniqueId from 'lodash/uniqueId';
import npmsRequest from '../../util/npmsRequest';

const maxResults = 10;

// TODO: Use a LRU to cache results

function normalizeQuery(query) {
    return query
    .replace(/[a-z0-9]+:([a-z0-9:]+)*/ig, '')  // Remove qualifiers
    .replace(/\s\s*/g, ' ')                    // Remove extra spaces
    .trim();
}

export function reset() {
    return {
        type: 'SearchSuggestions.RESET',
    };
}

export function fetch(query) {
    return (dispatch, getState) => {
        query = normalizeQuery(query);

        // Do nothing if current query is the same
        if (getState().searchSuggestions.query === query) {
            return;
        }

        // Reset if query is empty
        if (!query) {
            return dispatch(reset());
        }

        dispatch({
            type: 'SearchSuggestions.FETCH',
            meta: { uid: uniqueId('search-suggestions-') },
            payload: {
                data: query,
                promise: npmsRequest(`/search/suggestions?q=${encodeURIComponent(query)}&size=${maxResults}`),
            },
        })
        .catch(() => {});  // SearchSuggestions.FETCH_REJECTED will be dispatched, so swallow any error
    };
}

@@ -213,33 +213,64 @@
 ce(/
-%5Ba-z0-9%5D+:(%5Ba-z0-9:%5D+)*/i
+(%5CS+:'(?:%5B%5E'%5C%5C%5D%7C%5C%5C.)*')%7C(%5CS+:%22(?:%5B%5E%22%5C%5C%5D%7C%5C%5C.)*%22)%7C%5CS+:%5CS+/
 g, '
@@ -321,16 +321,47 @@
 /g, ' ')
+                               
         
@@ -394,16 +394,28 @@
 a spaces
+ left behind
 %0A    .tr

Improve removal of qualifiers when fetching suggestions.