Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
273 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
docs: | ||
node ./docs.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,238 @@ | ||
kareem | ||
====== | ||
# kareem | ||
|
||
Next-generation take on the [hooks](http://npmjs.org/package/hooks), meant to offer additional flexibility in allowing you to execute hooks whenever necessary, as opposed to simply wrapping a single function. | ||
|
||
Named for the NBA's all-time leading scorer Kareem Abdul-Jabbar, known for his mastery of the [hook shot](http://en.wikipedia.org/wiki/Kareem_Abdul-Jabbar#Skyhook) | ||
|
||
<img src="http://upload.wikimedia.org/wikipedia/commons/0/00/Kareem-Abdul-Jabbar_Lipofsky.jpg" width="220"> | ||
|
||
### pre hooks | ||
|
||
##### It runs without any hooks specified | ||
|
||
```javascript | ||
|
||
hooks.execPre('cook', null, function() { | ||
done(); | ||
}); | ||
|
||
``` | ||
|
||
##### It runs basic serial pre hooks | ||
|
||
```javascript | ||
|
||
var count = 0; | ||
|
||
hooks.pre('cook', function(done) { | ||
++count; | ||
done(); | ||
}); | ||
|
||
hooks.execPre('cook', null, function() { | ||
assert.equal(1, count); | ||
done(); | ||
}); | ||
|
||
``` | ||
|
||
##### It can run multipe pres | ||
|
||
```javascript | ||
|
||
var count1 = 0; | ||
var count2 = 0; | ||
|
||
hooks.pre('cook', function(done) { | ||
++count1; | ||
done(); | ||
}); | ||
|
||
hooks.pre('cook', function(done) { | ||
++count2; | ||
done(); | ||
}); | ||
|
||
hooks.execPre('cook', null, function() { | ||
assert.equal(1, count1); | ||
assert.equal(1, count2); | ||
done(); | ||
}); | ||
|
||
``` | ||
|
||
##### It can run fully synchronous pres | ||
|
||
```javascript | ||
|
||
var count1 = 0; | ||
var count2 = 0; | ||
|
||
hooks.pre('cook', function() { | ||
++count1; | ||
}); | ||
|
||
hooks.pre('cook', function() { | ||
++count2; | ||
}); | ||
|
||
hooks.execPre('cook', null, function() { | ||
assert.equal(1, count1); | ||
assert.equal(1, count2); | ||
done(); | ||
}); | ||
|
||
``` | ||
|
||
##### It properly attaches context to pre hooks | ||
|
||
```javascript | ||
|
||
hooks.pre('cook', function(done) { | ||
this.bacon = 3; | ||
done(); | ||
}); | ||
|
||
hooks.pre('cook', function(done) { | ||
this.eggs = 4; | ||
done(); | ||
}); | ||
|
||
var obj = { bacon: 0, eggs: 0 }; | ||
|
||
hooks.execPre('cook', obj, function() { | ||
assert.equal(3, obj.bacon); | ||
assert.equal(4, obj.eggs); | ||
done(); | ||
}); | ||
|
||
``` | ||
|
||
##### It can execute parallel (async) pre hooks | ||
|
||
```javascript | ||
|
||
hooks.pre('cook', true, function(next, done) { | ||
this.bacon = 3; | ||
next(); | ||
setTimeout(function() { | ||
done(); | ||
}, 5); | ||
}); | ||
|
||
hooks.pre('cook', true, function(next, done) { | ||
next(); | ||
var _this = this; | ||
setTimeout(function() { | ||
_this.eggs = 4; | ||
done(); | ||
}, 10); | ||
}); | ||
|
||
hooks.pre('cook', function(next) { | ||
this.waffles = false; | ||
next(); | ||
}); | ||
|
||
var obj = { bacon: 0, eggs: 0 }; | ||
|
||
hooks.execPre('cook', obj, function() { | ||
assert.equal(3, obj.bacon); | ||
assert.equal(4, obj.eggs); | ||
assert.equal(false, obj.waffles); | ||
done(); | ||
}); | ||
|
||
``` | ||
|
||
### post hooks | ||
|
||
##### It runs without any hooks specified | ||
|
||
```javascript | ||
|
||
hooks.execPost('cook', null, [], function() { | ||
done(); | ||
}); | ||
|
||
``` | ||
|
||
##### It executes with parameters passed in | ||
|
||
```javascript | ||
|
||
hooks.post('cook', function(eggs, bacon, callback) { | ||
assert.equal(1, eggs); | ||
assert.equal(2, bacon); | ||
callback(); | ||
}); | ||
|
||
hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) { | ||
assert.ifError(error); | ||
assert.equal(1, eggs); | ||
assert.equal(2, bacon); | ||
done(); | ||
}); | ||
|
||
``` | ||
|
||
### wrap() | ||
|
||
##### It wraps pre and post calls into one call | ||
|
||
```javascript | ||
|
||
hooks.pre('cook', true, function(next, done) { | ||
this.bacon = 3; | ||
next(); | ||
setTimeout(function() { | ||
done(); | ||
}, 5); | ||
}); | ||
|
||
hooks.pre('cook', true, function(next, done) { | ||
next(); | ||
var _this = this; | ||
setTimeout(function() { | ||
_this.eggs = 4; | ||
done(); | ||
}, 10); | ||
}); | ||
|
||
hooks.pre('cook', function(next) { | ||
this.waffles = false; | ||
next(); | ||
}); | ||
|
||
hooks.post('cook', function(obj) { | ||
obj.tofu = 'no'; | ||
}); | ||
|
||
var obj = { bacon: 0, eggs: 0 }; | ||
|
||
var args = [obj]; | ||
args.push(function(error, result) { | ||
assert.ifError(error); | ||
assert.equal(3, obj.bacon); | ||
assert.equal(4, obj.eggs); | ||
assert.equal(false, obj.waffles); | ||
assert.equal('no', obj.tofu); | ||
|
||
assert.equal(obj, result); | ||
done(); | ||
}); | ||
|
||
hooks.wrap( | ||
'cook', | ||
function(o, callback) { | ||
assert.equal(3, obj.bacon); | ||
assert.equal(4, obj.eggs); | ||
assert.equal(false, obj.waffles); | ||
assert.equal(undefined, obj.tofu); | ||
callback(); | ||
}, | ||
obj, | ||
args); | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
var acquit = require('acquit'); | ||
var acquit = require('acquit'); | ||
|
||
var content = require('fs').readFileSync('./test/examples.test.js').toString(); | ||
var blocks = acquit.parse(content); | ||
|
||
var mdOutput = | ||
'# kareem\n\n' + | ||
'Next-generation take on the [hooks](http://npmjs.org/package/hooks), ' + | ||
'meant to offer additional flexibility in allowing you to execute hooks ' + | ||
'whenever necessary, as opposed to simply wrapping a single function.\n\n' + | ||
'Named for the NBA\'s all-time leading scorer Kareem Abdul-Jabbar, known ' + | ||
'for his mastery of the [hook shot](http://en.wikipedia.org/wiki/Kareem_Abdul-Jabbar#Skyhook)\n\n' + | ||
'<img src="http://upload.wikimedia.org/wikipedia/commons/0/00/Kareem-Abdul-Jabbar_Lipofsky.jpg" width="220">\n\n' | ||
'## API\n\n'; | ||
|
||
for (var i = 0; i < blocks.length; ++i) { | ||
var describe = blocks[i]; | ||
mdOutput += '### ' + describe.contents + '\n\n'; | ||
mdOutput += describe.comments[0] ? | ||
trimEachLine(describe.comments[0]) + '\n\n' : | ||
''; | ||
|
||
for (var j = 0; j < describe.blocks.length; ++j) { | ||
var it = describe.blocks[j]; | ||
mdOutput += '##### It ' + it.contents + '\n\n'; | ||
mdOutput += it.comments[0] ? trimEachLine(it.comments[0]) + '\n\n' : ''; | ||
mdOutput += '```javascript\n'; | ||
mdOutput += ' ' + it.code + '\n'; | ||
mdOutput += '```\n\n'; | ||
} | ||
} | ||
|
||
require('fs').writeFileSync('README.md', mdOutput); |