Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Jan 26, 2019
0 parents commit 70195eb
Show file tree
Hide file tree
Showing 12 changed files with 6,939 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
node_js:
- "node"
branches:
only:
- master
env:
- TEST_SUITE=lint
- TEST_SUITE=test
- TEST_SUITE=build
script:
- npm run $TEST_SUITE
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2019 CEZAR AUGUSTO <boss@cezaraugusto.net>

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[![Written in TypeScript](https://img.shields.io/badge/TypeScript-100%25-blue.svg)](https://github.com/cezaraugusto/random-from-array) [![npm](https://img.shields.io/npm/v/random-from-array.svg)]() [![Build Status](https://travis-ci.org/cezaraugusto/random-from-array.svg?branch=master)](https://travis-ci.org/cezaraugusto/random-from-array)

> TypeScript module with type definition support by default. Also works with other JavaScript projects.
# random-from-array

Given an array, return a random value from it.

## Useful when

An array of elements must output a random value.

## Installation

```sh
$ npm i random-from-array
```

## Usage

[Try it on CodeSandbox](https://codesandbox.io/s/20pp8xjlor)

```js
import randomFromArray from 'random-from-array'

randomFromArray(['bossa nova', 'jazz', 'rock']) // one of: 'bossa nova' or 'jazz' or 'rock'

// also works with an array of objects
const arrayOfObj = [
{ value: 'bossa nova' },
{ value: 'jazz' },
{ value: 'rock' }
]

randomFromArray(arrayOfObj) // one of: { value: 'bossa nova' } or { value: 'jazz' } or { value: 'rock' }
```

## License

random-from-array © [Cezar Augusto](https://twitter.com/cezaraugusto). Released under the MIT License.
4 changes: 4 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"preset": "ts-jest",
"testMatch": ["<rootDir>/tests/index.test.ts"]
}
5 changes: 5 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Given an array, return a random value from it
* @returns {any} - a single random value from the input's array
*/
export default (arr: Array<any>): any => arr[Math.floor((Math.random() * arr.length))]
Loading

0 comments on commit 70195eb

Please sign in to comment.