Commit c368d425 authored by isaacs's avatar isaacs

Add support for a map

Need to run some tests that emit very large buffers, don't wanna blow up
the jsons

Also: update tap
parent f7760870
......@@ -46,9 +46,20 @@ assert.deepEqual(array,
[ 'blaz', 'blorrg' ] ])
```
## `eventsToArray(emitter, [ignoreList])`
## `eventsToArray(emitter, [ignoreList], [mapFunction])`
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.
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 = []
ee.emit = (function (orig) {
......@@ -16,7 +17,7 @@ function eventsToArray (ee, ignore) {
eventsToArray(arg, ignore) :
arg
}
array.push(args)
array.push(map(args))
}
return orig.apply(this, arguments)
......
......@@ -8,10 +8,10 @@
},
"dependencies": {},
"devDependencies": {
"tap": "^1.2.0"
"tap": "^10.3.2"
},
"scripts": {
"test": "tap test/*.js"
"test": "tap test/*.js --100"
},
"repository": {
"type": "git",
......
......@@ -32,3 +32,29 @@ test('basic', function (t) {
[ 'blaz', 'blorrg' ] ])
t.end()
})
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 (args) {
return args.map(function (arg) { return arg + '' })
})
emitter.emit('foo', new Buffer('hello'))
t.same(array, [ ['foo', 'hello' ] ])
t.end()
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment