Skip to content

Commit

Permalink
Ignore errors when manipulating classes (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakiestfu authored Jul 15, 2019
1 parent 885e30f commit 04f6c74
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 101 deletions.
56 changes: 31 additions & 25 deletions build/study.dev.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
studyjs - A client side A/B tester
@version v5.0.2
@version v5.0.3
@link https://github.com/dollarshaveclub/study
@author Jacob Kelley <[email protected]>
@license MIT
Expand Down Expand Up @@ -37,7 +37,7 @@
var getDefaultBucket = function getDefaultBucket(buckets) {
var defaultBuckets = Object.keys(buckets).filter(function (name) {
var x = buckets[name];
return x.default || x.winner;
return x["default"] || x.winner;
});
return defaultBuckets[0] || Object.keys(buckets)[0];
};
Expand Down Expand Up @@ -112,37 +112,43 @@
};

_proto.removeClasses = function removeClasses(testName, exceptClassName) {
var root = this.root;
if (!root) return; // classList does not support returning all classes

var currentClassNames = root.className.split(/\s+/g).map(function (x) {
return x.trim();
}).filter(Boolean);
currentClassNames.filter(function (x) {
return x.indexOf(testName + "--") === 0;
}).filter(function (className) {
return className !== exceptClassName;
}).forEach(function (className) {
return root.classList.remove(className);
});
try {
var root = this.root;
if (!root) return; // classList does not support returning all classes

var currentClassNames = root.className.split(/\s+/g).map(function (x) {
return x.trim();
}).filter(Boolean);
currentClassNames.filter(function (x) {
return x.indexOf(testName + "--") === 0;
}).filter(function (className) {
return className !== exceptClassName;
}).forEach(function (className) {
return root.classList.remove(className);
});
} catch (_) {// Ignore
}
};

_proto.applyClasses = function applyClasses() {
var _this2 = this;

var userAssignments = this.userAssignments,
root = this.root;
if (!root) return;
Object.keys(userAssignments).forEach(function (testName) {
var bucket = userAssignments[testName];
var className = bucket ? testName + "--" + bucket : null; // remove all classes related to this bucket
try {
var userAssignments = this.userAssignments,
root = this.root;
if (!root) return;
Object.keys(userAssignments).forEach(function (testName) {
var bucket = userAssignments[testName];
var className = bucket ? testName + "--" + bucket : null; // remove all classes related to this bucket

_this2.removeClasses(testName, className); // only assign a class is the test is assigned to a bucket
// this removes then adds a class, which is not ideal but is clean
_this2.removeClasses(testName, className); // only assign a class is the test is assigned to a bucket
// this removes then adds a class, which is not ideal but is clean


if (className) root.classList.add(className);
});
if (className) root.classList.add(className);
});
} catch (_) {// Ignore
}
};

_proto.assignAll = function assignAll() {
Expand Down
56 changes: 31 additions & 25 deletions build/study.es.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
studyjs - A client side A/B tester
@version v5.0.2
@version v5.0.3
@link https://github.com/dollarshaveclub/study
@author Jacob Kelley <[email protected]>
@license MIT
Expand Down Expand Up @@ -31,7 +31,7 @@ var chooseWeightedItem = function chooseWeightedItem(names, weights) {
var getDefaultBucket = function getDefaultBucket(buckets) {
var defaultBuckets = Object.keys(buckets).filter(function (name) {
var x = buckets[name];
return x.default || x.winner;
return x["default"] || x.winner;
});
return defaultBuckets[0] || Object.keys(buckets)[0];
};
Expand Down Expand Up @@ -106,37 +106,43 @@ function () {
};

_proto.removeClasses = function removeClasses(testName, exceptClassName) {
var root = this.root;
if (!root) return; // classList does not support returning all classes

var currentClassNames = root.className.split(/\s+/g).map(function (x) {
return x.trim();
}).filter(Boolean);
currentClassNames.filter(function (x) {
return x.indexOf(testName + "--") === 0;
}).filter(function (className) {
return className !== exceptClassName;
}).forEach(function (className) {
return root.classList.remove(className);
});
try {
var root = this.root;
if (!root) return; // classList does not support returning all classes

var currentClassNames = root.className.split(/\s+/g).map(function (x) {
return x.trim();
}).filter(Boolean);
currentClassNames.filter(function (x) {
return x.indexOf(testName + "--") === 0;
}).filter(function (className) {
return className !== exceptClassName;
}).forEach(function (className) {
return root.classList.remove(className);
});
} catch (_) {// Ignore
}
};

_proto.applyClasses = function applyClasses() {
var _this2 = this;

var userAssignments = this.userAssignments,
root = this.root;
if (!root) return;
Object.keys(userAssignments).forEach(function (testName) {
var bucket = userAssignments[testName];
var className = bucket ? testName + "--" + bucket : null; // remove all classes related to this bucket
try {
var userAssignments = this.userAssignments,
root = this.root;
if (!root) return;
Object.keys(userAssignments).forEach(function (testName) {
var bucket = userAssignments[testName];
var className = bucket ? testName + "--" + bucket : null; // remove all classes related to this bucket

_this2.removeClasses(testName, className); // only assign a class is the test is assigned to a bucket
// this removes then adds a class, which is not ideal but is clean
_this2.removeClasses(testName, className); // only assign a class is the test is assigned to a bucket
// this removes then adds a class, which is not ideal but is clean


if (className) root.classList.add(className);
});
if (className) root.classList.add(className);
});
} catch (_) {// Ignore
}
};

_proto.assignAll = function assignAll() {
Expand Down
56 changes: 31 additions & 25 deletions build/study.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
studyjs - A client side A/B tester
@version v5.0.2
@version v5.0.3
@link https://github.com/dollarshaveclub/study
@author Jacob Kelley <[email protected]>
@license MIT
Expand Down Expand Up @@ -37,7 +37,7 @@
var getDefaultBucket = function getDefaultBucket(buckets) {
var defaultBuckets = Object.keys(buckets).filter(function (name) {
var x = buckets[name];
return x.default || x.winner;
return x["default"] || x.winner;
});
return defaultBuckets[0] || Object.keys(buckets)[0];
};
Expand Down Expand Up @@ -112,37 +112,43 @@
};

_proto.removeClasses = function removeClasses(testName, exceptClassName) {
var root = this.root;
if (!root) return; // classList does not support returning all classes

var currentClassNames = root.className.split(/\s+/g).map(function (x) {
return x.trim();
}).filter(Boolean);
currentClassNames.filter(function (x) {
return x.indexOf(testName + "--") === 0;
}).filter(function (className) {
return className !== exceptClassName;
}).forEach(function (className) {
return root.classList.remove(className);
});
try {
var root = this.root;
if (!root) return; // classList does not support returning all classes

var currentClassNames = root.className.split(/\s+/g).map(function (x) {
return x.trim();
}).filter(Boolean);
currentClassNames.filter(function (x) {
return x.indexOf(testName + "--") === 0;
}).filter(function (className) {
return className !== exceptClassName;
}).forEach(function (className) {
return root.classList.remove(className);
});
} catch (_) {// Ignore
}
};

_proto.applyClasses = function applyClasses() {
var _this2 = this;

var userAssignments = this.userAssignments,
root = this.root;
if (!root) return;
Object.keys(userAssignments).forEach(function (testName) {
var bucket = userAssignments[testName];
var className = bucket ? testName + "--" + bucket : null; // remove all classes related to this bucket
try {
var userAssignments = this.userAssignments,
root = this.root;
if (!root) return;
Object.keys(userAssignments).forEach(function (testName) {
var bucket = userAssignments[testName];
var className = bucket ? testName + "--" + bucket : null; // remove all classes related to this bucket

_this2.removeClasses(testName, className); // only assign a class is the test is assigned to a bucket
// this removes then adds a class, which is not ideal but is clean
_this2.removeClasses(testName, className); // only assign a class is the test is assigned to a bucket
// this removes then adds a class, which is not ideal but is clean


if (className) root.classList.add(className);
});
if (className) root.classList.add(className);
});
} catch (_) {// Ignore
}
};

_proto.assignAll = function assignAll() {
Expand Down
4 changes: 2 additions & 2 deletions build/study.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "studyjs",
"version": "5.0.2",
"version": "5.0.3",
"author": "Jacob Kelley <[email protected]>",
"homepage": "https://github.com/dollarshaveclub/study",
"description": "A client side A/B tester",
Expand Down
Loading

0 comments on commit 04f6c74

Please sign in to comment.