Added Array.isArray() (fix #160)

This commit is contained in:
Gordon Williams (u36) 2014-01-21 17:02:45 +00:00
parent 658a28df0f
commit b234317d25
3 changed files with 51 additions and 0 deletions

View File

@ -3,6 +3,7 @@
Fix parse order for 'typeof' (fix #172)
Added Number object (fix #165)
Bounded ftoa (stops crash when printing Number.MAX_VALUE) - helps with #185
Added Array.isArray() (fix #160)
1v45 : Fix parseFloat("foo") not returning NaN (and assert) - fix #149
Remove Integer.parseInt

View File

@ -326,3 +326,10 @@ JsVar *jswrap_array_slice(JsVar *parent, JsVar *startVar, JsVar *endVar) {
void jswrap_array_forEach(JsVar *parent, JsVar *funcVar, JsVar *thisVar) {
_jswrap_array_map_or_forEach(parent, funcVar, thisVar, false);
}
/*JSON{ "type":"staticmethod", "class": "Array", "name" : "isArray",
"description" : "Returns true if the provided object is an array",
"generate_full" : "jsvIsArray(var)",
"params" : [ [ "var", "JsVar", "The variable to be tested"] ],
"return" : ["bool", "True if var is an array, false if not."]
}*/

43
tests/test_isarray.js Normal file
View File

@ -0,0 +1,43 @@
var tests=0,testPass=0;
function test(a,b) {
tests++;
if (Array.isArray(a)==b) {
//console.log("Test "+tests+" passed.");
testPass++;
} else {
console.log("Test "+tests+" failed.");
}
}
Array.isArrayT = function (v) {
test(v,true);
}
Array.isArrayF = function (v) {
test(v,false);
}
// all following calls return true
Array.isArrayT([]);
Array.isArrayT([1]);
Array.isArrayT( new Array() );
//Array.isArrayT( Array.prototype ); // Little known fact: Array.prototype itself is an array.
// Oh come on... Is there any good reason for this being an array apart from spec compliance?
// Implementing this differently is going to be hard
// all following calls return false
Array.isArrayF();
Array.isArrayF({});
Array.isArrayF(null);
Array.isArrayF(undefined);
Array.isArrayF(17);
Array.isArrayF("Array");
Array.isArrayF(true);
Array.isArrayF(false);
Array.isArrayF({ __proto__ : Array.prototype });
// GW added
Array.isArrayF(new Uint8Array(3));
result = tests==testPass;