mirror of
https://github.com/github/codeql-action.git
synced 2025-12-28 02:00:12 +08:00
37 lines
630 B
TypeScript
37 lines
630 B
TypeScript
/**
|
|
Convert a value to an array.
|
|
|
|
_Specifying `null` or `undefined` results in an empty array._
|
|
|
|
@example
|
|
```
|
|
import arrify from 'arrify';
|
|
|
|
arrify('🦄');
|
|
//=> ['🦄']
|
|
|
|
arrify(['🦄']);
|
|
//=> ['🦄']
|
|
|
|
arrify(new Set(['🦄']));
|
|
//=> ['🦄']
|
|
|
|
arrify(null);
|
|
//=> []
|
|
|
|
arrify(undefined);
|
|
//=> []
|
|
```
|
|
*/
|
|
export default function arrify<ValueType>(
|
|
value: ValueType
|
|
): ValueType extends (null | undefined)
|
|
? [] // eslint-disable-line @typescript-eslint/ban-types
|
|
: ValueType extends string
|
|
? [string]
|
|
: ValueType extends readonly unknown[]
|
|
? ValueType
|
|
: ValueType extends Iterable<infer T>
|
|
? T[]
|
|
: [ValueType];
|