├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v2 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare const randomItem: { 2 | /** 3 | Get a random item from an array. 4 | 5 | @example 6 | ``` 7 | import randomItem from 'random-item'; 8 | 9 | randomItem(['🐴', '🦄', '🌈']); 10 | //=> '🦄' 11 | ``` 12 | */ 13 | (input: readonly T[]): T; 14 | 15 | /** 16 | Get multiple random items from an array. 17 | 18 | This is the equivalent of calling `randomItem()` multiple times so the returned array may contain duplicates. 19 | 20 | @example 21 | ``` 22 | import randomItem from 'random-item'; 23 | 24 | randomItem.multiple(['🐴', '🦄', '🌈'], 2); 25 | //=> ['🌈', '🦄'] 26 | ``` 27 | */ 28 | multiple(input: readonly T[], count: number): T[]; 29 | }; 30 | 31 | export default randomItem; 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function randomItem(array) { 2 | if (!Array.isArray(array)) { 3 | throw new TypeError('Expected an array'); 4 | } 5 | 6 | return array[Math.floor(Math.random() * array.length)]; 7 | } 8 | 9 | randomItem.multiple = (array, count) => { 10 | if (!(Number.isInteger(count) && count >= 0)) { 11 | throw new TypeError('Expected a non-negative integer'); 12 | } 13 | 14 | return Array.from({length: count}, () => randomItem(array)); 15 | }; 16 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import randomItem from './index.js'; 3 | 4 | expectType(randomItem(['🐴', '🦄', '🌈'])); 5 | expectType(randomItem(['🐴', '🦄', '🌈', 1])); 6 | 7 | expectType(randomItem.multiple(['🐴', '🦄', '🌈'], 2)); 8 | expectType>(randomItem.multiple(['🐴', '🦄', '🌈', 1], 2)); 9 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random-item", 3 | "version": "4.0.1", 4 | "description": "Get a random item from an array", 5 | "license": "MIT", 6 | "repository": "sindresorhus/random-item", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "random", 27 | "randomize", 28 | "math", 29 | "array", 30 | "item", 31 | "element", 32 | "pick", 33 | "select" 34 | ], 35 | "devDependencies": { 36 | "ava": "^3.15.0", 37 | "stable-fn": "^3.0.0", 38 | "tsd": "^0.14.0", 39 | "xo": "^0.38.2" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # random-item 2 | 3 | > Get a random item from an array 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install random-item 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import randomItem from 'random-item'; 15 | 16 | randomItem(['🐴', '🦄', '🌈']); 17 | //=> '🦄' 18 | 19 | randomItem.multiple(['🐴', '🦄', '🌈'], 2); 20 | //=> ['🌈', '🦄'] 21 | ``` 22 | 23 | ## Related 24 | 25 | - [random-int](https://github.com/sindresorhus/random-int) - Generate a random integer 26 | - [random-float](https://github.com/sindresorhus/random-float) - Generate a random float 27 | - [random-obj-key](https://github.com/sindresorhus/random-obj-key) - Get a random key from an object 28 | - [random-obj-prop](https://github.com/sindresorhus/random-obj-prop) - Get a random property from an object 29 | - [unique-random](https://github.com/sindresorhus/unique-random) - Generate random numbers that are consecutively unique 30 | - [unique-random-array](https://github.com/sindresorhus/unique-random-array) - Get consecutively unique elements from an array 31 | - [crypto-random-string](https://github.com/sindresorhus/crypto-random-string) - Generate a cryptographically strong random string 32 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import stableFunction from 'stable-fn'; 3 | import randomItem from './index.js'; 4 | 5 | const fixture = ['a', 'b', 'c', 'd', 'e']; 6 | 7 | test('main', t => { 8 | t.false(stableFunction(() => randomItem(fixture))); 9 | 10 | for (let index = 0; index < 1000; index++) { 11 | t.is(typeof randomItem(fixture), 'string'); 12 | } 13 | }); 14 | 15 | test('main - argument validation', t => { 16 | t.throws(() => { 17 | randomItem('a'); 18 | }, { 19 | instanceOf: TypeError, 20 | message: 'Expected an array' 21 | }); 22 | }); 23 | 24 | test('.multiple()', t => { 25 | const result = randomItem.multiple(fixture, 4); 26 | t.is(result.length, 4); 27 | t.true(result.every(value => fixture.includes(value))); 28 | }); 29 | 30 | test('.multiple() - argument validation', t => { 31 | t.throws(() => { 32 | randomItem.multiple(fixture, -1); 33 | randomItem.multiple(fixture, 'a'); 34 | }, { 35 | instanceOf: TypeError, 36 | message: 'Expected a non-negative integer' 37 | }); 38 | }); 39 | --------------------------------------------------------------------------------