Skip to content

Commit

Permalink
feat(options): add options object
Browse files Browse the repository at this point in the history
allows passing custom context and custom arguments to given fn

Fixes #1
  • Loading branch information
tunnckoCore committed Oct 30, 2016
1 parent e52f201 commit 3e35605
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
23 changes: 16 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,38 @@
* @api public
*/

module.exports = function tryCatchCallback (fn, cb, passCallback) {
module.exports = function tryCatchCallback (fn, cb, opts) {
if (typeof fn !== 'function') {
throw new TypeError('try-catch-callback: expect `fn` to be a function')
}
if (typeof cb !== 'function') {
return function thunk (done) {
tryCatch(fn, done, passCallback)
tryCatch.call(this, fn, done, opts)
}
}
tryCatch(fn, cb, passCallback)
tryCatch.call(this, fn, cb, opts)
}

function tryCatch (fn, cb, passCallback) {
function tryCatch (fn, cb, opts) {
if (typeof cb !== 'function') {
throw new TypeError('try-catch-callback: expect `cb` to be a function')
}
var options = opts && typeof opts === 'object' ? opts : {}
var ctx = options.context || this
var args = arrayify(options.args)
var ret = null

try {
ret = passCallback === true ? fn(cb) : fn()
ret = fn.apply(ctx, options.passCallback ? args.concat(cb) : args)
} catch (err) {
if (!cb.called) return cb(err)
return cb(err)
}

if (!cb.called) return cb(null, ret)
cb(null, ret)
}

function arrayify (val) {
if (!val) return []
if (Array.isArray(val)) return val
return [val]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test": "npm run coverage",
"posttest": "npm run lint:coverage",
"coverage": "nyc node test.js",
"lint:coverage": "nyc check-coverage --lines 100 --statements 100 --functions 100",
"lint:coverage": "nyc check-coverage --lines 100 --branches 100 --statements 100 --functions 100",
"report-coverage": "nyc report --reporter=text-lcov | coveralls",
"prerelease": "npm test",
"release": "standard-version --sign --no-verify",
Expand Down
29 changes: 28 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,38 @@ test('should return thunk if no `cb` passed', function (done) {
test('should pass the `cb` to `fn` if 3rd arg is strictly `true`', function (done) {
tryCatch(function (cb) {
test.strictEqual(typeof cb, 'function')
}, done, true)
}, done, { passCallback: true })
})

test('should `fn` not have arguments if 3rd arg is not `true`', function (done) {
tryCatch(function () {
test.strictEqual(arguments.length, 0)
}, done)
})

test('should be able to pass custom arguments to `fn` through options', function (done) {
tryCatch(function (foo, bar, qux) {
test.strictEqual(arguments.length, 3)
test.strictEqual(foo, 1)
test.strictEqual(bar, true)
test.strictEqual(qux, 'foo')
}, done, { args: [1, true, 'foo'] })
done()
})

test('should be able to pass custom `fn` context through options', function (done) {
tryCatch(function (aa) {
test.strictEqual(aa, 'bb')
test.strictEqual(this.foo, 'bar')
}, done, {
context: { foo: 'bar' },
args: 'bb'
})
})

test('should pass custom context using `tryCatch.call`', function (done) {
tryCatch.call({ abc: 'def', qux: true }, function () {
test.strictEqual(this.abc, 'def')
test.strictEqual(this.qux, true)
}, done)
})

0 comments on commit 3e35605

Please sign in to comment.