forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcastArray.js
More file actions
41 lines (39 loc) · 685 Bytes
/
castArray.js
File metadata and controls
41 lines (39 loc) · 685 Bytes
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
/**
* 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