All files / latest/src/handlers/get/src simpleGet.js

100% Statements 31/31
100% Branches 4/4
100% Functions 1/1
100% Lines 31/31

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 321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 509x 509x 509x 1556x 549x 549x 1556x 1007x 1007x 1007x 1556x 1556x 1556x 1556x 1556x 1556x 509x 509x 1x 1x  
const validateOutput = require('./validateOutput');
 
/**
 * Retreives single value from objects specified path from a simple format
 * (array representation, without queries)
 * @param {Object} obj - object/array from which value should be retreived.
 * @param {Array} path - array representation of path to set.
 * @returns {any} returns value found at specified path, in case that multiple logical checks
 * satisfy the first element will be returned
 */
const simpleGet = (obj, path) => {
  const arrayPath = path;
  let tempObject = obj;
  arrayPath.every((element, index) => {
    if (Array.isArray(tempObject)) {
      const elementValue = element.number;
      tempObject = tempObject[elementValue];
    } else {
      const elementValue = element.string;
      tempObject = tempObject[elementValue];
    }
    const {
      shouldItContinue, newTempObject,
    } = validateOutput(tempObject, arrayPath.length - 1 === index);
    tempObject = newTempObject;
    return shouldItContinue;
  });
  return tempObject;
};
 
module.exports = simpleGet;