Skip to content
Myr

fix: switch Set with array; update tests; drop toLowerCase()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by tunnckoCore · committed by tunnckoCore · parent e7c222c · Browse files · 7 files+45 added−37 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+45 added−37 removed

  • tests:+31 added−23 removed
  • other:+14 added−14 removed

7 files+45 added−37 removed

added:dist-using-set/mitt.js+2 added
added:dist-using-set/mitt.js.map+1 added
1{"version":3,"sources":["../src/index.js"],"names":["mitt","let","ret","all","Object","create","on","type","handler","list","add","off","delete","emit","arg1","arg2","arg3","forEach","toLowerCase","Set"],"mappings":"AAIA,QAAwBA,KACvBC,GAAIC,IACHC,IAAKC,OAAOC,OAAO,MAUnBC,GAAE,SAACC,EAAMC,GAER,MADAC,GAAKF,GAAMG,IAAIF,GACRN,GAWRS,IAAG,SAACJ,EAAMC,GAET,MADAC,GAAKF,GAAMK,OAAOJ,GACXN,GAcRW,KAAI,SAACN,EAAMO,EAAMC,EAAMC,GAGtB,MAFAP,GAAKF,GAAMU,QAAQ,SAACT,GAAS,MAAGA,GAAQM,EAAMC,EAAMC,KACpDP,EAAK,KAAKQ,QAAQ,SAACT,GAAS,MAAGA,GAAQD,EAAMO,EAAMC,EAAMC,KAClDd,IAKLO,EAAO,SAACF,GACX,MAAOL,GAAIC,IAAII,EAAOA,EAAKW,iBAAmBhB,EAAIC,IAAII,GAAQ,GAAIY,MAGnE,OAAOjB","file":"mitt.js","sourcesContent":["/** Mitt: Tiny (~200b) functional event emitter / pubsub.\n * @name mitt\n * @returns {Mitt}\n */\nexport default function mitt () {\n\tlet ret = {\n\t\tall: Object.create(null),\n\n\t\t/**\n\t\t * Register an event handler for the given type.\n\t\t *\n\t\t * @param {String} type Type of event to listen for, or `\"*\"` for all events\n\t\t * @param {Function} handler Function to call in response to given event\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberOf mitt\n\t\t */\n\t\ton(type, handler) {\n\t\t\tlist(type).add(handler);\n\t\t\treturn ret;\n\t\t},\n\n\t\t/**\n\t\t * Remove an event handler for the given type.\n\t\t *\n\t\t * @param {String} type Type of event to unregister `handler` from, or `\"*\"`\n\t\t * @param {Function} handler Handler function to remove\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberOf mitt\n\t\t */\n\t\toff(type, handler) {\n\t\t\tlist(type).delete(handler);\n\t\t\treturn ret;\n\t\t},\n\n\t\t/**\n\t\t * Invoke all handlers for the given type.\n\t\t * If present, `\"*\"` handlers are invoked prior to type-matched handlers.\n\t\t *\n\t\t * @param {String} type The event type to invoke\n\t\t * @param {Any} [arg1] A value (first argument), passed to each handler\n\t\t * @param {Any} [arg2] A value (second argument), passed to each handler\n\t\t * @param {Any} [arg3] A value (third argument), passed to each handler\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberof mitt\n\t\t */\n\t\temit(type, arg1, arg2, arg3) {\n\t\t\tlist(type).forEach((handler) => handler(arg1, arg2, arg3));\n\t\t\tlist('*').forEach((handler) => handler(type, arg1, arg2, arg3));\n\t\t\treturn ret;\n\t\t}\n\t};\n\n\t// Get or create a named handler list\n\tlet list = (type) => {\n\t\treturn ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set());\n\t};\n\n\treturn ret;\n}\n"]}
added:dist-using-set/mitt.umd.js+2 added
added:dist-using-set/mitt.umd.js.map+1 added
1{"version":3,"sources":["../src/index.js"],"names":["mitt","let","ret","all","Object","create","on","type","handler","list","add","off","delete","emit","arg1","arg2","arg3","forEach","toLowerCase","Set"],"mappings":"mKAIA,QAAwBA,KACvBC,GAAIC,IACHC,IAAKC,OAAOC,OAAO,MAUnBC,GAAE,SAACC,EAAMC,GAER,MADAC,GAAKF,GAAMG,IAAIF,GACRN,GAWRS,IAAG,SAACJ,EAAMC,GAET,MADAC,GAAKF,GAAMK,OAAOJ,GACXN,GAcRW,KAAI,SAACN,EAAMO,EAAMC,EAAMC,GAGtB,MAFAP,GAAKF,GAAMU,QAAQ,SAACT,GAAS,MAAGA,GAAQM,EAAMC,EAAMC,KACpDP,EAAK,KAAKQ,QAAQ,SAACT,GAAS,MAAGA,GAAQD,EAAMO,EAAMC,EAAMC,KAClDd,IAKLO,EAAO,SAACF,GACX,MAAOL,GAAIC,IAAII,EAAOA,EAAKW,iBAAmBhB,EAAIC,IAAII,GAAQ,GAAIY,MAGnE,OAAOjB","file":"mitt.umd.js","sourcesContent":["/** Mitt: Tiny (~200b) functional event emitter / pubsub.\n * @name mitt\n * @returns {Mitt}\n */\nexport default function mitt () {\n\tlet ret = {\n\t\tall: Object.create(null),\n\n\t\t/**\n\t\t * Register an event handler for the given type.\n\t\t *\n\t\t * @param {String} type Type of event to listen for, or `\"*\"` for all events\n\t\t * @param {Function} handler Function to call in response to given event\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberOf mitt\n\t\t */\n\t\ton(type, handler) {\n\t\t\tlist(type).add(handler);\n\t\t\treturn ret;\n\t\t},\n\n\t\t/**\n\t\t * Remove an event handler for the given type.\n\t\t *\n\t\t * @param {String} type Type of event to unregister `handler` from, or `\"*\"`\n\t\t * @param {Function} handler Handler function to remove\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberOf mitt\n\t\t */\n\t\toff(type, handler) {\n\t\t\tlist(type).delete(handler);\n\t\t\treturn ret;\n\t\t},\n\n\t\t/**\n\t\t * Invoke all handlers for the given type.\n\t\t * If present, `\"*\"` handlers are invoked prior to type-matched handlers.\n\t\t *\n\t\t * @param {String} type The event type to invoke\n\t\t * @param {Any} [arg1] A value (first argument), passed to each handler\n\t\t * @param {Any} [arg2] A value (second argument), passed to each handler\n\t\t * @param {Any} [arg3] A value (third argument), passed to each handler\n\t\t * @return {Object} the `mitt` instance for chaining\n\t\t * @memberof mitt\n\t\t */\n\t\temit(type, arg1, arg2, arg3) {\n\t\t\tlist(type).forEach((handler) => handler(arg1, arg2, arg3));\n\t\t\tlist('*').forEach((handler) => handler(type, arg1, arg2, arg3));\n\t\t\treturn ret;\n\t\t}\n\t};\n\n\t// Get or create a named handler list\n\tlet list = (type) => {\n\t\treturn ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set());\n\t};\n\n\treturn ret;\n}\n"]}
modified:src/index.js+8 added-13 removed
modified:test/index.js+31 added-23 removed
25 unmodified lines
26 it('should register handler for new type', () => {27 let foo = () => {};28 inst.on('foo', foo);2930 expect(events).to.have.property('foo').that.deep.equals(new Set([foo]));30 expect(events).to.have.property('foo').that.deep.equals([foo]);31 });3233 it('should register handlers for any type strings', () => {34 let foo = () => {};35 inst.on('constructor', foo);3637 expect(events).to.have.property('constructor').that.deep.equals(new Set([foo]));37 expect(events).to.have.property('constructor').that.deep.equals([foo]);38 });3940 it('should append handler for existing type', () => {41 let foo = () => {};42 let bar = () => {};43 inst.on('foo', foo);44 inst.on('foo', bar);4546 expect(events).to.have.property('foo').that.deep.equals(new Set([foo, foo, bar]));46 expect(events).to.have.property('foo').that.deep.equals([foo, bar]);47 });4849 it('should normalize case', () => {49 it('should NOT normalize case', () => {50 let foo = () => {};51 inst.on('FOO', foo);52 inst.on('Bar', foo);53 inst.on('baz:baT!', foo);5455 expect(events).to.have.property('foo').that.deep.equals(new Set([foo]));56 expect(events).to.have.property('bar').that.deep.equals(new Set([foo]));57 expect(events).to.have.property('baz:bat!').that.deep.equals(new Set([foo]));55 expect(events).to.have.property('FOO').that.deep.equals([foo]);56 expect(events).to.not.have.property('foo');57 expect(events).to.have.property('Bar').that.deep.equals([foo]);58 expect(events).to.not.have.property('bar');59 expect(events).to.have.property('baz:baT!').that.deep.equals([foo]);60 });61 });6263 describe('off()', () => {
12 unmodified lines
76 });7778 it('should normalize case', () => {79 let foo = () => {};78 inst.on('foo', foo);79 inst.on('bar', foo);80 inst.on('FOO', foo);81 inst.on('Bar', foo);82 inst.on('baz:bat!', foo);8384 inst.off('FOO', foo);85 inst.off('Bar', foo);86 inst.off('baz:baT!', foo);8786 expect(events).to.have.property('foo').that.is.empty;87 expect(events).to.have.property('bar').that.is.empty;88 expect(events).to.have.property('baz:bat!').that.is.empty;88 expect(events).to.have.property('FOO').that.is.empty;89 expect(events).to.not.have.property('foo');90 expect(events).to.have.property('Bar').that.is.empty;91 expect(events).to.not.have.property('bar');92 expect(events).to.have.property('baz:baT!').that.is.empty;93 });94 });9596 describe('emit()', () => {
13 unmodified lines
110111 inst.emit('foo', event);112 });113110 it('should invoke handler with multiple arguments', () => {111 inst.on('foo', (aaa, bbb, ccc) => {114 it('should invoke handler with multiple (max 2) arguments', () => {115 inst.on('foo', (aaa, bbb) => {116 expect(aaa).to.be.equal(111);117 expect(bbb).to.be.equal(222);114 expect(ccc).to.be.equal(333);118 });116 inst.emit('foo', 111, 222, 333);119 inst.emit('foo', 111, 222);120 });121119 it('should ignore case', () => {122 it('should NOT ignore case', () => {123 let foo = spy(),124 bar = spy(),125 num = 123;122 events.foo = [foo];126 events.Foo = [foo];127 events.FOO = [bar];128129 inst.emit('FOO', num);130 inst.emit('Foo', num);131127 let args = foo.args[0];132 let args1 = foo.args[0];133 let args2 = bar.args[0];134129 expect(foo).to.have.been.calledTwice;130 expect(args).to.be.deep.equal([num, undefined, undefined]);135 expect(foo).to.have.been.calledOnce;136 expect(bar).to.have.been.calledOnce;137 expect(args1).to.be.deep.equal([num, undefined]);138 expect(args2).to.be.deep.equal([num, undefined]);139 });140141 it('should invoke * handlers', () => {142 let star = spy(),
6 unmodified lines
149 let args1 = star.args[0],150 args2 = star.args[1];151152 expect(star).to.have.been.calledTwice;145 expect(args1).to.deep.equal(['foo', aa, undefined, undefined]);146 expect(args2).to.deep.equal(['bar', aa, undefined, undefined]);153 expect(args1).to.deep.equal(['foo', aa, undefined]);154 expect(args2).to.deep.equal(['bar', aa, undefined]);155 });156 });157});
modified:README.md-1 removed