mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-30 15:07:50 +00:00
42 lines
697 B
JavaScript
42 lines
697 B
JavaScript
|
|
/**
|
|
* Casts `value` as an array if it's not one.
|
|
*
|
|
* @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();
|
|
* // => []
|
|
*
|
|
* const array = [1, 2, 3];
|
|
* console.log(castArray(array) === array);
|
|
* // => true
|
|
*/
|
|
function castArray(...args) {
|
|
if (!args.length) {
|
|
return [];
|
|
}
|
|
const value = args[0];
|
|
return Array.isArray(value) ? value : [value];
|
|
}
|
|
|
|
export default castArray;
|