random-item
Advanced tools
+27
-10
@@ -1,14 +0,31 @@ | ||
| /** | ||
| Get a random item from an array. | ||
| declare const randomItem: { | ||
| /** | ||
| Get a random item from an array. | ||
| @example | ||
| ``` | ||
| import randomItem = require('random-item'); | ||
| @example | ||
| ``` | ||
| import randomItem = require('random-item'); | ||
| randomItem(['🐴', '🦄', '🌈']); | ||
| //=> '🦄' | ||
| ``` | ||
| */ | ||
| declare function randomItem<T>(input: readonly T[]): T; | ||
| randomItem(['🐴', '🦄', '🌈']); | ||
| //=> '🦄' | ||
| ``` | ||
| */ | ||
| <T>(input: readonly T[]): T; | ||
| /** | ||
| Get multiple random items from an array. | ||
| This is the equivalent of calling `randomItem()` multiple times so the returned array may contain duplicates. | ||
| @example | ||
| ``` | ||
| import randomItem = require('random-item'); | ||
| randomItem.multiple(['🐴', '🦄', '🌈'], 2); | ||
| //=> ['🌈', '🦄'] | ||
| ``` | ||
| */ | ||
| multiple<T>(input: readonly T[], count: number): T[]; | ||
| } | ||
| export = randomItem; |
+8
-0
@@ -10,1 +10,9 @@ 'use strict'; | ||
| }; | ||
| module.exports.multiple = (array, count) => { | ||
| if (!Number.isInteger(count) && count >= 0) { | ||
| throw new TypeError('Expected a non-negative integer'); | ||
| } | ||
| return [...new Array(count)].map(() => module.exports(array)); | ||
| }; |
+3
-2
| { | ||
| "name": "random-item", | ||
| "version": "3.0.0", | ||
| "version": "3.1.0", | ||
| "description": "Get a random item from an array", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/random-item", | ||
| "funding": "https://github.com/sponsors/sindresorhus", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| "url": "https://sindresorhus.com" | ||
| }, | ||
@@ -12,0 +13,0 @@ "engines": { |
+4
-9
@@ -1,6 +0,5 @@ | ||
| # random-item [](https://travis-ci.org/sindresorhus/random-item) | ||
| # random-item [](https://travis-ci.com/github/sindresorhus/random-item) | ||
| > Get a random item from an array | ||
| ## Install | ||
@@ -12,3 +11,2 @@ | ||
| ## Usage | ||
@@ -21,5 +19,7 @@ | ||
| //=> '🦄' | ||
| randomItem.multiple(['🐴', '🦄', '🌈'], 2); | ||
| //=> ['🌈', '🦄'] | ||
| ``` | ||
| ## Related | ||
@@ -34,6 +34,1 @@ | ||
| - [crypto-random-string](https://github.com/sindresorhus/crypto-random-string) - Generate a cryptographically strong random string | ||
| ## License | ||
| MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
4003
20.54%37
105.56%32
-13.51%