try-expr
v0.1.0
Published
Try/catch as an expression with filtered catch clauses.
Downloads
11
Readme
try-expr
Try/catch as an expression with filtered catch clauses.
Install
Installation of the npm package:
> npm install --save try-expr
Usage
import tryExpr from 'try-expr'
const data = tryExpr(
JSON.parse
).catch(SyntaxError, error => {
// syntax error, returns a default value
return null
})(jsonValue)
Ideal with promises:
const safeParseJson = tryExpr(
JSON.parse
).catch(SyntaxError, error => {
return null
})
readFile('./data.json').then(safeParseJson).then(data => {
// do something with the data
})
Works with async functions:
const safeReadFile = tryExpr(
readFile
).catch({ code: 'ENOENT'}, error => {
return ''
})
safeReadFile('./data.json').then(data => {
// do something with the data
})
If no function is passed to `tryExpr()`, the first param of the chain will be considered as the error to match `catch` clauses with, which makes it handy with `Promise#catch()`:
```js
readFile('./data.json').catch(
tryExpr().catch({ code: 'ENOENT' }, error => {
// file does not exist, return an empty string for instance
return ''
})
)
Development
# Install dependencies
> npm install
# Run the tests
> npm test
# Continuously compile
> npm run dev
# Continuously run the tests
> npm run dev-test
# Build for production (automatically called by npm install)
> npm run build
Contributions
Contributions are very welcomed, either on the documentation or on the code.
You may:
- report any issue you've encountered;
- fork and create a pull request.
License
ISC © Julien Fontanet