Installations
npm install @smakss/random-array-element
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>=18.0.0
Node Version
18.19.0
NPM Version
10.2.3
Releases
Upgrade dependencies
Published on 31 Dec 2023
Stable release of 2.0.2
Published on 28 Nov 2023
Upgrade dependencies
Published on 28 Nov 2023
Update package.json and dependencies
Published on 16 Nov 2023
Migrate to typescript and enhance DX tools
Published on 04 Nov 2023
Migrate to typescript
Published on 04 Nov 2023
Contributors
Unable to fetch Contributors
Languages
TypeScript (52.65%)
JavaScript (41.35%)
Shell (6.01%)
Developer
Download Statistics
Total Downloads
10,802
Last Day
23
Last Week
77
Last Month
226
Last Year
1,157
GitHub Statistics
4 Stars
72 Commits
1 Watching
7 Branches
1 Contributors
Bundle Size
399.00 B
Minified
265.00 B
Minified + Gzipped
Package Meta Information
Latest Version
2.0.3
Package Id
@smakss/random-array-element@2.0.3
Unpacked Size
14.21 kB
Size
3.64 kB
File Count
9
NPM Version
10.2.3
Node Version
18.19.0
Publised On
31 Dec 2023
Total Downloads
Cumulative downloads
Total Downloads
10,802
Last day
-67.1%
23
Compared to previous day
Last week
-35.3%
77
Compared to previous week
Last month
276.7%
226
Compared to previous month
Last year
-56.9%
1,157
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
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 on RunKit.
or
Installation
Install the package using npm or Yarn:
1npm i @smakss/random-array-element 2# or 3yarn add @smakss/random-array-element
Usage
To include it with CommonJS module you can do this:
1const randomArrayElement = require('@smakss/random-array-element');
For ECMAScript modules:
1import randomArrayElement from '@smakss/random-array-element';
Example usage:
1// Initialize once for an array
2const getRandomElement = randomArrayElement(['apple', 'banana', 'cherry']);
3
4console.log(getRandomElement()); // Result: 'banana' (example output)
5console.log(getRandomElement()); // Result: 'apple' (example output)
6// ...after all items have been returned, it resets.
7console.log(getRandomElement()); // Result: 'cherry' (example output)
8// ...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:
1const getRandomElement = randomArrayElement([]); 2console.log(getRandomElement()); // Result: -1
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines and details.
Code of Conduct
To ensure a welcoming and safe community, our Code of Conduct outlines expected behaviors for all participants.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
4 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dangerous workflow patterns detected
Details
- Warn: script injection with untrusted input ' github.head_ref ': .github/workflows/ci.yml:17
Reason
Found 0/18 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Warn: no topLevel permission defined: .github/workflows/npm-publish.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:56: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:54: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:55: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:58: update your workflow using https://app.stepsecurity.io/secureworkflow/SMAKSS/random-array-element/npm-publish.yml/master?enable=pin
- Info: 0 out of 13 GitHub-owned GitHubAction dependencies pinned
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 13 are checked with a SAST tool
Score
1.8
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More