kareem 2.2.3 → 2.3.3

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/README.md CHANGED
@@ -24,11 +24,9 @@ appropriate, giving you more fine-grained control over your function hooks.
24
24
  #### It runs without any hooks specified
25
25
 
26
26
  ```javascript
27
-
28
- hooks.execPre('cook', null, function() {
29
- done();
30
- });
31
-
27
+ hooks.execPre('cook', null, function() {
28
+ // ...
29
+ });
32
30
  ```
33
31
 
34
32
  #### It runs basic serial pre hooks
@@ -38,44 +36,38 @@ when your pre hook is finished.
38
36
 
39
37
 
40
38
  ```javascript
41
-
42
- var count = 0;
43
-
44
- hooks.pre('cook', function(done) {
45
- ++count;
46
- done();
47
- });
48
-
49
- hooks.execPre('cook', null, function() {
50
- assert.equal(1, count);
51
- done();
52
- });
53
-
39
+ var count = 0;
40
+
41
+ hooks.pre('cook', function(done) {
42
+ ++count;
43
+ done();
44
+ });
45
+
46
+ hooks.execPre('cook', null, function() {
47
+ assert.equal(1, count);
48
+ });
54
49
  ```
55
50
 
56
51
  #### It can run multipe pre hooks
57
52
 
58
53
  ```javascript
59
-
60
- var count1 = 0;
61
- var count2 = 0;
62
-
63
- hooks.pre('cook', function(done) {
64
- ++count1;
65
- done();
66
- });
67
-
68
- hooks.pre('cook', function(done) {
69
- ++count2;
70
- done();
71
- });
72
-
73
- hooks.execPre('cook', null, function() {
74
- assert.equal(1, count1);
75
- assert.equal(1, count2);
76
- done();
77
- });
78
-
54
+ var count1 = 0;
55
+ var count2 = 0;
56
+
57
+ hooks.pre('cook', function(done) {
58
+ ++count1;
59
+ done();
60
+ });
61
+
62
+ hooks.pre('cook', function(done) {
63
+ ++count2;
64
+ done();
65
+ });
66
+
67
+ hooks.execPre('cook', null, function() {
68
+ assert.equal(1, count1);
69
+ assert.equal(1, count2);
70
+ });
79
71
  ```
80
72
 
81
73
  #### It can run fully synchronous pre hooks
@@ -85,25 +77,22 @@ fully synchronous.
85
77
 
86
78
 
87
79
  ```javascript
88
-
89
- var count1 = 0;
90
- var count2 = 0;
91
-
92
- hooks.pre('cook', function() {
93
- ++count1;
94
- });
95
-
96
- hooks.pre('cook', function() {
97
- ++count2;
98
- });
99
-
100
- hooks.execPre('cook', null, function(error) {
101
- assert.equal(null, error);
102
- assert.equal(1, count1);
103
- assert.equal(1, count2);
104
- done();
105
- });
106
-
80
+ var count1 = 0;
81
+ var count2 = 0;
82
+
83
+ hooks.pre('cook', function() {
84
+ ++count1;
85
+ });
86
+
87
+ hooks.pre('cook', function() {
88
+ ++count2;
89
+ });
90
+
91
+ hooks.execPre('cook', null, function(error) {
92
+ assert.equal(null, error);
93
+ assert.equal(1, count1);
94
+ assert.equal(1, count2);
95
+ });
107
96
  ```
108
97
 
109
98
  #### It properly attaches context to pre hooks
@@ -112,27 +101,24 @@ Pre save hook functions are bound to the second parameter to `execPre()`
112
101
 
113
102
 
114
103
  ```javascript
115
-
116
- hooks.pre('cook', function(done) {
117
- this.bacon = 3;
118
- done();
119
- });
120
-
121
- hooks.pre('cook', function(done) {
122
- this.eggs = 4;
123
- done();
124
- });
125
-
126
- var obj = { bacon: 0, eggs: 0 };
127
-
128
- // In the pre hooks, `this` will refer to `obj`
129
- hooks.execPre('cook', obj, function(error) {
130
- assert.equal(null, error);
131
- assert.equal(3, obj.bacon);
132
- assert.equal(4, obj.eggs);
133
- done();
134
- });
135
-
104
+ hooks.pre('cook', function(done) {
105
+ this.bacon = 3;
106
+ done();
107
+ });
108
+
109
+ hooks.pre('cook', function(done) {
110
+ this.eggs = 4;
111
+ done();
112
+ });
113
+
114
+ var obj = { bacon: 0, eggs: 0 };
115
+
116
+ // In the pre hooks, `this` will refer to `obj`
117
+ hooks.execPre('cook', obj, function(error) {
118
+ assert.equal(null, error);
119
+ assert.equal(3, obj.bacon);
120
+ assert.equal(4, obj.eggs);
121
+ });
136
122
  ```
137
123
 
138
124
  #### It can execute parallel (async) pre hooks
@@ -144,38 +130,35 @@ async pre hooks have called `done()`.
144
130
 
145
131
 
146
132
  ```javascript
147
-
148
- hooks.pre('cook', true, function(next, done) {
149
- this.bacon = 3;
150
- next();
151
- setTimeout(function() {
152
- done();
153
- }, 5);
154
- });
155
-
156
- hooks.pre('cook', true, function(next, done) {
157
- next();
158
- var _this = this;
159
- setTimeout(function() {
160
- _this.eggs = 4;
161
- done();
162
- }, 10);
163
- });
164
-
165
- hooks.pre('cook', function(next) {
166
- this.waffles = false;
167
- next();
168
- });
169
-
170
- var obj = { bacon: 0, eggs: 0 };
171
-
172
- hooks.execPre('cook', obj, function() {
173
- assert.equal(3, obj.bacon);
174
- assert.equal(4, obj.eggs);
175
- assert.equal(false, obj.waffles);
176
- done();
177
- });
178
-
133
+ hooks.pre('cook', true, function(next, done) {
134
+ this.bacon = 3;
135
+ next();
136
+ setTimeout(function() {
137
+ done();
138
+ }, 5);
139
+ });
140
+
141
+ hooks.pre('cook', true, function(next, done) {
142
+ next();
143
+ var _this = this;
144
+ setTimeout(function() {
145
+ _this.eggs = 4;
146
+ done();
147
+ }, 10);
148
+ });
149
+
150
+ hooks.pre('cook', function(next) {
151
+ this.waffles = false;
152
+ next();
153
+ });
154
+
155
+ var obj = { bacon: 0, eggs: 0 };
156
+
157
+ hooks.execPre('cook', obj, function() {
158
+ assert.equal(3, obj.bacon);
159
+ assert.equal(4, obj.eggs);
160
+ assert.equal(false, obj.waffles);
161
+ });
179
162
  ```
180
163
 
181
164
  #### It supports returning a promise
@@ -186,148 +169,162 @@ next middleware.
186
169
 
187
170
 
188
171
  ```javascript
189
-
190
- hooks.pre('cook', function() {
191
- return new Promise(resolve => {
192
- setTimeout(() => {
193
- this.bacon = 3;
194
- resolve();
195
- }, 100);
196
- });
197
- });
198
-
199
- var obj = { bacon: 0 };
200
-
201
- hooks.execPre('cook', obj, function() {
202
- assert.equal(3, obj.bacon);
203
- done();
204
- });
205
-
172
+ hooks.pre('cook', function() {
173
+ return new Promise(resolve => {
174
+ setTimeout(() => {
175
+ this.bacon = 3;
176
+ resolve();
177
+ }, 100);
178
+ });
179
+ });
180
+
181
+ var obj = { bacon: 0 };
182
+
183
+ hooks.execPre('cook', obj, function() {
184
+ assert.equal(3, obj.bacon);
185
+ });
206
186
  ```
207
187
 
208
188
  ## post hooks
209
189
 
190
+ acquit:ignore:end
191
+
210
192
  #### It runs without any hooks specified
211
193
 
212
194
  ```javascript
213
-
214
- hooks.execPost('cook', null, [1], function(error, eggs) {
215
- assert.ifError(error);
216
- assert.equal(1, eggs);
217
- done();
218
- });
219
-
195
+ hooks.execPost('cook', null, [1], function(error, eggs) {
196
+ assert.ifError(error);
197
+ assert.equal(1, eggs);
198
+ done();
199
+ });
220
200
  ```
221
201
 
222
202
  #### It executes with parameters passed in
223
203
 
224
204
  ```javascript
225
-
226
- hooks.post('cook', function(eggs, bacon, callback) {
227
- assert.equal(1, eggs);
228
- assert.equal(2, bacon);
229
- callback();
230
- });
231
-
232
- hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
233
- assert.ifError(error);
234
- assert.equal(1, eggs);
235
- assert.equal(2, bacon);
236
- done();
237
- });
238
-
205
+ hooks.post('cook', function(eggs, bacon, callback) {
206
+ assert.equal(1, eggs);
207
+ assert.equal(2, bacon);
208
+ callback();
209
+ });
210
+
211
+ hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
212
+ assert.ifError(error);
213
+ assert.equal(1, eggs);
214
+ assert.equal(2, bacon);
215
+ });
239
216
  ```
240
217
 
241
218
  #### It can use synchronous post hooks
242
219
 
243
220
  ```javascript
244
-
245
- var execed = {};
246
-
247
- hooks.post('cook', function(eggs, bacon) {
248
- execed.first = true;
249
- assert.equal(1, eggs);
250
- assert.equal(2, bacon);
251
- });
252
-
253
- hooks.post('cook', function(eggs, bacon, callback) {
254
- execed.second = true;
255
- assert.equal(1, eggs);
256
- assert.equal(2, bacon);
257
- callback();
258
- });
259
-
260
- hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
261
- assert.ifError(error);
262
- assert.equal(2, Object.keys(execed).length);
263
- assert.ok(execed.first);
264
- assert.ok(execed.second);
265
- assert.equal(1, eggs);
266
- assert.equal(2, bacon);
267
- done();
268
- });
269
-
221
+ var execed = {};
222
+
223
+ hooks.post('cook', function(eggs, bacon) {
224
+ execed.first = true;
225
+ assert.equal(1, eggs);
226
+ assert.equal(2, bacon);
227
+ });
228
+
229
+ hooks.post('cook', function(eggs, bacon, callback) {
230
+ execed.second = true;
231
+ assert.equal(1, eggs);
232
+ assert.equal(2, bacon);
233
+ callback();
234
+ });
235
+
236
+ hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
237
+ assert.ifError(error);
238
+ assert.equal(2, Object.keys(execed).length);
239
+ assert.ok(execed.first);
240
+ assert.ok(execed.second);
241
+ assert.equal(1, eggs);
242
+ assert.equal(2, bacon);
243
+ });
244
+ ```
245
+
246
+ #### It supports returning a promise
247
+
248
+ You can also return a promise from your post hooks instead of calling
249
+ `next()`. When the returned promise resolves, kareem will kick off the
250
+ next middleware.
251
+
252
+
253
+ ```javascript
254
+ hooks.post('cook', function(bacon) {
255
+ return new Promise(resolve => {
256
+ setTimeout(() => {
257
+ this.bacon = 3;
258
+ resolve();
259
+ }, 100);
260
+ });
261
+ });
262
+
263
+ var obj = { bacon: 0 };
264
+
265
+ hooks.execPost('cook', obj, obj, function() {
266
+ assert.equal(obj.bacon, 3);
267
+ });
270
268
  ```
271
269
 
272
270
  ## wrap()
273
271
 
272
+ acquit:ignore:end
273
+
274
274
  #### It wraps pre and post calls into one call
275
275
 
276
276
  ```javascript
277
-
278
- hooks.pre('cook', true, function(next, done) {
279
- this.bacon = 3;
280
- next();
281
- setTimeout(function() {
282
- done();
283
- }, 5);
284
- });
285
-
286
- hooks.pre('cook', true, function(next, done) {
287
- next();
288
- var _this = this;
289
- setTimeout(function() {
290
- _this.eggs = 4;
291
- done();
292
- }, 10);
293
- });
294
-
295
- hooks.pre('cook', function(next) {
296
- this.waffles = false;
297
- next();
298
- });
299
-
300
- hooks.post('cook', function(obj) {
301
- obj.tofu = 'no';
302
- });
303
-
304
- var obj = { bacon: 0, eggs: 0 };
305
-
306
- var args = [obj];
307
- args.push(function(error, result) {
308
- assert.ifError(error);
309
- assert.equal(null, error);
310
- assert.equal(3, obj.bacon);
311
- assert.equal(4, obj.eggs);
312
- assert.equal(false, obj.waffles);
313
- assert.equal('no', obj.tofu);
314
-
315
- assert.equal(obj, result);
316
- done();
317
- });
318
-
319
- hooks.wrap(
320
- 'cook',
321
- function(o, callback) {
322
- assert.equal(3, obj.bacon);
323
- assert.equal(4, obj.eggs);
324
- assert.equal(false, obj.waffles);
325
- assert.equal(undefined, obj.tofu);
326
- callback(null, o);
327
- },
328
- obj,
329
- args);
330
-
277
+ hooks.pre('cook', true, function(next, done) {
278
+ this.bacon = 3;
279
+ next();
280
+ setTimeout(function() {
281
+ done();
282
+ }, 5);
283
+ });
284
+
285
+ hooks.pre('cook', true, function(next, done) {
286
+ next();
287
+ var _this = this;
288
+ setTimeout(function() {
289
+ _this.eggs = 4;
290
+ done();
291
+ }, 10);
292
+ });
293
+
294
+ hooks.pre('cook', function(next) {
295
+ this.waffles = false;
296
+ next();
297
+ });
298
+
299
+ hooks.post('cook', function(obj) {
300
+ obj.tofu = 'no';
301
+ });
302
+
303
+ var obj = { bacon: 0, eggs: 0 };
304
+
305
+ var args = [obj];
306
+ args.push(function(error, result) {
307
+ assert.ifError(error);
308
+ assert.equal(null, error);
309
+ assert.equal(3, obj.bacon);
310
+ assert.equal(4, obj.eggs);
311
+ assert.equal(false, obj.waffles);
312
+ assert.equal('no', obj.tofu);
313
+
314
+ assert.equal(obj, result);
315
+ });
316
+
317
+ hooks.wrap(
318
+ 'cook',
319
+ function(o, callback) {
320
+ assert.equal(3, obj.bacon);
321
+ assert.equal(4, obj.eggs);
322
+ assert.equal(false, obj.waffles);
323
+ assert.equal(undefined, obj.tofu);
324
+ callback(null, o);
325
+ },
326
+ obj,
327
+ args);
331
328
  ```
332
329
 
333
330
  ## createWrapper()
@@ -335,73 +332,70 @@ next middleware.
335
332
  #### It wraps wrap() into a callable function
336
333
 
337
334
  ```javascript
338
-
339
- hooks.pre('cook', true, function(next, done) {
340
- this.bacon = 3;
341
- next();
342
- setTimeout(function() {
343
- done();
344
- }, 5);
345
- });
346
-
347
- hooks.pre('cook', true, function(next, done) {
348
- next();
349
- var _this = this;
350
- setTimeout(function() {
351
- _this.eggs = 4;
352
- done();
353
- }, 10);
354
- });
355
-
356
- hooks.pre('cook', function(next) {
357
- this.waffles = false;
358
- next();
359
- });
360
-
361
- hooks.post('cook', function(obj) {
362
- obj.tofu = 'no';
363
- });
364
-
365
- var obj = { bacon: 0, eggs: 0 };
366
-
367
- var cook = hooks.createWrapper(
368
- 'cook',
369
- function(o, callback) {
370
- assert.equal(3, obj.bacon);
371
- assert.equal(4, obj.eggs);
372
- assert.equal(false, obj.waffles);
373
- assert.equal(undefined, obj.tofu);
374
- callback(null, o);
375
- },
376
- obj);
377
-
378
- cook(obj, function(error, result) {
379
- assert.ifError(error);
380
- assert.equal(3, obj.bacon);
381
- assert.equal(4, obj.eggs);
382
- assert.equal(false, obj.waffles);
383
- assert.equal('no', obj.tofu);
384
-
385
- assert.equal(obj, result);
386
- done();
387
- });
388
-
335
+ hooks.pre('cook', true, function(next, done) {
336
+ this.bacon = 3;
337
+ next();
338
+ setTimeout(function() {
339
+ done();
340
+ }, 5);
341
+ });
342
+
343
+ hooks.pre('cook', true, function(next, done) {
344
+ next();
345
+ var _this = this;
346
+ setTimeout(function() {
347
+ _this.eggs = 4;
348
+ done();
349
+ }, 10);
350
+ });
351
+
352
+ hooks.pre('cook', function(next) {
353
+ this.waffles = false;
354
+ next();
355
+ });
356
+
357
+ hooks.post('cook', function(obj) {
358
+ obj.tofu = 'no';
359
+ });
360
+
361
+ var obj = { bacon: 0, eggs: 0 };
362
+
363
+ var cook = hooks.createWrapper(
364
+ 'cook',
365
+ function(o, callback) {
366
+ assert.equal(3, obj.bacon);
367
+ assert.equal(4, obj.eggs);
368
+ assert.equal(false, obj.waffles);
369
+ assert.equal(undefined, obj.tofu);
370
+ callback(null, o);
371
+ },
372
+ obj);
373
+
374
+ cook(obj, function(error, result) {
375
+ assert.ifError(error);
376
+ assert.equal(3, obj.bacon);
377
+ assert.equal(4, obj.eggs);
378
+ assert.equal(false, obj.waffles);
379
+ assert.equal('no', obj.tofu);
380
+
381
+ assert.equal(obj, result);
382
+ });
389
383
  ```
390
384
 
391
385
  ## clone()
392
386
 
387
+ acquit:ignore:end
388
+
393
389
  #### It clones a Kareem object
394
390
 
395
391
  ```javascript
396
-
397
- var k1 = new Kareem();
398
- k1.pre('cook', function() {});
399
- k1.post('cook', function() {});
400
-
401
- var k2 = k1.clone();
402
- assert.deepEqual(['cook'], Object.keys(k2._pres));
403
- assert.deepEqual(['cook'], Object.keys(k2._posts));
404
-
392
+ var k1 = new Kareem();
393
+ k1.pre('cook', function() {});
394
+ k1.post('cook', function() {});
395
+
396
+ var k2 = k1.clone();
397
+ assert.deepEqual(Array.from(k2._pres.keys()), ['cook']);
398
+ assert.deepEqual(Array.from(k2._posts.keys()), ['cook']);
405
399
  ```
406
400
 
407
401
  ## merge()
@@ -409,20 +403,18 @@ next middleware.
409
403
  #### It pulls hooks from another Kareem object
410
404
 
411
405
  ```javascript
412
-
413
- var k1 = new Kareem();
414
- var test1 = function() {};
415
- k1.pre('cook', test1);
416
- k1.post('cook', function() {});
417
-
418
- var k2 = new Kareem();
419
- var test2 = function() {};
420
- k2.pre('cook', test2);
421
- var k3 = k2.merge(k1);
422
- assert.equal(k3._pres['cook'].length, 2);
423
- assert.equal(k3._pres['cook'][0].fn, test2);
424
- assert.equal(k3._pres['cook'][1].fn, test1);
425
- assert.equal(k3._posts['cook'].length, 1);
426
-
406
+ var k1 = new Kareem();
407
+ var test1 = function() {};
408
+ k1.pre('cook', test1);
409
+ k1.post('cook', function() {});
410
+
411
+ var k2 = new Kareem();
412
+ var test2 = function() {};
413
+ k2.pre('cook', test2);
414
+ var k3 = k2.merge(k1);
415
+ assert.equal(k3._pres.get('cook').length, 2);
416
+ assert.equal(k3._pres.get('cook')[0].fn, test2);
417
+ assert.equal(k3._pres.get('cook')[1].fn, test1);
418
+ assert.equal(k3._posts.get('cook').length, 1);
427
419
  ```
428
420