Files
lodash/castArray.js
2017-01-09 17:38:33 -08:00

44 lines
736 B
JavaScript

import isArray from './isArray.js';
/**
* Casts `value` as an array if it's not one.
*
* @static
* @since 4.4.0
* @category Lang
* @param {*} value The value to inspect.
* @returns {Array} Returns the cast array.
* @example
*
* castArray(1);
* // => [1]
*
* castArray({ 'a': 1 });
* // => [{ 'a': 1 }]
*
* castArray('abc');
* // => ['abc']
*
* castArray(null);
* // => [null]
*
* castArray(undefined);
* // => [undefined]
*
* castArray();
* // => []
*
* var array = [1, 2, 3];
* console.log(castArray(array) === array);
* // => true
*/
function castArray(...args) {
if (!args.length) {
return [];
}
const value = args[0];
return isArray(value) ? value : [value];
}
export default castArray;