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

100% Statements 174/174
97.92% Branches 47/48
100% Functions 18/18
100% Lines 174/174

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 1751x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 337x 337x 337x 337x 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 1x 1x 1x 1x 1x 482x 4x 4x 482x 1x 1x 1x 1x 1x 112x 112x 112x 7x 7x 7x 112x 112x 1x 1x 1x 1x 1x 7x 7x 7x 1x 1x 1x 1x 1x 19x 19x 19x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 171x 171x 171x 33x 33x 171x 112x 112x 112x 112x 112x 112x 112x 26x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 344x 344x 7x 7x 344x 8x 8x 344x 5x 5x 344x 201x 6x 6x 201x 5x 5x 201x 19x 19x 171x 171x 123x 123x 1x 1x  
const createRegExpFromString = require('./queryElementTransformHelpers/createRegexpFromString');
const addFunctionToQuery = require('./queryElementTransformHelpers/addFunctionToQuery');
 
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 $Function
 */
const checkIfFunction = (queryElement) => (queryElement && typeof queryElement === 'string' && queryElement.substring(0, 10) === '$Function(');
 
/**
 * 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 Function and returns the function itself
 */
const handleFunctions = (element, funcs) => {
  const remainingElement = element.substr(10, element.length - 11);
  return { value: { function: addFunctionToQuery(remainingElement, funcs) } };
};
 
/**
 * 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
 * @param {Object} funcs - object with functions provided by the user that may be part of the query
 * @returns {Object} - object with keyName the type of element and keyValue the value of element
 */
// eslint-disable-next-line complexity
const queryElementTransformer = (element, funcs) => {
  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);
    }
    if (checkIfFunction(element)) {
      return handleFunctions(element, funcs);
    }
    return handlePaths(element);
  }
  return { value: element };
};
 
module.exports = queryElementTransformer;