mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Added Array.isArray() (fix #160)
This commit is contained in:
parent
658a28df0f
commit
b234317d25
@ -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
|
||||
|
||||
@ -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
43
tests/test_isarray.js
Normal 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;
|
||||
Loading…
x
Reference in New Issue
Block a user