Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
electerious committed Jul 30, 2016
0 parents commit 66532a6
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
coverage/
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- '6'
- '5'
- '4'
after_success:
- 'npm run coveralls'
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Tobias Reich (http://electerious.com/)

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.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# nice-try

[![Travis Build Status](https://travis-ci.org/electerious/nice-try.svg?branch=master)](https://travis-ci.org/electerious/nice-try) [![Coverage Status](https://coveralls.io/repos/github/electerious/nice-try/badge.svg?branch=master)](https://coveralls.io/github/electerious/nice-try?branch=master) [![Dependencies](https://david-dm.org/electerious/nice-try.svg)](https://david-dm.org/electerious/nice-try#info=dependencies)

A function that tries to execute a function and discards any error that occurs.

## Install

```
npm install nice-try
```

## Usage

```js
const niceTry = require('nice-try')

niceTry(() => JSON.parse('true')) // true
niceTry(() => JSON.parse('truee')) // undefined
niceTry() // undefined
niceTry(true) // undefined
```

## API

### Parameters

- `fn` `{Function}` Function that might or might not throw an error.

### Returns

- `value` `{?*}` Return-value of the function when no error occurred.
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "nice-try",
"version": "1.0.0",
"authors": [
"Tobias Reich <tobias@electerious.com>"
],
"description": "Tries to execute a function and discards any error that occurs",
"main": "src/index.js",
"keywords": [
"try",
"catch",
"error"
],
"license": "MIT",
"homepage": "http://github.com/electerious/nice-try",
"repository": {
"type": "git",
"url": "https://github.com/electerious/nice-try.git"
},
"files": [
"src"
],
"scripts": {
"coveralls": "cat ./coverage/lcov.info | coveralls",
"test": "istanbul cover _mocha"
},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"istanbul": "^0.4.4",
"mocha": "^2.5.3"
}
}
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

/*
* Tries to execute a function and discards any error that occurs.
* @param {Function} fn - Function that might or might not throw an error.
* @returns {?*} Return-value of the function when no error occurred.
*/
module.exports = function(fn) {

try { return fn() }
catch (e) {}

}
32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const assert = require('chai').assert
const index = require('./../src/index')

describe('index()', function() {

it('should return undefined when called with a function that throws an error', function() {

assert.strictEqual(index(() => JSON.parse('truee')), undefined)

})

it('should return undefined when called without a function', function() {

assert.strictEqual(index(), undefined)

})

it('should return undefined when called with a non-function parameter', function() {

assert.strictEqual(index(true), undefined)

})

it('should return a value when called with a function that returns a value', function() {

assert.strictEqual(index(() => JSON.parse('true')), true)

})

})

0 comments on commit 66532a6

Please sign in to comment.