diff --git a/index.js b/index.js index 883daa3..a177ab7 100644 --- a/index.js +++ b/index.js @@ -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. * @@ -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; }; diff --git a/test/emitter.js b/test/emitter.js index 54a7de6..94a1899 100644 --- a/test/emitter.js +++ b/test/emitter.js @@ -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; }) }) })