kareem 1.0.0 → 1.0.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/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  language: node_js
2
2
  node_js:
3
- - "0.11"
3
+ - "0.12"
4
4
  - "0.10"
5
+ - "iojs"
5
6
  script: "npm run-script test-travis"
6
7
  after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
package/index.js CHANGED
@@ -38,7 +38,7 @@ Kareem.prototype.execPre = function(name, context, callback) {
38
38
  }
39
39
 
40
40
  ++currentPre;
41
- next();
41
+ next.apply(context, arguments);
42
42
  },
43
43
  function(error) {
44
44
  if (error) {
@@ -54,7 +54,7 @@ Kareem.prototype.execPre = function(name, context, callback) {
54
54
  }
55
55
  });
56
56
  } else if (pre.fn.length > 0) {
57
- pre.fn.call(context, function(error) {
57
+ var args = [function(error) {
58
58
  if (error) {
59
59
  if (done) {
60
60
  return;
@@ -72,8 +72,14 @@ Kareem.prototype.execPre = function(name, context, callback) {
72
72
  }
73
73
  }
74
74
 
75
- next();
76
- });
75
+ next.apply(context, arguments);
76
+ }];
77
+ if (arguments.length >= 2) {
78
+ for (var i = 1; i < arguments.length; ++i) {
79
+ args.push(arguments[i]);
80
+ }
81
+ }
82
+ pre.fn.apply(context, args);
77
83
  } else {
78
84
  pre.fn.call(context);
79
85
  if (++currentPre >= numPres) {
@@ -133,7 +139,7 @@ Kareem.prototype.execPost = function(name, context, args, callback) {
133
139
  next();
134
140
  };
135
141
 
136
- Kareem.prototype.wrap = function(name, fn, context, args) {
142
+ Kareem.prototype.wrap = function(name, fn, context, args, useLegacyPost) {
137
143
  var lastArg = (args.length > 0 ? args[args.length - 1] : null);
138
144
  var _this = this;
139
145
 
@@ -155,6 +161,10 @@ Kareem.prototype.wrap = function(name, fn, context, args) {
155
161
  undefined;
156
162
  }
157
163
 
164
+ if (useLegacyPost && typeof lastArg === 'function') {
165
+ lastArg.apply(context, arguments);
166
+ }
167
+
158
168
  var argsWithoutError = Array.prototype.slice.call(arguments, 1);
159
169
  _this.execPost(name, context, argsWithoutError, function() {
160
170
  if (arguments[0]) {
@@ -163,7 +173,7 @@ Kareem.prototype.wrap = function(name, fn, context, args) {
163
173
  undefined;
164
174
  }
165
175
 
166
- return typeof lastArg === 'function' ?
176
+ return typeof lastArg === 'function' && !useLegacyPost ?
167
177
  lastArg.apply(context, arguments) :
168
178
  undefined;
169
179
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kareem",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Next-generation take on pre/post function hooks",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test/pre.test.js CHANGED
@@ -193,4 +193,34 @@ describe('execPre', function() {
193
193
  done();
194
194
  });
195
195
  });
196
+
197
+ it('allows passing arguments to the next pre', function(done) {
198
+ var execed = {};
199
+
200
+ hooks.pre('cook', function(next) {
201
+ execed.first = true;
202
+ next(null, 'test');
203
+ });
204
+
205
+ hooks.pre('cook', function(next, p) {
206
+ execed.second = true;
207
+ assert.equal(p, 'test');
208
+ next();
209
+ });
210
+
211
+ hooks.pre('cook', function(next, p) {
212
+ execed.third = true;
213
+ assert.ok(!p);
214
+ next();
215
+ });
216
+
217
+ hooks.execPre('cook', null, function(err) {
218
+ assert.ifError(err);
219
+ assert.equal(3, Object.keys(execed).length);
220
+ assert.ok(execed.first);
221
+ assert.ok(execed.second);
222
+ assert.ok(execed.third);
223
+ done();
224
+ });
225
+ });
196
226
  });
package/test/wrap.test.js CHANGED
@@ -248,4 +248,30 @@ describe('wrap()', function() {
248
248
  },
249
249
  25);
250
250
  });
251
+
252
+ it('can use legacy post behavior', function(done) {
253
+ var called = 0;
254
+ hooks.post('cook', function(callback) {
255
+ ++called;
256
+ callback();
257
+ });
258
+
259
+ var args = [function(error) {
260
+ assert.equal(called, 0);
261
+
262
+ setTimeout(function() {
263
+ assert.equal(called, 1);
264
+ done();
265
+ }, 0);
266
+ }];
267
+
268
+ hooks.wrap(
269
+ 'cook',
270
+ function(callback) {
271
+ callback();
272
+ },
273
+ null,
274
+ args,
275
+ true);
276
+ });
251
277
  });