add editorconfig, fix original identation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by tunnckoCore · committed by tunnckoCore · parent 8b4f2de · Browse files · 4 files·+80 added−71 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+80 added−71 removed
- tests:+17 added−17 removed
- other:+63 added−54 removed
Provenance
- Commit
e7c222c- Tree
784bfbf- Author
tunnckoCore <mameto_100@mail.bg>- Committer
tunnckoCore <mameto_100@mail.bg>- Parent
8b4f2de
4 files changed+80 added−71 removed
modified:src/index.js+48 added-48 removed
1234567678910111213141516171819209101112131415161718192021222324252627282930313233222324252627282930313233343536373839404142434445464748495051353637383940414243444546474849505152535455565354555657585859/** Mitt: Tiny (~200b) functional event emitter / pubsub. * @name mitt * @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;}modified:test/index.js+17 added-17 removed
11 unmodified lines12 let events, inst;13 14 beforeEach( () => {15 inst = mitt();16- events = inst.all;16+ events = inst.all;17 });18 19 describe('on()', () => {20 it('should be a function', () => {13 unmodified lines34 let foo = () => {};35 inst.on('constructor', foo);36 37 expect(events).to.have.property('constructor').that.deep.equals(new Set([foo]));38- });38+ });39 40 it('should append handler for existing type', () => {41 let foo = () => {};42 let bar = () => {};53 unmodified lines96 .that.is.a('function');97 });98 99 it('should invoke handler for type', () => {100- let event = { a: 'b' };100+ let event = { a: 'b' };101 102 inst.on('foo', (one, two) => {103- expect(one).to.deep.equal(event);104- expect(two).to.be.an('undefined');105- });103+ expect(one).to.deep.equal(event);104+ expect(two).to.be.an('undefined');105+ });106 107 inst.emit('foo', event);108 });109 110- it('should invoke handler with multiple arguments', () => {111- inst.on('foo', (aaa, bbb, ccc) => {112- expect(aaa).to.be.equal(111);113- expect(bbb).to.be.equal(222);114- expect(ccc).to.be.equal(333);115- });116- inst.emit('foo', 111, 222, 333);117- });110+ it('should invoke handler with multiple arguments', () => {111+ inst.on('foo', (aaa, bbb, ccc) => {112+ expect(aaa).to.be.equal(111);113+ expect(bbb).to.be.equal(222);114+ expect(ccc).to.be.equal(333);115+ });116+ inst.emit('foo', 111, 222, 333);117+ });118 119 it('should ignore case', () => {120 let foo = spy(),121 num = 123;1 unmodified line123 124 inst.emit('FOO', num);125 inst.emit('Foo', num);126 127- let args = foo.args[0];127+ let args = foo.args[0];128 129 expect(foo).to.have.been.calledTwice;130- expect(args).to.be.deep.equal([num, undefined, undefined]);130+ expect(args).to.be.deep.equal([num, undefined, undefined]);131 });132 133 it('should invoke * handlers', () => {134 let star = spy(),2 unmodified lines137 138 inst.emit('foo', aa);139 inst.emit('bar', aa);140 141- let args1 = star.args[0],141+ let args1 = star.args[0],142 args2 = star.args[1];143 144 expect(star).to.have.been.calledTwice;145 expect(args1).to.deep.equal(['foo', aa, undefined, undefined]);4 unmodified linesadded:.editorconfig+9 added
1+root = true2+3+[*]4+indent_style = tab5+indent_size = 46+charset = utf-87+trim_trailing_whitespace = true8+insert_final_newline = true9+modified:example.js+6 added-6 removed
2 unmodified lines3456789101112789101112131415164 unmodified lines2 unmodified linesvar 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)
var emitter = mitt()4 unmodified lines