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