Comparing version 0.3.1 to 0.4.0
17
ab.js
@@ -7,3 +7,3 @@ var Experiment = require('./experiment') | ||
var experiments = options.experiments || [] | ||
var enrolled = options.enrolled || [] | ||
var enrolled = options.enrolled || {} | ||
@@ -22,4 +22,7 @@ this.defaults = options.defaults || {} | ||
} | ||
for (var i = 0; i < enrolled.length; i++) { | ||
this.enroll(this.experiments.get(enrolled[i])) | ||
var keys = Object.keys(enrolled) | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
var x = enrolled[key] | ||
this.enroll(this.experiments.get(x), key) | ||
} | ||
@@ -44,3 +47,3 @@ } | ||
AB.prototype.enroll = function (experiment, now) { | ||
AB.prototype.enroll = function (experiment, subject, now) { | ||
now = now || this.now() | ||
@@ -52,3 +55,3 @@ if (typeof(experiment) === 'string') { | ||
if (experiment.isLive(now)) { | ||
this.enrolled.add(experiment) | ||
this.enrolled.add(experiment, experiment.key(subject)) | ||
} | ||
@@ -76,3 +79,3 @@ return this | ||
var s = util.merge(this.subject, subject) | ||
var x = this.enrolled.getFirstLive(variable, now) || | ||
var x = this.enrolled.getFirstMatch(variable, s, now) || | ||
this.experiments.getFirstEligible(variable, s, this.enrolled, now) || | ||
@@ -83,3 +86,3 @@ this.experiments.getReleased(variable, now) | ||
value = x.choose(s, now)[variable] | ||
this.enroll(x, now) | ||
this.enroll(x, s, now) | ||
} catch (e) {} | ||
@@ -86,0 +89,0 @@ } |
@@ -6,2 +6,3 @@ var util = require('./util') | ||
this.experimentsByVariable = {} | ||
this.activeSubjectKeys = {} | ||
} | ||
@@ -62,2 +63,18 @@ | ||
ExperimentIndex.prototype.getLive = function (variable, now) { | ||
return this.getByVariable(variable).filter( | ||
function (x) { | ||
return x.isLive(now) | ||
} | ||
) | ||
} | ||
ExperimentIndex.prototype.getFirstMatch = function (variable, subject, now) { | ||
return this.getLive(variable, now).filter( | ||
function (x) { | ||
return this.activeSubjectKeys[x.key(subject)] | ||
}.bind(this) | ||
)[0] | ||
} | ||
/*/ | ||
@@ -68,8 +85,3 @@ returns the first active experiment for the given variable and date, | ||
ExperimentIndex.prototype.getFirstLive = function (variable, now) { | ||
var experiments = this.getByVariable(variable) | ||
for (var i = 0; i < experiments.length; i++) { | ||
if (experiments[i].isLive(now)) { | ||
return experiments[i] | ||
} | ||
} | ||
return this.getLive(variable, now)[0] | ||
} | ||
@@ -85,3 +97,3 @@ | ||
enrolled = enrolled || new ExperimentIndex() | ||
function isEnrolled(name) { return !!enrolled.get(name) } | ||
function isEnrolled(name) { return !!enrolled.get(name, subject) } | ||
function noConflicts(x) { | ||
@@ -118,6 +130,10 @@ return Object.keys(x.conflictsWith).filter(isEnrolled).length === 0 | ||
/*/ | ||
returns the experiment with the given `name` | ||
returns the experiment with the given `name` optionally filtered by `subject` | ||
/*/ | ||
ExperimentIndex.prototype.get = function (name) { | ||
return this.experimentsByName[name] | ||
ExperimentIndex.prototype.get = function (name, subject) { | ||
var x = this.experimentsByName[name] | ||
if (subject && x && !this.activeSubjectKeys[x.key(subject)]) { | ||
return | ||
} | ||
return x | ||
} | ||
@@ -134,3 +150,3 @@ | ||
/*/ | ||
ExperimentIndex.prototype.add = function (experiment) { | ||
ExperimentIndex.prototype.add = function (experiment, key) { | ||
if (!experiment || this.get(experiment.name)) { | ||
@@ -149,2 +165,5 @@ return this | ||
this.experimentsByName[experiment.name] = experiment | ||
if (key) { | ||
this.activeSubjectKeys[key] = true | ||
} | ||
return this | ||
@@ -151,0 +170,0 @@ } |
@@ -69,2 +69,3 @@ var sha1 = require('js-sha1') | ||
Experiment.prototype.key = function (subject) { | ||
subject = subject || {} | ||
var attributes = this.attributes() | ||
@@ -71,0 +72,0 @@ var keyParts = [this.name] |
{ | ||
"name": "abatar", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "A/B testing minion", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -219,3 +219,3 @@ var test = require('tape') | ||
t.equal(index.getFirstEligible('b', {}, enrolled, t0), x, 'initial experiment') | ||
enrolled.add(x) // enroll in experiment X | ||
enrolled.add(x, x.key()) // enroll in experiment X | ||
t.equal(index.getFirstEligible('b', {}, enrolled, t2), x, 'consistent with enrollment') | ||
@@ -222,0 +222,0 @@ t.equal(index.getFirstEligible('c', {}, enrolled, t1), undefined, 'Y conflicts with X') |
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
48921
1109