All files / src buildResolvers.js

100% Statements 8/8
100% Branches 8/8
100% Functions 3/3
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21          9x   9x   9x 2x 2x     7x 7x          
/**
 * Returns an array of resolving functions to apply
 * @param {Object} mapping A transformation map
 */
export default function buildResolvers(mapping = {}) {
  return Object.keys(mapping).reduce(
    (resolvers, key) => {
      const val = mapping[key];
 
      if (val !== null && typeof val === 'function') {
        resolvers.push({ key, fn: val });
        return resolvers;
      }
 
      resolvers.push({ key, fn: (value = {}) => value[key] || val });
      return resolvers;
    },
    []
  );
}