Skip to content

Commit

Permalink
feat: support storing options on pre and post hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Sep 24, 2018
1 parent af653a3 commit 59220b9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
26 changes: 18 additions & 8 deletions index.js
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 || []);
}
};

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions test/post.test.js
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions test/pre.test.js
Expand Up @@ -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 = {};

Expand Down

0 comments on commit 59220b9

Please sign in to comment.