Skip to content

Commit

Permalink
builds
Browse files Browse the repository at this point in the history
  • Loading branch information
mattotodd committed Sep 26, 2017
1 parent e90fd0c commit a915c25
Show file tree
Hide file tree
Showing 4 changed files with 1,245 additions and 156 deletions.
1 change: 0 additions & 1 deletion lib/CoreExperiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ var CoreExperiment = function (_Component) {
var children = {};
_react2.default.Children.forEach(props.children, function (element) {
if (!_react2.default.isValidElement(element) || element.type.displayName !== "Pushtell.Variant") {
console.log(element.type);
var error = new Error("Pushtell Experiment children must be Pushtell Variant components.");
error.type = "PUSHTELL_INVALID_CHILD";
throw error;
Expand Down
35 changes: 28 additions & 7 deletions lib/Experiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,47 @@ var Experiment = function (_Component) {

return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Experiment.__proto__ || Object.getPrototypeOf(Experiment)).call.apply(_ref, [this].concat(args))), _this), _this.win = function () {
_emitter2.default.emitWin(_this.props.name);
}, _this.getRandomValue = function () {
}, _this.getSelectedVariant = function () {
/*
Choosing a weighted variant:
For C, A, B with weights 2, 4, 8
variants = A, B, C
weights = 4, 8, 2
weightSum = 14
weightedIndex = 9
AAAABBBBBBBBCC
========^
Select B
*/

// Sorted array of the variant names, example: ["A", "B", "C"]
var variants = _emitter2.default.getSortedVariants(_this.props.name);
// Array of the variant weights, also sorted by variant name. For example, if
// variant C had weight 2, variant A had weight 4, and variant B had weight 8
// return [4, 8, 2] to correspond with ["A", "B", "C"]
var weights = _emitter2.default.getSortedVariantWeights(_this.props.name);
// Sum the weights
var weightSum = weights.reduce(function (a, b) {
return a + b;
}, 0);
// A random number between 0 and weightSum
var weightedIndex = typeof _this.props.userIdentifier === 'string' ? Math.abs((0, _crc2.default)(_this.props.userIdentifier) % weightSum) : Math.floor(Math.random() * weightSum);
var randomValue = variants[variants.length - 1];
// Iterate through the sorted weights, and deduct each from the weightedIndex.
// If weightedIndex drops < 0, select the variant. If weightedIndex does not
// drop < 0, default to the last variant in the array that is initially assigned.
var selectedVariant = variants[variants.length - 1];
for (var index = 0; index < weights.length; index++) {
weightedIndex -= weights[index];
if (weightedIndex < 0) {
randomValue = variants[index];
selectedVariant = variants[index];
break;
}
}
_emitter2.default.setActiveVariant(_this.props.name, randomValue);
return randomValue;
_emitter2.default.setActiveVariant(_this.props.name, selectedVariant);
return selectedVariant;
}, _this.getLocalStorageValue = function () {
if (typeof _this.props.userIdentifier === "string") {
return _this.getRandomValue();
return _this.getSelectedVariant();
}
var activeValue = _emitter2.default.getActiveVariant(_this.props.name);
if (typeof activeValue === "string") {
Expand All @@ -117,7 +138,7 @@ var Experiment = function (_Component) {
_emitter2.default.setActiveVariant(_this.props.name, _this.props.defaultVariantName);
return _this.props.defaultVariantName;
}
return _this.getRandomValue();
return _this.getSelectedVariant();
}, _temp), _possibleConstructorReturn(_this, _ret);
}

Expand Down
6 changes: 5 additions & 1 deletion lib/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _createReactClass = require('create-react-class');

var _createReactClass2 = _interopRequireDefault(_createReactClass);

var _emitter = require('./emitter');

var _emitter2 = _interopRequireDefault(_emitter);
Expand Down Expand Up @@ -94,7 +98,7 @@ if (process.env.NODE_ENV === "production" || !_ExecutionEnvironment.canUseDOM) {

var style = null;

var Debugger = _react2.default.createClass({
var Debugger = (0, _createReactClass2.default)({
displayName: "Pushtell.Debugger",
getInitialState: function getInitialState() {
return {
Expand Down
Loading

0 comments on commit a915c25

Please sign in to comment.