modify-event
Advanced tools
Comparing version 2.0.0 to 2.0.1
57
index.js
@@ -1,5 +0,1 @@ | ||
/*! | ||
* modify-event | MIT (c) Shinnosuke Watanabe | ||
* https://github.com/shinnn/modify-event | ||
*/ | ||
'use strict'; | ||
@@ -10,36 +6,33 @@ | ||
module.exports = function modifyEvent(eventEmitter, targetEventName, fn) { | ||
if (!eventEmitter || typeof eventEmitter.emit !== 'function') { | ||
throw new TypeError( | ||
util.inspect(eventEmitter) + | ||
' doesn\'t have "emit" method.' + | ||
' The first argument to modify-event must be an instance of EventEmitter' + | ||
' or its inheritance.' | ||
); | ||
} | ||
if (!eventEmitter || typeof eventEmitter.emit !== 'function') { | ||
throw new TypeError(`${ | ||
util.inspect(eventEmitter) | ||
} doesn't have "emit" method. The first argument to modify-event must be an EventEmitter.`); | ||
} | ||
if (typeof targetEventName !== 'string') { | ||
throw new TypeError( | ||
util.inspect(targetEventName) + | ||
' is not a string. The second argument to modify-event must be an event name.' | ||
); | ||
} | ||
const targetEventNameType = typeof targetEventName; | ||
if (typeof fn !== 'function') { | ||
throw new TypeError( | ||
util.inspect(fn) + | ||
' is not a function. The third argument to modify-event must be a function.' | ||
); | ||
} | ||
if (targetEventNameType !== 'string' && targetEventNameType !== 'symbol') { | ||
throw new TypeError(`${ | ||
util.inspect(targetEventName) | ||
} is neither a string nor a symbol. The second argument to modify-event must be an event name.`); | ||
} | ||
const originalEmit = eventEmitter.emit.bind(eventEmitter); | ||
if (typeof fn !== 'function') { | ||
throw new TypeError(`${ | ||
util.inspect(fn) | ||
} is not a function. The third argument to modify-event must be a function.`); | ||
} | ||
eventEmitter.emit = function modifiedEmit(eventName, val) { | ||
if (eventName === targetEventName) { | ||
val = fn(val); | ||
} | ||
const originalEmit = eventEmitter.emit.bind(eventEmitter); | ||
return originalEmit(eventName, val); | ||
}; | ||
eventEmitter.emit = function modifiedEmit(eventName, val) { | ||
if (eventName === targetEventName) { | ||
val = fn(val); | ||
} | ||
return eventEmitter; | ||
return originalEmit(eventName, val); | ||
}; | ||
return eventEmitter; | ||
}; |
{ | ||
"name": "modify-event", | ||
"version": "2.0.0", | ||
"description": "Modify the value of the specific object's event", | ||
"repository": "shinnn/modify-event", | ||
"author": "Shinnosuke Watanabe <snnskwtnb@gmail.com> (https://github.com/shinnn)", | ||
"scripts": { | ||
"pretest": "eslint --fix --config @shinnn/node index.js test.js", | ||
"test": "node --strong_mode --throw-deprecation --track-heap-objects test.js | tap-spec", | ||
"coverage": "node --strong_mode --throw-deprecation --track-heap-objects node_modules/.bin/istanbul cover test.js" | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"modify", | ||
"value", | ||
"argument", | ||
"event", | ||
"emit", | ||
"change", | ||
"filter", | ||
"filtering", | ||
"update" | ||
], | ||
"devDependencies": { | ||
"@shinnn/eslint-config-node": "^2.0.0", | ||
"eslint": "^2.9.0", | ||
"istanbul": "^0.4.3", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.5.1" | ||
} | ||
"name": "modify-event", | ||
"version": "2.0.1", | ||
"description": "Modify the value of the specific object's event", | ||
"author": "Shinnosuke Watanabe (https://github.com/shinnn)", | ||
"repository": "shinnn/modify-event", | ||
"license": "MIT", | ||
"scripts": { | ||
"pretest": "eslint .", | ||
"test": "nyc node test.js" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"modify", | ||
"value", | ||
"argument", | ||
"event", | ||
"emit", | ||
"change", | ||
"filter", | ||
"filtering", | ||
"update" | ||
], | ||
"devDependencies": { | ||
"@shinnn/eslint-config": "^6.8.9", | ||
"eslint": "^5.12.0", | ||
"nyc": "^13.1.0", | ||
"nyc-config-common": "^1.0.1", | ||
"tape": "^4.9.2" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@shinnn" | ||
}, | ||
"nyc": { | ||
"extends": "nyc-config-common" | ||
} | ||
} |
# 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.org/shinnn/modify-event.svg?branch=master)](https://travis-ci.org/shinnn/modify-event) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/4noc4o9y40atakp3?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/modify-event) | ||
[![Coverage Status](https://img.shields.io/coveralls/shinnn/modify-event.svg)](https://coveralls.io/r/shinnn/modify-event) | ||
[![devDependency Status](https://david-dm.org/shinnn/modify-event/dev-status.svg)](https://david-dm.org/shinnn/modify-event#info=devDependencies) | ||
[![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) | ||
@@ -28,3 +26,3 @@ Modify the value of the specific object's [event](https://nodejs.org/api/events.html) | ||
[Use npm.](https://docs.npmjs.com/cli/install) | ||
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/). | ||
@@ -43,6 +41,6 @@ ``` | ||
*eventEmitter*: `Object` (an instance of [`EventEmitter`](https://nodejs.org/api/events.html#events_class_events_eventemitter) or its inheritance e.g. [`Stream`](https://nodejs.org/api/stream.html#stream_stream)) | ||
*eventName*: `String` (event name) | ||
*eventEmitter*: [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) | ||
*eventName*: `string` `symbol` (event name) | ||
*modifier*: `Function` | ||
Return: `Object` (Same as the first argument) | ||
Return: `EventEmitter` (a reference to the first argument) | ||
@@ -56,9 +54,10 @@ It changes the first argument of the event listeners for a given event, in response to the return value of the *modifier* function. | ||
const emitter = new EventEmitter(); | ||
const eventName = Symbol('custom event name'); | ||
modifyEvent(emitter, 'data', val => val + 'b'); | ||
modifyEvent(emitter, 'data', val => val + 'c'); | ||
modifyEvent(emitter, eventName, val => `${val}b`); | ||
modifyEvent(emitter, eventName, val => `${val}c`); | ||
emitter | ||
.on('data', listener) | ||
.emit('data', 'a'); | ||
.on(eventName, listener) | ||
.emit(eventName, 'a'); | ||
@@ -72,4 +71,4 @@ function listener(data) { | ||
Copyright (c) 2015 - 2016 [Shinnosuke Watanabe](https://github.com/shinnn) | ||
Copyright (c) 2015 - 2019 [Shinnosuke Watanabe](https://github.com/shinnn) | ||
Licensed under [the MIT License](./LICENSE). |
Sorry, the diff of this file is not supported yet
4741
28
71