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

73.91% Statements 17/23
59.09% Branches 13/22
100% Functions 2/2
100% Lines 15/15

    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
    397x
    397x
    397x
    397x
    397x
    397x
    332x

    332x
    332x
    41x
    41x

    332x
    27x

    397x

     /**
     * 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; i < collection.length; 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]";
    }