random-item
Advanced tools
+2
-5
| /** | ||
| Get a random item from an [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol). | ||
| Get a random item from an array. | ||
@@ -10,9 +10,6 @@ @example | ||
| //=> '🦄' | ||
| randomItem(new Set(['🐴', '🦄', '🌈']); | ||
| //=> '🌈' | ||
| ``` | ||
| */ | ||
| declare function randomItem<T>(input: Iterable<T>): T; | ||
| declare function randomItem<T>(input: readonly T[]): T; | ||
| export = randomItem; |
+3
-5
| 'use strict'; | ||
| module.exports = iterable => { | ||
| if (!Array.isArray(iterable) && typeof iterable[Symbol.iterator] !== 'function') { | ||
| throw new TypeError('Expected an array or an iterable'); | ||
| module.exports = array => { | ||
| if (!Array.isArray(array)) { | ||
| throw new TypeError('Expected an array'); | ||
| } | ||
| const array = Array.isArray(iterable) ? iterable : [...iterable]; | ||
| return array[Math.floor(Math.random() * array.length)]; | ||
| }; |
+2
-3
| { | ||
| "name": "random-item", | ||
| "version": "2.0.0", | ||
| "description": "Get a random item from an Iterable", | ||
| "version": "3.0.0", | ||
| "description": "Get a random item from an array", | ||
| "license": "MIT", | ||
@@ -27,3 +27,2 @@ "repository": "sindresorhus/random-item", | ||
| "array", | ||
| "iterable", | ||
| "item", | ||
@@ -30,0 +29,0 @@ "element", |
+1
-4
| # random-item [](https://travis-ci.org/sindresorhus/random-item) | ||
| > Get a random item from an [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) | ||
| > Get a random item from an array | ||
@@ -20,5 +20,2 @@ | ||
| //=> '🦄' | ||
| randomItem(new Set(['🐴', '🦄', '🌈']); | ||
| //=> '🌈' | ||
| ``` | ||
@@ -25,0 +22,0 @@ |
3321
-13.13%18
-14.29%37
-7.5%