1 | import Symbol from './_Symbol.js';
|
---|
2 | import copyArray from './_copyArray.js';
|
---|
3 | import getTag from './_getTag.js';
|
---|
4 | import isArrayLike from './isArrayLike.js';
|
---|
5 | import isString from './isString.js';
|
---|
6 | import iteratorToArray from './_iteratorToArray.js';
|
---|
7 | import mapToArray from './_mapToArray.js';
|
---|
8 | import setToArray from './_setToArray.js';
|
---|
9 | import stringToArray from './_stringToArray.js';
|
---|
10 | import values from './values.js';
|
---|
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 | export default toArray;
|
---|