diff options
| -rw-r--r-- | .editorconfig | 9 | ||||
| -rw-r--r-- | example.js | 12 | ||||
| -rw-r--r-- | src/index.js | 96 | ||||
| -rw-r--r-- | test/index.js | 34 |
4 files changed, 80 insertions, 71 deletions
diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3dbf828 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = tab +indent_size = 4 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + @@ -4,12 +4,12 @@ var mitt = require('./dist/mitt') var ee = mitt() ee - .on('*', (type, arg) => console.log('wildcard:', type, arg)) - .on('foo', (one, two, three) => console.log('foo1:', one, two, three)) - .on('foo', (one, two, three) => console.log('foo2:', one, two, three)) - .on('bar', (arg) => console.log('bar:', arg)) - .emit('foo', 1, 2, 3) - .emit('bar', 444) + .on('*', (type, arg) => console.log('wildcard:', type, arg)) + .on('foo', (one, two, three) => console.log('foo1:', one, two, three)) + .on('foo', (one, two, three) => console.log('foo2:', one, two, three)) + .on('bar', (arg) => console.log('bar:', arg)) + .emit('foo', 1, 2, 3) + .emit('bar', 444) console.log(ee.all) diff --git a/src/index.js b/src/index.js index 5140057..5e11237 100644 --- a/src/index.js +++ b/src/index.js @@ -3,57 +3,57 @@ * @returns {Mitt} */ export default function mitt () { - let ret = { - all: Object.create(null), + let ret = { + all: Object.create(null), - /** - * Register an event handler for the given type. - * - * @param {String} type Type of event to listen for, or `"*"` for all events - * @param {Function} handler Function to call in response to given event - * @return {Object} the `mitt` instance for chaining - * @memberOf mitt - */ - on(type, handler) { - list(type).add(handler); - return ret; - }, + /** + * Register an event handler for the given type. + * + * @param {String} type Type of event to listen for, or `"*"` for all events + * @param {Function} handler Function to call in response to given event + * @return {Object} the `mitt` instance for chaining + * @memberOf mitt + */ + on(type, handler) { + list(type).add(handler); + return ret; + }, - /** - * Remove an event handler for the given type. - * - * @param {String} type Type of event to unregister `handler` from, or `"*"` - * @param {Function} handler Handler function to remove - * @return {Object} the `mitt` instance for chaining - * @memberOf mitt - */ - off(type, handler) { - list(type).delete(handler); - return ret; - }, + /** + * Remove an event handler for the given type. + * + * @param {String} type Type of event to unregister `handler` from, or `"*"` + * @param {Function} handler Handler function to remove + * @return {Object} the `mitt` instance for chaining + * @memberOf mitt + */ + off(type, handler) { + list(type).delete(handler); + return ret; + }, - /** - * Invoke all handlers for the given type. - * If present, `"*"` handlers are invoked prior to type-matched handlers. - * - * @param {String} type The event type to invoke - * @param {Any} [arg1] A value (first argument), passed to each handler - * @param {Any} [arg2] A value (second argument), passed to each handler - * @param {Any} [arg3] A value (third argument), passed to each handler - * @return {Object} the `mitt` instance for chaining - * @memberof mitt - */ - emit(type, arg1, arg2, arg3) { - list(type).forEach((handler) => handler(arg1, arg2, arg3)); - list('*').forEach((handler) => handler(type, arg1, arg2, arg3)); - return ret; - } - }; + /** + * Invoke all handlers for the given type. + * If present, `"*"` handlers are invoked prior to type-matched handlers. + * + * @param {String} type The event type to invoke + * @param {Any} [arg1] A value (first argument), passed to each handler + * @param {Any} [arg2] A value (second argument), passed to each handler + * @param {Any} [arg3] A value (third argument), passed to each handler + * @return {Object} the `mitt` instance for chaining + * @memberof mitt + */ + emit(type, arg1, arg2, arg3) { + list(type).forEach((handler) => handler(arg1, arg2, arg3)); + list('*').forEach((handler) => handler(type, arg1, arg2, arg3)); + return ret; + } + }; - // Get or create a named handler list - let list = (type) => { - return ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set()); - }; + // Get or create a named handler list + let list = (type) => { + return ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set()); + }; - return ret; + return ret; } diff --git a/test/index.js b/test/index.js index 4d818b5..d0bcefd 100644 --- a/test/index.js +++ b/test/index.js @@ -13,7 +13,7 @@ describe('mitt#', () => { beforeEach( () => { inst = mitt(); - events = inst.all; + events = inst.all; }); describe('on()', () => { @@ -35,7 +35,7 @@ describe('mitt#', () => { inst.on('constructor', foo); expect(events).to.have.property('constructor').that.deep.equals(new Set([foo])); - }); + }); it('should append handler for existing type', () => { let foo = () => {}; @@ -97,24 +97,24 @@ describe('mitt#', () => { }); it('should invoke handler for type', () => { - let event = { a: 'b' }; + let event = { a: 'b' }; inst.on('foo', (one, two) => { - expect(one).to.deep.equal(event); - expect(two).to.be.an('undefined'); - }); + expect(one).to.deep.equal(event); + expect(two).to.be.an('undefined'); + }); inst.emit('foo', event); }); - it('should invoke handler with multiple arguments', () => { - inst.on('foo', (aaa, bbb, ccc) => { - expect(aaa).to.be.equal(111); - expect(bbb).to.be.equal(222); - expect(ccc).to.be.equal(333); - }); - inst.emit('foo', 111, 222, 333); - }); + it('should invoke handler with multiple arguments', () => { + inst.on('foo', (aaa, bbb, ccc) => { + expect(aaa).to.be.equal(111); + expect(bbb).to.be.equal(222); + expect(ccc).to.be.equal(333); + }); + inst.emit('foo', 111, 222, 333); + }); it('should ignore case', () => { let foo = spy(), @@ -124,10 +124,10 @@ describe('mitt#', () => { inst.emit('FOO', num); inst.emit('Foo', num); - let args = foo.args[0]; + let args = foo.args[0]; expect(foo).to.have.been.calledTwice; - expect(args).to.be.deep.equal([num, undefined, undefined]); + expect(args).to.be.deep.equal([num, undefined, undefined]); }); it('should invoke * handlers', () => { @@ -138,7 +138,7 @@ describe('mitt#', () => { inst.emit('foo', aa); inst.emit('bar', aa); - let args1 = star.args[0], + let args1 = star.args[0], args2 = star.args[1]; expect(star).to.have.been.calledTwice; |
