diff --git a/@kiva/kv-components/utils/arrayUtils.js b/@kiva/kv-components/utils/arrayUtils.js new file mode 100644 index 00000000..c02bfe05 --- /dev/null +++ b/@kiva/kv-components/utils/arrayUtils.js @@ -0,0 +1,31 @@ +/** + * Groups an array of objects by a specified key. + * @param {Array} array - The array of objects to be grouped. + * @param {string} groupByKey - The key based on which the grouping should be done. + * @returns {Object} - An object where keys are unique values of the specified key, + * and values are arrays of objects that share the same key value. + */ + +const groupBy = (array, groupByKey) => { + return array.reduce((groups, currentItem) => { + const keyValue = currentItem[groupByKey]; + if (!groups[keyValue]) { + // eslint-disable-next-line no-param-reassign + groups[keyValue] = []; + } + groups[keyValue].push(currentItem); + return groups; + }, {}); +}; + +/** + * Returns a comparator function for sorting an array of objects by a specified key. + * @param {string} key - The key based on which the sorting should be done. + * @returns {Function} - A comparator function that can be used with the Array.sort() method. + */ +const sortBy = (key) => { + // eslint-disable-next-line no-nested-ternary + return (a, b) => ((a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0)); +}; + +export { groupBy, sortBy }; diff --git a/@kiva/kv-components/utils/comparators.js b/@kiva/kv-components/utils/comparators.js index fefed30d..6e7815f9 100644 --- a/@kiva/kv-components/utils/comparators.js +++ b/@kiva/kv-components/utils/comparators.js @@ -38,3 +38,26 @@ export function startsWith(query, key) { return bi === 0 ? 1 : abc()(as, bs); }; } + +/** + * Returns a comparison function to be used by Array.prototype.sort(). The comparison function will + * sort elements to match their relative position in the given list. + * + * const list = ['a', 'b', 'c', 'd']; + * const arrayToSort = ['d', 'a']; + * arrayToSort.sort(indexIn(list)); // will return ['a','d'] + * + * @param {array} list + * @param {string} [key] - compare property 'key' of elements rather than the elements themselves + * @returns {function} + */ +export function indexIn(list, key) { + if (!Array.isArray(list)) { + throw new TypeError('list must be an array'); + } + return (a, b) => { + const as = key ? a[key] : a; + const bs = key ? b[key] : b; + return list.indexOf(as) < list.indexOf(bs) ? -1 : 1; + }; +} diff --git a/@kiva/kv-components/utils/siteSearch.js b/@kiva/kv-components/utils/siteSearch.js index a0096e63..1b145908 100644 --- a/@kiva/kv-components/utils/siteSearch.js +++ b/@kiva/kv-components/utils/siteSearch.js @@ -4,6 +4,7 @@ import { useEventListener } from './event'; import markMatches from './markMatches'; import { hasExcludedQueryParams } from './loanSearch/queryParamUtils'; import SearchEngine from './searchEngine'; +import { groupBy } from './arrayUtils'; // instances const instances = {}; @@ -37,15 +38,7 @@ function makeInstance() { const suggestionSections = computed(() => { // Group the results by their group name - const resultGroups = rawSuggestions.value.reduce((groups, result) => { - const { group } = result; - if (!groups[group]) { - // eslint-disable-next-line no-param-reassign - groups[group] = []; - } - groups[group].push(result); - return groups; - }, {}); + const resultGroups = groupBy(rawSuggestions.value, 'group'); // From the groups, build the sections of suggestions to display return Object.keys(resultGroups) diff --git a/@kiva/kv-components/vue/KvWwwHeader.vue b/@kiva/kv-components/vue/KvWwwHeader.vue index 021b9c64..24273772 100644 --- a/@kiva/kv-components/vue/KvWwwHeader.vue +++ b/@kiva/kv-components/vue/KvWwwHeader.vue @@ -30,6 +30,7 @@ @@ -108,14 +115,13 @@ diff --git a/@kiva/kv-components/vue/KvWwwHeader/KvHeaderSearchBar.vue b/@kiva/kv-components/vue/KvWwwHeader/KvHeaderSearchBar.vue index 24de6553..c90f0789 100644 --- a/@kiva/kv-components/vue/KvWwwHeader/KvHeaderSearchBar.vue +++ b/@kiva/kv-components/vue/KvWwwHeader/KvHeaderSearchBar.vue @@ -36,6 +36,7 @@