function GroupBy(predicate, options) { return function runGroupBy(...rawSources) { const sources = normalizeReshapeSources(rawSources); if (sources.length === 0) { return []; } const referenceSource = sources[0]; const items = flatten(sources.map((source) => source.getItems())); const groupedItems = items.reduce((acc, item) => { const key = predicate(item); if (!acc.hasOwnProperty(key)) { acc[key] = []; } acc[key].push(item); return acc; }, {}); return Object.entries(groupedItems).map(([groupName, groupItems]) => { const userSource = options.getSource({ name: groupName, items: groupItems, }); return { ...referenceSource, sourceId: groupName, getItems() { return groupItems; }, ...userSource, templates: { header({html}) { return html ` ${name}
`; }, ...referenceSource.templates, ...userSource.templates, }, }; }); }; } autocomplete({ container: '#autocomplete', placeholder: 'Ara', detachedMediaQuery: '', plugins: [recentSearchesPlugin], openOnFocus: true, reshape({ sourcesBySourceId }) { const { recentSearchesPlugin, ...rest } = sourcesBySourceId; return [ GroupBy(products), Object.values(rest), ]; }, });