Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Apr 26, 2015
0 parents commit e9ca5b2
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
git:
depth: 2
language: node_js
node_js:
- iojs
- node
after_script:
- npm run-script coveralls
notifications:
email: false
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Shinnosuke Watanabe

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# modify-error-event

[![NPM version](https://img.shields.io/npm/v/modify-error-event.svg)](https://www.npmjs.com/package/modify-error-event)
[![Build Status](https://travis-ci.org/shinnn/modify-error-event.svg?branch=master)](https://travis-ci.org/shinnn/modify-error-event)
[![Build status](https://ci.appveyor.com/api/projects/status/s8nn7eflwuqml299?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/modify-error-event)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/modify-error-event.svg)](https://coveralls.io/r/shinnn/modify-error-event)
[![Dependency Status](https://img.shields.io/david/shinnn/modify-error-event.svg?label=deps)](https://david-dm.org/shinnn/modify-error-event)
[![devDependency Status](https://img.shields.io/david/dev/shinnn/modify-error-event.svg?label=devDeps)](https://david-dm.org/shinnn/modify-error-event#info=devDependencies)

Modify the value of the specific object's `error` [event](https://nodejs.org/api/events.html)

```javascript
var EventEmitter = require('events').EventEmitter;
var modifyErrorEvent = require('modify-error-event');

var emitter = new EventEmitter();

modifyErrorEvent(emitter, function(err) {
err.message = 'bar';
return err;
});

emitter.on('error', function(err) {
err.message; //=> 'bar'
});

emitter.emit('error', new Error('foo'));
```

## Installation

[Use npm.](https://docs.npmjs.com/cli/install)

```
npm install modify-error-event
```

## API

```javascript
var modifyErrorEvent = require('modify-error-event');
```

### modifyErrorEvent(*eventEmitter*, *modifier*)

*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))
*modifier*: `Function`
Return: `Object` (Same as the first argument)

It changes the first argument of the `error` event listeners in response to the return value of the *modifier* function.

```javascript
var EventEmitter = require('events').EventEmitter;
var modifyErrorEvent = require('modify-error-event');

var emitter = new EventEmitter();

modifyErrorEvent(emitter, function(err) {
err.message += 'b';
return err;
});

modifyErrorEvent(emitter, function(val) {
err.message += 'c';
return err;
});

emitter
.on('error', listener)
.emit('error', new Error('a'));

function listener(err) {
err.message; //=> 'abc'
}
```

## License

Copyright (c) 2015 [Shinnosuke Watanabe](https://github.com/shinnn)

Licensed under [the MIT License](./LICENSE).
27 changes: 27 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
init:
- git config --global core.autocrlf input

shallow_clone: true

version: '{build}'

skip_tags: true

environment:
matrix:
- nodejs_version: '1'
- nodejs_version: '0'

install:
- ps: Install-Product node $env:nodejs_version
- npm install --production
- npm install tape

build: off

test_script:
- ps: 'node test.js #PowerShell'
- cmd: node test.js

cache:
- node_modules
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*!
* modify-error-event | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/modify-error-event
*/
'use strict';

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

module.exports = function modifyErrorEvent(eventEmitter, fn) {
return modifyEvent(eventEmitter, 'error', fn);
};
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "modify-error-event",
"version": "1.0.0",
"description": "Modify the value of the specific object's error event",
"repository": "shinnn/modify-error-event",
"scripts": {
"pretest": "eslint --config node_modules/@shinnn/eslintrc-node/rc.json index.js test.js",
"test": "node test.js",
"coverage": "istanbul cover test.js",
"coveralls": "${npm_package_scripts_coverage} && istanbul-coveralls"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/shinnn/modify-error-event/blob/master/LICENSE"
}
],
"files": [
"index.js"
],
"keywords": [
"modify",
"value",
"argument",
"event",
"emit",
"error",
"change",
"update"
],
"dependencies": {
"modify-event": "^1.0.0"
},
"devDependencies": {
"@shinnn/eslintrc-node": "^1.0.1",
"eslint": "^0.20.0",
"istanbul": "^0.3.13",
"istanbul-coveralls": "^1.0.2",
"tape": "^4.0.0"
}
}
47 changes: 47 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

var EventEmitter = require('events').EventEmitter;

var modifyErrorEvent = require('./');
var test = require('tape');

var emitter = new EventEmitter();

test('modifyErrorEvent()', function(t) {
t.plan(7);

t.equal(modifyErrorEvent.name, 'modifyErrorEvent', 'should have a function name.');

t.strictEqual(
modifyErrorEvent(emitter, function(data) {
return data * 2;
}),
emitter,
'should return the modified EventEmitter.'
);

emitter
.on('error', function(data) {
t.strictEqual(this, emitter, 'should call the listener in the same context as original\'s.');
t.strictEqual(data, 2, 'should modify the value of event.');
})
.emit('error', 1);

t.throws(
modifyErrorEvent.bind(null, null, t.fail),
/TypeError.* must be an instance of EventEmitter or its inheritance\./,
'should throw a type error when the first argument is not an object.'
);

t.throws(
modifyErrorEvent.bind(null, {}, t.fail),
/TypeError.* must be an instance of EventEmitter or its inheritance\./,
'should throw a type error when the first argument is not an instance of EventEmitter.'
);

t.throws(
modifyErrorEvent.bind(null, emitter, 'foo'),
/TypeError.*foo is not a function. The third argument to modify-event must be a function\./,
'should throw a type error when the third argument is not a function.'
);
});

0 comments on commit e9ca5b2

Please sign in to comment.