Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/modify-event
Modify the value of the specific object's event
https://github.com/shinnn/modify-event
Last synced: 26 days ago
JSON representation
Modify the value of the specific object's event
- Host: GitHub
- URL: https://github.com/shinnn/modify-event
- Owner: shinnn
- License: mit
- Created: 2014-04-20T05:56:47.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-01-10T01:47:40.000Z (almost 6 years ago)
- Last Synced: 2024-10-13T03:11:00.635Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 89.8 KB
- Stars: 6
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# modify-event
[![npm version](https://img.shields.io/npm/v/modify-event.svg)](https://www.npmjs.com/package/modify-event)
[![Build Status](https://travis-ci.com/shinnn/modify-event.svg?branch=master)](https://travis-ci.com/shinnn/modify-event)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/modify-event.svg)](https://coveralls.io/github/shinnn/modify-event)Modify the value of the specific object's [event](https://nodejs.org/api/events.html)
```javascript
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](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install modify-event
```## API
```javascript
const modifyEvent = require('modify-event');
```### modifyEvent(*eventEmitter*, *eventName*, *modifier*)
*eventEmitter*: [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter)
*eventName*: `string` `symbol` (event name)
*modifier*: `Function`
Return: `EventEmitter` (a reference to 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.
```javascript
const {EventEmitter} = require('events');
const modifyEvent = require('modify-event');const emitter = new EventEmitter();
const eventName = Symbol('custom event name');modifyEvent(emitter, eventName, val => `${val}b`);
modifyEvent(emitter, eventName, val => `${val}c`);emitter
.on(eventName, listener)
.emit(eventName, 'a');function listener(data) {
data; //=> 'abc'
}
```## License
Copyright (c) 2015 - 2019 [Shinnosuke Watanabe](https://github.com/shinnn)
Licensed under [the MIT License](./LICENSE).