pax_global_header00006660000000000000000000000064130727473310014521gustar00rootroot0000000000000052 comment=00039bdbfed0505824a5756f9fb2b8827be62702 events-to-array-1.1.2/000077500000000000000000000000001307274733100145625ustar00rootroot00000000000000events-to-array-1.1.2/.travis.yml000066400000000000000000000001571307274733100166760ustar00rootroot00000000000000language: node_js before_script: npm install -g npm@latest node_js: - '0.8' - '0.10' - '0.12' - 'iojs' events-to-array-1.1.2/LICENSE000066400000000000000000000013751307274733100155750ustar00rootroot00000000000000The 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-1.1.2/README.md000066400000000000000000000043651307274733100160510ustar00rootroot00000000000000# 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)` events-to-array-1.1.2/etoa.js000066400000000000000000000015431307274733100160530ustar00rootroot00000000000000module.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 } events-to-array-1.1.2/package.json000066400000000000000000000014051307274733100170500ustar00rootroot00000000000000{ "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 (http://blog.izs.me/)", "license": "ISC", "bugs": { "url": "https://github.com/isaacs/events-to-array/issues" }, "homepage": "https://github.com/isaacs/events-to-array" } events-to-array-1.1.2/test/000077500000000000000000000000001307274733100155415ustar00rootroot00000000000000events-to-array-1.1.2/test/basic.js000066400000000000000000000033761307274733100171710ustar00rootroot00000000000000var 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() })