/
index.js
52 lines (45 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var typeOf = require('type')
/**
* Evaluates _obj_ to determine if it's an array, an array-like collection, or
* something else. This is useful when working with the function `arguments`
* collection and `HTMLElement` collections.
* Note: This implementation doesn't consider elements that are also
*
*
collections, such as `<form>` and `<select>`, to be array-like.
@method test
@param {Object} obj Object to test.
@return {Number} A number indicating the results of the test:
* 0: Neither an array nor an array-like collection.
* 1: Real array.
* 2: Array-like collection.
@api private
**/
module.exports = function isCollection(obj) {
var type = typeOf(obj)
if (type === 'array') return 1
switch (type) {
case 'arguments': return 2
case 'object':
if (isNodeList(obj)) return 2
try {
// indexed, but no tagName (element) or scrollTo/document (window. From DOM.isWindow test which we can't use here),
// or functions without apply/call (Safari
// HTMLElementCollection bug).
if ('length' in obj
&& !obj.tagName
&& !(obj.scrollTo && obj.document)
&& !obj.apply) {
return 2
}
} catch (ex) {}
default:
return 0
}
}
function isNodeList(nodes) {
return typeof nodes === 'object'
&& /^\[object (NodeList)\]$/.test(Object.prototype.toString.call(nodes))
&& nodes.hasOwnProperty('length')
&& (nodes.length == 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0))
}