Skip to content

Commit

Permalink
change .has to .hasListeners
Browse files Browse the repository at this point in the history
as per guillermo request, which makes sense! inheriting
from emitter and then overriding .has with your own thing
could potentially suck
  • Loading branch information
tj committed Jul 12, 2012
1 parent e62db4c commit bcb45a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ Emitter.prototype.emit = function(event){
return this;
};

/**
* Return array of callbacks for `event`.
*
* @param {String} event
* @return {Array}
* @api public
*/

Emitter.prototype.listeners = function(event){
return this.callbacks[event] || [];
};

/**
* Check if this emitter has `event` handlers.
*
Expand All @@ -109,8 +121,7 @@ Emitter.prototype.emit = function(event){
* @api public
*/

Emitter.prototype.has = function(event){
var fns = this.callbacks[event];
return !!(fns && fns.length);
Emitter.prototype.hasListeners = function(event){
return !! this.listeners(event).length;
};

24 changes: 21 additions & 3 deletions test/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,37 @@ describe('Emitter', function(){
})
})

describe('.has(event)', function(){
describe('.listeners(event)', function(){
describe('when handlers are present', function(){
it('should return an array of callbacks', function(){
var emitter = new Emitter;
function foo(){}
emitter.on('foo', foo);
emitter.listeners('foo').should.eql([foo]);
})
})

describe('when no handlers are present', function(){
it('should return an empty array', function(){
var emitter = new Emitter;
emitter.listeners('foo').should.eql([]);
})
})
})

describe('.hasListeners(event)', function(){
describe('when handlers are present', function(){
it('should return true', function(){
var emitter = new Emitter;
emitter.on('foo', function(){});
emitter.has('foo').should.be.true;
emitter.hasListeners('foo').should.be.true;
})
})

describe('when no handlers are present', function(){
it('should return false', function(){
var emitter = new Emitter;
emitter.has('foo').should.be.false;
emitter.hasListeners('foo').should.be.false;
})
})
})
Expand Down

0 comments on commit bcb45a5

Please sign in to comment.