Skip to content

Commit

Permalink
Merge pull request #53 from primus/eventNames
Browse files Browse the repository at this point in the history
Add `eventNames` method
  • Loading branch information
3rd-Eden committed Mar 16, 2016
2 parents 4bab525 + 0df1c92 commit d4161d0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 11 deletions.
29 changes: 28 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var has = Object.prototype.hasOwnProperty;

//
// We store our EE objects in a plain object whose properties are event names.
// If `Object.create(null)` is not supported we prefix the event names with a
Expand Down Expand Up @@ -34,13 +36,38 @@ function EE(fn, context, once) {
function EventEmitter() { /* Nothing to set */ }

/**
* Holds the assigned EventEmitters by name.
* Hold the assigned EventEmitters by name.
*
* @type {Object}
* @private
*/
EventEmitter.prototype._events = undefined;

/**
* Return an array listing the events for which the emitter has registered
* listeners.
*
* @returns {Array}
* @api public
*/
EventEmitter.prototype.eventNames = function eventNames() {
var events = this._events
, names = []
, name;

if (!events) return names;

for (name in events) {
if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
}

if (Object.getOwnPropertySymbols) {
return names.concat(Object.getOwnPropertySymbols(events));
}

return names;
};

/**
* Return a list of assigned event listeners.
*
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"devDependencies": {
"assume": "1.3.x",
"istanbul": "0.4.x",
"mocha": "2.3.x",
"mocha": "2.4.x",
"pre-commit": "1.1.x",
"zuul": "3.9.x"
"zuul": "3.10.x"
}
}
76 changes: 68 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('EventEmitter', function tests() {
it('can emit the function with multiple arguments', function () {
var e = new EventEmitter();

for(var i = 0; i < 100; i++) {
for (var i = 0; i < 100; i++) {
(function (j) {
for (var i = 0, args = []; i < j; i++) {
args.push(j);
Expand Down Expand Up @@ -253,14 +253,14 @@ describe('EventEmitter', function tests() {
});

it('returns an array of function', function () {
var e = new EventEmitter();
var e = new EventEmitter();

function foo() {}
function foo() {}

e.on('foo', foo);
assume(e.listeners('foo')).is.a('array');
assume(e.listeners('foo').length).equals(1);
assume(e.listeners('foo')).deep.equals([foo]);
e.on('foo', foo);
assume(e.listeners('foo')).is.a('array');
assume(e.listeners('foo').length).equals(1);
assume(e.listeners('foo')).deep.equals([foo]);
});

it('is not vulnerable to modifications', function () {
Expand Down Expand Up @@ -502,7 +502,67 @@ describe('EventEmitter', function tests() {
});
});

describe('#setMaxListeners', function () {
describe('EventEmitter#eventNames', function () {
it('returns an empty array when there are no events', function () {
var e = new EventEmitter();

assume(e.eventNames()).eql([]);

e.on('foo', function () {});
e.removeAllListeners('foo');

assume(e.eventNames()).eql([]);
});

it('returns an array listing the events that have listeners', function () {
var e = new EventEmitter();

function bar() {}

e.on('foo', function () {});
e.on('bar', bar);

assume(e.eventNames()).eql(['foo', 'bar']);

e.removeListener('bar', bar);

assume(e.eventNames()).eql(['foo']);
});

it('does not return inherited properties', function () {
var e = new EventEmitter();

function Dummy() {}
function Collection() {}

Dummy.prototype.foo = 'foo';
Collection.prototype = new Dummy();
Collection.prototype.constructor = Collection;

e._events = new Collection();

assume(e._events.foo).equal('foo');
assume(e.eventNames()).eql([]);
});

if ('undefined' !== typeof Symbol) it('includes ES6 symbols', function () {
var e = new EventEmitter()
, s = Symbol('s');

function foo() {}

e.on('foo', foo);
e.on(s, function () {});

assume(e.eventNames()).eql(['foo', s]);

e.removeListener('foo', foo);

assume(e.eventNames()).eql([s]);
});
});

describe('EventEmitter#setMaxListeners', function () {
it('is a function', function () {
var e = new EventEmitter();

Expand Down

0 comments on commit d4161d0

Please sign in to comment.