@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
79 lines • 3.43 kB
JavaScript
import { sortObjectArrayByStringKey, sortObjectArrayByStringKeyCollator } from './sorting/objects';
/**
*
* @param items
* @param keys
* @param keyDelim
* @param groupFilterKey
* @param groupItemOrderKey
* @param sort
* @param convertNullToEmpty - Added for potential multi-lingual issues
* @param localLanguage
*/
export function groupArrayItemsByField(items, keys, keyDelim, groupFilterKey, groupItemOrderKey, sort, convertNullToEmpty = false, localLanguage = 'en') {
let summary = {
keys: [],
items: [],
groups: [],
filteredGroups: [],
filteredKeys: [],
};
items.map(itemX => {
let item = itemX; //Added to remove typescript error
let thisKey = keys.map(key => { return item[key]; }).join(keyDelim);
let thisKey2 = item[keys[0]];
thisKey2 += keyDelim + item[keys[1]];
let keyIndex = summary.keys.indexOf(thisKey);
if (keyIndex < 0) {
summary.keys.push(thisKey);
keyIndex = summary.keys.length - 1;
let thisKeyBasic = thisKey.split('~');
let localTime = new Date(thisKeyBasic[0].replace(' ', '').trim());
localTime = localTime.toLocaleString();
summary.groups.push({ key: thisKey, items: [], groupFilter: null, localTime: localTime });
//Set the groupFilter which is intended to be an easy way to filter this group...
//For instance, the All items are pre-filtered by the site the item pertains to.
//The groupFilter could be the list in that site which the items in the group have in common
let filterKeys = groupFilterKey.split('.');
if (filterKeys.length === 1) {
summary.groups[keyIndex].groupFilter = item[groupFilterKey];
}
else {
summary.groups[keyIndex].groupFilter = item[filterKeys[0]][filterKeys[1]];
}
}
summary.groups[keyIndex].items.push(item);
});
if (groupItemOrderKey !== null && groupItemOrderKey !== undefined && groupItemOrderKey !== '') {
summary.groups.map(group => {
let okToSort = true;
group.items.map(itemX => {
let item = itemX; //Added to remove typescript error
if (item[groupItemOrderKey] === null) {
okToSort = false;
}
else if (item[groupItemOrderKey] === undefined) {
okToSort = false;
}
});
if (okToSort === true) {
let newItems = [];
if (localLanguage !== '') {
newItems = sortObjectArrayByStringKeyCollator(group.items, sort, groupItemOrderKey, convertNullToEmpty, localLanguage);
}
else {
newItems = sortObjectArrayByStringKey(group.items, sort, groupItemOrderKey);
}
group.items = newItems;
}
else {
console.log('Unable to sort this group of items... one of the keyValues was not valid.', group);
}
});
}
summary.filteredGroups = summary.groups;
summary.filteredKeys = summary.keys;
// console.log( 'History summary: ', summary );
return summary;
}
//# sourceMappingURL=grouping.js.map