@the-control-group/react-split-test
Advanced tools
Comparing version 0.9.0 to 0.9.1
@@ -1,114 +0,68 @@ | ||
"use strict"; | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
export default class Experiment extends Component { | ||
static propTypes = { | ||
children: PropTypes.node.isRequired, | ||
id: PropTypes.string.isRequired, | ||
session: PropTypes.bool, | ||
onParticipate: PropTypes.func | ||
}; | ||
var _react = _interopRequireWildcard(require("react")); | ||
static defaultProps = { | ||
onParticipate: () => {} | ||
}; | ||
var _propTypes = _interopRequireDefault(require("prop-types")); | ||
constructor(props) { | ||
super(props); | ||
var _Variation = _interopRequireDefault(require("./Variation")); | ||
this.cacheKey = `@tcg-split-test:${this.props.id}`; | ||
this.storage = this.props.session ? sessionStorage : localStorage; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
this.state = { | ||
selectedVariation: this.chooseVariation() | ||
}; | ||
} | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
chooseVariation() { | ||
let cachedData; | ||
try { | ||
cachedData = JSON.parse(this.storage.getItem(this.cacheKey)); | ||
} catch (e) { | ||
// No/invalid cache | ||
} | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
let selectedVariation; | ||
if(cachedData) { | ||
selectedVariation = cachedData.selectedVariation; | ||
} else { | ||
const variations = []; | ||
React.Children.forEach(this.props.children, c => { | ||
if(c.props.isVariation) variations.push(c.props.id); | ||
}); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
selectedVariation = variations[Math.floor(Math.random() * variations.length)]; | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
const experimentData = { | ||
id: this.props.id, | ||
selectedVariation | ||
}; | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
this.props.onParticipate(experimentData); | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
this.storage.setItem(this.cacheKey, JSON.stringify(experimentData)); | ||
} | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
return selectedVariation; | ||
} | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
render() { | ||
const { selectedVariation } = this.state; | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
return React.Children.map(this.props.children, child => { | ||
if(child.props.isVariation && child.props.id !== selectedVariation) return; | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var Experiment = | ||
/*#__PURE__*/ | ||
function (_Component) { | ||
_inherits(Experiment, _Component); | ||
function Experiment(props) { | ||
var _this; | ||
_classCallCheck(this, Experiment); | ||
_this = _possibleConstructorReturn(this, _getPrototypeOf(Experiment).call(this, props)); | ||
_this.cacheKey = "@tcg-split-test:".concat(_this.props.id); | ||
_this.storage = _this.props.session ? sessionStorage : localStorage; | ||
_this.state = { | ||
selectedVariation: _this.chooseVariation() | ||
}; | ||
return _this; | ||
} | ||
_createClass(Experiment, [{ | ||
key: "chooseVariation", | ||
value: function chooseVariation() { | ||
var cachedData; | ||
try { | ||
cachedData = JSON.parse(this.storage.getItem(this.cacheKey)); | ||
} catch (e) {// No/invalid cache | ||
} | ||
var selectedVariation; | ||
if (cachedData) { | ||
selectedVariation = cachedData.selectedVariation; | ||
} else { | ||
var variations = []; | ||
_react.default.Children.forEach(this.props.children, function (c) { | ||
if (c.type === _Variation.default) variations.push(c.props.id); | ||
}); | ||
selectedVariation = variations[Math.floor(Math.random() * variations.length)]; | ||
var experimentData = { | ||
id: this.props.id, | ||
selectedVariation: selectedVariation | ||
}; | ||
this.props.onParticipate(experimentData); | ||
this.storage.setItem(this.cacheKey, JSON.stringify(experimentData)); | ||
} | ||
return selectedVariation; | ||
} | ||
}, { | ||
key: "render", | ||
value: function render() { | ||
var selectedVariation = this.state.selectedVariation; | ||
return _react.default.Children.map(this.props.children, function (child) { | ||
if (child.type === _Variation.default && child.props.id !== selectedVariation) return; | ||
return child; | ||
}); | ||
} | ||
}]); | ||
return Experiment; | ||
}(_react.Component); | ||
exports.default = Experiment; | ||
_defineProperty(Experiment, "propTypes", { | ||
children: _propTypes.default.node.isRequired, | ||
id: _propTypes.default.string.isRequired, | ||
session: _propTypes.default.bool, | ||
onParticipate: _propTypes.default.func | ||
}); | ||
_defineProperty(Experiment, "defaultProps", { | ||
onParticipate: function onParticipate() {} | ||
}); | ||
return child; | ||
}); | ||
} | ||
} |
@@ -1,30 +0,6 @@ | ||
"use strict"; | ||
export { default as Experiment } from './Experiment'; | ||
export { default as Variation } from './Variation'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.clearExperimentCache = clearExperimentCache; | ||
Object.defineProperty(exports, "Experiment", { | ||
enumerable: true, | ||
get: function get() { | ||
return _Experiment.default; | ||
} | ||
}); | ||
Object.defineProperty(exports, "Variation", { | ||
enumerable: true, | ||
get: function get() { | ||
return _Variation.default; | ||
} | ||
}); | ||
var _Experiment = _interopRequireDefault(require("./Experiment")); | ||
var _Variation = _interopRequireDefault(require("./Variation")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function clearExperimentCache() { | ||
Object.keys(localStorage).forEach(function (k) { | ||
return /^@tcg-split-test:/.test(k) && localStorage.removeItem(k); | ||
}); | ||
} | ||
export function clearExperimentCache() { | ||
Object.keys(localStorage).forEach(k => /^@tcg-split-test:/.test(k) && localStorage.removeItem(k)); | ||
} |
@@ -1,26 +0,18 @@ | ||
"use strict"; | ||
import React, { Fragment } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
const Variation = ({ children }) => ( | ||
<Fragment>{children}</Fragment> | ||
); | ||
var _react = _interopRequireWildcard(require("react")); | ||
Variation.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
isVariation: PropTypes.bool, | ||
id: PropTypes.string.isRequired | ||
}; | ||
var _propTypes = _interopRequireDefault(require("prop-types")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
var Variation = function Variation(_ref) { | ||
var children = _ref.children; | ||
return _react.default.createElement(_react.Fragment, null, children); | ||
Variation.defaultProps = { | ||
isVariation: true | ||
}; | ||
Variation.propTypes = { | ||
children: _propTypes.default.node.isRequired, | ||
id: _propTypes.default.string.isRequired | ||
}; | ||
var _default = Variation; | ||
exports.default = _default; | ||
export default Variation; |
{ | ||
"name": "@the-control-group/react-split-test", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "A/B Split Testing Component for React", | ||
@@ -10,4 +10,5 @@ "files": [ | ||
"scripts": { | ||
"prepublishOnly": "npm test && npm run transpile", | ||
"transpile": "rm -rf lib/ && babel src -d lib", | ||
"watch": "cpx \"src/**/*.{less,js}\" lib --watch", | ||
"copy": "rm -rf lib/ && cpx \"src/*.js\" lib", | ||
"prepublishOnly": "npm test && npm run copy", | ||
"start": "react-devtools & webpack-dev-server --hot --mode development", | ||
@@ -33,7 +34,2 @@ "test": "eslint src" | ||
"homepage": "https://github.com/the-control-group/react-split-test#readme", | ||
"browserslist": [ | ||
"last 1 version", | ||
"not dead", | ||
"> 1% in US" | ||
], | ||
"devDependencies": { | ||
@@ -43,3 +39,2 @@ "@babel/cli": "^7.0.0-rc.1", | ||
"@babel/plugin-proposal-class-properties": "^7.0.0-rc.1", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-rc.1", | ||
"@babel/preset-env": "^7.0.0-rc.1", | ||
@@ -49,2 +44,3 @@ "@babel/preset-react": "^7.0.0-rc.1", | ||
"babel-loader": "^8.0.0-beta.4", | ||
"cpx": "^1.5.0", | ||
"eslint": "^5.3.0", | ||
@@ -51,0 +47,0 @@ "eslint-plugin-react": "^7.10.0", |
@@ -0,3 +1,8 @@ | ||
[![npm (scoped)](https://img.shields.io/npm/v/@the-control-group/react-split-test.svg?style=flat-square)](https://www.npmjs.com/package/@the-control-group/react-split-test) | ||
# React Split Test | ||
A/B Experiment component that allows for simple split tests to be run in a React application. Experiments can also be nested to run mutually exclusive multi-variate experiments. | ||
## Batteries not included | ||
This package does not come transpiled - the importing library is responsible for setting proper browser targets and transpilation options. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
4289
72