[Pkg-javascript-commits] [dojo] 18/149: isArray() and isArrayLike() fixes:
David Prévot
taffit at moszumanska.debian.org
Sat Feb 27 03:13:42 UTC 2016
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository dojo.
commit 4c89d551d73649aac52c8ce3cb7e6edba1dcc4d9
Author: Graeme Yeates <megawac at gmail.com>
Date: Sun Aug 10 10:22:31 2014 -0400
isArray() and isArrayLike() fixes:
* lang.isArrayLike should return bool for falsey arguments
* correct isArray for falsey values, cross frames and array extensions
Fixes #18251.
---
_base/lang.js | 8 +++-----
tests/_base/lang.js | 27 +++++++++++++++++++++++++++
2 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/_base/lang.js b/_base/lang.js
index a14ccc0..e544eab 100644
--- a/_base/lang.js
+++ b/_base/lang.js
@@ -256,13 +256,12 @@ define(["./kernel", "../has", "../sniff"], function(dojo, has){
return (typeof it == "string" || it instanceof String); // Boolean
},
- isArray: function(it){
+ isArray: Array.isArray || function(it){
// summary:
// Return true if it is an Array.
- // Does not work on Arrays created in other windows.
// it: anything
// Item to test.
- return it && (it instanceof Array || typeof it == "array"); // Boolean
+ return opts.call(it) == "[object Array]"; // Boolean
},
isFunction: function(it){
@@ -296,7 +295,7 @@ define(["./kernel", "../has", "../sniff"], function(dojo, has){
// and DOM collections will return true when passed to
// isArrayLike(), but will return false when passed to
// isArray().
- return it && it !== undefined && // Boolean
+ return !!it && // Boolean
// keep out built-in constructors (Number, String, ...) which have length
// properties
!lang.isString(it) && !lang.isFunction(it) &&
@@ -613,4 +612,3 @@ define(["./kernel", "../has", "../sniff"], function(dojo, has){
return lang;
});
-
diff --git a/tests/_base/lang.js b/tests/_base/lang.js
index 5802f1c..2f47fc1 100644
--- a/tests/_base/lang.js
+++ b/tests/_base/lang.js
@@ -111,6 +111,19 @@ define(["doh/main", "dojo/_base/array", "dojo/_base/lang", "dojo/_base/kernel"
t.assertTrue(lang.isArray([]));
t.assertTrue(lang.isArray(new Array()));
t.assertFalse(lang.isArray({}));
+ t.assertFalse(lang.isArray(''));
+ t.assertFalse(lang.isArray(0));
+ t.assertFalse(lang.isArray(NaN));
+ t.assertFalse(lang.isArray(null));
+ t.assertFalse(lang.isArray(undefined));
+ if(typeof window != "undefined"){
+ t.assertFalse(lang.isArray(window));
+ }
+ t.assertFalse(lang.isArray(Function));
+
+ function Tricky() {}
+ Tricky.prototype = [];
+ t.assertFalse(lang.isArray(new Tricky));
},
function isArrayLike(t){
@@ -118,6 +131,20 @@ define(["doh/main", "dojo/_base/array", "dojo/_base/lang", "dojo/_base/kernel"
t.assertTrue(lang.isArrayLike(new Array()));
t.assertFalse(lang.isArrayLike({}));
t.assertTrue(lang.isArrayLike(arguments));
+ t.assertFalse(lang.isArrayLike(""));
+ t.assertFalse(lang.isArrayLike(false));
+ t.assertFalse(lang.isArrayLike(NaN));
+ t.assertFalse(lang.isArrayLike(undefined));
+ t.assertFalse(lang.isArrayLike(null));
+ if(typeof window != "undefined"){
+ t.assertTrue(lang.isArrayLike(window));
+ }
+ t.assertFalse(lang.isArrayLike(Function));
+
+ t.assertTrue(lang.isArrayLike({0: 1, 1: 2, length: 2}));
+ function Tricky() {}
+ Tricky.prototype = [];
+ t.assertTrue(lang.isArrayLike(new Tricky));
},
function isString(t){
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/dojo.git
More information about the Pkg-javascript-commits
mailing list