Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 4 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 (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-16T23:10:36.000Z (12 months ago)
- Last Synced: 2024-10-10T20:41:10.183Z (4 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
![npm](https://img.shields.io/npm/v/@smakss/random-array-element) ![NPM](https://img.shields.io/npm/l/@smakss/random-array-element) ![npm](https://img.shields.io/npm/dt/@smakss/random-array-element) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/random-array-element)
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
[![View @smakss/random-array-element](https://codesandbox.io/static/img/play-codesandbox.svg)](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.