From 3e35605b7f7ea92b25e2c8c6f5c6bb811d9aa58c Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Sun, 30 Oct 2016 04:54:38 +0200 Subject: [PATCH] feat(options): add options object allows passing custom context and custom arguments to given fn Fixes #1 --- index.js | 23 ++++++++++++++++------- package.json | 2 +- test.js | 29 ++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index f670257..0056918 100644 --- a/index.js +++ b/index.js @@ -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] } diff --git a/package.json b/package.json index e73e891..17b1a55 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test.js b/test.js index fa4c280..2ad668c 100644 --- a/test.js +++ b/test.js @@ -66,7 +66,7 @@ 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) { @@ -74,3 +74,30 @@ test('should `fn` not have arguments if 3rd arg is not `true`', function (done) 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) +})