Modify the value of the specific object's event
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitattributes
.gitignore
.travis.yml
LICENSE
README.md
index.js
package-lock.json
package.json
test.js

README.md

modify-event

NPM version Build Status Coverage Status devDependency Status

Modify the value of the specific object's event

const {EventEmitter} = require('events');
const modifyEvent = require('modify-event');

const emitter = new EventEmitter();

modifyEvent(emitter, 'foo', val => val * 2);

emitter.on('foo', data => {
  data; //=> 2
});

emitter.emit('foo', 1);

Installation

Use npm.

npm install modify-event

API

const modifyEvent = require('modify-event');

modifyEvent(eventEmitter, eventName, modifier)

eventEmitter: Object (an instance of EventEmitter or its inheritance e.g. Stream)
eventName: String (event name)
modifier: Function
Return: Object (Same as the first argument)

It changes the first argument of the event listeners for a given event, in response to the return value of the modifier function.

const {EventEmitter} = require('events');
const modifyEvent = require('modify-event');

const emitter = new EventEmitter();

modifyEvent(emitter, 'data', val => val + 'b');
modifyEvent(emitter, 'data', val => val + 'c');

emitter
.on('data', listener)
.emit('data', 'a');

function listener(data) {
  data; //=> 'abc'
}

License

Copyright (c) 2015 - 2017 Shinnosuke Watanabe

Licensed under the MIT License.