Comparing version 1.3.0 to 1.4.0
42
index.js
@@ -6,7 +6,16 @@ "use strict"; | ||
var extend = require('extend'); | ||
var NORMAL = 1; | ||
var BEFORE = 2; | ||
var AFTER = 3; | ||
var NORMAL = 'NORMAL'; | ||
var BEFORE = 'BEFORE'; | ||
var AFTER = 'AFTER'; | ||
function addHook(type, name, key, fn) { | ||
var attach = function() { | ||
return addHook(type, name, key, fn); | ||
}; | ||
if (typeof key === "function") { | ||
fn = key; | ||
key = undefined; | ||
} | ||
if (Array.isArray(name)) { | ||
@@ -16,15 +25,19 @@ name.forEach(function(n) { | ||
}); | ||
return; | ||
} else { | ||
store.add(type, name, { | ||
fn: fn, | ||
key: key | ||
}); | ||
} | ||
if (typeof key === "function") { | ||
fn = key; | ||
key = undefined; | ||
} | ||
store.add(type, name, { | ||
fn: fn, | ||
key: key | ||
}); | ||
return { | ||
key: key, | ||
run: fn, | ||
name: name, | ||
type: type, | ||
attach: attach | ||
}; | ||
} | ||
exports.register = addHook.bind(this, NORMAL); | ||
exports.on = exports.register = addHook.bind(this, NORMAL); | ||
@@ -115,5 +128,4 @@ exports.before = addHook.bind(this, BEFORE); | ||
exports.___clearHooks = function(name) { | ||
exports.clear = exports.___clearHooks = function(name) { | ||
store.clear(name); | ||
}; | ||
{ | ||
"name": "pudge", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "simple node.js hook utility", | ||
@@ -25,9 +25,12 @@ "main": "index.js", | ||
"dependencies": { | ||
"bluebird": "2.10.2", | ||
"extend": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "2.3.3", | ||
"sinon": "1.17.1" | ||
"mocha": "2.3.4", | ||
"sinon": "1.17.2", | ||
"bluebird": "3.1.1" | ||
}, | ||
"peerDependencies": { | ||
"bluebird": "*" | ||
} | ||
} |
53
test.js
@@ -120,4 +120,5 @@ "use strict"; | ||
var endTime = Date.now(); | ||
assert(endTime > startTime + 80, 'should be greater than 80ms'); | ||
assert(endTime < startTime + 100, 'should be less than 100ms'); | ||
var diff = endTime - startTime; | ||
assert(endTime >= startTime + 80, 'should be greater than 80ms -> ' + diff); | ||
assert(endTime < startTime + 100, 'should be less than 100ms -> ' + diff); | ||
done(); | ||
@@ -147,2 +148,38 @@ }).catch(done); | ||
describe('metadata', function() { | ||
it('should register and return without key', function() { | ||
var awesomeFn = function() {}; | ||
var meta = pudge.register('OLA', awesomeFn); | ||
assert(meta.run === awesomeFn, 'run should be passed back'); | ||
assert(meta.key === undefined, 'key should be undefined'); | ||
assert(meta.name === "OLA", 'name should be defined'); | ||
}); | ||
it('should register and return with key', function() { | ||
var awesomeFn = function() {}; | ||
var meta = pudge.register('OLA', 'name', awesomeFn); | ||
assert(meta.run === awesomeFn, 'run should be passed back'); | ||
assert(meta.key === 'name', 'key should be undefined'); | ||
assert(meta.name === "OLA", 'name should be defined'); | ||
}); | ||
it('should return NORMAL type for register', function() { | ||
var awesomeFn = function() {}; | ||
var meta = pudge.register('OLA', 'name', awesomeFn); | ||
assert(meta.type === "NORMAL"); | ||
}); | ||
it('should return BEFORE type for before', function() { | ||
var awesomeFn = function() {}; | ||
var meta = pudge.before('OLA', 'name', awesomeFn); | ||
assert(meta.type === "BEFORE"); | ||
}); | ||
it('should return AFTER type for after', function() { | ||
var awesomeFn = function() {}; | ||
var meta = pudge.after('OLA', 'name', awesomeFn); | ||
assert(meta.type === "AFTER"); | ||
}); | ||
}); | ||
describe('store', function() { | ||
@@ -160,2 +197,14 @@ beforeEach(function() { | ||
it('should attach it\'self after being cleared', function(done) { | ||
var hook = pudge.register('EAT_SOMETHING', function(result) { | ||
assert(result === 'ok'); | ||
done(); | ||
}); | ||
store.clear(); | ||
assert(store.get('NORMAL', 'EAT_SOMETHING').length === 0, 'have length of 0'); | ||
hook.attach(); | ||
assert(store.get('NORMAL', 'EAT_SOMETHING').length === 1, 'have length of 1'); | ||
pudge.run('EAT_SOMETHING', 'ok'); | ||
}); | ||
}); |
14113
313
3
+ Addedbluebird@3.7.2(transitive)
- Removedbluebird@2.10.2
- Removedbluebird@2.10.2(transitive)