From 05d95f0470a60ecda8194da3daf2a6c7b06faea3 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk <tj@vision-media.ca> Date: Thu, 10 May 2012 10:15:18 -0700 Subject: [PATCH] Added Emitter#has(event) --- Readme.md | 4 ++++ index.js | 6 ++++++ test/emitter.js | 17 +++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Readme.md b/Readme.md index 81a8096..37714b8 100644 --- a/Readme.md +++ b/Readme.md @@ -29,3 +29,7 @@ $ npm install emitter-component ### Emitter#emit(event, ...) Emit an `event` with variable option args. + +### Emitter#has(event) + + Check if this emitter has `event` handlers. \ No newline at end of file diff --git a/index.js b/index.js index 972dd4e..f107e9d 100644 --- a/index.js +++ b/index.js @@ -98,3 +98,9 @@ Emitter.prototype.emit = function(event){ return this; }; + +Emitter.prototype.has = function(event){ + var fns = this.callbacks[event]; + return !!(fns && fns.length); +}; + diff --git a/test/emitter.js b/test/emitter.js index a9ee9b4..8a34df9 100644 --- a/test/emitter.js +++ b/test/emitter.js @@ -77,4 +77,21 @@ describe('Emitter', function(){ calls.should.eql([]); }) }) + + describe('.has(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; + }) + }) + + describe('when no handlers are present', function(){ + it('should return false', function(){ + var emitter = new Emitter; + emitter.has('foo').should.be.false; + }) + }) + }) }) \ No newline at end of file