An open API service indexing awesome lists of open source software.

https://github.com/fantasyui-com/oneof

Return a random item from a list. [Library, Algorithm, Standard Library]
https://github.com/fantasyui-com/oneof

library

Last synced: 4 days ago
JSON representation

Return a random item from a list. [Library, Algorithm, Standard Library]

Awesome Lists containing this project

README

          

# oneof

Select a random item from an array.

## Installation

```sh
npm install oneof
```

## Usage

```js
import oneof from 'oneof';

oneof(['apple', 'banana', 'cherry']); // => 'banana' (random)
oneof([1, 2, 3, 4, 5]); // => 3 (random)
oneof(['only']); // => 'only'
oneof([]); // => null
oneof(); // => null
```

## API

### oneof(array)

Returns a random item from the array, or `null` if the array is empty or undefined.

| Parameter | Type | Description |
|-----------|---------|--------------------------|
| `array` | `Array` | The array to select from |

**Returns:** A random item from the array, or `null`.

## License

ISC