kareem 1.2.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +45 -12
- package/package.json +1 -1
- package/test/examples.test.js +18 -0
- package/test/pre.test.js +21 -0
- package/test/wrap.test.js +2 -2
package/index.js
CHANGED
|
@@ -5,13 +5,18 @@ function Kareem() {
|
|
|
5
5
|
this._posts = {};
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
Kareem.prototype.execPre = function(name, context, callback) {
|
|
8
|
+
Kareem.prototype.execPre = function(name, context, args, callback) {
|
|
9
|
+
if (arguments.length === 3) {
|
|
10
|
+
callback = args;
|
|
11
|
+
args = [];
|
|
12
|
+
}
|
|
9
13
|
var pres = this._pres[name] || [];
|
|
10
14
|
var numPres = pres.length;
|
|
11
15
|
var numAsyncPres = pres.numAsync || 0;
|
|
12
16
|
var currentPre = 0;
|
|
13
17
|
var asyncPresLeft = numAsyncPres;
|
|
14
18
|
var done = false;
|
|
19
|
+
var $args = args;
|
|
15
20
|
|
|
16
21
|
if (!numPres) {
|
|
17
22
|
return process.nextTick(function() {
|
|
@@ -74,10 +79,9 @@ Kareem.prototype.execPre = function(name, context, callback) {
|
|
|
74
79
|
|
|
75
80
|
next.apply(context, arguments);
|
|
76
81
|
}];
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
82
|
+
var _args = arguments.length >= 2 ? arguments : [null].concat($args);
|
|
83
|
+
for (var i = 1; i < _args.length; ++i) {
|
|
84
|
+
args.push(_args[i]);
|
|
81
85
|
}
|
|
82
86
|
pre.fn.apply(context, args);
|
|
83
87
|
} else {
|
|
@@ -96,7 +100,7 @@ Kareem.prototype.execPre = function(name, context, callback) {
|
|
|
96
100
|
}
|
|
97
101
|
};
|
|
98
102
|
|
|
99
|
-
next();
|
|
103
|
+
next.apply(null, [null].concat(args));
|
|
100
104
|
};
|
|
101
105
|
|
|
102
106
|
Kareem.prototype.execPreSync = function(name, context) {
|
|
@@ -132,13 +136,17 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
132
136
|
var post = posts[currentPost];
|
|
133
137
|
var numArgs = 0;
|
|
134
138
|
var argLength = args.length;
|
|
139
|
+
var newArgs = [];
|
|
135
140
|
for (var i = 0; i < argLength; ++i) {
|
|
136
141
|
numArgs += args[i] && args[i]._kareemIgnore ? 0 : 1;
|
|
142
|
+
if (!args[i] || !args[i]._kareemIgnore) {
|
|
143
|
+
newArgs.push(args[i]);
|
|
144
|
+
}
|
|
137
145
|
}
|
|
138
146
|
|
|
139
147
|
if (firstError) {
|
|
140
148
|
if (post.length === numArgs + 2) {
|
|
141
|
-
post.apply(context, [firstError].concat(
|
|
149
|
+
post.apply(context, [firstError].concat(newArgs).concat(function(error) {
|
|
142
150
|
if (error) {
|
|
143
151
|
firstError = error;
|
|
144
152
|
}
|
|
@@ -162,7 +170,7 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
162
170
|
return next();
|
|
163
171
|
}
|
|
164
172
|
if (post.length === numArgs + 1) {
|
|
165
|
-
post.apply(context,
|
|
173
|
+
post.apply(context, newArgs.concat(function(error) {
|
|
166
174
|
if (error) {
|
|
167
175
|
firstError = error;
|
|
168
176
|
return next();
|
|
@@ -175,7 +183,7 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
|
|
|
175
183
|
next();
|
|
176
184
|
}));
|
|
177
185
|
} else {
|
|
178
|
-
post.apply(context,
|
|
186
|
+
post.apply(context, newArgs);
|
|
179
187
|
|
|
180
188
|
if (++currentPost >= numPosts) {
|
|
181
189
|
return callback.apply(null, [null].concat(args));
|
|
@@ -233,10 +241,15 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
233
241
|
}
|
|
234
242
|
options = options || {};
|
|
235
243
|
|
|
236
|
-
this.execPre(name, context, function(error) {
|
|
244
|
+
this.execPre(name, context, args, function(error) {
|
|
237
245
|
if (error) {
|
|
238
|
-
|
|
239
|
-
|
|
246
|
+
var numCallbackParams = options.numCallbackParams || 0;
|
|
247
|
+
var nulls = [];
|
|
248
|
+
for (var i = 0; i < numCallbackParams; ++i) {
|
|
249
|
+
nulls.push(null);
|
|
250
|
+
}
|
|
251
|
+
return _handleWrapError(_this, error, name, context, nulls,
|
|
252
|
+
options, lastArg);
|
|
240
253
|
}
|
|
241
254
|
|
|
242
255
|
var end = (typeof lastArg === 'function' ? args.length - 1 : args.length);
|
|
@@ -309,6 +322,7 @@ Kareem.prototype.clone = function() {
|
|
|
309
322
|
continue;
|
|
310
323
|
}
|
|
311
324
|
n._pres[key] = this._pres[key].slice();
|
|
325
|
+
n._pres[key].numAsync = this._pres[key].numAsync;
|
|
312
326
|
}
|
|
313
327
|
for (var key in this._posts) {
|
|
314
328
|
if (!this._posts.hasOwnProperty(key)) {
|
|
@@ -320,4 +334,23 @@ Kareem.prototype.clone = function() {
|
|
|
320
334
|
return n;
|
|
321
335
|
};
|
|
322
336
|
|
|
337
|
+
Kareem.prototype.merge = function(other) {
|
|
338
|
+
var ret = this.clone();
|
|
339
|
+
for (var key in other._pres) {
|
|
340
|
+
if (!other._pres.hasOwnProperty(key)) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
ret._pres[key] = (ret._pres[key] || []).concat(other._pres[key].slice());
|
|
344
|
+
ret._pres[key].numAsync += other._pres[key].numAsync;
|
|
345
|
+
}
|
|
346
|
+
for (var key in other._posts) {
|
|
347
|
+
if (!other._posts.hasOwnProperty(key)) {
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
ret._posts[key] = (ret._posts[key] || []).concat(other._posts[key].slice());
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return ret;
|
|
354
|
+
};
|
|
355
|
+
|
|
323
356
|
module.exports = Kareem;
|
package/package.json
CHANGED
package/test/examples.test.js
CHANGED
|
@@ -337,3 +337,21 @@ describe('clone()', function() {
|
|
|
337
337
|
assert.deepEqual(['cook'], Object.keys(k2._posts));
|
|
338
338
|
});
|
|
339
339
|
});
|
|
340
|
+
|
|
341
|
+
describe('merge()', function() {
|
|
342
|
+
it('pulls hooks from another Kareem object', function() {
|
|
343
|
+
var k1 = new Kareem();
|
|
344
|
+
var test1 = function() {};
|
|
345
|
+
k1.pre('cook', test1);
|
|
346
|
+
k1.post('cook', function() {});
|
|
347
|
+
|
|
348
|
+
var k2 = new Kareem();
|
|
349
|
+
var test2 = function() {};
|
|
350
|
+
k2.pre('cook', test2);
|
|
351
|
+
var k3 = k2.merge(k1);
|
|
352
|
+
assert.equal(k3._pres['cook'].length, 2);
|
|
353
|
+
assert.equal(k3._pres['cook'][0].fn, test2);
|
|
354
|
+
assert.equal(k3._pres['cook'][1].fn, test1);
|
|
355
|
+
assert.equal(k3._posts['cook'].length, 1);
|
|
356
|
+
});
|
|
357
|
+
});
|
package/test/pre.test.js
CHANGED
|
@@ -135,6 +135,27 @@ describe('execPre', function() {
|
|
|
135
135
|
});
|
|
136
136
|
});
|
|
137
137
|
|
|
138
|
+
it('async pres with clone()', function(done) {
|
|
139
|
+
var execed = false;
|
|
140
|
+
|
|
141
|
+
hooks.pre('cook', true, function(next, done) {
|
|
142
|
+
execed = true;
|
|
143
|
+
setTimeout(
|
|
144
|
+
function() {
|
|
145
|
+
done();
|
|
146
|
+
},
|
|
147
|
+
5);
|
|
148
|
+
|
|
149
|
+
next();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
hooks.clone().execPre('cook', null, function(err) {
|
|
153
|
+
assert.ifError(err);
|
|
154
|
+
assert.ok(execed);
|
|
155
|
+
done();
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
138
159
|
it('returns correct error when async pre errors', function(done) {
|
|
139
160
|
var execed = {};
|
|
140
161
|
|
package/test/wrap.test.js
CHANGED
|
@@ -130,7 +130,7 @@ describe('wrap()', function() {
|
|
|
130
130
|
done(new Error('fail'));
|
|
131
131
|
});
|
|
132
132
|
|
|
133
|
-
hooks.post('cook', function(error, callback) {
|
|
133
|
+
hooks.post('cook', function(error, res, callback) {
|
|
134
134
|
callback(new Error('another error occurred'));
|
|
135
135
|
});
|
|
136
136
|
|
|
@@ -148,7 +148,7 @@ describe('wrap()', function() {
|
|
|
148
148
|
},
|
|
149
149
|
null,
|
|
150
150
|
args,
|
|
151
|
-
{ useErrorHandlers: true });
|
|
151
|
+
{ useErrorHandlers: true, numCallbackParams: 1 });
|
|
152
152
|
});
|
|
153
153
|
|
|
154
154
|
it('error handlers with no callback', function(done) {
|