All files common-utils.js

100% Statements 18/18
100% Branches 10/10
100% Functions 7/7
100% Lines 12/12
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  10x 73x     5x     5x     5x 206x   204x   204x   199x       5x 104x   191x  
 
const isObjectOfType = typeName => a =>
    ({}).toString.call(a) === `[object ${typeName}]`;
 
// isList :: * -> Boolean
export const isList = isObjectOfType('Array');
 
// isObject:: * -> Boolean[object 
export const isObject = isObjectOfType('Object');
 
// match :: EnumTagType -> Pattern -> b
export const match = (instance, pattern) => {
    if (!instance || !instance.name) throw new Error('Invalid instance passed');
 
    const action = pattern[instance.name] || pattern._;
 
    if(!action) throw new Error('Non-Exhaustive pattern. You must pass fallback case `_` in the pattern');
 
    return action(...instance.args);
};
 
// listToObject :: (a -> String, a -> b, [a]) -> Object b
export const listToObject = (toKey, toValue, list) =>
    list.reduce((obj, item) => ({ ...obj, [toKey(item)]: toValue(item) }), {});
 
export const isConstructor = constructors => t => constructors.indexOf(t) !== -1 || constructors.indexOf(t.name) !== -1