Skip to content

Commit

Permalink
refactor: use es6 arrow functions, const, let
Browse files Browse the repository at this point in the history
BREAKING CHANGES:
- `const`, `let` not supported in node < 6
- array instead of array-like object should be used for `NewsEmitter#addHistory()`
fent committed Sep 23, 2018
1 parent 24cf7a1 commit 0a006c9
Showing 4 changed files with 58 additions and 66 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -33,11 +33,11 @@ news.emit('item', { title: 'hey' });

Creates an instance of a NewsEmitter. `options` can be

* `filter` - An array of events that will be affected. If not given, events names will not be filtered.
* `filter` - An array of events that will be affected. If not given, event names will not be filtered.
* `ignore` - An array of events that will be ignored. Defaults to `['newListener']`.
* `maxHistory` - Maximum number of history items to remember. Default is `10`.
* `manageHistory` - If true, does not add emitted events to history. Instead expects you to manually manage history with `addHistory()`. Defaults to `false`.
* `identifier` - Function used to compare one event to another. Takes 1 arguments, the array-like `arguments` that `NewsEmitter#emit()` is called with. Should return a string. Default is `JSON.stringify`.
* `identifier` - Function used to compare one event to another. Takes 1 argument, the array of arguments that `NewsEmitter#emit()` is called with. Should return a string. Default is `JSON.stringify`.

### NewsEmitter#emit(event)

55 changes: 26 additions & 29 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ module.exports = class NewsEmitter extends EventEmitter {
/**
* Emits only new events.
* @param (Object) options
* @param {Object} options
* @constructor
*/
constructor(options) {
@@ -14,60 +14,57 @@ module.exports = class NewsEmitter extends EventEmitter {
this.historyMap = {};

// Set default options.
this.options = {
this.options = Object.assign({
filter: null,
ignore: ['newListener'],
maxHistory: 10,
manageHistory: false,
identifier: JSON.stringify
};

options = options || {};
Object.assign(this.options, options);
}, options);

// Validate options.
if (this.options.filter !== null &&
!Array.isArray(this.options.filter)) {
throw new Error('options.filter must be an array if given');
throw Error('options.filter must be an array if given');
}

if (!Array.isArray(this.options.ignore)) {
throw new Error('options.ignore must be an array');
throw Error('options.ignore must be an array');
}

if (typeof this.options.maxHistory !== 'number' ||
this.options.maxHistory < 0) {
throw new Error('options.maxHistory must be a positive integer');
throw Error('options.maxHistory must be a positive integer');
}

if (typeof this.options.identifier !== 'function') {
throw new Error('options.identifier must be a function');
throw Error('options.identifier must be a function');
}
}


/**
* Emits event, only if not already in history.
*
* @param (String) event
* @param (Object) params...
* @return (Boolean) Wether or not event was emitted
* @param {string} event
* @param {Object} ...args
* @return {boolean} Wether or not event was emitted
*/
emit(event) {
emit(event, ...args) {
if (Array.isArray(this.options.filter) &&
this.options.filter.indexOf(event) === -1 ||
this.options.ignore.indexOf(event) !== -1) {
super.emit.apply(this, arguments);
super.emit(event, ...args);
return true;
}

var tistory = this.history[event] || [];
var tistoryMap = this.historyMap[event] || {};
var key = this.options.identifier(arguments);
var found = tistoryMap[key] !== undefined;
const tistory = this.history[event] || [];
const tistoryMap = this.historyMap[event] || {};
const key = this.options.identifier([event].concat(args));
const found = tistoryMap[key] !== undefined;

// Add event to history and truncate history.
var auto = !this.options.manageHistory;
const auto = !this.options.manageHistory;
if (auto) {
if (found) {
// Remove event from the list of already there.
@@ -91,7 +88,7 @@ module.exports = class NewsEmitter extends EventEmitter {

} else {
// If not found in history, this is news.
super.emit.apply(this, arguments);
super.emit(event, ...args);
return true;
}
}
@@ -100,7 +97,7 @@ module.exports = class NewsEmitter extends EventEmitter {
/**
* Resets event history.
*
* @param (String?) event
* @param {!String} event
*/
reset(event) {
if (event) {
@@ -116,16 +113,16 @@ module.exports = class NewsEmitter extends EventEmitter {
/**
* Manual managing of event history.
*
* @param (String) event
* @param (Array.<Object>) arr An array of items to add to history.
* @param {string} event
* @param {Array.<Object>} arr An array of items to add to history.
*/
addHistory(event, arr) {
var tistory = this.history[event] || [];
var tistoryMap = this.historyMap[event] || {};
const tistory = this.history[event] || [];
const tistoryMap = this.historyMap[event] || {};

for (var i = 0, len = arr.length; i < len; i++) {
var key = this.options.identifier(arr[i]);
var found = tistoryMap[key] !== undefined;
for (let item of arr) {
const key = this.options.identifier(item);
const found = tistoryMap[key] !== undefined;
if (found) {
tistory.splice(tistoryMap[key], 1);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@
"mocha": "^5.1.0"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"license": "MIT"
}
63 changes: 29 additions & 34 deletions test/main-test.js
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@ const assert = require('assert');


describe('NEWS Emitter', () => {
var news = new NewsEmitter();
var results1 = [];
var results2 = [];
const news = new NewsEmitter();
const results1 = [];
const results2 = [];

news.on('foo', (item) => {
results1.push(item);
@@ -23,12 +23,11 @@ describe('NEWS Emitter', () => {
assert.ok(!news.emit('foo', { title: 'hey' }));
assert.ok(!news.emit('foo', { title: 'hey' }));

var expected = [
assert.deepEqual(results1, [
{ title: 'hello there' },
{ title: 'hello world' },
{ title: 'hey' }
];
assert.deepEqual(results1, expected);
]);
});

it('Keeps different history for each event', () => {
@@ -39,21 +38,20 @@ describe('NEWS Emitter', () => {
assert.ok(news.emit('bar', { title: 'hello world' }));
assert.ok(!news.emit('bar', { title: 'hello world' }));

var expected = [
assert.deepEqual(results2, [
{ so: 'lucky' },
{ so: { so: 'lucky' } },
{ title: 'hello world' }
];
assert.deepEqual(results2, expected);
]);
});
});


describe('Filter events', () => {
var news = new NewsEmitter({ filter: ['live', 'forever'] });
var results1 = [];
var results2 = [];
var results3 = [];
const news = new NewsEmitter({ filter: ['live', 'forever'] });
const results1 = [];
const results2 = [];
const results3 = [];

news.on('live', (a, b, c) => {
results1.push([a, b, c]);
@@ -81,32 +79,29 @@ describe('Filter events', () => {
news.emit('forever', 'a', 'b');
news.emit('forever', 1, 2, 3);

var expected1 = [
assert.deepEqual(results1, [
['a', 'b', undefined],
['a', 'b', 'c'],
['foo', undefined, undefined]
];
assert.deepEqual(results1, expected1);
]);

var expected2 = [
assert.deepEqual(results2, [
['a', 'b', undefined],
[1, 2, 3]
];
assert.deepEqual(results2, expected2);
]);

var expected3 = ['foo', 'foo', 'foo'];
assert.deepEqual(results3, expected3);
assert.deepEqual(results3, ['foo', 'foo', 'foo']);
});
});


describe('Custom identifier', () => {
var news = new NewsEmitter({
const news = new NewsEmitter({
history: 50,
identifier: a => /^foo/.test(a[1]),
});
var results1 = [];
var results2 = [];
const results1 = [];
const results2 = [];

news.on('foo', (item) => {
results1.push(item);
@@ -136,17 +131,17 @@ describe('Custom identifier', () => {


describe('Self manage history', () => {
var news = new NewsEmitter();
var results1 = [];
it('Is able to compare to history items we give it', () => {
const news = new NewsEmitter();
const results1 = [];

news.on('foo', (a) => {
results1.push(a);
});
news.on('foo', (a) => {
results1.push(a);
});

it('Is able to compare to history items we give it', () => {
news.addHistory('foo', [
{ 0: 'foo', 1: 'hello' },
{ 0: 'foo', 1: 'hello world' }
['foo', 'hello'],
['foo', 'hello world']
]);

news.emit('foo', 'a');
@@ -157,8 +152,8 @@ describe('Self manage history', () => {
assert.deepEqual(results1, ['a']);

news.addHistory('foo', [
{ 0: 'foo', 1: 'b' },
{ 0: 'foo', 1: 'c' }
['foo', 'b'],
['foo', 'c']
]);

news.emit('foo', 'a');

0 comments on commit 0a006c9

Please sign in to comment.