Upgrading to GitLab 11.10.0. Expect errors and see debian-infrastructure-announce@lists.debian.org for further information.

Commit d73f5c06 authored by Bastien ROUCARIÈS's avatar Bastien ROUCARIÈS

Add 'module-events-to-array/' from commit '00039bdb'

git-subtree-dir: module-events-to-array
git-subtree-mainline: 9ff67edd
git-subtree-split: 00039bdb
parents 9ff67edd 00039bdb
language: node_js
before_script: npm install -g npm@latest
node_js:
- '0.8'
- '0.10'
- '0.12'
- 'iojs'
The ISC License
Copyright (c) Isaac Z. Schlueter and Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# events-to-array
Put a bunch of emitted events in an array, for testing.
If any of the emitted arguments are event emitters, then they'll also
be tracked, and replaced in the array with their tracking array.
(This is less confusing in practice than it sounds in text, see
below.) The only caveat is that the events in the child event emitter
and in the parent are not preserved in order, so this lib doesn't tell
you whether the child events happened before or after any subsequent
parent events.
## USAGE
```
var assert = require('assert')
var EE = require('events')
var etoa = require('events-to-array')
var emitter = new EE()
var array = etoa(emitter)
emitter.emit('foo', 1, 2, 3)
emitter.emit('bar', { x: 1 })
// nested events get tracked as well
var subemit = new EE()
emitter.emit('sub', subemit)
subemit.emit('childEvent', { some: 'data' })
subemit.emit('anotherone', { some: 'data' }, 'many', 'args')
// CAVEAT! See above in the wordy part of this readme.
// Note that the blaz/blorrg event comes after the child, and there's
// no way to know that the child 'order not preserved' event happened
// after.
emitter.emit('blaz', 'blorrg')
subemit.emit('order', 'not', 'preserved between child and parent')
// check out the array whenever
assert.deepEqual(array,
[ [ 'foo', 1, 2, 3 ],
[ 'bar', { x: 1 } ],
[ 'sub',
[ [ 'childEvent', { some: 'data' } ],
[ 'anotherone', { some: 'data' }, 'many', 'args' ],
[ 'order', 'not', 'preserved between child and parent' ] ] ],
[ 'blaz', 'blorrg' ] ])
```
## `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 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)`
module.exports = eventsToArray
var EE = require('events').EventEmitter
function eventsToArray (ee, ignore, map) {
ignore = ignore || []
map = map || function (x) { return x }
var array = []
ee.emit = (function (orig) {
return function etoaWrap (ev) {
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] = 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)
}
return orig.apply(this, arguments)
}
})(ee.emit)
return array
}
{
"name": "events-to-array",
"version": "1.1.2",
"description": "Put a bunch of emitted events in an array, for testing.",
"main": "etoa.js",
"directories": {
"test": "test"
},
"dependencies": {},
"devDependencies": {
"tap": "^10.3.2"
},
"scripts": {
"test": "tap test/*.js --100",
"preversion": "npm test",
"postversion": "npm publish",
"postpublish": "git push origin --all; git push origin --tags"
},
"repository": {
"type": "git",
"url": "https://github.com/isaacs/events-to-array"
},
"author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
"license": "ISC",
"bugs": {
"url": "https://github.com/isaacs/events-to-array/issues"
},
"homepage": "https://github.com/isaacs/events-to-array"
}
var test = require('tap').test
var EE = require('events').EventEmitter
var etoa = require('../etoa.js')
test('basic', function (t) {
var emitter = new EE()
var array = etoa(emitter, ['ignore', 'alsoignore'])
emitter.emit('foo', 1, 2, 3)
emitter.emit('ignore', 'should not see this')
emitter.emit('bar', { x: 1 })
// nested events get tracked as well
var subemit = new EE()
emitter.emit('sub', subemit)
subemit.emit('childEvent', { some: 'data' })
subemit.emit('alsoignore', 'should not see this')
subemit.emit('anotherone', { some: 'data' }, 'many', 'args')
// CAVEAT!
emitter.emit('blaz', 'blorrg')
subemit.emit('order', 'not', 'preserved between child and parent')
// check out the array whenever
t.same(array,
[ [ 'foo', 1, 2, 3 ],
[ 'bar', { x: 1 } ],
[ 'sub',
[ [ 'childEvent', { some: 'data' } ],
[ 'anotherone', { some: 'data' }, 'many', 'args' ],
[ 'order', 'not', 'preserved between child and parent' ] ] ],
[ '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 (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()
})
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