All files / modules/filtering index.js

100% Statements 21/21
100% Branches 2/2
100% Functions 5/5
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                  14x 1x 1x 3x   1x                     14x 1x 1x 1x 6x 6x 6x 3x   3x   6x 6x   1x 3x   1x     14x        
/**
 * @module filtering
 */
/**
 * Filter and sort ascending the input records
 * based on their titles.
 * @param {Array} records
 * @return {Array} sorted & filtered records
 */
const filterAndSortByTitle = (records) => {
    const uniqueObjects = new Map();
    records.forEach(e => {
        uniqueObjects.set(e.title, e);
    });
    return [...uniqueObjects.values()].sort((a, b) => a.title.localeCompare(b.title));
};
 
/**
 * Group all input records based on parent variable name.
 * Returns an object where the object parameters are the
 * different values of the parent variable name.
 * @param {Array} records which have a parent ID
 * @param {String} parentIdName variable name for parent ID
 * @return {Object} grouped records
 */
const groupByParent = (records, parentIdName) => {
    const recordsMap = new Map();
    const sortedObject = {};
    records.forEach(e => {
        const parentId = e[parentIdName];
        let record = [];
        if (!recordsMap.has(parentId)) {
            record = [];
        } else {
            record = recordsMap.get(parentId);
        }
        record.push(e);
        recordsMap.set(parentId, record);
    });
    for (const [key, value] of recordsMap) {
        sortedObject[key] = value;
    }
    return sortedObject;
};
 
module.exports = {
    filterAndSortByTitle,
    groupByParent,
};