Commit c9edf4e0 authored by isaacs's avatar isaacs

call map() with args.map, not map(args)

parent e10b12d6
......@@ -63,3 +63,5 @@ 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)`
......@@ -11,13 +11,19 @@ function eventsToArray (ee, ignore, map) {
if (ignore.indexOf(ev) === -1) {
var l = arguments.length
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)
}
array.push(map(args))
args = args.map(map)
args = args.map(function (arg, index) {
return swap[index] || arg
})
array.push(args)
}
return orig.apply(this, arguments)
......
......@@ -50,11 +50,16 @@ test('ignore nothing', function (t) {
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 + '' })
var array = etoa(emitter, ['ignore'], function (arg) {
return arg + ''
})
emitter.emit('foo', new Buffer('hello'))
t.same(array, [ ['foo', '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()
})
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