Permalink
Browse files

Update isArray_ function and add test coverage

  • Loading branch information...
1 parent 5e3eb88 commit 6ca766d5eae5e6ae2d3f9e8222dbd042e7e92dca Josh Susser committed Feb 25, 2010
Showing with 20 additions and 11 deletions.
  1. +2 −6 lib/jasmine-0.10.0.js
  2. +17 −0 spec/suites/UtilSpec.js
  3. +1 −5 src/base.js
View
@@ -89,11 +89,7 @@ jasmine.getEnv = function() {
* @returns {Boolean}
*/
jasmine.isArray_ = function(value) {
- return value &&
- typeof value === 'object' &&
- typeof value.length === 'number' &&
- typeof value.splice === 'function' &&
- !(value.propertyIsEnumerable('length'));
+ return Object.prototype.toString.apply(value) === '[object Array]';
};
/**
@@ -2261,5 +2257,5 @@ jasmine.version_= {
"major": 0,
"minor": 10,
"build": 0,
- "revision": 1267068340
+ "revision": 1267068885
};
View
@@ -20,4 +20,21 @@ describe("jasmine.util", function() {
expect(destination).toEqual({foo: null});
});
});
+
+ describe("isArray_", function() {
+ it("should return true if the argument is an array", function() {
+ expect(jasmine.isArray_([])).toBe(true);
+ expect(jasmine.isArray_(['a'])).toBe(true);
+ expect(jasmine.isArray_(new Array())).toBe(true);
+ });
+
+ it("should return false if the argument is not an array", function() {
+ expect(jasmine.isArray_(undefined)).toBe(false);
+ expect(jasmine.isArray_({})).toBe(false);
+ expect(jasmine.isArray_(function() {})).toBe(false);
+ expect(jasmine.isArray_('foo')).toBe(false);
+ expect(jasmine.isArray_(5)).toBe(false);
+ expect(jasmine.isArray_(null)).toBe(false);
+ });
+ });
});
View
@@ -89,11 +89,7 @@ jasmine.getEnv = function() {
* @returns {Boolean}
*/
jasmine.isArray_ = function(value) {
- return value &&
- typeof value === 'object' &&
- typeof value.length === 'number' &&
- typeof value.splice === 'function' &&
- !(value.propertyIsEnumerable('length'));
+ return Object.prototype.toString.apply(value) === '[object Array]';
};
/**

2 comments on commit 6ca766d

Contributor

xian replied Feb 25, 2010

Interesting, is that the safest way? Does it break if some jerk redefines Object.prototype.toArray?

It would break if somebody redefined Object.prototype.toString. I would prefer:

 return value instanceof Array

If you're against duck typing.

Please sign in to comment.