UNPKG

redux-ab-test

Version:

A/B testing React components with Redux and debug tools. Isomorphic with a simple, universal interface. Well documented and lightweight. Tested in popular browsers and Node.js. Includes helpers for React, Redux, and Segment.io

124 lines (100 loc) 4.25 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.requireChildrenAreVariations = exports.requireChildIsVariation = exports.getVariationsByName = exports.groupExperimentsByName = exports.groupChildrenByName = undefined; var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _immutable = require('immutable'); var _immutable2 = _interopRequireDefault(_immutable); var _variation = require('./variation'); var _variation2 = _interopRequireDefault(_variation); var _logger = require('./logger'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var groupChildrenByName = exports.groupChildrenByName = function () { function groupChildrenByName(children) { var childrenByName = {}; var count = _react2['default'].Children.count(children); (0, _logger.logger)(__filename + ' groupChildrenByName: count=' + count); if (count === 1) { if (!children.props.name) { throw new Error('No name prop found on child=' + (0, _stringify2['default'])(children.props)); } childrenByName[children.props.name] = children; } else { _react2['default'].Children.forEach(children, function (child) { if (!child.props.name) { throw new Error('No name prop found on child=' + (0, _stringify2['default'])(child.props.name)); } childrenByName[child.props.name] = child; }); } (0, _logger.logger)(__filename + ' groupChildrenByName: count=' + count + ' childrenByName=\'' + (0, _keys2['default'])(childrenByName).join('|') + '\''); return childrenByName; } return groupChildrenByName; }(); var groupExperimentsByName = exports.groupExperimentsByName = function () { function groupExperimentsByName(state) { var reduxAbTest = state.reduxAbTest; var experiments = reduxAbTest.getIn(['experiments'], _immutable2['default'].List()); var experimentsByName = {}; experiments.forEach(function (e) { experimentsByName[e.get('name', '').toLowerCase()] = e; }); return experimentsByName; } return groupExperimentsByName; }(); var getVariationsByName = exports.getVariationsByName = function () { function getVariationsByName(experiment) { if (!experiment) { return {}; } var variations = {}; experiment.get('variations', _immutable2['default'].List()).forEach(function (variation) { variations[variation.get('name', '')] = variation; }); return variations; } return getVariationsByName; }(); var requireChildIsVariation = exports.requireChildIsVariation = function () { function requireChildIsVariation(componentName, index, child) { if (child.type === _variation2['default']) { return undefined; } return new Error('\'' + componentName + '\' should have a children of the type: \'Variation\': child.index=\'' + index + '\', child.type=\'' + child.type + '\''); } return requireChildIsVariation; }(); var requireChildrenAreVariations = exports.requireChildrenAreVariations = function () { function requireChildrenAreVariations(props, propName, componentName) { var children = props[propName]; // Only accept children of the appropriate type var childrenCount = _react2['default'].Children.count(children); switch (childrenCount) { case 0: return new Error('\'' + componentName + '\' should have at least one child of the type: \'Variation\': children.count=\'' + childrenCount + '\''); case 1: return requireChildIsVariation(componentName, 0, children); default: { var errors = _react2['default'].Children.map(function (index, child) { return requireChildIsVariation(componentName, index, child); }).filter(function (err) { return err; }); if (errors.length !== 0) { return errors[0]; } return undefined; } } } return requireChildrenAreVariations; }();