All files / src createModule.js

100% Statements 15/15
100% Branches 6/6
100% Functions 7/7
100% Lines 15/15

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        7x 7x 217x 217x 217x                   4x 4x       1x     5x 14x 3x   14x       2x 2x             4x    
import { createSelector } from 'reselect';
import moduleRegistry from './moduleRegistry';
 
export function generateId() {
  const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
  return RFC4122_TEMPLATE.replace(/[xy]/g, function(c) {
    var r = (Math.random() * 16) | 0,
      v = c == 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}
 
/**
 *
 * @param {Module} moduleObj - Module object
 * @returns {Object}
 */
export default function createModule(moduleObj) {
  const id = `${generateId()}`;
  const finalObj = {
    ...moduleObj,
    id,
    getName() {
      return moduleRegistry.getName(id);
    },
    getSelector() {
      return function(state) {
        if (!finalObj.__name) {
          finalObj.__name = moduleRegistry.getName(id);
        }
        return finalObj.__name ? state[finalObj.__name] : null;
      };
    },
    select(cb) {
      const getModuleState = finalObj.getSelector();
      return createSelector(
        getModuleState,
        cb
      );
    },
  };
 
  return finalObj;
}