Skip to content

Commit f53f26a

Browse files
committed
featur(try-to-catch add
0 parents  commit f53f26a

File tree

10 files changed

+229
-0
lines changed

10 files changed

+229
-0
lines changed

.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"env"
4+
]
5+
}

.eslintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true,
5+
"browser": true
6+
},
7+
"parserOptions": {
8+
"ecmaVersion": 2017,
9+
"sourceType": "module"
10+
},
11+
"rules": {
12+
"indent": ["error", 4],
13+
"semi": "error",
14+
"no-use-before-define": ["error", "nofunc"]
15+
},
16+
"extends": [
17+
"eslint:recommended"
18+
]
19+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.nyc_output
3+
legacy
4+

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.*
2+
test
3+

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
node_js:
3+
- 6
4+
- 8
5+
- 9
6+
7+
script:
8+
- npm run lint
9+
- npm run coverage && npm run report
10+
11+
notifications:
12+
email:
13+
on_success: never
14+
on_failure: change
15+
16+
sudo: false
17+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) coderaiser
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Try to Catch [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]
2+
3+
[NPMIMGURL]: https://img.shields.io/npm/v/try-to-catch.svg?style=flat
4+
[BuildStatusIMGURL]: https://img.shields.io/travis/coderaiser/try-to-catch/master.svg?style=flat
5+
[DependencyStatusIMGURL]: https://img.shields.io/gemnasium/coderaiser/try-to-catch.svg?style=flat
6+
[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat
7+
[NPMURL]: https://npmjs.org/package/try-to-catch "npm"
8+
[BuildStatusURL]: https://travis-ci.org/coderaiser/try-to-catch "Build Status"
9+
[DependencyStatusURL]: https://gemnasium.com/coderaiser/try-to-catch "Dependency Status"
10+
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"
11+
12+
[CoverageURL]: https://coveralls.io/github/coderaiser/try-to-catch?branch=master
13+
[CoverageIMGURL]: https://coveralls.io/repos/coderaiser/try-to-catch/badge.svg?branch=master&service=github
14+
15+
Functional `try-catch` wrapper for `promises`.
16+
17+
## Install
18+
19+
```
20+
npm i try-to-catch
21+
```
22+
23+
## API
24+
25+
### tryToCatch(fn, [arg1, arg2, ..., argN])
26+
27+
Wrap function to `await try-catch block` resolve `[error, result]`;
28+
29+
### Example
30+
31+
```js
32+
const fs = require('fs');
33+
const tryCatch = require('.');
34+
const {promisify} = require('util');
35+
const readFile = promisify(fs.readFile);
36+
const readDir = promisify(fs.readdir);
37+
38+
read(process.argv[2])
39+
.then(console.log)
40+
.catch(console.error);
41+
42+
async function read(path) {
43+
const [error, data] = await tryCatch(readFile, path, 'utf8');
44+
45+
if (!error)
46+
return data;
47+
48+
if (error.code !== 'EISDIR')
49+
return error;
50+
51+
return await readDir(path);
52+
}
53+
```
54+
55+
## Environments
56+
57+
In old `node.js` environments that not fully supports `es2015`, `try-to-catch` can be used with:
58+
59+
```js
60+
var tryToCatch = require('try-to-catch/legacy');
61+
```
62+
63+
## License
64+
65+
MIT
66+

lib/try-to-catch.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const success = (a) => [null, a];
4+
const fail = (a) => [a];
5+
6+
module.exports = (fn, ...args) => {
7+
check(fn);
8+
9+
return fn(...args)
10+
.then(success)
11+
.catch(fail);
12+
};
13+
14+
function check(fn) {
15+
if (typeof fn !== 'function')
16+
throw Error('fn should be a function!');
17+
}
18+

package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "try-to-catch",
3+
"version": "1.0.0",
4+
"author": "coderaiser <[email protected]> (https://github.com/coderaiser)",
5+
"description": "function try-catch wrapper for promises",
6+
"homepage": "http://github.com/coderaiser/try-to-catch",
7+
"main": "lib/try-to-catch.js",
8+
"repository": {
9+
"type": "git",
10+
"url": "git://github.com/coderaiser/try-to-catch.git"
11+
},
12+
"scripts": {
13+
"test": "tape 'test/*.js'",
14+
"watch:test": "nodemon -w lib -w test -x \"npm test\"",
15+
"lint": "eslint lib test",
16+
"coverage": "nyc npm test",
17+
"report": "nyc report --reporter=text-lcov | coveralls",
18+
"build": "babel lib -d legacy",
19+
"wisdom": "redrun build legacy",
20+
"legacy": "echo \"require('./try-to-catch')\"> legacy/index.js"
21+
},
22+
"dependencies": {
23+
},
24+
"keywords": [
25+
"try",
26+
"catch",
27+
"function",
28+
"promise",
29+
"async",
30+
"await",
31+
"try-catch"
32+
],
33+
"devDependencies": {
34+
"babel-cli": "^6.26.0",
35+
"babel-preset-env": "^1.6.1",
36+
"coveralls": "^3.0.0",
37+
"eslint": "^4.17.0",
38+
"nodemon": "^1.14.12",
39+
"nyc": "^11.4.1",
40+
"redrun": "^5.10.2",
41+
"tape": "^4.8.0"
42+
},
43+
"license": "MIT"
44+
}

test/try-to-catch.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const test = require('tape');
4+
const tryToCatch = require('..');
5+
6+
test('try-to-catch: no args', (t) => {
7+
const fn = () => tryToCatch();
8+
9+
t.throws(fn, /fn should be a function!/, 'should throw');
10+
t.end();
11+
});
12+
13+
test('try-to-catch: resolves', async (t) => {
14+
const message = 'hello';
15+
const fn = (a) => Promise.resolve(a);
16+
17+
const [, result] = await tryToCatch(fn, message);
18+
19+
t.equal(result, message, 'should equal');
20+
t.end();
21+
});
22+
23+
test('try-to-catch: rejects', async (t) => {
24+
const message = 'hello';
25+
const fn = (a) => Promise.reject(a);
26+
27+
const [error] = await tryToCatch(fn, message);
28+
29+
t.equal(error, message, 'should equal');
30+
t.end();
31+
});
32+

0 commit comments

Comments
 (0)