kareem 2.4.0 → 2.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/Makefile DELETED
@@ -1,5 +0,0 @@
1
- docs:
2
- node ./docs.js
3
-
4
- coverage:
5
- ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*
package/docs.js DELETED
@@ -1,39 +0,0 @@
1
- var acquit = require('acquit');
2
-
3
- require('acquit-ignore')();
4
-
5
- var content = require('fs').readFileSync('./test/examples.test.js').toString();
6
- var blocks = acquit.parse(content);
7
-
8
- var mdOutput =
9
- '# kareem\n\n' +
10
- ' [![Build Status](https://travis-ci.org/vkarpov15/kareem.svg?branch=master)](https://travis-ci.org/vkarpov15/kareem)\n' +
11
- ' [![Coverage Status](https://img.shields.io/coveralls/vkarpov15/kareem.svg)](https://coveralls.io/r/vkarpov15/kareem)\n\n' +
12
- 'Re-imagined take on the [hooks](http://npmjs.org/package/hooks) module, ' +
13
- 'meant to offer additional flexibility in allowing you to execute hooks ' +
14
- 'whenever necessary, as opposed to simply wrapping a single function.\n\n' +
15
- 'Named for the NBA\'s all-time leading scorer Kareem Abdul-Jabbar, known ' +
16
- 'for his mastery of the [hook shot](http://en.wikipedia.org/wiki/Kareem_Abdul-Jabbar#Skyhook)\n\n' +
17
- '<img src="http://upload.wikimedia.org/wikipedia/commons/0/00/Kareem-Abdul-Jabbar_Lipofsky.jpg" width="220">\n\n' +
18
- '# API\n\n';
19
-
20
- for (var i = 0; i < blocks.length; ++i) {
21
- var describe = blocks[i];
22
- mdOutput += '## ' + describe.contents + '\n\n';
23
- mdOutput += describe.comments[0] ?
24
- acquit.trimEachLine(describe.comments[0]) + '\n\n' :
25
- '';
26
-
27
- for (var j = 0; j < describe.blocks.length; ++j) {
28
- var it = describe.blocks[j];
29
- mdOutput += '#### It ' + it.contents + '\n\n';
30
- mdOutput += it.comments[0] ?
31
- acquit.trimEachLine(it.comments[0]) + '\n\n' :
32
- '';
33
- mdOutput += '```javascript\n';
34
- mdOutput += it.code + '\n';
35
- mdOutput += '```\n\n';
36
- }
37
- }
38
-
39
- require('fs').writeFileSync('README.md', mdOutput);
package/gulpfile.js DELETED
@@ -1,18 +0,0 @@
1
- var gulp = require('gulp');
2
- var mocha = require('gulp-mocha');
3
- var config = require('./package.json');
4
- var jscs = require('gulp-jscs');
5
-
6
- gulp.task('mocha', function() {
7
- return gulp.src('./test/*').
8
- pipe(mocha({ reporter: 'dot' }));
9
- });
10
-
11
- gulp.task('jscs', function() {
12
- return gulp.src('./index.js').
13
- pipe(jscs(config.jscsConfig));
14
- });
15
-
16
- gulp.task('watch', function() {
17
- gulp.watch('./index.js', ['jscs', 'mocha']);
18
- });
@@ -1,426 +0,0 @@
1
- var assert = require('assert');
2
- var Kareem = require('../');
3
-
4
- /* Much like [hooks](https://npmjs.org/package/hooks), kareem lets you define
5
- * pre and post hooks: pre hooks are called before a given function executes.
6
- * Unlike hooks, kareem stores hooks and other internal state in a separate
7
- * object, rather than relying on inheritance. Furthermore, kareem exposes
8
- * an `execPre()` function that allows you to execute your pre hooks when
9
- * appropriate, giving you more fine-grained control over your function hooks.
10
- */
11
- describe('pre hooks', function() {
12
- var hooks;
13
-
14
- beforeEach(function() {
15
- hooks = new Kareem();
16
- });
17
-
18
- it('runs without any hooks specified', function(done) {
19
- hooks.execPre('cook', null, function() {
20
- // ...
21
- // acquit:ignore:start
22
- done();
23
- // acquit:ignore:end
24
- });
25
- });
26
-
27
- /* pre hook functions take one parameter, a "done" function that you execute
28
- * when your pre hook is finished.
29
- */
30
- it('runs basic serial pre hooks', function(done) {
31
- var count = 0;
32
-
33
- hooks.pre('cook', function(done) {
34
- ++count;
35
- done();
36
- });
37
-
38
- hooks.execPre('cook', null, function() {
39
- assert.equal(1, count);
40
- // acquit:ignore:start
41
- done();
42
- // acquit:ignore:end
43
- });
44
- });
45
-
46
- it('can run multipe pre hooks', function(done) {
47
- var count1 = 0;
48
- var count2 = 0;
49
-
50
- hooks.pre('cook', function(done) {
51
- ++count1;
52
- done();
53
- });
54
-
55
- hooks.pre('cook', function(done) {
56
- ++count2;
57
- done();
58
- });
59
-
60
- hooks.execPre('cook', null, function() {
61
- assert.equal(1, count1);
62
- assert.equal(1, count2);
63
- // acquit:ignore:start
64
- done();
65
- // acquit:ignore:end
66
- });
67
- });
68
-
69
- /* If your pre hook function takes no parameters, its assumed to be
70
- * fully synchronous.
71
- */
72
- it('can run fully synchronous pre hooks', function(done) {
73
- var count1 = 0;
74
- var count2 = 0;
75
-
76
- hooks.pre('cook', function() {
77
- ++count1;
78
- });
79
-
80
- hooks.pre('cook', function() {
81
- ++count2;
82
- });
83
-
84
- hooks.execPre('cook', null, function(error) {
85
- assert.equal(null, error);
86
- assert.equal(1, count1);
87
- assert.equal(1, count2);
88
- // acquit:ignore:start
89
- done();
90
- // acquit:ignore:end
91
- });
92
- });
93
-
94
- /* Pre save hook functions are bound to the second parameter to `execPre()`
95
- */
96
- it('properly attaches context to pre hooks', function(done) {
97
- hooks.pre('cook', function(done) {
98
- this.bacon = 3;
99
- done();
100
- });
101
-
102
- hooks.pre('cook', function(done) {
103
- this.eggs = 4;
104
- done();
105
- });
106
-
107
- var obj = { bacon: 0, eggs: 0 };
108
-
109
- // In the pre hooks, `this` will refer to `obj`
110
- hooks.execPre('cook', obj, function(error) {
111
- assert.equal(null, error);
112
- assert.equal(3, obj.bacon);
113
- assert.equal(4, obj.eggs);
114
- // acquit:ignore:start
115
- done();
116
- // acquit:ignore:end
117
- });
118
- });
119
-
120
- /* Like the hooks module, you can declare "async" pre hooks - these take two
121
- * parameters, the functions `next()` and `done()`. `next()` passes control to
122
- * the next pre hook, but the underlying function won't be called until all
123
- * async pre hooks have called `done()`.
124
- */
125
- it('can execute parallel (async) pre hooks', function(done) {
126
- hooks.pre('cook', true, function(next, done) {
127
- this.bacon = 3;
128
- next();
129
- setTimeout(function() {
130
- done();
131
- }, 5);
132
- });
133
-
134
- hooks.pre('cook', true, function(next, done) {
135
- next();
136
- var _this = this;
137
- setTimeout(function() {
138
- _this.eggs = 4;
139
- done();
140
- }, 10);
141
- });
142
-
143
- hooks.pre('cook', function(next) {
144
- this.waffles = false;
145
- next();
146
- });
147
-
148
- var obj = { bacon: 0, eggs: 0 };
149
-
150
- hooks.execPre('cook', obj, function() {
151
- assert.equal(3, obj.bacon);
152
- assert.equal(4, obj.eggs);
153
- assert.equal(false, obj.waffles);
154
- // acquit:ignore:start
155
- done();
156
- // acquit:ignore:end
157
- });
158
- });
159
-
160
- /* You can also return a promise from your pre hooks instead of calling
161
- * `next()`. When the returned promise resolves, kareem will kick off the
162
- * next middleware.
163
- */
164
- it('supports returning a promise', function(done) {
165
- hooks.pre('cook', function() {
166
- return new Promise(resolve => {
167
- setTimeout(() => {
168
- this.bacon = 3;
169
- resolve();
170
- }, 100);
171
- });
172
- });
173
-
174
- var obj = { bacon: 0 };
175
-
176
- hooks.execPre('cook', obj, function() {
177
- assert.equal(3, obj.bacon);
178
- // acquit:ignore:start
179
- done();
180
- // acquit:ignore:end
181
- });
182
- });
183
- });
184
-
185
- describe('post hooks', function() {
186
- var hooks;
187
-
188
- beforeEach(function() {
189
- hooks = new Kareem();
190
- });
191
-
192
- it('runs without any hooks specified', function(done) {
193
- hooks.execPost('cook', null, [1], function(error, eggs) {
194
- assert.ifError(error);
195
- assert.equal(1, eggs);
196
- done();
197
- });
198
- });
199
-
200
- it('executes with parameters passed in', function(done) {
201
- hooks.post('cook', function(eggs, bacon, callback) {
202
- assert.equal(1, eggs);
203
- assert.equal(2, bacon);
204
- callback();
205
- });
206
-
207
- hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
208
- assert.ifError(error);
209
- assert.equal(1, eggs);
210
- assert.equal(2, bacon);
211
- // acquit:ignore:start
212
- done();
213
- // acquit:ignore:end
214
- });
215
- });
216
-
217
- it('can use synchronous post hooks', function(done) {
218
- var execed = {};
219
-
220
- hooks.post('cook', function(eggs, bacon) {
221
- execed.first = true;
222
- assert.equal(1, eggs);
223
- assert.equal(2, bacon);
224
- });
225
-
226
- hooks.post('cook', function(eggs, bacon, callback) {
227
- execed.second = true;
228
- assert.equal(1, eggs);
229
- assert.equal(2, bacon);
230
- callback();
231
- });
232
-
233
- hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
234
- assert.ifError(error);
235
- assert.equal(2, Object.keys(execed).length);
236
- assert.ok(execed.first);
237
- assert.ok(execed.second);
238
- assert.equal(1, eggs);
239
- assert.equal(2, bacon);
240
- // acquit:ignore:start
241
- done();
242
- // acquit:ignore:end
243
- });
244
- });
245
-
246
- /* You can also return a promise from your post hooks instead of calling
247
- * `next()`. When the returned promise resolves, kareem will kick off the
248
- * next middleware.
249
- */
250
- it('supports returning a promise', function(done) {
251
- hooks.post('cook', function(bacon) {
252
- return new Promise(resolve => {
253
- setTimeout(() => {
254
- this.bacon = 3;
255
- resolve();
256
- }, 100);
257
- });
258
- });
259
-
260
- var obj = { bacon: 0 };
261
-
262
- hooks.execPost('cook', obj, obj, function() {
263
- assert.equal(obj.bacon, 3);
264
- // acquit:ignore:start
265
- done();
266
- // acquit:ignore:end
267
- });
268
- });
269
- });
270
-
271
- describe('wrap()', function() {
272
- var hooks;
273
-
274
- beforeEach(function() {
275
- hooks = new Kareem();
276
- });
277
-
278
- it('wraps pre and post calls into one call', function(done) {
279
- hooks.pre('cook', true, function(next, done) {
280
- this.bacon = 3;
281
- next();
282
- setTimeout(function() {
283
- done();
284
- }, 5);
285
- });
286
-
287
- hooks.pre('cook', true, function(next, done) {
288
- next();
289
- var _this = this;
290
- setTimeout(function() {
291
- _this.eggs = 4;
292
- done();
293
- }, 10);
294
- });
295
-
296
- hooks.pre('cook', function(next) {
297
- this.waffles = false;
298
- next();
299
- });
300
-
301
- hooks.post('cook', function(obj) {
302
- obj.tofu = 'no';
303
- });
304
-
305
- var obj = { bacon: 0, eggs: 0 };
306
-
307
- var args = [obj];
308
- args.push(function(error, result) {
309
- assert.ifError(error);
310
- assert.equal(null, error);
311
- assert.equal(3, obj.bacon);
312
- assert.equal(4, obj.eggs);
313
- assert.equal(false, obj.waffles);
314
- assert.equal('no', obj.tofu);
315
-
316
- assert.equal(obj, result);
317
- // acquit:ignore:start
318
- done();
319
- // acquit:ignore:end
320
- });
321
-
322
- hooks.wrap(
323
- 'cook',
324
- function(o, callback) {
325
- assert.equal(3, obj.bacon);
326
- assert.equal(4, obj.eggs);
327
- assert.equal(false, obj.waffles);
328
- assert.equal(undefined, obj.tofu);
329
- callback(null, o);
330
- },
331
- obj,
332
- args);
333
- });
334
- });
335
-
336
- describe('createWrapper()', function() {
337
- var hooks;
338
-
339
- beforeEach(function() {
340
- hooks = new Kareem();
341
- });
342
-
343
- it('wraps wrap() into a callable function', function(done) {
344
- hooks.pre('cook', true, function(next, done) {
345
- this.bacon = 3;
346
- next();
347
- setTimeout(function() {
348
- done();
349
- }, 5);
350
- });
351
-
352
- hooks.pre('cook', true, function(next, done) {
353
- next();
354
- var _this = this;
355
- setTimeout(function() {
356
- _this.eggs = 4;
357
- done();
358
- }, 10);
359
- });
360
-
361
- hooks.pre('cook', function(next) {
362
- this.waffles = false;
363
- next();
364
- });
365
-
366
- hooks.post('cook', function(obj) {
367
- obj.tofu = 'no';
368
- });
369
-
370
- var obj = { bacon: 0, eggs: 0 };
371
-
372
- var cook = hooks.createWrapper(
373
- 'cook',
374
- function(o, callback) {
375
- assert.equal(3, obj.bacon);
376
- assert.equal(4, obj.eggs);
377
- assert.equal(false, obj.waffles);
378
- assert.equal(undefined, obj.tofu);
379
- callback(null, o);
380
- },
381
- obj);
382
-
383
- cook(obj, function(error, result) {
384
- assert.ifError(error);
385
- assert.equal(3, obj.bacon);
386
- assert.equal(4, obj.eggs);
387
- assert.equal(false, obj.waffles);
388
- assert.equal('no', obj.tofu);
389
-
390
- assert.equal(obj, result);
391
- // acquit:ignore:start
392
- done();
393
- // acquit:ignore:end
394
- });
395
- });
396
- });
397
-
398
- describe('clone()', function() {
399
- it('clones a Kareem object', function() {
400
- var k1 = new Kareem();
401
- k1.pre('cook', function() {});
402
- k1.post('cook', function() {});
403
-
404
- var k2 = k1.clone();
405
- assert.deepEqual(Array.from(k2._pres.keys()), ['cook']);
406
- assert.deepEqual(Array.from(k2._posts.keys()), ['cook']);
407
- });
408
- });
409
-
410
- describe('merge()', function() {
411
- it('pulls hooks from another Kareem object', function() {
412
- var k1 = new Kareem();
413
- var test1 = function() {};
414
- k1.pre('cook', test1);
415
- k1.post('cook', function() {});
416
-
417
- var k2 = new Kareem();
418
- var test2 = function() {};
419
- k2.pre('cook', test2);
420
- var k3 = k2.merge(k1);
421
- assert.equal(k3._pres.get('cook').length, 2);
422
- assert.equal(k3._pres.get('cook')[0].fn, test2);
423
- assert.equal(k3._pres.get('cook')[1].fn, test1);
424
- assert.equal(k3._posts.get('cook').length, 1);
425
- });
426
- });
package/test/misc.test.js DELETED
@@ -1,71 +0,0 @@
1
- 'use strict';
2
-
3
- const assert = require('assert');
4
- const Kareem = require('../');
5
-
6
- describe('hasHooks', function() {
7
- it('returns false for toString (Automattic/mongoose#6538)', function() {
8
- const k = new Kareem();
9
- assert.ok(!k.hasHooks('toString'));
10
- });
11
- });
12
-
13
- describe('merge', function() {
14
- it('handles async pres if source doesnt have them', function() {
15
- const k1 = new Kareem();
16
- k1.pre('cook', true, function(next, done) {
17
- execed.first = true;
18
- setTimeout(
19
- function() {
20
- done('error!');
21
- },
22
- 5);
23
-
24
- next();
25
- });
26
-
27
- assert.equal(k1._pres.get('cook').numAsync, 1);
28
-
29
- const k2 = new Kareem();
30
- const k3 = k2.merge(k1);
31
- assert.equal(k3._pres.get('cook').numAsync, 1);
32
- });
33
- });
34
-
35
- describe('filter', function() {
36
- it('returns clone with only hooks that match `fn()`', function() {
37
- const k1 = new Kareem();
38
-
39
- k1.pre('update', { document: true }, f1);
40
- k1.pre('update', { query: true }, f2);
41
- k1.pre('remove', { document: true }, f3);
42
-
43
- k1.post('update', { document: true }, f1);
44
- k1.post('update', { query: true }, f2);
45
- k1.post('remove', { document: true }, f3);
46
-
47
- const k2 = k1.filter(hook => hook.document);
48
- assert.equal(k2._pres.get('update').length, 1);
49
- assert.equal(k2._pres.get('update')[0].fn, f1);
50
- assert.equal(k2._pres.get('remove').length, 1);
51
- assert.equal(k2._pres.get('remove')[0].fn, f3);
52
-
53
- assert.equal(k2._posts.get('update').length, 1);
54
- assert.equal(k2._posts.get('update')[0].fn, f1);
55
- assert.equal(k2._posts.get('remove').length, 1);
56
- assert.equal(k2._posts.get('remove')[0].fn, f3);
57
-
58
- const k3 = k1.filter(hook => hook.query);
59
- assert.equal(k3._pres.get('update').length, 1);
60
- assert.equal(k3._pres.get('update')[0].fn, f2);
61
- assert.ok(!k3._pres.has('remove'));
62
-
63
- assert.equal(k3._posts.get('update').length, 1);
64
- assert.equal(k3._posts.get('update')[0].fn, f2);
65
- assert.ok(!k3._posts.has('remove'));
66
-
67
- function f1() {}
68
- function f2() {}
69
- function f3() {}
70
- });
71
- });