|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | 3 | Object.defineProperty(exports, "__esModule", { |
4 | | - value: true |
| 4 | + value: true |
5 | 5 | }); |
6 | 6 |
|
7 | 7 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; |
@@ -39,97 +39,97 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function" |
39 | 39 | var store = void 0; |
40 | 40 |
|
41 | 41 | var noopStore = { |
42 | | - getItem: function getItem() {}, |
43 | | - setItem: function setItem() {} |
| 42 | + getItem: function getItem() {}, |
| 43 | + setItem: function setItem() {} |
44 | 44 | }; |
45 | 45 |
|
46 | 46 | if (typeof window !== 'undefined' && 'localStorage' in window && window['localStorage'] !== null) { |
47 | | - try { |
48 | | - var key = '__pushtell_react__'; |
49 | | - window.localStorage.setItem(key, key); |
50 | | - if (window.localStorage.getItem(key) !== key) { |
51 | | - store = noopStore; |
52 | | - } else { |
53 | | - window.localStorage.removeItem(key); |
54 | | - store = window.localStorage; |
55 | | - } |
56 | | - } catch (e) { |
57 | | - store = noopStore; |
| 47 | + try { |
| 48 | + var key = '__pushtell_react__'; |
| 49 | + window.localStorage.setItem(key, key); |
| 50 | + if (window.localStorage.getItem(key) !== key) { |
| 51 | + store = noopStore; |
| 52 | + } else { |
| 53 | + window.localStorage.removeItem(key); |
| 54 | + store = window.localStorage; |
58 | 55 | } |
59 | | -} else { |
| 56 | + } catch (e) { |
60 | 57 | store = noopStore; |
| 58 | + } |
| 59 | +} else { |
| 60 | + store = noopStore; |
61 | 61 | } |
62 | 62 |
|
63 | 63 | _emitter2.default.addActiveVariantListener(function (experimentName, variantName, skipSave) { |
64 | | - if (skipSave) { |
65 | | - return; |
66 | | - } |
67 | | - store.setItem('PUSHTELL-' + experimentName, variantName); |
| 64 | + if (skipSave) { |
| 65 | + return; |
| 66 | + } |
| 67 | + store.setItem('PUSHTELL-' + experimentName, variantName); |
68 | 68 | }); |
69 | 69 |
|
70 | 70 | var Experiment = function (_Component) { |
71 | | - _inherits(Experiment, _Component); |
| 71 | + _inherits(Experiment, _Component); |
72 | 72 |
|
73 | | - function Experiment() { |
74 | | - var _ref; |
| 73 | + function Experiment() { |
| 74 | + var _ref; |
75 | 75 |
|
76 | | - var _temp, _this, _ret; |
| 76 | + var _temp, _this, _ret; |
77 | 77 |
|
78 | | - _classCallCheck(this, Experiment); |
| 78 | + _classCallCheck(this, Experiment); |
79 | 79 |
|
80 | | - for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { |
81 | | - args[_key] = arguments[_key]; |
82 | | - } |
83 | | - |
84 | | - return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Experiment.__proto__ || Object.getPrototypeOf(Experiment)).call.apply(_ref, [this].concat(args))), _this), _this.win = function () { |
85 | | - _emitter2.default.emitWin(_this.props.name); |
86 | | - }, _this.getLocalStorageValue = function () { |
87 | | - var activeValue = _emitter2.default.getActiveVariant(_this.props.name); |
88 | | - if (typeof activeValue === "string") { |
89 | | - return activeValue; |
90 | | - } |
91 | | - var storedValue = store.getItem('PUSHTELL-' + _this.props.name); |
92 | | - if (typeof storedValue === "string") { |
93 | | - _emitter2.default.setActiveVariant(_this.props.name, storedValue, true); |
94 | | - return storedValue; |
95 | | - } |
96 | | - if (typeof _this.props.defaultVariantName === 'string') { |
97 | | - _emitter2.default.setActiveVariant(_this.props.name, _this.props.defaultVariantName); |
98 | | - return _this.props.defaultVariantName; |
99 | | - } |
100 | | - var variants = _emitter2.default.getSortedVariants(_this.props.name); |
101 | | - var weights = _emitter2.default.getSortedVariantWeights(_this.props.name); |
102 | | - var weightSum = weights.reduce(function (a, b) { |
103 | | - return a + b; |
104 | | - }, 0); |
105 | | - var weightedIndex = typeof _this.props.userIdentifier === 'string' ? Math.abs((0, _crc2.default)(_this.props.userIdentifier) % weightSum) : Math.floor(Math.random() * weightSum); |
106 | | - var randomValue = variants[variants.length - 1]; |
107 | | - for (var index = 0; index < weights.length; index++) { |
108 | | - weightedIndex -= weights[index]; |
109 | | - if (weightedIndex < 0) { |
110 | | - randomValue = variants[index]; |
111 | | - break; |
112 | | - } |
113 | | - } |
114 | | - _emitter2.default.setActiveVariant(_this.props.name, randomValue); |
115 | | - return randomValue; |
116 | | - }, _temp), _possibleConstructorReturn(_this, _ret); |
| 80 | + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { |
| 81 | + args[_key] = arguments[_key]; |
117 | 82 | } |
118 | 83 |
|
119 | | - _createClass(Experiment, [{ |
120 | | - key: "render", |
121 | | - value: function render() { |
122 | | - return _react2.default.createElement(_CoreExperiment2.default, _extends({}, this.props, { value: this.getLocalStorageValue })); |
| 84 | + return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Experiment.__proto__ || Object.getPrototypeOf(Experiment)).call.apply(_ref, [this].concat(args))), _this), _this.win = function () { |
| 85 | + _emitter2.default.emitWin(_this.props.name); |
| 86 | + }, _this.getLocalStorageValue = function () { |
| 87 | + var activeValue = _emitter2.default.getActiveVariant(_this.props.name); |
| 88 | + if (typeof activeValue === "string") { |
| 89 | + return activeValue; |
| 90 | + } |
| 91 | + var storedValue = store.getItem('PUSHTELL-' + _this.props.name); |
| 92 | + if (typeof storedValue === "string") { |
| 93 | + _emitter2.default.setActiveVariant(_this.props.name, storedValue, true); |
| 94 | + return storedValue; |
| 95 | + } |
| 96 | + if (typeof _this.props.defaultVariantName === 'string') { |
| 97 | + _emitter2.default.setActiveVariant(_this.props.name, _this.props.defaultVariantName); |
| 98 | + return _this.props.defaultVariantName; |
| 99 | + } |
| 100 | + var variants = _emitter2.default.getSortedVariants(_this.props.name); |
| 101 | + var weights = _emitter2.default.getSortedVariantWeights(_this.props.name); |
| 102 | + var weightSum = weights.reduce(function (a, b) { |
| 103 | + return a + b; |
| 104 | + }, 0); |
| 105 | + var weightedIndex = typeof _this.props.userIdentifier === 'string' ? Math.abs((0, _crc2.default)(_this.props.userIdentifier) % weightSum) : Math.floor(Math.random() * weightSum); |
| 106 | + var randomValue = variants[variants.length - 1]; |
| 107 | + for (var index = 0; index < weights.length; index++) { |
| 108 | + weightedIndex -= weights[index]; |
| 109 | + if (weightedIndex < 0) { |
| 110 | + randomValue = variants[index]; |
| 111 | + break; |
123 | 112 | } |
124 | | - }]); |
| 113 | + } |
| 114 | + _emitter2.default.setActiveVariant(_this.props.name, randomValue); |
| 115 | + return randomValue; |
| 116 | + }, _temp), _possibleConstructorReturn(_this, _ret); |
| 117 | + } |
| 118 | + |
| 119 | + _createClass(Experiment, [{ |
| 120 | + key: "render", |
| 121 | + value: function render() { |
| 122 | + return _react2.default.createElement(_CoreExperiment2.default, _extends({}, this.props, { value: this.getLocalStorageValue })); |
| 123 | + } |
| 124 | + }]); |
125 | 125 |
|
126 | | - return Experiment; |
| 126 | + return Experiment; |
127 | 127 | }(_react.Component); |
128 | 128 |
|
129 | 129 | Experiment.propTypes = { |
130 | | - name: _propTypes2.default.string.isRequired, |
131 | | - defaultVariantName: _propTypes2.default.string, |
132 | | - userIdentifier: _propTypes2.default.string |
| 130 | + name: _propTypes2.default.string.isRequired, |
| 131 | + defaultVariantName: _propTypes2.default.string, |
| 132 | + userIdentifier: _propTypes2.default.string |
133 | 133 | }; |
134 | 134 | Experiment.displayName = "Pushtell.Experiment"; |
135 | 135 | exports.default = Experiment; |
|
0 commit comments