Files
lodash/toArray.js
John-David Dalton 466c67a8b6 Bump to v4.1.0.
2016-01-29 01:20:57 -08:00

58 lines
1.4 KiB
JavaScript

import Symbol from './_Symbol';
import copyArray from './_copyArray';
import getTag from './_getTag';
import isArrayLike from './isArrayLike';
import isString from './isString';
import iteratorToArray from './_iteratorToArray';
import mapToArray from './_mapToArray';
import setToArray from './_setToArray';
import stringToArray from './_stringToArray';
import values from './values';
/** `Object#toString` result references. */
var mapTag = '[object Map]',
setTag = '[object Set]';
/** Built-in value references. */
var iteratorSymbol = typeof (iteratorSymbol = Symbol && Symbol.iterator) == 'symbol' ? iteratorSymbol : undefined;
/**
* Converts `value` to an array.
*
* @static
* @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 (iteratorSymbol && value[iteratorSymbol]) {
return iteratorToArray(value[iteratorSymbol]());
}
var tag = getTag(value),
func = tag == mapTag ? mapToArray : (tag == setTag ? setToArray : values);
return func(value);
}
export default toArray;