All files / latest/src/helpers/pathTransformer/src queryElementTransformer.js

100% Statements 156/156
97.73% Branches 43/44
100% Functions 16/16
100% Lines 156/156

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 136x 136x 136x 136x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 204x 4x 4x 204x 1x 1x 1x 1x 1x 55x 55x 55x 5x 5x 5x 55x 55x 1x 1x 1x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 67x 67x 67x 3x 3x 67x 55x 55x 55x 55x 55x 55x 55x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 140x 140x 4x 4x 140x 2x 2x 140x 5x 5x 140x 78x 6x 6x 78x 5x 5x 67x 67x 51x 51x 1x 1x  
const createRegExpFromString = require('./createRegExpFromString');
 
const specials = ['null', 'undefined', 'true', 'false'];
 
// eslint-disable-next-line complexity
const handleSpecials = (element) => {
  switch (element) {
    case 'null':
      return null;
    case 'true':
      return true;
    case 'false':
      return false;
    default:
      return undefined;
  }
};
/**
 * Logical check on whether input is a number
 */
const isNumberNumber = (element) => !Number.isNaN(Number(element));
 
/**
 * Removes first and last character from string
 */
const removeOpeningClosingChars = (element) => element.substr(1, element.length - 2);
 
/**
 * Logical check on whether input is enclosed in single/double brackets
 */
const isStringString = (element) => {
  const firstChar = element.charAt(0);
  const lastChar = element.charAt(element.length - 1);
  return (firstChar === lastChar && (firstChar === '"' || firstChar === "'"));
};
 
/**
 * Logical check on whether string starts with a dollar sign
 */
const checkIfFirstCharIsDollarSign = (queryElement) => (queryElement && typeof queryElement === 'string' && queryElement.charAt(0) === '$');
 
/**
 * Logical check on whether string starts with $JSON
 */
const checkIfJSON = (queryElement) => (queryElement && typeof queryElement === 'string' && queryElement.substring(0, 6) === '$JSON(');
 
 /**
 * Logical check on whether string starts with $RegExp
 */
const checkIfRegExp = (queryElement) => (queryElement && typeof queryElement === 'string' && queryElement.substring(0, 8) === '$RegExp(');
 
/**
 * Logical check on whether string starts with a dot
 */
const checkIfFirstCharIsDot = (queryElement) => (queryElement && typeof queryElement === 'string' && queryElement.charAt(0) === '.');
 
/**
 * Logical check on whether input indicates a custom query (end or append)
 */
const isCustomQuery = (queryElement) => (queryElement && ['end', 'append'].indexOf(queryElement) > -1);
 
/**
 * Removes first character from string
 */
const removeFirstChar = (queryElement) => queryElement.substr(1);
 
/**
 * Throws error on falsy element
 */
const validateElement = (element) => {
  if (!element) {
    throw new Error('Query element is invalid.');
  }
};
 
/**
 * Determines the relative depth of relative path (minus one for each dot)
 */
const determineRelativeDepth = (element) => {
  let thisElement = element;
  let relativeDepth = 0;
  while (checkIfFirstCharIsDot(thisElement)) {
    relativeDepth += -1;
    thisElement = removeFirstChar(thisElement);
  }
  return { thisElement, relativeDepth };
};
 
/**
 * Removes indicators that element is JSON and returns parsed JSON into value key
 */
const handleJson = (element) => {
  const remainingElement = element.substr(6, element.length - 7);
  return { value: JSON.parse(remainingElement) };
};
 
/**
 * Removes indicators that element is RegExp and returns a new RegExp
 */
 const handleRegExp = (element) => {
  const remainingElement = element.substr(8, element.length - 9);
  return { value: { regex: createRegExpFromString(remainingElement) } };
};
 
/**
 * Determines whether string indicates an absolute or relative path, returns object
 * with keyName the type of path (absolute, or relative) and keyValue the value of element
 */
const handlePaths = (element) => {
  let thisElement = element;
  thisElement = thisElement.substr(1);
  if (isCustomQuery(thisElement)) {
    return { custom: thisElement };
  }
  if (checkIfFirstCharIsDot(thisElement)) {
    thisElement = removeFirstChar(thisElement);
    const checkedRelativeDepth = determineRelativeDepth(thisElement);
    const { relativeDepth } = checkedRelativeDepth;
    thisElement = checkedRelativeDepth.thisElement;
    validateElement(thisElement);
    return { relativePath: thisElement, relativeDepth };
  }
  validateElement(thisElement);
  return { absolutePath: thisElement };
};
 
/**
 * Transforms each element of query into workable object representation
 * @param {String} element - string representation of query element
 * @returns {Object} - object with keyName the type of element and keyValue the value of element
 */
// eslint-disable-next-line complexity
const queryElementTransformer = (element) => {
  validateElement(element);
  if (specials.indexOf(element) > -1) {
    return { value: handleSpecials(element) };
  }
  if (element && isStringString(element) && typeof element === 'string') {
    return { value: removeOpeningClosingChars(element) };
  }
  if (isNumberNumber(element)) {
    return { value: Number(element) };
  }
  if (checkIfFirstCharIsDollarSign(element)) {
    if (checkIfJSON(element)) {
      return handleJson(element);
    }
    if (checkIfRegExp(element)) {
      return handleRegExp(element);
    }
    return handlePaths(element);
  }
  return { value: element };
};
 
module.exports = queryElementTransformer;