https://github.com/smakss/random-array-element
Selects a random array element without repetition.
https://github.com/smakss/random-array-element
array arrays dom hacktoberfest javascript npm npm-install npm-package random random-array-element random-generation random-without-repetition randomizer select-random yarn yarn-package yarn-packages
Last synced: 5 months ago
JSON representation
Selects a random array element without repetition.
- Host: GitHub
- URL: https://github.com/smakss/random-array-element
- Owner: SMAKSS
- License: mit
- Created: 2020-05-04T19:28:58.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-16T23:10:36.000Z (over 1 year ago)
- Last Synced: 2025-01-31T07:11:48.779Z (5 months ago)
- Topics: array, arrays, dom, hacktoberfest, javascript, npm, npm-install, npm-package, random, random-array-element, random-generation, random-without-repetition, randomizer, select-random, yarn, yarn-package, yarn-packages
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@smakss/random-array-element
- Size: 263 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: Readme.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Random Array Element without Repetition
   
Selecting a random element from an array is simple with `Math.random()`. However, if you need to ensure that each element is only selected once until all elements have been chosen, `@smakss/random-array-element` is the ideal solution. Utilizing closures, this package allows you to initialize a function once and then repeatedly obtain unique, randomly-selected elements from your array, without repeats until the array is exhausted.
## Demo
You can check the [working demo](https://runkit.com/smakss/random-array-element) on RunKit.
or
[](https://codesandbox.io/s/smakss-random-array-element-7yizos?fontsize=14&hidenavigation=1&theme=dark)
## Installation
Install the package using npm or Yarn:
```bash
npm i @smakss/random-array-element
# or
yarn add @smakss/random-array-element
```## Usage
To include it with CommonJS module you can do this:
```js
const randomArrayElement = require('@smakss/random-array-element');
```For ECMAScript modules:
```js
import randomArrayElement from '@smakss/random-array-element';
```Example usage:
```js
// Initialize once for an array
const getRandomElement = randomArrayElement(['apple', 'banana', 'cherry']);console.log(getRandomElement()); // Result: 'banana' (example output)
console.log(getRandomElement()); // Result: 'apple' (example output)
// ...after all items have been returned, it resets.
console.log(getRandomElement()); // Result: 'cherry' (example output)
// ...continues to return a new random item.
```If an empty or non-array input is passed, the function will return -1, indicating no selection can be made:
```js
const getRandomElement = randomArrayElement([]);
console.log(getRandomElement()); // Result: -1
```## Contributing
Contributions are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines and details.
## Code of Conduct
To ensure a welcoming and safe community, our [Code of Conduct](./CODE_OF_CONDUCT.md) outlines expected behaviors for all participants.