-
Notifications
You must be signed in to change notification settings - Fork 24
/
react-ab.js
182 lines (147 loc) · 4.51 KB
/
react-ab.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
;(function (root, factory) {
/* istanbul ignore next */
if (typeof module !== "undefined" && module.exports) {
module.exports = factory(require("react"), require("create-react-class"), require("prop-types"));
} else if (typeof define === "function" && define.amd) {
define(["react", "create-react-class", "prop-types"], factory);
} else {
root.ReactAB = factory(root.React, root.React.createClass, root.React.PropTypes);
}
})(this, function (React, createClass, PropTypes) {
"use strict";
var exports = {};
/* istanbul ignore next */
var random = function () {
try {
var arr = new Uint16Array(1);
window.crypto.getRandomValues(arr);
return arr[0] / 65536;
} catch(e) {
return Math.random();
}
};
var browserCookie = {
get: function (name) {
var eq = name + "="
, ca = document.cookie.split(";")
, c = null;
for(var i=0;i < ca.length;i += 1) {
c = ca[i];
while (c.charAt(0) === " ") {
c = c.substring(1, c.length);
}
if (c.indexOf(eq) === 0) {
return decodeURIComponent(c.substring(eq.length, c.length));
}
}
return null;
}
, set: function (name, value, seconds) {
var key = name + "=" + encodeURIComponent(value)
, expires = ""
, path = "path=/"
, date = null;
seconds = typeof seconds === "undefined" ? 365 * 24 * 60 * 60 : seconds;
date = new Date();
date.setTime(date.getTime()+(seconds*1000));
expires = "expires=" + date.toGMTString();
document.cookie = [key, expires, path].join(";");
}
, clear: function (name) {
browserCookie.set(name, "", -1);
}
};
exports.Variant = createClass({
displayName: "Variant"
, propTypes: {
name: PropTypes.string.isRequired
, children: PropTypes.node
}
, render: function () {
if (React.Children.count(this.props.children) === 1 && React.isValidElement(this.props.children)) {
return this.props.children;
}
return React.createElement("span", null, this.props.children);
}
});
exports.Experiment = createClass({
displayName: "Experiment"
/* Interface */
, propTypes: {
name: PropTypes.string.isRequired
, children: PropTypes.array.isRequired
, onChoice: PropTypes.func.isRequired
, random: PropTypes.func
, get: PropTypes.func
, set: PropTypes.func
, clear: PropTypes.func
}
, contextTypes: {
randomExperiment: PropTypes.func
, getExperiment: PropTypes.func
, setExperiment: PropTypes.func
, clearExperiment: PropTypes.func
}
/* Variables */
, _index: -1
/* Private */
, _random: function () {
var fn = this.props.random || this.context.randomExperiment || random;
return fn();
}
, _defaultFunc: function (name, fn) {
return this.props[name] || this.context[name + "Experiment"] || browserCookie[name];
}
, _keyName: function () {
return "react_ab_" + this.props.name;
}
, _get: function () {
return this._defaultFunc("get")(this._keyName());
}
, _set: function (v) {
return this._defaultFunc("set")(this._keyName(), v);
}
, _clear: function () {
return this._defaultFunc("clear")(this._keyName());
}
/* Lifecycle */
, componentWillMount: function () {
var variant = this._get();
for (var i = 0; i < this.props.children.length; i += 1) {
if (variant === this.props.children[i].props.name) {
this._index = i;
this.props.onChoice(this.props.name, this.props.children[i].props.name, i, true);
return ;
}
}
this.chooseVariant(false);
}
/* Methods */
, chooseVariant: function (update) {
if (typeof update === "undefined") { update = true; }
var index = Math.floor(this._random() * this.props.children.length)
, variant = this.props.children[index].props.name;
this._set(variant);
this._index = index;
this.props.onChoice(this.props.name, variant, index, false);
if (update) {
this.forceUpdate();
}
return index;
}
, getVariant: function () {
var child = this.props.children[this._index]
, variant = child.props.name;
return variant;
}
, clearCookie: function () {
this._clear();
}
/* Render */
, render: function () {
var child = this.props.children[this._index];
return child;
}
});
return exports;
});