diff --git a/index.js b/index.js index 3de3dfe..a8f3eff 100644 --- a/index.js +++ b/index.js @@ -137,7 +137,7 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) { } var next = function() { - var post = posts[currentPost]; + var post = posts[currentPost].fn; var numArgs = 0; var argLength = args.length; var newArgs = []; @@ -222,7 +222,7 @@ Kareem.prototype.execPostSync = function(name, context, args) { const numPosts = posts.length; for (let i = 0; i < numPosts; ++i) { - posts[i].apply(context, args || []); + posts[i].fn.apply(context, args || []); } }; @@ -341,7 +341,11 @@ Kareem.prototype.createWrapper = function(name, fn, context, options) { }; Kareem.prototype.pre = function(name, isAsync, fn, error, unshift) { - if (typeof arguments[1] !== 'boolean') { + let options = {}; + if (typeof isAsync === 'object' && isAsync != null) { + options = isAsync; + isAsync = options.isAsync; + } else if (typeof arguments[1] !== 'boolean') { error = fn; fn = isAsync; isAsync = false; @@ -356,21 +360,27 @@ Kareem.prototype.pre = function(name, isAsync, fn, error, unshift) { } if (unshift) { - pres.unshift({ fn: fn, isAsync: isAsync }); + pres.unshift(Object.assign({}, options, { fn: fn, isAsync: isAsync })); } else { - pres.push({ fn: fn, isAsync: isAsync }); + pres.push(Object.assign({}, options, { fn: fn, isAsync: isAsync })); } return this; }; -Kareem.prototype.post = function(name, fn, unshift) { +Kareem.prototype.post = function(name, options, fn, unshift) { const hooks = get(this._posts, name, []); + if (typeof options === 'function') { + unshift = !!fn; + fn = options; + options = {}; + } + if (unshift) { - hooks.unshift(fn); + hooks.unshift(Object.assign({}, options, { fn: fn })); } else { - hooks.push(fn); + hooks.push(Object.assign({}, options, { fn: fn })); } this._posts.set(name, hooks); return this; diff --git a/test/post.test.js b/test/post.test.js index 9316707..8cb2067 100644 --- a/test/post.test.js +++ b/test/post.test.js @@ -27,8 +27,17 @@ describe('execPost', function() { var f2 = function() {}; hooks.post('cook', f1); hooks.post('cook', f2, true); - assert.strictEqual(hooks._posts.get('cook')[0], f2); - assert.strictEqual(hooks._posts.get('cook')[1], f1); + assert.strictEqual(hooks._posts.get('cook')[0].fn, f2); + assert.strictEqual(hooks._posts.get('cook')[1].fn, f1); + }); + + it('arbitrary options', function() { + const f1 = function() {}; + const f2 = function() {}; + hooks.post('cook', { foo: 'bar' }, f1); + hooks.post('cook', { bar: 'baz' }, f2, true); + assert.equal(hooks._posts.get('cook')[1].foo, 'bar'); + assert.equal(hooks._posts.get('cook')[0].bar, 'baz'); }); it('multiple posts', function(done) { diff --git a/test/pre.test.js b/test/pre.test.js index 8aa8788..10240f4 100644 --- a/test/pre.test.js +++ b/test/pre.test.js @@ -65,6 +65,15 @@ describe('execPre', function() { assert.strictEqual(hooks._pres.get('cook')[1].fn, f1); }); + it('arbitrary options', function() { + const f1 = function() {}; + const f2 = function() {}; + hooks.pre('cook', { foo: 'bar' }, f1); + hooks.pre('cook', { bar: 'baz' }, f2, null, true); + assert.equal(hooks._pres.get('cook')[1].foo, 'bar'); + assert.equal(hooks._pres.get('cook')[0].bar, 'baz'); + }); + it('handles async errors', function(done) { var execed = {};