{"id":14965883,"url":"https://github.com/smakss/random-array-element","last_synced_at":"2025-10-25T13:30:49.320Z","repository":{"id":40409569,"uuid":"261276719","full_name":"SMAKSS/random-array-element","owner":"SMAKSS","description":"Selects a random array element without repetition.","archived":false,"fork":false,"pushed_at":"2024-02-16T23:10:36.000Z","size":269,"stargazers_count":4,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-31T07:11:48.779Z","etag":null,"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"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@smakss/random-array-element","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SMAKSS.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-04T19:28:58.000Z","updated_at":"2023-11-04T16:15:00.000Z","dependencies_parsed_at":"2024-09-14T01:21:43.316Z","dependency_job_id":"bc142cac-9614-4200-869a-6f1a113b4a67","html_url":"https://github.com/SMAKSS/random-array-element","commit_stats":{"total_commits":70,"total_committers":5,"mean_commits":14.0,"dds":0.5714285714285714,"last_synced_commit":"63da5ee8a15cbf0e8f785af89d8e5ebe57555aef"},"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAKSS%2Frandom-array-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAKSS%2Frandom-array-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAKSS%2Frandom-array-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAKSS%2Frandom-array-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SMAKSS","download_url":"https://codeload.github.com/SMAKSS/random-array-element/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238147555,"owners_count":19424283,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["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"],"created_at":"2024-09-24T13:35:30.853Z","updated_at":"2025-10-25T13:30:48.979Z","avatar_url":"https://github.com/SMAKSS.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Random Array Element without Repetition\n\n![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)\n\nSelecting 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.\n\n## Demo\n\nYou can check the [working demo](https://runkit.com/smakss/random-array-element) on RunKit.\n\nor\n\n[![View @smakss/random-array-element](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/smakss-random-array-element-7yizos?fontsize=14\u0026hidenavigation=1\u0026theme=dark)\n\n## Installation\n\nInstall the package using npm or Yarn:\n\n```bash\nnpm i @smakss/random-array-element\n# or\nyarn add @smakss/random-array-element\n```\n\n## Usage\n\nTo include it with CommonJS module you can do this:\n\n```js\nconst randomArrayElement = require('@smakss/random-array-element');\n```\n\nFor ECMAScript modules:\n\n```js\nimport randomArrayElement from '@smakss/random-array-element';\n```\n\nExample usage:\n\n```js\n// Initialize once for an array\nconst getRandomElement = randomArrayElement(['apple', 'banana', 'cherry']);\n\nconsole.log(getRandomElement()); // Result: 'banana' (example output)\nconsole.log(getRandomElement()); // Result: 'apple' (example output)\n// ...after all items have been returned, it resets.\nconsole.log(getRandomElement()); // Result: 'cherry' (example output)\n// ...continues to return a new random item.\n```\n\nIf an empty or non-array input is passed, the function will return -1, indicating no selection can be made:\n\n```js\nconst getRandomElement = randomArrayElement([]);\nconsole.log(getRandomElement()); // Result: -1\n```\n\n## Contributing\n\nContributions are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines and details.\n\n## Code of Conduct\n\nTo ensure a welcoming and safe community, our [Code of Conduct](./CODE_OF_CONDUCT.md) outlines expected behaviors for all participants.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmakss%2Frandom-array-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmakss%2Frandom-array-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmakss%2Frandom-array-element/lists"}