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
Provenance
- Commit
0c8202b- Tree
921df0a- Author
tunnckoCore <mameto_100@mail.bg>- Committer
tunnckoCore <mameto_100@mail.bg>- Parent
e7c222c
7 files changed+45 added−37 removed
added:dist-using-set/mitt.js+2 added
12function n(){var n={all:Object.create(null),on:function(e,r){return t(e).add(r),n},off:function(e,r){return t(e).delete(r),n},emit:function(e,r,u,o){return t(e).forEach(function(n){return n(r,u,o)}),t("*").forEach(function(n){return n(e,r,u,o)}),n}},t=function(t){return n.all[t=t.toLowerCase()]||(n.all[t]=new Set)};return n}module.exports=n;//# sourceMappingURL=mitt.js.mapNo newline at end of fileadded: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
12!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.mitt=e()}(this,function(){function n(){var n={all:Object.create(null),on:function(t,o){return e(t).add(o),n},off:function(t,o){return e(t).delete(o),n},emit:function(t,o,r,u){return e(t).forEach(function(n){return n(o,r,u)}),e("*").forEach(function(n){return n(t,o,r,u)}),n}},e=function(e){return n.all[e=e.toLowerCase()]||(n.all[e]=new Set)};return n}return n});//# sourceMappingURL=mitt.umd.js.mapNo newline at end of fileadded: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
12345677891011122 unmodified lines151617181819202122234 unmodified lines28293031313233343536372 unmodified lines40414243424445464647484748495051525253545556575354/** Mitt: Tiny (~200b) functional event emitter / pubsub. * @name mitt * @returns {Mitt} */export default function mitt () { let all = Object.create(null) let ret = { all: Object.create(null), all,
/** * Register an event handler for the given type. *2 unmodified lines * @return {Object} the `mitt` instance for chaining * @memberOf mitt */ on(type, handler) { list(type).add(handler); (all[type] || (all[type] = [])).push(handler); return ret; },
/**4 unmodified lines * @return {Object} the `mitt` instance for chaining * @memberOf mitt */ off(type, handler) { list(type).delete(handler); let e = all[type] || (all[type] = []); e.splice(e.indexOf(handler) >>> 0, 1); return ret; },
/**2 unmodified lines * * @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)); emit(type, arg1, arg2) { (all[type] || []).map((handler) => { handler(arg1, arg2); }); (all['*'] || []).map((handler) => { handler(type, arg1, arg2); }); return ret; } };
// Get or create a named handler list let list = (type) => { return ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set()); };
return ret;}modified:test/index.js+31 added-23 removed
25 unmodified lines26 it('should register handler for new type', () => {27 let foo = () => {};28 inst.on('foo', foo);29 30- expect(events).to.have.property('foo').that.deep.equals(new Set([foo]));30+ expect(events).to.have.property('foo').that.deep.equals([foo]);31 });32 33 it('should register handlers for any type strings', () => {34 let foo = () => {};35 inst.on('constructor', foo);36 37- expect(events).to.have.property('constructor').that.deep.equals(new Set([foo]));37+ expect(events).to.have.property('constructor').that.deep.equals([foo]);38 });39 40 it('should append handler for existing type', () => {41 let foo = () => {};42 let bar = () => {};43 inst.on('foo', foo);44 inst.on('foo', bar);45 46- 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 });48 49- 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);54 55- 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 });62 63 describe('off()', () => {12 unmodified lines76 });77 78 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);83 84 inst.off('FOO', foo);85 inst.off('Bar', foo);86 inst.off('baz:baT!', foo);87 86- 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 });95 96 describe('emit()', () => {13 unmodified lines110 111 inst.emit('foo', event);112 });113 110- 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 });121 119- 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];128 129 inst.emit('FOO', num);130 inst.emit('Foo', num);131 127- let args = foo.args[0];132+ let args1 = foo.args[0];133+ let args2 = bar.args[0];134 129- 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 });140 141 it('should invoke * handlers', () => {142 let star = spy(),6 unmodified lines149 let args1 = star.args[0],150 args2 = star.args[1];151 152 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
60 unmodified lines61626364656566676820 unmodified lines60 unmodified lines
- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke- `arg1` **\[Any]** A value (first argument), passed to each handler- `arg2` **\[Any]** A value (second argument), passed to each handler- `arg3` **\[Any]** A value (third argument), passed to each handler
Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining
### on20 unmodified lines