toArray.js
1.37 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var Symbol = require('./_Symbol'),
copyArray = require('./_copyArray'),
getTag = require('./_getTag'),
isArrayLike = require('./isArrayLike'),
isString = require('./isString'),
iteratorToArray = require('./_iteratorToArray'),
mapToArray = require('./_mapToArray'),
setToArray = require('./_setToArray'),
stringToArray = require('./_stringToArray'),
values = require('./values');
/** `Object#toString` result references. */
var mapTag = '[object Map]',
setTag = '[object Set]';
/** Built-in value references. */
var symIterator = Symbol ? Symbol.iterator : undefined;
/**
* Converts `value` to an array.
*
* @static
* @since 0.1.0
* @memberOf _
* @category Lang
* @param {*} value The value to convert.
* @returns {Array} Returns the converted array.
* @example
*
* _.toArray({ 'a': 1, 'b': 2 });
* // => [1, 2]
*
* _.toArray('abc');
* // => ['a', 'b', 'c']
*
* _.toArray(1);
* // => []
*
* _.toArray(null);
* // => []
*/
function toArray(value) {
if (!value) {
return [];
}
if (isArrayLike(value)) {
return isString(value) ? stringToArray(value) : copyArray(value);
}
if (symIterator && value[symIterator]) {
return iteratorToArray(value[symIterator]());
}
var tag = getTag(value),
func = tag == mapTag ? mapToArray : (tag == setTag ? setToArray : values);
return func(value);
}
module.exports = toArray;