All files / src functional.js

100% Statements 30/30
100% Branches 24/24
100% Functions 14/14
100% Lines 22/22

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 751x                         8x   2x     1x 20x 3x   17x 17x 150x   17x             25x     14x 6x     1x 16x 42x 45x           19x   2x     11x     25x   1x                              
import {
  compose,
  composeP,
  filter as rfilter,
  forEach,
  forEachObjIndexed,
  keys,
  map,
  mapAccum,
  reduce as rreduce,
} from 'ramda';
 
// each :: (a -> *), [a]|Object -> undefined
const each = (f, x) => x
  ? !Array.isArray(x) ? forEachObjIndexed(f, x) : forEach(f, x)
  : (x) => !Array.isArray(x) ? forEachObjIndexed(f, x) : forEach(f, x);
 
// reductor :: ((a, b, x) -> a), a, x -> a
const reductor = (f, a, x) => {
  if(Array.isArray(x)){
    return x.reduce(f, a);
  }else{
    let acc = a;
    for(let k in x){
      acc = f(acc, x[k], k, x);
    }
    return acc;
  }
};
 
// reduce :: ((a, b, x) -> a) -> a -> x -> a
// reduce :: ((a, b, x) -> a), a -> x -> a
// reduce :: ((a, b, x) -> a), a, x -> a
const reduce = (f, a, x) => x
  ? reductor(f, a, x)
  : a
    ? (x) => reductor(f, a, x)
    : (a, x) => x ? reductor(f, a, x) : (x) => reductor(f, a, x);
 
// filter :: Object -> Object
const filterObjIndexed = (f, x) => {
  return compose(
    rreduce((acc, k) => { acc[k] = x[k]; return acc; }, {}),
    rfilter((k) => f(x[k], k, x)),
    keys
  )(x);
};
 
// filter :: Object|[a] -> Object|[a]
const filter = (f, x) => x
  ? !Array.isArray(x) ? filterObjIndexed(f, x) : rfilter(f, x)
  : (x) => !Array.isArray(x) ? filterObjIndexed(f, x) : rfilter(f, x);
 
// tautology :: * -> Bool
const tautology = () => true;
 
// thrower :: string, Error|undefined -> undefined
const thrower = (msg, Type = Error) => { throw new Type(msg); };
 
module.exports = {
  each,
  filter,
  tautology,
  thrower,
  R: {
    compose,
    composeP,
    map,
    mapAccum,
    reduce,
    each,
    filter,
  },
};