1 | var Symbol = require('./_Symbol'),
|
---|
2 | copyArray = require('./_copyArray'),
|
---|
3 | getTag = require('./_getTag'),
|
---|
4 | isArrayLike = require('./isArrayLike'),
|
---|
5 | isString = require('./isString'),
|
---|
6 | iteratorToArray = require('./_iteratorToArray'),
|
---|
7 | mapToArray = require('./_mapToArray'),
|
---|
8 | setToArray = require('./_setToArray'),
|
---|
9 | stringToArray = require('./_stringToArray'),
|
---|
10 | values = require('./values');
|
---|
11 |
|
---|
12 | /** `Object#toString` result references. */
|
---|
13 | var mapTag = '[object Map]',
|
---|
14 | setTag = '[object Set]';
|
---|
15 |
|
---|
16 | /** Built-in value references. */
|
---|
17 | var symIterator = Symbol ? Symbol.iterator : undefined;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Converts `value` to an array.
|
---|
21 | *
|
---|
22 | * @static
|
---|
23 | * @since 0.1.0
|
---|
24 | * @memberOf _
|
---|
25 | * @category Lang
|
---|
26 | * @param {*} value The value to convert.
|
---|
27 | * @returns {Array} Returns the converted array.
|
---|
28 | * @example
|
---|
29 | *
|
---|
30 | * _.toArray({ 'a': 1, 'b': 2 });
|
---|
31 | * // => [1, 2]
|
---|
32 | *
|
---|
33 | * _.toArray('abc');
|
---|
34 | * // => ['a', 'b', 'c']
|
---|
35 | *
|
---|
36 | * _.toArray(1);
|
---|
37 | * // => []
|
---|
38 | *
|
---|
39 | * _.toArray(null);
|
---|
40 | * // => []
|
---|
41 | */
|
---|
42 | function toArray(value) {
|
---|
43 | if (!value) {
|
---|
44 | return [];
|
---|
45 | }
|
---|
46 | if (isArrayLike(value)) {
|
---|
47 | return isString(value) ? stringToArray(value) : copyArray(value);
|
---|
48 | }
|
---|
49 | if (symIterator && value[symIterator]) {
|
---|
50 | return iteratorToArray(value[symIterator]());
|
---|
51 | }
|
---|
52 | var tag = getTag(value),
|
---|
53 | func = tag == mapTag ? mapToArray : (tag == setTag ? setToArray : values);
|
---|
54 |
|
---|
55 | return func(value);
|
---|
56 | }
|
---|
57 |
|
---|
58 | module.exports = toArray;
|
---|