All files / predicate/lib predicates.js

100% Statements 81/81
100% Branches 25/25
100% Functions 18/18
100% Lines 81/81

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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196        1x 1x   1x 1x 1x   1x     1x 1x                 1x 47x     1x 1x       1x 392x     1x   9x     1x 1x   1x 24x     1x       1x 20x                   1x             1x 1x 27x         1x 19x     1x 1x 1x       1x   1x 14x     1x 1x     1x 227x       1x 1x   1x 1x   1x 1x   1x 150x     1x 21x     1x 1x 1x   1x 10x             1x 10x             1x 16x 1x     15x 4x     11x 1x     10x     1x 1x 2x     1x 17x 2x     15x 8x     7x 6x     1x     1x                 1x 8x       1x 52x 52x    
'use strict';
 
/* eslint-disable func-names, no-multi-assign */
 
const utils = require('./utils');
const other = require('./other');
 
const { and } = other;
const { or } = other;
const predicate = module.exports;
 
const { curry } = utils;
 
/* istanbul ignore else */
if (Object.is) {
  predicate.is = curry(Object.is);
} else {
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
  predicate.is = curry(function objectIs(v1, v2) {
    /* eslint-disable-next-line no-self-compare */
    return v1 === v2 ? v1 !== 0 || 1 / v1 === 1 / v2 : v1 !== v1 && v2 !== v2;
  });
}
 
predicate.exists = function(val) {
  return val != null;
};
 
predicate.truthy = utils.toBoolFn(and(utils.identity, predicate.exists));
predicate.falsey = utils.complement(predicate.truthy);
 
// ---- value comparision methods
 
predicate.equal = curry(function(a, b) {
  return a === b;
});
 
predicate.eq = curry(function(a, b) {
  // eslint-disable-next-line eqeqeq
  return a == b;
});
 
predicate.null = predicate.equal(null);
predicate.undef = predicate.equal(undefined);
 
predicate.lt = predicate.less = curry(function(a, b) {
  return a < b;
});
 
predicate.ltEq = predicate.le = predicate.lessEq = curry(
  or(predicate.equal, predicate.less)
);
 
predicate.gt = predicate.greater = curry(function(a, b) {
  return a > b;
});
 
/**
 * Returns the result of a >= b
 *
 * @param {*} a -
 * @param {*} b -
 * @return {boolean} - The result of a >= b
 */
predicate.gtEq = predicate.ge = predicate.greaterEq = curry(
  or(predicate.equal, predicate.greater)
);
 
// --- Type checking predicates
 
// Forces objects toString called returned as [object Object] for instance
const toString = Object.prototype.toString;
const eqToStr = curry(function(str, val) {
  return predicate.equal(str, toString.call(val));
});
 
// ---- Object type checks
 
predicate.object = predicate.obj = function(val) {
  return val === Object(val);
};
 
predicate.array = predicate.arr = Array.isArray;
predicate.date = eqToStr('[object Date]');
predicate.regex = predicate.regexp = predicate.rgx = predicate.RegExp = eqToStr(
  '[object RegExp]'
);
 
predicate.nan = predicate.NaN = predicate.is(NaN);
 
predicate.instance = curry(function(Cls, inst) {
  return inst instanceof Cls;
});
 
predicate.arguments = eqToStr('[object Arguments]');
predicate.error = predicate.instance(Error);
 
// creates fns for predicate.string, etc
const typeofBuilder = curry(function(type, val) {
  return predicate.equal(type, typeof val);
});
 
// --- Create typeof functions
predicate.function = typeofBuilder('function');
predicate.fn = predicate.function;
 
predicate.string = typeofBuilder('string');
predicate.str = predicate.string;
 
predicate.boolean = typeofBuilder('boolean');
predicate.bool = predicate.boolean;
 
predicate.number = predicate.num = function(val) {
  return typeof val === 'number' && predicate.not.NaN(val);
};
 
predicate.int = function(val) {
  return predicate.num(val) && predicate.zero(utils.mod(val, 1));
};
 
predicate.pos = and(predicate.num, predicate.greater(0));
predicate.neg = and(predicate.num, predicate.less(0));
predicate.zero = and(predicate.num, predicate.equal(0));
 
predicate.even = function(val) {
  return (
    predicate.num(val) &&
    predicate.not.zero(val) &&
    predicate.zero(utils.mod(val, 2))
  );
};
 
predicate.odd = function(val) {
  return (
    predicate.num(val) &&
    predicate.not.zero(val) &&
    predicate.not.zero(utils.mod(val, 2))
  );
};
 
predicate.contains = predicate.includes = curry(function(arrOrString, val) {
  if (and(predicate.not.array, predicate.not.string)(arrOrString)) {
    throw new TypeError('Expected an array or string');
  }
 
  if (predicate.string(arrOrString) && !predicate.string(val)) {
    return false;
  }
 
  if (predicate.NaN(val)) {
    return arrOrString.some(predicate.NaN);
  }
 
  return arrOrString.indexOf(val) !== -1;
});
 
const hasOwnProp = Object.prototype.hasOwnProperty;
predicate.has = curry(function(o, key) {
  return hasOwnProp.call(o, key);
});
 
predicate.empty = function(o) {
  if (predicate.not.exists(o)) {
    return true;
  }
 
  if (or(predicate.arr, predicate.str)(o)) {
    return !o.length;
  }
 
  if (predicate.obj(o)) {
    return Object.keys(o).length < 1;
  }
 
  throw new TypeError();
};
 
predicate.primitive = or(
  predicate.string,
  predicate.num,
  predicate.bool,
  predicate.null,
  predicate.undef,
  predicate.NaN
);
 
predicate.matches = curry(function(rgx, val) {
  return rgx.test(val);
});
 
// Assign inverse of each predicate
predicate.not = Object.keys(predicate).reduce(function(acc, fnName) {
  acc[fnName] = utils.complement(predicate[fnName]);
  return acc;
}, {});