All files / latest/src/helpers/mergeFunctions index.js

100% Statements 24/24
100% Branches 6/6
100% Functions 1/1
100% Lines 24/24

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 251x 1x 1x 1x 1x 1x 1x 1x 1x 556x 11x 11x 11x 6x 1x 1x 5x 5x 11x 11x 545x 545x 1x 1x  
const makeObject = require('../../handlers/make/makeObject');
 
/**
 * Merge two Objects containing functions into a single object
 * @param {Object} localFunctions - local object containing functions for specific query
 * @param {Object} globalFunctions - object containing functions for all queries
 * @returns {Object} - merged localFunctions and globalFunctions
 */
const mergeFunctions = (localFunctions, globalFunctions) => {
  if (localFunctions) {
    const local = makeObject(localFunctions);
    const allFunctions = { ...globalFunctions };
    Object.keys(local).forEach((key) => {
      if (allFunctions[key]) {
        throw new Error(`Conflicting function name ${key}.`);
      }
      allFunctions[key] = local[key];
    });
    return allFunctions;
  }
  return globalFunctions;
};
 
module.exports = mergeFunctions;