# All files / src/utils to-array.js

76% Statements 19/25
59.09% Branches 13/22
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

    17x
    364x
    364x
    364x
    364x
    364x
    364x
    360x

    360x
    360x
    41x
    41x

    360x
    27x

    364x

     /**
     * Source: https://github.com/timoxley/to-array
     *
     * 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 public
     */

    module.exports = function toArray(collection) {
      Iif (typeof collection === 'undefined') return []
      Iif (collection === null) return [null]
      Iif (collection === window) return [window]
      Iif (typeof collection === 'string') return [collection]
      Iif (isArray(collection)) return collection
      if (typeof collection.length != 'number') return [collection]
      Iif (typeof collection === 'function' && collection instanceof Function) return [collection]

      var arr = [];
      for (var i = 0, il = collection.length; i < il; i++) {
        Eif (Object.prototype.hasOwnProperty.call(collection, i) || i in collection) {
          arr.push(collection[i])
        }
      }
      if (!arr.length) return []
      return arr
    }

    function isArray(arr) {
      return Object.prototype.toString.call(arr) === '[object Array]'
    }