Skip to content

Commit

Permalink
feat(options): allow passing options object
Browse files Browse the repository at this point in the history
directly passed to try-catch-core, add more tests for guarantees
  • Loading branch information
tunnckoCore committed Nov 1, 2016
1 parent 2420002 commit 765a4fd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ var utils = require('./utils')
* @api public
*/

module.exports = function alwaysDone (fn, done) {
module.exports = function alwaysDone (fn, opts, done) {
if (typeof opts === 'function') {
done = opts
opts = null
}
if (typeof done === 'function') {
utils.tryCatchCore(fn, function cb (err, val) {
utils.tryCatchCore.call(this, fn, opts, function cb (err, val) {
// handle errors
if (err) {
done(err)
Expand All @@ -64,9 +68,7 @@ module.exports = function alwaysDone (fn, done) {

// handle observables
if (val && typeof val.subscribe === 'function') {
utils.tryCatchCore(function (cb) {
utils.subscribe(val, cb)
}, done)
utils.subscribe(val, done)
return
}

Expand All @@ -83,7 +85,7 @@ module.exports = function alwaysDone (fn, done) {
}

return function thunk (cb) {
return alwaysDone(fn, cb)
return alwaysDone.call(this, fn, opts, cb)
}
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"lazy-cache": "^2.0.1",
"on-stream-end": "^1.0.0",
"stream-exhaust": "^1.0.1",
"try-catch-core": "^1.0.3"
"try-catch-core": "^2.0.1"
},
"devDependencies": {
"assertit": "^0.1.0",
Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ require('./test/promises')
require('./test/streams')

require('./test/sync')

require('./test/options')
73 changes: 73 additions & 0 deletions test/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*!
* always-done <https://github.com/hybridables/always-done>
*
* Copyright (c) 2015-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/

/* jshint asi:true */

'use strict'

var fs = require('fs')
var test = require('assertit')
var path = require('path')
var alwaysDone = require('../index')

test('should allow passing custom context through options', function (done) {
alwaysDone(function () {
test.strictEqual(this.num, 123)
test.strictEqual(this.bool, true)
test.strictEqual(arguments.length, 0)
}, {
context: { num: 123, bool: true }
}, done)
})

test('should allow passing custom context through `.call`', function (done) {
alwaysDone.call({
foo: 'bar'
}, function () {
test.strictEqual(this.foo, 'bar')
test.strictEqual(arguments.length, 0)
}, done)
})

test('should allow passing custom arguments through options', function (done) {
var filepath = path.join(__dirname, '../package.json')
alwaysDone(fs.readFile, {
args: [filepath, 'utf8']
}, function (err, res) {
test.strictEqual(err, null)
test.strictEqual(typeof res, 'string')

var json = JSON.parse(res)
test.strictEqual(json.license, 'MIT')
done()
})
})

test('should pass callback arg, when `fn` is async or when sync but passCallback: true', function (done) {
alwaysDone(function (foo, bar) {
test.strictEqual(arguments.length, 2)
test.strictEqual(foo, 'bar')
test.strictEqual(bar, 'qux')
}, {
args: ['bar', 'qux']
}, function (err) {
test.strictEqual(err, null)
alwaysDone(function (aaa, bbb) {
test.strictEqual(arguments.length, 3)
test.strictEqual(aaa, 111)
test.strictEqual(bbb, 222)

// get the callback (3rd argument)
// intentionally getting it in this way
var cb = arguments[2]
cb()
}, {
passCallback: true,
args: [111, 222]
}, done)
})
})

0 comments on commit 765a4fd

Please sign in to comment.