Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

random-item

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

random-item - npm Package Compare versions

Comparing version
1.0.0
to
2.0.0
+17
index.d.ts
/**
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)];
};
{
"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"
}
}
# random-item [![Build Status](https://travis-ci.org/sindresorhus/random-item.svg?branch=master)](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