Showing whether zjrJS.Doc. addArrayComprehension() actually works.
Used zjrJS Version:
Stable Minified version 20141202t2322 of zjrJS.
Preconditions
Javascript must be allowed to run on this page.
The version of zjrJS must be at least 20120504t1629. (Most of the Array Comprehension was added in version 20120411t0914.)
Successful When...
If the used browser natively supports array comprehension (MSIE anything older than the 9 version), the following fail before pressing the "Add Comprehension" button in the form, and succeed afterwards:
Javascript must be allowed to run on this page.
The version of zjrJS must be at least 20120504t1629. (Most of the Array Comprehension was added in version 20120411t0914.)
Successful When...
If the used browser natively supports array comprehension (MSIE anything older than the 9 version), the following fail before pressing the "Add Comprehension" button in the form, and succeed afterwards:
- In the form, pressing the List All button generates a listing of 3 fruits and 1 non-fruit in the Output box. The undefined item is ignored. This tests forEach().
- In the form, pressing the Get Fruits Only button filters the listing and shows only the 3 fruits in the Output box, omitting both the dumpling and the undefined item. This tests filter().
- In the form, pressing the Everything Is a Fruit button determines that not every item in the listing is a fruit, and presents this in the Output box. This tests every().
- In the form, pressing the Where is the Coconut? button determines that the position of the coconut in the dataset equals 3 (the coconut is the 4th item, and since javascript arrays start counting at 0, the 4th item has position 3), and presents this in the Output box. This tests indexOf().
- In the form, pressing the Tally Lengths button sums up the lengths of all 4 items to 26, and presents this in the Output box. This tests reduce().
Test Code:
var test=({
data: []
,
placeHolder: ({})
,
addComprehension: function() { "use strict";
zjrJS.Doc.addArrayComprehension();
}
,
init: function() { "use strict";
test.data = [ "apple", "banana", undefined, "coconut", "dumpling" ];
test.placeHolder = zjrJS.Doc.eid("placeholder");
}
,
listAll: function () { "use strict";
zjrJS.Doc.rC(test.placeHolder);
test.data.forEach(
test.lister, test.placeHolder
);
}
,
lister: function (itemValue, itemIndex, allItems ) { "use strict";
var D = zjrJS.Doc, li = D.cE( "li" );
D.aC( D.cT( itemValue ), li );
D.aC( li, this );
D = null;
}
,
showFruits: function() { "use strict";
var filteredData = test.data.filter(
test.fruitFilter
);
zjrJS.Doc.rC(test.placeHolder);
filteredData.forEach(
test.lister, test.placeHolder
);
}
,
fruitFilter: function(itemValue, itemIndex, allItems) { "use strict";
var notAFruit = "dumpling";
if (!itemValue) {
return false;
}
if (itemValue === notAFruit) {
return false;
}
return true;
}
,
everythingIsFruit: function() { "use strict";
var booleanEverythingIsAFruit = test.data.every(
test.fruitFilter
);
zjrJS.Doc.rC(test.placeHolder);
if (booleanEverythingIsAFruit) {
[ "Yes, everything is a fruit" ].forEach(
test.lister, test.placeHolder
);
} else {
[ "No, some aren't a fruit" ].forEach(
test.lister, test.placeHolder
);
}
}
,
whereIsTheCoconut: function() { "use strict";
var i = test.data.indexOf( "coconut");
[ "The coconut holds position " + i ].forEach(
test.lister, test.placeHolder
);
}
,
tallyLengths: function() { "use strict";
var tallyLength = test.data.reduce(
test.lengthTallier, 0
);
[ "The total length of all items = " + tallyLength ].forEach(
test.lister, test.placeHolder
);
}
,
lengthTallier: function(accumulator, itemValue) { "use strict";
if (!accumulator || isNaN(accumulator)) { accumulator = Number(0); }
if (!!itemValue) { if (!!itemValue.length) {
accumulator += Number(itemValue.length);
} }
return accumulator;
}
});
zjrJS.Doc.aE(window, "load", test.init);