kareem 1.3.0 → 1.5.0

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 CHANGED
@@ -209,14 +209,7 @@ Kareem.prototype.execPostSync = function(name, context) {
209
209
  function _handleWrapError(instance, error, name, context, args, options, callback) {
210
210
  if (options.useErrorHandlers) {
211
211
  var _options = { error: error };
212
- var newArgs = [];
213
- // Filter out trailing undefineds
214
- for (var i = args.length; i >= 0; --i) {
215
- if (newArgs.length > 0 || args[i] !== void 0) {
216
- newArgs.unshift(args[i]);
217
- }
218
- }
219
- return instance.execPost(name, context, newArgs, _options, function(error) {
212
+ return instance.execPost(name, context, args, _options, function(error) {
220
213
  return typeof callback === 'function' && callback(error);
221
214
  });
222
215
  } else {
@@ -257,10 +250,13 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
257
250
  fn.apply(context, args.slice(0, end).concat(function() {
258
251
  var args = arguments;
259
252
  var argsWithoutError = Array.prototype.slice.call(arguments, 1);
253
+ if (options.nullResultByDefault && argsWithoutError.length === 0) {
254
+ argsWithoutError.push(null);
255
+ }
260
256
  if (arguments[0]) {
261
257
  // Assume error
262
258
  return _handleWrapError(_this, arguments[0], name, context,
263
- args, options, lastArg);
259
+ argsWithoutError, options, lastArg);
264
260
  } else {
265
261
  if (useLegacyPost && typeof lastArg === 'function') {
266
262
  lastArg.apply(context, arguments);
@@ -322,6 +318,7 @@ Kareem.prototype.clone = function() {
322
318
  continue;
323
319
  }
324
320
  n._pres[key] = this._pres[key].slice();
321
+ n._pres[key].numAsync = this._pres[key].numAsync;
325
322
  }
326
323
  for (var key in this._posts) {
327
324
  if (!this._posts.hasOwnProperty(key)) {
@@ -333,4 +330,23 @@ Kareem.prototype.clone = function() {
333
330
  return n;
334
331
  };
335
332
 
333
+ Kareem.prototype.merge = function(other) {
334
+ var ret = this.clone();
335
+ for (var key in other._pres) {
336
+ if (!other._pres.hasOwnProperty(key)) {
337
+ continue;
338
+ }
339
+ ret._pres[key] = (ret._pres[key] || []).concat(other._pres[key].slice());
340
+ ret._pres[key].numAsync += other._pres[key].numAsync;
341
+ }
342
+ for (var key in other._posts) {
343
+ if (!other._posts.hasOwnProperty(key)) {
344
+ continue;
345
+ }
346
+ ret._posts[key] = (ret._posts[key] || []).concat(other._posts[key].slice());
347
+ }
348
+
349
+ return ret;
350
+ };
351
+
336
352
  module.exports = Kareem;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kareem",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "Next-generation take on pre/post function hooks",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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