random-item
Advanced tools
+17
| /** | ||
| Get a random item from an [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol). | ||
| @example | ||
| ``` | ||
| import randomItem = require('random-item'); | ||
| randomItem(['🐴', '🦄', '🌈']); | ||
| //=> '🦄' | ||
| randomItem(new Set(['🐴', '🦄', '🌈']); | ||
| //=> '🌈' | ||
| ``` | ||
| */ | ||
| declare function randomItem<T>(input: Iterable<T>): T; | ||
| export = randomItem; |
+7
-4
| 'use strict'; | ||
| module.exports = function (arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new TypeError('Expected an array'); | ||
| module.exports = iterable => { | ||
| if (!Array.isArray(iterable) && typeof iterable[Symbol.iterator] !== 'function') { | ||
| throw new TypeError('Expected an array or an iterable'); | ||
| } | ||
| return arr[Math.floor(Math.random() * arr.length)]; | ||
| const array = Array.isArray(iterable) ? iterable : [...iterable]; | ||
| return array[Math.floor(Math.random() * array.length)]; | ||
| }; |
+37
-36
| { | ||
| "name": "random-item", | ||
| "version": "1.0.0", | ||
| "description": "Get a random item from an array", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/random-item", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=0.10.0" | ||
| }, | ||
| "scripts": { | ||
| "test": "node test.js" | ||
| }, | ||
| "files": [ | ||
| "index.js" | ||
| ], | ||
| "keywords": [ | ||
| "rand", | ||
| "random", | ||
| "randomize", | ||
| "math", | ||
| "array", | ||
| "arr", | ||
| "item", | ||
| "el", | ||
| "element", | ||
| "pick", | ||
| "select" | ||
| ], | ||
| "devDependencies": { | ||
| "ava": "0.0.4", | ||
| "stable-fn": "^1.0.0" | ||
| } | ||
| "name": "random-item", | ||
| "version": "2.0.0", | ||
| "description": "Get a random item from an Iterable", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/random-item", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=8" | ||
| }, | ||
| "scripts": { | ||
| "test": "xo && ava && tsd" | ||
| }, | ||
| "files": [ | ||
| "index.js", | ||
| "index.d.ts" | ||
| ], | ||
| "keywords": [ | ||
| "random", | ||
| "randomize", | ||
| "math", | ||
| "array", | ||
| "iterable", | ||
| "item", | ||
| "element", | ||
| "pick", | ||
| "select" | ||
| ], | ||
| "devDependencies": { | ||
| "ava": "^1.4.1", | ||
| "stable-fn": "^2.0.0", | ||
| "tsd": "^0.7.2", | ||
| "xo": "^0.24.0" | ||
| } | ||
| } |
+10
-6
| # random-item [](https://travis-ci.org/sindresorhus/random-item) | ||
| > Get a random item from an array | ||
| > Get a random item from an [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) | ||
@@ -9,3 +9,3 @@ | ||
| ``` | ||
| $ npm install --save random-item | ||
| $ npm install random-item | ||
| ``` | ||
@@ -17,6 +17,9 @@ | ||
| ```js | ||
| var randomItem = require('random-item'); | ||
| const randomItem = require('random-item'); | ||
| randomItem(['pony', 'unicorn', 'rainbow']); | ||
| //=> 'unicorn' | ||
| randomItem(['🐴', '🦄', '🌈']); | ||
| //=> '🦄' | ||
| randomItem(new Set(['🐴', '🦄', '🌈']); | ||
| //=> '🌈' | ||
| ``` | ||
@@ -33,2 +36,3 @@ | ||
| - [unique-random-array](https://github.com/sindresorhus/unique-random-array) - Get consecutively unique elements from an array | ||
| - [crypto-random-string](https://github.com/sindresorhus/crypto-random-string) - Generate a cryptographically strong random string | ||
@@ -38,2 +42,2 @@ | ||
| MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
| MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
3823
27.05%5
25%21
200%40
11.11%0
-100%4
100%