events-to-array
Advanced tools
Comparing version 1.0.2 to 1.1.2
15
etoa.js
module.exports = eventsToArray | ||
var EE = require('events').EventEmitter | ||
function eventsToArray (ee, ignore) { | ||
function eventsToArray (ee, ignore, map) { | ||
ignore = ignore || [] | ||
map = map || function (x) { return x } | ||
var array = [] | ||
@@ -13,8 +14,14 @@ | ||
var args = new Array(l) | ||
// intentionally sparse array | ||
var swap = [] | ||
for (var i = 0; i < l; i++) { | ||
var arg = arguments[i] | ||
args[i] = (arg instanceof EE) ? | ||
eventsToArray(arg, ignore) : | ||
arg | ||
args[i] = arguments[i] | ||
if (arg instanceof EE) | ||
swap[i] = eventsToArray(arg, ignore, map) | ||
} | ||
args = args.map(map) | ||
args = args.map(function (arg, index) { | ||
return swap[index] || arg | ||
}) | ||
array.push(args) | ||
@@ -21,0 +28,0 @@ } |
{ | ||
"name": "events-to-array", | ||
"version": "1.0.2", | ||
"version": "1.1.2", | ||
"description": "Put a bunch of emitted events in an array, for testing.", | ||
@@ -11,6 +11,9 @@ "main": "etoa.js", | ||
"devDependencies": { | ||
"tap": "^0.7.1" | ||
"tap": "^10.3.2" | ||
}, | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
"test": "tap test/*.js --100", | ||
"preversion": "npm test", | ||
"postversion": "npm publish", | ||
"postpublish": "git push origin --all; git push origin --tags" | ||
}, | ||
@@ -17,0 +20,0 @@ "repository": { |
@@ -49,3 +49,3 @@ # events-to-array | ||
## `eventsToArray(emitter, [ignoreList])` | ||
## `eventsToArray(emitter, [ignoreList], [mapFunction])` | ||
@@ -55,2 +55,15 @@ Returns an array with all the events emitted by the emitter. | ||
It's your responsibility to know when to check it for the events that | ||
you expect. | ||
you expected to have received. | ||
The `ignoreList` is an array of event names to ignore. | ||
The `mapFunction` is a function that takes a list of arguments and | ||
returns a potentially-mutated array of arguments. Note that child | ||
event emitters will already have been swapped out for an | ||
events-to-array list so that nested events are caught. | ||
This is handy, for example, for swapping out large `Buffer` objects | ||
with something like `{type: 'buffer', length: 123456}` rather than | ||
blow up the JSON fixtures. | ||
The map function is called on the args list as `map(arg, index, list)` |
@@ -35,1 +35,32 @@ var test = require('tap').test | ||
}) | ||
test('ignore nothing', function (t) { | ||
var emitter = new EE() | ||
var array = etoa(emitter) | ||
emitter.emit('foo', 1, 2, 3) | ||
emitter.emit('ignore', 'should see this') | ||
emitter.emit('bar', { x: 1 }) | ||
t.same(array, | ||
[ [ 'foo', 1, 2, 3 ], | ||
[ 'ignore', 'should see this' ], | ||
[ 'bar', { x: 1 } ] ]) | ||
t.end() | ||
}) | ||
test('the map is not the territory', function (t) { | ||
var emitter = new EE() | ||
// cast all to strings | ||
var array = etoa(emitter, ['ignore'], function (arg) { | ||
return arg + '' | ||
}) | ||
emitter.emit('foo', new Buffer('hello')) | ||
var sub = new EE() | ||
emitter.emit('sub', sub) | ||
sub.emit('obj', { toString: function () { return 'toString fn' } }) | ||
t.same(array, | ||
[ ['foo', 'hello' ], | ||
[ 'sub', [ [ 'obj', 'toString fn' ] ] ] ]) | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47779
16
501
68
1