blob: cc495d6b8d06e5f771bef84633f315b180409845 (
plain)
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
|
/**
* Convert an array-like object into an `Array`.
* If `collection` is already an `Array`, then will return a clone of `collection`.
*
* @param {Array | Mixed} collection An `Array` or array-like object to convert e.g. `arguments` or `NodeList`
* @return {Array} Naive conversion of `collection` to a new `Array`.
* @api private
*/
module.exports = function toArray(collection) {
if (typeof collection === 'undefined') return []
if (collection === null) return [null]
if (collection === window) return [window]
if (typeof collection === 'string') return [collection]
if (Array.isArray(collection)) return collection.slice()
if (typeof collection.length != 'number') return [collection]
if (typeof collection === 'function') return [collection]
var arr = []
for (var i = 0; i < collection.length; i++) {
if (collection.hasOwnProperty(i) || i in collection) {
arr.push(collection[i])
}
}
if (!arr.length) return []
return arr
}
|