-
Notifications
You must be signed in to change notification settings - Fork 24
/
react-ab.js
166 lines (135 loc) · 4.13 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
;(function (root, factory) {
/* istanbul ignore next */
if (typeof module !== "undefined" && module.exports) {
module.exports = factory(require("react"));
} else if (typeof define === "function" && define.amd) {
define(["react"], factory);
} else {
root.ReactAB = factory(root.React);
}
})(this, function (React) {
"use strict";
var exports = {};
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;
if (typeof seconds !== "undefined") {
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 = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired
, children: React.PropTypes.node.isRequired
}
, render: function () {
if (React.Children.count(this.props.children) === 1) {
return this.props.children;
}
return React.createElement("span", null, this.props.children);
}
});
exports.Experiment = React.createClass({
getInitialState: function () {
return {
index: null
};
}
, propTypes: {
name: React.PropTypes.string.isRequired
, children: React.PropTypes.array.isRequired
, onChoice: React.PropTypes.func.isRequired
, random: React.PropTypes.func
, get: React.PropTypes.func
, set: React.PropTypes.func
, clear: React.PropTypes.func
}
, contextTypes: {
randomExperiment: React.PropTypes.func
, getExperiment: React.PropTypes.func
, setExperiment: React.PropTypes.func
, clearExperiment: React.PropTypes.func
}
, random: function () {
return this.props.random || this.context.randomExperiment || random;
}
, get: function () {
return this.props.get || this.context.getExperiment || browserCookie.get;
}
, set: function () {
return this.props.set || this.context.setExperiment || browserCookie.set;
}
, clear: function () {
return this.props.clear || this.context.clearExperiment || browserCookie.clear;
}
, componentWillMount: function () {
var variant = this.get()(this.cookieName());
for (var i = 0; i < this.props.children.length; i += 1) {
if (variant === this.props.children[i].props.name) {
this.setState({ index: i });
this.props.onChoice(this.props.name, this.props.children[i].props.name, i, true);
return ;
}
}
this.chooseVariant();
}
, chooseVariant: function (fire) {
var index = Math.floor(this.random()() * this.props.children.length)
, variant = this.props.children[index].props.name;
this.set()(this.cookieName(), variant);
this.setState({ index: index });
this.props.onChoice(this.props.name, variant, index, false);
return index;
}
, getVariant: function () {
var child = this.props.children[this.state.index]
, variant = child.props.name;
return variant;
}
, cookieName: function () {
return "react_ab_" + this.props.name;
}
, clearCookie: function () {
this.clear()(this.cookieName());
}
, render: function () {
var child = this.props.children[this.state.index];
return child;
}
});
return exports;
});