All files / predicate/lib utils.js

100% Statements 30/30
100% Branches 6/6
100% Functions 12/12
100% Lines 27/27

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        1x 1x     1x   37x 37x   37x       1x 17x 436x 1x   548x       1x 3x 3x 4x       1x 56x 345x   345x 343x       1x 28x     1x   1x 24x     1x 24x    
'use strict';
 
/* eslint-disable func-names, no-multi-assign */
 
const predicate = module.exports;
const slice = Array.prototype.slice;
 
// Useful for debuging curried functions
const setSrc = function(curried, src) {
  /* eslint-disable no-param-reassign */
  curried.toString = () => src.toString();
  curried.src = src;
  /* eslint-enable no-param-reassign */
  return curried;
};
 
// Curry's fn's with arity 2
const curry = (predicate.curry = function(f) {
  return setSrc(function curried(a, b) {
    if (!arguments.length) {
      throw new TypeError('Function called with no arguments');
    }
    return arguments.length === 1 ? setSrc(c => f(a, c), f) : f(a, b);
  }, f);
});
 
predicate.partial = function(fn) {
  const args = slice.call(arguments, 1);
  return function() {
    return fn.apply(null, args.concat(slice.call(arguments)));
  };
};
 
predicate.complement = predicate.invert = function(pred) {
  return function() {
    const ret = pred.apply(null, arguments);
    // Handle curried fns
    if (typeof ret === 'function') return predicate.complement(ret);
    return !ret;
  };
};
 
predicate.mod = curry(function(a, b) {
  return a % b;
});
 
predicate.assign = curry(Object.assign);
 
predicate.identity = function(v) {
  return v;
};
 
predicate.toBoolFn = curry(function(fn, v) {
  return !!fn(v);
});