1 | /**
|
2 | Convert a value to an array.
|
3 |
|
4 | _Specifying `null` or `undefined` results in an empty array._
|
5 |
|
6 | @example
|
7 | ```
|
8 | import arrify from 'arrify';
|
9 |
|
10 | arrify('🦄');
|
11 | //=> ['🦄']
|
12 |
|
13 | arrify(['🦄']);
|
14 | //=> ['🦄']
|
15 |
|
16 | arrify(new Set(['🦄']));
|
17 | //=> ['🦄']
|
18 |
|
19 | arrify(null);
|
20 | //=> []
|
21 |
|
22 | arrify(undefined);
|
23 | //=> []
|
24 | ```
|
25 | */
|
26 | export default function arrify<ValueType>(
|
27 | value: ValueType
|
28 | ): ValueType extends (null | undefined)
|
29 | ? [] // eslint-disable-line @typescript-eslint/ban-types
|
30 | : ValueType extends string
|
31 | ? [string]
|
32 | : ValueType extends readonly unknown[]
|
33 | ? ValueType
|
34 | : ValueType extends Iterable<infer T>
|
35 | ? T[]
|
36 | : [ValueType];
|