All files / latest/src/helpers/query/src logicalValidator.js

100% Statements 48/48
100% Branches 30/30
100% Functions 1/1
100% Lines 48/48

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 491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 248x 90x 90x 248x 6x 6x 232x 40x 40x 224x 5x 5x 224x 3x 3x 224x 5x 5x 248x 24x 24x 248x 16x 16x 224x 20x 12x 12x 20x 4x 4x 4x 4x 39x 39x 1x 1x  
const stringify = require('../../../handlers/basic/stringify');
 
/**
 * Checks value with checkValue depending on operator
 * @param {Array} value - input value to check
 * @param {String} operator - operator
 * @param {Any} checkValue - other input value to check agains value
 * @returns {Boolean} - true if it complies, false otherwise
 */
// eslint-disable-next-line complexity
const logicalValidator = (value, operator, checkValue) => {
  if (operator === '=') {
    return (stringify(value) === stringify(checkValue));
  }
  if (operator === '!=') {
    return (stringify(value) !== stringify(checkValue));
  }
  if (operator === '>=') {
    return (value >= checkValue);
  }
  if (operator === '>') {
    return (value > checkValue);
  }
  if (operator === '<=') {
    return (value <= checkValue);
  }
  if (operator === '<') {
    return (value < checkValue);
  }
  if (operator === '∈' || operator === '@') {
    return (checkValue.indexOf(value) > -1);
  }
  if (operator === '∉' || operator === '!@') {
    return (checkValue.indexOf(value) === -1);
  }
  if (operator === '?') {
    if(checkValue.regex) {
      return checkValue.regex.test(value);
    }
    if(value.regex){
      return value.regex.test(checkValue);
    }
    return false;
  }
  return !!value;
};
 
module.exports = logicalValidator;