Skip to content

Commit

Permalink
Add BOOMR.utils.arrayFind function
Browse files Browse the repository at this point in the history
  • Loading branch information
querymetrics authored and nicjansma committed Apr 4, 2018
1 parent 94b92e5 commit 2a8b36d
Show file tree
Hide file tree
Showing 20 changed files with 262 additions and 28 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"comma-dangle": [2, "never"],
"operator-linebreak": [2, "after"],
"space-in-parens": [2, "never"],
"no-debugger": "error",

//
// Disabled rules
Expand Down
45 changes: 40 additions & 5 deletions boomerang.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ BOOMR_check_doc_domain();
nest_level = 0;
}

if (Object.prototype.toString.call(o) === "[object Array]") {
if (BOOMR.utils.isArray(o)) {
for (k = 0; k < o.length; k++) {
if (nest_level > 0 && o[k] !== null && typeof o[k] === "object") {
value.push(
Expand Down Expand Up @@ -615,7 +615,7 @@ BOOMR_check_doc_domain();
* @returns {string} Cleaned up URL
*/
cleanupURL: function(url, urlLimit) {
if (!url || Object.prototype.toString.call(url) === "[object Array]") {
if (!url || BOOMR.utils.isArray(url)) {
return "";
}

Expand Down Expand Up @@ -689,6 +689,11 @@ BOOMR_check_doc_domain();
arrayFilter: function(array, predicate) {
var result = [];

if (!(BOOMR.utils.isArray(array) || (array && typeof array.length === "number")) ||
typeof predicate !== "function") {
return result;
}

if (typeof array.filter === "function") {
result = array.filter(predicate);
}
Expand All @@ -706,6 +711,37 @@ BOOMR_check_doc_domain();
}
return result;
},
/**
* `find` for arrays
*
* @private
* @param {Array} array The array to iterate over.
* @param {Function} predicate The function invoked per iteration.
* @returns {Array} Returns the value of first element that satisfies the predicate.
*/
arrayFind: function(array, predicate) {
if (!(BOOMR.utils.isArray(array) || (array && typeof array.length === "number")) ||
typeof predicate !== "function") {
return undefined;
}

if (typeof array.find === "function") {
return array.find(predicate);
}
else {
var index = -1,
length = array.length,
value;

while (++index < length) {
value = array[index];
if (predicate(value, index, array)) {
return value;
}
}
return undefined;
}
},
/**
* @desc
* Add a MutationObserver for a given element and terminate after `timeout`ms.
Expand Down Expand Up @@ -818,7 +854,7 @@ BOOMR_check_doc_domain();

for (k in vars) {
if (vars.hasOwnProperty(k)) {
if (Object.prototype.toString.call(vars[k]) === "[object Array]") {
if (BOOMR.utils.isArray(vars[k])) {
for (i = 0; i < vars[k].length; ++i) {
l += BOOMR.utils.pushVars(form, vars[k][i], k + "[" + i + "]");
}
Expand Down Expand Up @@ -1468,8 +1504,7 @@ BOOMR_check_doc_domain();
return this;
}

if (arguments.length === 1 &&
Object.prototype.toString.apply(arg0) === "[object Array]") {
if (arguments.length === 1 && BOOMR.utils.isArray(arg0)) {
params = arg0;
}
else {
Expand Down
2 changes: 1 addition & 1 deletion plugins/restiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ see: http://www.w3.org/TR/resource-timing/
// If this is a link, set its flags
if (t.initiatorType === "link" && links[t.name]) {
// split on ASCII whitespace
links[t.name].rel.split(/[\u0009\u000A\u000C\u000D\u0020]+/).find(function(rel) { //eslint-disable-line no-loop-func
BOOMR.utils.arrayFind(links[t.name].rel.split(/[\u0009\u000A\u000C\u000D\u0020]+/), function(rel) { //eslint-disable-line no-loop-func
// `rel`s are case insensitive
rel = rel.toLowerCase();

Expand Down
7 changes: 5 additions & 2 deletions tests/page-templates/11-restiming/00-clear-onbeacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
describe("e2e/11-restiming/00-clear-onbeacon", function() {
var t = BOOMR_test;

it("Should pass basic beacon validation", function(done){
it("Should pass basic beacon validation", function(done) {
t.validateBeaconWasSent(done);
});

it("Should clear ResourceTiming array after beacon (if ResourceTiming is enabled)", function(){
it("Should clear ResourceTiming array after beacon (if ResourceTiming is enabled)", function() {
if (t.isResourceTimingSupported()) {
var entries = window.performance.getEntriesByType("resource");
assert.equal(entries.length, 0);
}
else {
this.skip();
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
describe("e2e/11-restiming/01-clear-onbeacon-disabled", function() {
var t = BOOMR_test;

it("Should pass basic beacon validation", function(done){
it("Should pass basic beacon validation", function(done) {
t.validateBeaconWasSent(done);
});

it("Should not clear ResourceTiming array after beacon (if ResourceTiming is enabled)", function(){
it("Should not clear ResourceTiming array after beacon (if ResourceTiming is enabled)", function() {
if (t.isResourceTimingSupported()) {
var entries = window.performance.getEntriesByType("resource");
assert.isTrue(entries.length > 0);
}
else {
this.skip();
}
});
});
13 changes: 11 additions & 2 deletions tests/page-templates/11-restiming/02-resource-dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("e2e/11-restiming/02-resource-dimensions", function() {
t.validateBeaconWasSent(done);
});

it("Should have dimensions for the IMG on the page (if ResourceTiming is supported)", function(){
it("Should have dimensions for the IMG on the page (if ResourceTiming is supported)", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

Expand All @@ -21,9 +21,12 @@ describe("e2e/11-restiming/02-resource-dimensions", function() {
// 2000 nw = 1jk
assert.match(b.restiming, /\*05k,b4,dw,2s,km,1jk\b/);
}
else {
this.skip();
}
});

it("Should have dimensions for the IFRAME on the page (if ResourceTiming is supported)", function(){
it("Should have dimensions for the IFRAME on the page (if ResourceTiming is supported)", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

Expand All @@ -33,6 +36,9 @@ describe("e2e/11-restiming/02-resource-dimensions", function() {
// 200 x = 5k
assert.include(b.restiming, "*01e,1e,m8,5k");
}
else {
this.skip();
}
});

it("Should not have timepoints for resources on the page (even if ResourceTiming is supported)", function() {
Expand All @@ -51,5 +57,8 @@ describe("e2e/11-restiming/02-resource-dimensions", function() {
assert.strictEqual(decompressed[tp[0]][1], 21400);
*/
}
else {
this.skip();
}
});
});
9 changes: 9 additions & 0 deletions tests/page-templates/11-restiming/03-url-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe("e2e/11-restiming/03-url-length", function() {
var b = tf.lastBeacon();
assert.isDefined(b.restiming);
}
else {
this.skip();
}
});

it("Should have trimmed the long URL (if ResourceTiming is supported)", function() {
Expand All @@ -27,6 +30,9 @@ describe("e2e/11-restiming/03-url-length", function() {
return r.name.indexOf("blackhole?...") !== -1;
}), "Find blackhole?...");
}
else {
this.skip();
}
});

it("Should have trimmed the pixel URL (if ResourceTiming is supported)", function() {
Expand All @@ -39,5 +45,8 @@ describe("e2e/11-restiming/03-url-length", function() {
return r.name.indexOf("/foo/...") !== -1;
}), "Find /foo/...");
}
else {
this.skip();
}
});
});
5 changes: 4 additions & 1 deletion tests/page-templates/11-restiming/04-resource-sizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("e2e/11-restiming/04-resource-sizes", function() {
t.validateBeaconWasSent(done);
});

it("Should have sizes for the IMG on the page (if ResourceTiming2 is supported)", function(){
it("Should have sizes for the IMG on the page (if ResourceTiming2 is supported)", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

Expand All @@ -28,5 +28,8 @@ describe("e2e/11-restiming/04-resource-sizes", function() {
}
assert.equal(3, cnt);
}
else {
this.skip();
}
});
});
9 changes: 9 additions & 0 deletions tests/page-templates/11-restiming/06-iframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ describe("e2e/11-restiming/06-iframes", function() {
var b = tf.lastBeacon();
assert.isDefined(b.restiming);
}
else {
this.skip();
}
});

it("Should have all of the resouces on the page", function() {
Expand All @@ -37,6 +40,9 @@ describe("e2e/11-restiming/06-iframes", function() {
}), "Finding " + url);
}
}
else {
this.skip();
}
});

it("Should have the IMG in the IFRAME", function() {
Expand All @@ -49,5 +55,8 @@ describe("e2e/11-restiming/06-iframes", function() {
return r.name.indexOf("/assets/img.jpg?iframe") !== -1;
}), "Finding /assets/img.jpg?iframe in the IFRAME");
}
else {
this.skip();
}
});
});
32 changes: 25 additions & 7 deletions tests/page-templates/11-restiming/06-svg-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,85 @@ describe("e2e/11-restiming/06-svg-image", function() {
return null;
}

it("Should pass basic beacon validation", function(done){
it("Should pass basic beacon validation", function(done) {
t.validateBeaconWasSent(done);
});

it("Should found the SVG:image element", function(){
it("Should found the SVG:image element", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));
var img = findSvgImage(resources);
assert.isDefined(img, "Image is not null");
}
else {
this.skip();
}
});

it("Should have set the SVG:image initiatorType to IMAGE", function(){
it("Should have set the SVG:image initiatorType to IMAGE", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));
var img = findSvgImage(resources);
assert.equal(img.initiatorType, "image");
}
else {
this.skip();
}
});

it("Should have captured the SVG:image element height", function(){
it("Should have captured the SVG:image element height", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));
var img = findSvgImage(resources);
assert.equal(img.height, 200);
}
else {
this.skip();
}
});

it("Should have captured the SVG:image element width", function(){
it("Should have captured the SVG:image element width", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));
var img = findSvgImage(resources);
assert.equal(img.width, 400);
}
else {
this.skip();
}
});

it("Should have captured the SVG:image element top", function(){
it("Should have captured the SVG:image element top", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));
var img = findSvgImage(resources);
assert.operator(img.y, ">=", 20);
}
else {
this.skip();
}
});

it("Should have captured the SVG:image element left", function(){
it("Should have captured the SVG:image element left", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

var resources = ResourceTimingDecompression.decompressResources(JSON.parse(b.restiming));
var img = findSvgImage(resources);
assert.operator(img.x, ">=", 10);
}
else {
this.skip();
}
});
});
3 changes: 3 additions & 0 deletions tests/page-templates/11-restiming/06-type-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ describe("e2e/11-restiming/06-type-filter", function() {
assert.equal(resources[0].initiatorType, "img");
assert.include(resources[0].name, "img.jpg");
}
else {
this.skip();
}
});
});
7 changes: 5 additions & 2 deletions tests/page-templates/11-restiming/07-page-ready-held.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ describe("e2e/11-restiming/07-page-ready-held", function() {
var t = BOOMR_test;
var tf = BOOMR.plugins.TestFramework;

it("Should pass basic beacon validation", function(done){
it("Should pass basic beacon validation", function(done) {
t.validateBeaconWasSent(done);
});

it("Should include img.jpg in the ResourceTiming data", function(){
it("Should include img.jpg in the ResourceTiming data", function() {
if (t.isResourceTimingSupported()) {
var b = tf.beacons[0];

Expand All @@ -25,5 +25,8 @@ describe("e2e/11-restiming/07-page-ready-held", function() {

assert.isTrue(found, "Found img.jpg");
}
else {
this.skip();
}
});
});
Loading

0 comments on commit 2a8b36d

Please sign in to comment.