From 80bbdbeb4a0d3e8df2d40857f1be0f30a70f23f8 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 29 May 2019 11:10:00 -0400 Subject: fix test runner --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index 9497394..d80b306 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "umd:main": "dist/mitt.umd.js", "scripts": { "bump": "standard-version", - "testonly": "mocha --require reify/node --require flow-remove-types/register test/**/*.js", + "testonly": "mocha --require esm --require flow-remove-types/register test/**/*.js", "lint": "eslint src test", "test": "flow && npm run lint && npm run testonly", "build": "npm-run-all --silent clean -p rollup -p minify:* -s docs size", @@ -73,13 +73,13 @@ "chai": "^3.5.0", "documentation": "^4.0.0-beta4", "eslint": "^3.13.1", + "esm": "^3.2.25", "flow-bin": "^0.38.0", "flow-remove-types": "^1.2.0", "gzip-size-cli": "^1.0.0", "mocha": "^3.2.0", "npm-run-all": "^2.1.1", "pretty-bytes-cli": "^2.0.0", - "reify": "^0.8.3", "rimraf": "^2.5.2", "rollup": "^0.41.4", "rollup-plugin-buble": "^0.15.0", -- cgit v1.2.3 From 06c1fc22223713729d69c35a8fd4cc1a825fa443 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Mon, 21 Oct 2019 12:44:50 -0400 Subject: chore(release): 1.2.1 --- CHANGELOG.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'package.json') diff --git a/CHANGELOG.md b/CHANGELOG.md index e5bcdca..ab76272 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +## [1.2.1](https://github.com/developit/mitt/compare/v1.1.3...v1.2.1) (2019-10-21) + + + ## [1.1.3](https://github.com/developit/mitt/compare/v1.1.2...v1.1.3) (2017-12-07) diff --git a/package.json b/package.json index d80b306..dc4a1c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mitt", - "version": "1.1.3", + "version": "1.2.1", "description": "Tiny 200b functional Event Emitter / pubsub.", "jsnext:main": "dist/mitt.es.js", "module": "dist/mitt.es.js", -- cgit v1.2.3 From a4e20b1849fa327b87da55786999fd8c550a0dfa Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Mon, 21 Oct 2019 12:53:47 -0400 Subject: 1.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'package.json') diff --git a/package.json b/package.json index dc4a1c8..bb87fea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mitt", - "version": "1.2.1", + "version": "1.2.0", "description": "Tiny 200b functional Event Emitter / pubsub.", "jsnext:main": "dist/mitt.es.js", "module": "dist/mitt.es.js", -- cgit v1.2.3 From dbd661a075916409cd816f7057e228614779ae33 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Mon, 21 Oct 2019 13:00:49 -0400 Subject: update deps --- package.json | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index bb87fea..6275b3a 100644 --- a/package.json +++ b/package.json @@ -46,8 +46,11 @@ ] }, "eslintConfig": { - "parser": "babel-eslint", "extends": "eslint:recommended", + "parser": "babel-eslint", + "parserOptions": { + "sourceType": "module" + }, "env": { "browser": true, "mocha": true, @@ -66,13 +69,13 @@ "typings": "./mitt.d.ts", "devDependencies": { "babel-core": "^6.9.1", - "babel-eslint": "^7.1.1", + "babel-eslint": "^10.0.3", "babel-plugin-transform-flow-strip-types": "^6.21.0", "babel-preset-es2015": "^6.9.0", "babel-preset-stage-0": "^6.5.0", "chai": "^3.5.0", "documentation": "^4.0.0-beta4", - "eslint": "^3.13.1", + "eslint": "^6.5.1", "esm": "^3.2.25", "flow-bin": "^0.38.0", "flow-remove-types": "^1.2.0", -- cgit v1.2.3 From be5237f301b1059ffff9e1e795fd66594644892a Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Tue, 26 May 2020 22:45:53 +0100 Subject: Migrate to TypeScript and use Map (#99) Migrate to TypeScript & Microbundle, move to Map for event handler storage --- README.md | 34 ++++++++++++------------- mitt.d.ts | 53 --------------------------------------- package.json | 60 +++++++++++++++++++-------------------------- rollup.config.js | 20 --------------- src/index.js | 63 ----------------------------------------------- src/index.ts | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/index.js | 63 ++++++++++++++++++++++++++--------------------- typings.json | 5 ---- 8 files changed, 152 insertions(+), 221 deletions(-) delete mode 100644 mitt.d.ts delete mode 100644 rollup.config.js delete mode 100644 src/index.js create mode 100644 src/index.ts delete mode 100644 typings.json (limited to 'package.json') diff --git a/README.md b/README.md index 37a135d..b48fa97 100644 --- a/README.md +++ b/README.md @@ -95,13 +95,19 @@ const emitter: mitt.Emitter = mitt(); -### mitt +#### Table of Contents -Mitt: Tiny (~200b) functional event emitter / pubsub. +- [mitt](#mitt) +- [on](#on) + - [Parameters](#parameters) +- [off](#off) + - [Parameters](#parameters-1) +- [emit](#emit) + - [Parameters](#parameters-2) -**Parameters** +### mitt -- `all` **EventHandlerMap** +Mitt: Tiny (~200b) functional event emitter / pubsub. Returns **Mitt** @@ -109,18 +115,18 @@ Returns **Mitt** Register an event handler for the given type. -**Parameters** +#### Parameters -- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events +- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `"*"` for all events - `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event ### off Remove an event handler for the given type. -**Parameters** +#### Parameters -- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"` +- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `"*"` - `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove ### emit @@ -128,11 +134,11 @@ Remove an event handler for the given type. Invoke all handlers for the given type. If present, `"*"` handlers are invoked after type-matched handlers. -_Note: Manually firing "*" handlers is not supported._ +Note: Manually firing "\*" handlers is not supported. -**Parameters** +#### Parameters -- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke +- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** The event type to invoke - `evt` **Any?** Any value (object is recommended and powerful), passed to each handler ## Contribute @@ -140,12 +146,6 @@ _Note: Manually firing "*" handlers is not supported._ First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else. -Development Start: - -This project is typed with Flow Type annotations. To ensure you have the proper typings for this project run - -`flow-typed install` - ### Reporting Issues Found a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues). diff --git a/mitt.d.ts b/mitt.d.ts deleted file mode 100644 index 6cb62d8..0000000 --- a/mitt.d.ts +++ /dev/null @@ -1,53 +0,0 @@ -declare var mitt: mitt.MittStatic; - -declare module "mitt" { - export = mitt; -} - -declare namespace mitt { - type Handler = (event?: any) => void; - type WildcardHandler = (type: string, event?: any) => void; - - interface MittStatic { - (all?: {[key: string]: Array}): Emitter; - } - - interface Emitter { - /** - * Register an event handler for the given type. - * - * @param {string} type Type of event to listen for, or `"*"` for all events. - * @param {Handler} handler Function to call in response to the given event. - * - * @memberOf Mitt - */ - on(type: string, handler: Handler): void; - on(type: "*", handler: WildcardHandler): void; - - /** - * Function to call in response to the given event - * - * @param {string} type Type of event to unregister `handler` from, or `"*"` - * @param {Handler} handler Handler function to remove. - * - * @memberOf Mitt - */ - off(type: string, handler: Handler): void; - off(type: "*", handler: WildcardHandler): void; - - /** - * 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} [event] An event object, passed to each handler - * - * @memberOf Mitt - */ - emit(type: string, event?: any): void; - /** - * Note: Manually firing "*" events is unsupported. - */ - emit(type: "*", event?: any): void; - } -} diff --git a/package.json b/package.json index 6275b3a..ac14731 100644 --- a/package.json +++ b/package.json @@ -3,20 +3,20 @@ "version": "1.2.0", "description": "Tiny 200b functional Event Emitter / pubsub.", "jsnext:main": "dist/mitt.es.js", + "source": "src/index.ts", "module": "dist/mitt.es.js", "main": "dist/mitt.js", "umd:main": "dist/mitt.umd.js", + "typings": "dist/index.d.ts", "scripts": { "bump": "standard-version", - "testonly": "mocha --require esm --require flow-remove-types/register test/**/*.js", - "lint": "eslint src test", - "test": "flow && npm run lint && npm run testonly", - "build": "npm-run-all --silent clean -p rollup -p minify:* -s docs size", + "testonly": "mocha --require esm --require ts-node/register test/**/*.js", + "lint": "eslint src test --ext ts --ext js", + "test": "tsc src/index.ts --noEmit && npm run lint && npm run testonly", + "bundle": "microbundle", + "build": "npm-run-all --silent clean -p bundle -s docs size", "clean": "rimraf dist", - "rollup": "rollup -c", - "minify:cjs": "uglifyjs $npm_package_main -cm toplevel -o $npm_package_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_main}.map", - "minify:umd": "uglifyjs $npm_package_umd_main -cm -o $npm_package_umd_main -p relative --in-source-map ${npm_package_umd_main}.map --source-map ${npm_package_umd_main}.map", - "docs": "documentation readme src/index.js --section API -q", + "docs": "documentation readme src/index.ts --section API -q --parse-extension ts", "size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size | pretty-bytes)\"", "release": "npm run build -s && npm run bump && git push --follow-tags origin master && npm publish" }, @@ -33,21 +33,15 @@ "license": "MIT", "files": [ "src", - "dist", - "mitt.d.ts" + "dist" ], - "babel": { - "presets": [ - "es2015", - "stage-0" - ], - "plugins": [ - "transform-flow-strip-types" - ] - }, "eslintConfig": { - "extends": "eslint:recommended", - "parser": "babel-eslint", + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended" + ], + "parser": "@typescript-eslint/parser", "parserOptions": { "sourceType": "module" }, @@ -63,34 +57,30 @@ "semi": [ 2, "always" - ] + ], + "@typescript-eslint/no-explicit-any": 0, + "@typescript-eslint/explicit-function-return-type": 0, + "@typescript-eslint/no-empty-function": 0 } }, - "typings": "./mitt.d.ts", "devDependencies": { - "babel-core": "^6.9.1", - "babel-eslint": "^10.0.3", - "babel-plugin-transform-flow-strip-types": "^6.21.0", - "babel-preset-es2015": "^6.9.0", - "babel-preset-stage-0": "^6.5.0", + "@typescript-eslint/eslint-plugin": "^2.34.0", + "@typescript-eslint/parser": "^2.34.0", "chai": "^3.5.0", - "documentation": "^4.0.0-beta4", + "documentation": "^13.0.0", "eslint": "^6.5.1", "esm": "^3.2.25", - "flow-bin": "^0.38.0", - "flow-remove-types": "^1.2.0", "gzip-size-cli": "^1.0.0", + "microbundle": "^0.12.0", "mocha": "^3.2.0", "npm-run-all": "^2.1.1", "pretty-bytes-cli": "^2.0.0", "rimraf": "^2.5.2", - "rollup": "^0.41.4", - "rollup-plugin-buble": "^0.15.0", - "rollup-plugin-flow": "^1.1.1", "sinon": "^1.17.4", "sinon-chai": "^2.8.0", "standard-version": "^4.0.0", "strip-json-comments-cli": "^1.0.1", - "uglify-js": "^2.6.2" + "ts-node": "^8.10.1", + "typescript": "^3.9.3" } } diff --git a/rollup.config.js b/rollup.config.js deleted file mode 100644 index 9eba049..0000000 --- a/rollup.config.js +++ /dev/null @@ -1,20 +0,0 @@ -import buble from 'rollup-plugin-buble'; -import flow from 'rollup-plugin-flow'; -import fs from 'fs'; - -const pkg = JSON.parse(fs.readFileSync('./package.json')); - -export default { - entry: 'src/index.js', - useStrict: false, - sourceMap: true, - plugins: [ - flow(), - buble() - ], - targets: [ - { dest: pkg.main, format: 'cjs' }, - { dest: pkg.module, format: 'es' }, - { dest: pkg['umd:main'], format: 'umd', moduleName: pkg.name } - ] -}; diff --git a/src/index.js b/src/index.js deleted file mode 100644 index d9b36f8..0000000 --- a/src/index.js +++ /dev/null @@ -1,63 +0,0 @@ -// @flow -// An event handler can take an optional event argument -// and should not return a value -type EventHandler = (event?: any) => void; -type WildCardEventHandler = (type: string, event?: any) => void - -// An array of all currently registered event handlers for a type -type EventHandlerList = Array; -type WildCardEventHandlerList = Array; -// A map of event types and their corresponding event handlers. -type EventHandlerMap = { - '*'?: WildCardEventHandlerList, - [type: string]: EventHandlerList, -}; - -/** Mitt: Tiny (~200b) functional event emitter / pubsub. - * @name mitt - * @returns {Mitt} - */ -export default function mitt(all: EventHandlerMap) { - all = all || Object.create(null); - - return { - /** - * 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 - * @memberOf mitt - */ - on(type: string, handler: EventHandler) { - (all[type] || (all[type] = [])).push(handler); - }, - - /** - * 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 - * @memberOf mitt - */ - off(type: string, handler: EventHandler) { - if (all[type]) { - all[type].splice(all[type].indexOf(handler) >>> 0, 1); - } - }, - - /** - * Invoke all handlers for the given type. - * If present, `"*"` handlers are invoked after type-matched handlers. - * - * Note: Manually firing "*" handlers is not supported. - * - * @param {String} type The event type to invoke - * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler - * @memberOf mitt - */ - emit(type: string, evt: any) { - (all[type] || []).slice().map((handler) => { handler(evt); }); - (all['*'] || []).slice().map((handler) => { handler(type, evt); }); - } - }; -} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..f0a2ebb --- /dev/null +++ b/src/index.ts @@ -0,0 +1,75 @@ +type EventType = string | symbol; + +// An event handler can take an optional event argument +// and should not return a value +type Handler = (event?: any) => void; +type WildcardHandler= (type: EventType, event?: any) => void + +// An array of all currently registered event handlers for a type +type EventHandlerList = Array; +type WildCardEventHandlerList = Array; + +// A map of event types and their corresponding event handlers. +type EventHandlerMap = Map; + +export interface Emitter { + on(type: EventType, handler: Handler): void; + on(type: "*", handler: WildcardHandler): void; + + off(type: EventType, handler: Handler): void; + off(type: "*", handler: WildcardHandler): void; + + emit(type: EventType, event?: T): void; + emit(type: "*", event?: any): void; +} + +/** Mitt: Tiny (~200b) functional event emitter / pubsub. + * @name mitt + * @returns {Mitt} + */ +export default function mitt(all: EventHandlerMap): Emitter { + all = all || new Map(); + + return { + /** + * Register an event handler for the given type. + * + * @param {string|symbol} type Type of event to listen for, or `"*"` for all events + * @param {Function} handler Function to call in response to given event + * @memberOf mitt + */ + on(type: EventType, handler: Handler) { + const handlers = (all.get(type) || []); + handlers.push(handler); + all.set(type, handlers); + }, + + /** + * Remove an event handler for the given type. + * + * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"` + * @param {Function} handler Handler function to remove + * @memberOf mitt + */ + off(type: EventType, handler: Handler) { + if (all.has(type)) { + all.get(type).splice(all.get(type).indexOf(handler) >>> 0, 1); + } + }, + + /** + * Invoke all handlers for the given type. + * If present, `"*"` handlers are invoked after type-matched handlers. + * + * Note: Manually firing "*" handlers is not supported. + * + * @param {string|symbol} type The event type to invoke + * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler + * @memberOf mitt + */ + emit(type: EventType, evt: any) { + ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); }); + ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); }); + } + }; +} diff --git a/test/index.js b/test/index.js index 3e0ef78..554f1f1 100644 --- a/test/index.js +++ b/test/index.js @@ -12,7 +12,7 @@ describe('mitt#', () => { let events, inst; beforeEach( () => { - events = Object.create(null); + events = new Map(); inst = mitt(events); }); @@ -24,39 +24,46 @@ describe('mitt#', () => { }); it('should register handler for new type', () => { - let foo = () => {}; + const foo = () => {}; inst.on('foo', foo); - expect(events).to.have.property('foo').that.deep.equals([foo]); + expect(events.get('foo')).to.deep.equal([foo]); }); it('should register handlers for any type strings', () => { - let foo = () => {}; + const foo = () => {}; inst.on('constructor', foo); - expect(events).to.have.property('constructor').that.deep.equals([foo]); + expect(events.get('constructor')).to.deep.equal([foo]); }); it('should append handler for existing type', () => { - let foo = () => {}; - let bar = () => {}; + const foo = () => {}; + const bar = () => {}; inst.on('foo', foo); inst.on('foo', bar); - expect(events).to.have.property('foo').that.deep.equals([foo, bar]); + expect(events.get('foo')).to.deep.equal([foo, bar]); }); it('should NOT normalize case', () => { - let foo = () => {}; + const foo = () => {}; inst.on('FOO', foo); inst.on('Bar', foo); inst.on('baz:baT!', foo); - expect(events).to.have.property('FOO').that.deep.equals([foo]); - expect(events).to.not.have.property('foo'); - expect(events).to.have.property('Bar').that.deep.equals([foo]); - expect(events).to.not.have.property('bar'); - expect(events).to.have.property('baz:baT!').that.deep.equals([foo]); + expect(events.get('FOO')).to.deep.equal([foo]); + expect(events.has('foo')).to.equal(false); + expect(events.get('Bar')).to.deep.equal([foo]); + expect(events.has('bar')).to.equal(false); + expect(events.get('baz:baT!')).to.deep.equal([foo]); + }); + + it('can take symbols for event types', () => { + const foo = () => {}; + const eventType = Symbol('eventType'); + inst.on(eventType, foo); + expect(events.get(eventType)).to.deep.equal([foo]); }); }); @@ -68,15 +75,15 @@ describe('mitt#', () => { }); it('should remove handler for type', () => { - let foo = () => {}; + const foo = () => {}; inst.on('foo', foo); inst.off('foo', foo); - expect(events).to.have.property('foo').that.is.empty; + expect(events.get('foo')).to.be.empty; }); it('should NOT normalize case', () => { - let foo = () => {}; + const foo = () => {}; inst.on('FOO', foo); inst.on('Bar', foo); inst.on('baz:bat!', foo); @@ -85,11 +92,11 @@ describe('mitt#', () => { inst.off('Bar', foo); inst.off('baz:baT!', foo); - expect(events).to.have.property('FOO').that.is.empty; - expect(events).to.not.have.property('foo'); - expect(events).to.have.property('Bar').that.is.empty; - expect(events).to.not.have.property('bar'); - expect(events).to.have.property('baz:bat!').with.length(1); + expect(events.get('FOO')).to.be.empty; + expect(events.has('foo')).to.equal(false); + expect(events.get('Bar')).to.be.empty; + expect(events.has('bar')).to.equal(false); + expect(events.get('baz:bat!')).to.have.lengthOf(1); }); }); @@ -101,7 +108,7 @@ describe('mitt#', () => { }); it('should invoke handler for type', () => { - let event = { a: 'b' }; + const event = { a: 'b' }; inst.on('foo', (one, two) => { expect(one).to.deep.equal(event); @@ -112,10 +119,10 @@ describe('mitt#', () => { }); it('should NOT ignore case', () => { - let onFoo = spy(), + const onFoo = spy(), onFOO = spy(); - events.Foo = [onFoo]; - events.FOO = [onFOO]; + events.set('Foo', [onFoo]); + events.set('FOO', [onFOO]); inst.emit('Foo', 'Foo arg'); inst.emit('FOO', 'FOO arg'); @@ -125,11 +132,11 @@ describe('mitt#', () => { }); it('should invoke * handlers', () => { - let star = spy(), + const star = spy(), ea = { a: 'a' }, eb = { b: 'b' }; - events['*'] = [star]; + events.set('*', [star]); inst.emit('foo', ea); expect(star).to.have.been.calledOnce.and.calledWith('foo', ea); diff --git a/typings.json b/typings.json deleted file mode 100644 index ad8ab5f..0000000 --- a/typings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "mitt", - "main": "mitt.d.ts", - "version": false -} \ No newline at end of file -- cgit v1.2.3 From 34ce9a46ccc5b17681b2ffab234575a05ecdf4d1 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 26 May 2020 17:46:48 -0400 Subject: remove older gzip size calculation --- package.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index ac14731..eb07568 100644 --- a/package.json +++ b/package.json @@ -14,10 +14,9 @@ "lint": "eslint src test --ext ts --ext js", "test": "tsc src/index.ts --noEmit && npm run lint && npm run testonly", "bundle": "microbundle", - "build": "npm-run-all --silent clean -p bundle -s docs size", + "build": "npm-run-all --silent clean -p bundle -s docs", "clean": "rimraf dist", "docs": "documentation readme src/index.ts --section API -q --parse-extension ts", - "size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size | pretty-bytes)\"", "release": "npm run build -s && npm run bump && git push --follow-tags origin master && npm publish" }, "repository": "developit/mitt", @@ -70,16 +69,13 @@ "documentation": "^13.0.0", "eslint": "^6.5.1", "esm": "^3.2.25", - "gzip-size-cli": "^1.0.0", "microbundle": "^0.12.0", "mocha": "^3.2.0", "npm-run-all": "^2.1.1", - "pretty-bytes-cli": "^2.0.0", "rimraf": "^2.5.2", "sinon": "^1.17.4", "sinon-chai": "^2.8.0", "standard-version": "^4.0.0", - "strip-json-comments-cli": "^1.0.1", "ts-node": "^8.10.1", "typescript": "^3.9.3" } -- cgit v1.2.3 From 0ead0bafdee03a0da3ea552d847b712b7825d562 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 26 May 2020 17:51:54 -0400 Subject: simple build script --- package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index eb07568..b18580a 100644 --- a/package.json +++ b/package.json @@ -5,11 +5,11 @@ "jsnext:main": "dist/mitt.es.js", "source": "src/index.ts", "module": "dist/mitt.es.js", + "esmodules": "dist/mitt.modern.js", "main": "dist/mitt.js", "umd:main": "dist/mitt.umd.js", "typings": "dist/index.d.ts", "scripts": { - "bump": "standard-version", "testonly": "mocha --require esm --require ts-node/register test/**/*.js", "lint": "eslint src test --ext ts --ext js", "test": "tsc src/index.ts --noEmit && npm run lint && npm run testonly", @@ -17,7 +17,7 @@ "build": "npm-run-all --silent clean -p bundle -s docs", "clean": "rimraf dist", "docs": "documentation readme src/index.ts --section API -q --parse-extension ts", - "release": "npm run build -s && npm run bump && git push --follow-tags origin master && npm publish" + "release": "npm run -s build -s && npm t && git tag $npm_package_version && git push && git push --tags && npm publish" }, "repository": "developit/mitt", "keywords": [ @@ -75,7 +75,6 @@ "rimraf": "^2.5.2", "sinon": "^1.17.4", "sinon-chai": "^2.8.0", - "standard-version": "^4.0.0", "ts-node": "^8.10.1", "typescript": "^3.9.3" } -- cgit v1.2.3 From 45944633f2289e0237ac548a32bf1be0742dff20 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 26 May 2020 18:43:49 -0400 Subject: 2.0.0 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index b18580a..a0dbdf1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mitt", - "version": "1.2.0", + "version": "2.0.0", "description": "Tiny 200b functional Event Emitter / pubsub.", "jsnext:main": "dist/mitt.es.js", "source": "src/index.ts", @@ -17,7 +17,7 @@ "build": "npm-run-all --silent clean -p bundle -s docs", "clean": "rimraf dist", "docs": "documentation readme src/index.ts --section API -q --parse-extension ts", - "release": "npm run -s build -s && npm t && git tag $npm_package_version && git push && git push --tags && npm publish" + "release": "npm run -s build -s && npm t && git commit -m $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, "repository": "developit/mitt", "keywords": [ -- cgit v1.2.3 From dd64eea1de4cc6488363c5701b4890ac92fe09b2 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 26 May 2020 18:53:49 -0400 Subject: Update all dependencies --- package.json | 23 ++++++++++++++--------- test/index.js | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index a0dbdf1..f2298f8 100644 --- a/package.json +++ b/package.json @@ -59,22 +59,27 @@ ], "@typescript-eslint/no-explicit-any": 0, "@typescript-eslint/explicit-function-return-type": 0, + "@typescript-eslint/explicit-module-boundary-types": 0, "@typescript-eslint/no-empty-function": 0 } }, "devDependencies": { - "@typescript-eslint/eslint-plugin": "^2.34.0", - "@typescript-eslint/parser": "^2.34.0", - "chai": "^3.5.0", + "@types/chai": "^4.2.11", + "@types/mocha": "^7.0.2", + "@types/sinon": "^9.0.4", + "@types/sinon-chai": "^3.2.4", + "@typescript-eslint/eslint-plugin": "^3.0.1", + "@typescript-eslint/parser": "^3.0.1", + "chai": "^4.2.0", "documentation": "^13.0.0", - "eslint": "^6.5.1", + "eslint": "^7.1.0", "esm": "^3.2.25", "microbundle": "^0.12.0", - "mocha": "^3.2.0", - "npm-run-all": "^2.1.1", - "rimraf": "^2.5.2", - "sinon": "^1.17.4", - "sinon-chai": "^2.8.0", + "mocha": "^7.2.0", + "npm-run-all": "^4.1.5", + "rimraf": "^3.0.2", + "sinon": "^9.0.2", + "sinon-chai": "^3.5.0", "ts-node": "^8.10.1", "typescript": "^3.9.3" } diff --git a/test/index.js b/test/index.js index 020c91f..dceb4b7 100644 --- a/test/index.js +++ b/test/index.js @@ -159,7 +159,7 @@ describe('mitt#', () => { inst.emit('foo', ea); expect(star).to.have.been.calledOnce.and.calledWith('foo', ea); - star.reset(); + star.resetHistory(); inst.emit('bar', eb); expect(star).to.have.been.calledOnce.and.calledWith('bar', eb); -- cgit v1.2.3 From 59cc3d1bc3b85d347baa8883b6206ba88a6071c0 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 27 May 2020 12:35:39 -0400 Subject: Use eslint-config-developit --- package.json | 3 ++- src/index.ts | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index f2298f8..08b12d2 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ ], "eslintConfig": { "extends": [ - "eslint:recommended", + "developit", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended" ], @@ -73,6 +73,7 @@ "chai": "^4.2.0", "documentation": "^13.0.0", "eslint": "^7.1.0", + "eslint-config-developit": "^1.2.0", "esm": "^3.2.25", "microbundle": "^0.12.0", "mocha": "^7.2.0", diff --git a/src/index.ts b/src/index.ts index e1503e2..f8e7e64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,13 +14,13 @@ type EventHandlerMap = Map(type: EventType, event?: T): void; - emit(type: "*", event?: any): void; + emit(type: '*', event?: any): void; } /** Mitt: Tiny (~200b) functional event emitter / pubsub. @@ -31,6 +31,7 @@ export default function mitt(all?: EventHandlerMap): Emitter { all = all || new Map(); return { + /** * Register an event handler for the given type. * @param {string|symbol} type Type of event to listen for, or `"*"` for all events -- cgit v1.2.3 From eb2be7caf6c88a41a184ef5ea6e4675f0f372771 Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Wed, 27 May 2020 18:39:01 +0100 Subject: Export Mitt types for TS consumers (#101) * Export Mitt types for TS consumers * Add rudimentary tests for exported TS types * Run tests against the generated output instead of src --- package.json | 11 +++++++++-- src/index.ts | 12 ++++++------ test/index.js | 20 +++++++++++++++++--- test/types.ts | 20 ++++++++++++++++++++ tsconfig.json | 11 +++++++++++ 5 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 test/types.ts create mode 100644 tsconfig.json (limited to 'package.json') diff --git a/package.json b/package.json index 08b12d2..36fb941 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,10 @@ "umd:main": "dist/mitt.umd.js", "typings": "dist/index.d.ts", "scripts": { - "testonly": "mocha --require esm --require ts-node/register test/**/*.js", + "test": "npm-run-all --silent typecheck lint testonly", + "testonly": "mocha --require esm test/**/*.js", "lint": "eslint src test --ext ts --ext js", - "test": "tsc src/index.ts --noEmit && npm run lint && npm run testonly", + "typecheck": "tsc **/*.ts --noEmit", "bundle": "microbundle", "build": "npm-run-all --silent clean -p bundle -s docs", "clean": "rimraf dist", @@ -23,6 +24,7 @@ "keywords": [ "events", "eventemitter", + "emitter", "pubsub" ], "homepage": "https://github.com/developit/mitt", @@ -47,6 +49,7 @@ "env": { "browser": true, "mocha": true, + "jest": false, "es6": true }, "globals": { @@ -57,12 +60,16 @@ 2, "always" ], + "jest/valid-expect": 0, "@typescript-eslint/no-explicit-any": 0, "@typescript-eslint/explicit-function-return-type": 0, "@typescript-eslint/explicit-module-boundary-types": 0, "@typescript-eslint/no-empty-function": 0 } }, + "eslintIgnore": [ + "dist" + ], "devDependencies": { "@types/chai": "^4.2.11", "@types/mocha": "^7.0.2", diff --git a/src/index.ts b/src/index.ts index f8e7e64..cfb1cf3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,16 @@ -type EventType = string | symbol; +export type EventType = string | symbol; // An event handler can take an optional event argument // and should not return a value -type Handler = (event?: any) => void; -type WildcardHandler= (type: EventType, event?: any) => void +export type Handler = (event?: any) => void; +export type WildcardHandler= (type: EventType, event?: any) => void // An array of all currently registered event handlers for a type -type EventHandlerList = Array; -type WildCardEventHandlerList = Array; +export type EventHandlerList = Array; +export type WildCardEventHandlerList = Array; // A map of event types and their corresponding event handlers. -type EventHandlerMap = Map; +export type EventHandlerMap = Map; export interface Emitter { on(type: EventType, handler: Handler): void; diff --git a/test/index.js b/test/index.js index dceb4b7..a837d36 100644 --- a/test/index.js +++ b/test/index.js @@ -1,11 +1,25 @@ -import mitt from '../src'; +import mitt from '..'; import chai, { expect } from 'chai'; import { spy } from 'sinon'; import sinonChai from 'sinon-chai'; chai.use(sinonChai); -it('should default export be a function', () => { - expect(mitt).to.be.a('function'); +describe('mitt', () => { + it('should default export be a function', () => { + expect(mitt).to.be.a('function'); + }); + + it('should accept an optional event handler map', () => { + expect(() => mitt(new Map())).not.to.throw; + const map = new Map(); + const a = spy(); + const b = spy(); + map.set('foo', [a, b]); + const events = mitt(map); + events.emit('foo'); + expect(a).to.have.been.calledOnce; + expect(b).to.have.been.calledOnce; + }); }); describe('mitt#', () => { diff --git a/test/types.ts b/test/types.ts new file mode 100644 index 0000000..23334bb --- /dev/null +++ b/test/types.ts @@ -0,0 +1,20 @@ +import mitt, { EventHandlerList, EventHandlerMap } from '..'; + +const events = mitt(); +function foo() {} +events.on('foo', foo); +events.emit('foo', 'hello'); + +// handler return type should be ignored: +events.on('foo', async e => e * 42); + +// event map type +const map = new Map([ + ['foo', [foo]] +]); +const events2 = mitt(map); +events2.emit('foo', 'hello'); + +// event map type & iterables +const map2 : EventHandlerMap = new Map(Object.entries(({ foo: [foo] }))); +mitt(map2); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..2610831 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "noEmit": true, + "declaration": true, + "moduleResolution": "node" + }, + "exclude": [ + "test" + ] +} -- cgit v1.2.3 From 59591757df1a563274e56ee1f80a54818834afca Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 27 May 2020 13:42:51 -0400 Subject: 2.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'package.json') diff --git a/package.json b/package.json index 36fb941..90b7b74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mitt", - "version": "2.0.0", + "version": "2.0.1", "description": "Tiny 200b functional Event Emitter / pubsub.", "jsnext:main": "dist/mitt.es.js", "source": "src/index.ts", -- cgit v1.2.3 From 5116df46a020ae498eec6783cfe9147fc9add015 Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Wed, 15 Jul 2020 15:31:43 +0100 Subject: Add generic types and update tests (#107) * Add generic types and update tests * Add generic types to `on`, `off` and `emit` to enable some nicer TS usage if you specify what type you're expecting from the `EventData`. * Move the tests to be TypeScript source. This will help catch errors in the tests if there are any type errors. * Create a new test to test the generic types and make sure they pass and error when expected. * Upgrade to Mocha 8. * Did some tidying up of the package.json scripts. * Tweak TS setup to validate tests * Fix d.ts generation and tests Co-authored-by: Jason Miller --- .editorconfig | 1 + .gitignore | 1 + README.md | 2 +- package.json | 35 +++++--- src/index.ts | 14 ++-- test/index.js | 182 ----------------------------------------- test/index_test.ts | 182 +++++++++++++++++++++++++++++++++++++++++ test/test-types-compilation.ts | 43 ++++++++++ test/types.ts | 20 ----- tsconfig.json | 8 +- 10 files changed, 265 insertions(+), 223 deletions(-) delete mode 100644 test/index.js create mode 100644 test/index_test.ts create mode 100644 test/test-types-compilation.ts delete mode 100644 test/types.ts (limited to 'package.json') diff --git a/.editorconfig b/.editorconfig index ac0adb7..04d7ef9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,6 +10,7 @@ insert_final_newline = true [{package.json,.*rc,*.yml}] indent_style = space indent_size = 2 +insert_final_newline = false [*.md] trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore index ecdffab..eb570d3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /test-reports /node_modules /npm-debug.log +/index.d.ts package-lock.json .DS_Store .idea diff --git a/README.md b/README.md index 0796e5f..b6a8022 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ const emitter: mitt.Emitter = mitt(); Mitt: Tiny (~200b) functional event emitter / pubsub. -Returns **Mitt** +Returns **Mitt** ### on diff --git a/package.json b/package.json index 90b7b74..a2c2b58 100644 --- a/package.json +++ b/package.json @@ -8,12 +8,13 @@ "esmodules": "dist/mitt.modern.js", "main": "dist/mitt.js", "umd:main": "dist/mitt.umd.js", - "typings": "dist/index.d.ts", + "typings": "index.d.ts", "scripts": { - "test": "npm-run-all --silent typecheck lint testonly", - "testonly": "mocha --require esm test/**/*.js", + "test": "npm-run-all --silent typecheck lint mocha test-types", + "mocha": "mocha test", + "test-types": "tsc test/test-types-compilation.ts --noEmit", "lint": "eslint src test --ext ts --ext js", - "typecheck": "tsc **/*.ts --noEmit", + "typecheck": "tsc --noEmit", "bundle": "microbundle", "build": "npm-run-all --silent clean -p bundle -s docs", "clean": "rimraf dist", @@ -34,8 +35,21 @@ "license": "MIT", "files": [ "src", - "dist" + "dist", + "index.d.ts" ], + "mocha": { + "extension": [ + "ts" + ], + "require": [ + "ts-node/register", + "esm" + ], + "spec": [ + "test/*_test.ts" + ] + }, "eslintConfig": { "extends": [ "developit", @@ -68,7 +82,8 @@ } }, "eslintIgnore": [ - "dist" + "dist", + "index.d.ts" ], "devDependencies": { "@types/chai": "^4.2.11", @@ -82,13 +97,13 @@ "eslint": "^7.1.0", "eslint-config-developit": "^1.2.0", "esm": "^3.2.25", - "microbundle": "^0.12.0", - "mocha": "^7.2.0", + "microbundle": "^0.12.3", + "mocha": "^8.0.1", "npm-run-all": "^4.1.5", "rimraf": "^3.0.2", "sinon": "^9.0.2", "sinon-chai": "^3.5.0", - "ts-node": "^8.10.1", + "ts-node": "^8.10.2", "typescript": "^3.9.3" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e640292..c681fd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,8 @@ export type EventType = string | symbol; // An event handler can take an optional event argument // and should not return a value -export type Handler = (event?: any) => void; -export type WildcardHandler = (type: EventType, event?: any) => void +export type Handler = (event?: T) => void; +export type WildcardHandler = (type: EventType, event?: any) => void; // An array of all currently registered event handlers for a type export type EventHandlerList = Array; @@ -13,10 +13,10 @@ export type WildCardEventHandlerList = Array; export type EventHandlerMap = Map; export interface Emitter { - on(type: EventType, handler: Handler): void; + on(type: EventType, handler: Handler): void; on(type: '*', handler: WildcardHandler): void; - off(type: EventType, handler: Handler): void; + off(type: EventType, handler: Handler): void; off(type: '*', handler: WildcardHandler): void; emit(type: EventType, event?: T): void; @@ -38,7 +38,7 @@ export default function mitt(all?: EventHandlerMap): Emitter { * @param {Function} handler Function to call in response to given event * @memberOf mitt */ - on(type: EventType, handler: Handler) { + on(type: EventType, handler: Handler) { const handlers = all.get(type); const added = handlers && handlers.push(handler); if (!added) { @@ -53,7 +53,7 @@ export default function mitt(all?: EventHandlerMap): Emitter { * @param {Function} handler Handler function to remove * @memberOf mitt */ - off(type: EventType, handler: Handler) { + off(type: EventType, handler: Handler) { const handlers = all.get(type); if (handlers) { handlers.splice(handlers.indexOf(handler) >>> 0, 1); @@ -70,7 +70,7 @@ export default function mitt(all?: EventHandlerMap): Emitter { * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler * @memberOf mitt */ - emit(type: EventType, evt: any) { + emit(type: EventType, evt: T) { ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); }); ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); }); } diff --git a/test/index.js b/test/index.js deleted file mode 100644 index a837d36..0000000 --- a/test/index.js +++ /dev/null @@ -1,182 +0,0 @@ -import mitt from '..'; -import chai, { expect } from 'chai'; -import { spy } from 'sinon'; -import sinonChai from 'sinon-chai'; -chai.use(sinonChai); - -describe('mitt', () => { - it('should default export be a function', () => { - expect(mitt).to.be.a('function'); - }); - - it('should accept an optional event handler map', () => { - expect(() => mitt(new Map())).not.to.throw; - const map = new Map(); - const a = spy(); - const b = spy(); - map.set('foo', [a, b]); - const events = mitt(map); - events.emit('foo'); - expect(a).to.have.been.calledOnce; - expect(b).to.have.been.calledOnce; - }); -}); - -describe('mitt#', () => { - let events, inst; - - beforeEach( () => { - events = new Map(); - inst = mitt(events); - }); - - describe('on()', () => { - it('should be a function', () => { - expect(inst) - .to.have.property('on') - .that.is.a('function'); - }); - - it('should register handler for new type', () => { - const foo = () => {}; - inst.on('foo', foo); - - expect(events.get('foo')).to.deep.equal([foo]); - }); - - it('should register handlers for any type strings', () => { - const foo = () => {}; - inst.on('constructor', foo); - - expect(events.get('constructor')).to.deep.equal([foo]); - }); - - it('should append handler for existing type', () => { - const foo = () => {}; - const bar = () => {}; - inst.on('foo', foo); - inst.on('foo', bar); - - expect(events.get('foo')).to.deep.equal([foo, bar]); - }); - - it('should NOT normalize case', () => { - const foo = () => {}; - inst.on('FOO', foo); - inst.on('Bar', foo); - inst.on('baz:baT!', foo); - - expect(events.get('FOO')).to.deep.equal([foo]); - expect(events.has('foo')).to.equal(false); - expect(events.get('Bar')).to.deep.equal([foo]); - expect(events.has('bar')).to.equal(false); - expect(events.get('baz:baT!')).to.deep.equal([foo]); - }); - - it('can take symbols for event types', () => { - const foo = () => {}; - const eventType = Symbol('eventType'); - inst.on(eventType, foo); - expect(events.get(eventType)).to.deep.equal([foo]); - }); - - // Adding the same listener multiple times should register it multiple times. - // See https://nodejs.org/api/events.html#events_emitter_on_eventname_listener - it('should add duplicate listeners', () => { - const foo = () => {}; - inst.on('foo', foo); - inst.on('foo', foo); - expect(events.get('foo')).to.deep.equal([foo, foo]); - }); - }); - - describe('off()', () => { - it('should be a function', () => { - expect(inst) - .to.have.property('off') - .that.is.a('function'); - }); - - it('should remove handler for type', () => { - const foo = () => {}; - inst.on('foo', foo); - inst.off('foo', foo); - - expect(events.get('foo')).to.be.empty; - }); - - it('should NOT normalize case', () => { - const foo = () => {}; - inst.on('FOO', foo); - inst.on('Bar', foo); - inst.on('baz:bat!', foo); - - inst.off('FOO', foo); - inst.off('Bar', foo); - inst.off('baz:baT!', foo); - - expect(events.get('FOO')).to.be.empty; - expect(events.has('foo')).to.equal(false); - expect(events.get('Bar')).to.be.empty; - expect(events.has('bar')).to.equal(false); - expect(events.get('baz:bat!')).to.have.lengthOf(1); - }); - - it('should remove only the first matching listener', () => { - const foo = () => {}; - inst.on('foo', foo); - inst.on('foo', foo); - inst.off('foo', foo); - expect(events.get('foo')).to.deep.equal([foo]); - inst.off('foo', foo); - expect(events.get('foo')).to.deep.equal([]); - }); - }); - - describe('emit()', () => { - it('should be a function', () => { - expect(inst) - .to.have.property('emit') - .that.is.a('function'); - }); - - it('should invoke handler for type', () => { - const event = { a: 'b' }; - - inst.on('foo', (one, two) => { - expect(one).to.deep.equal(event); - expect(two).to.be.an('undefined'); - }); - - inst.emit('foo', event); - }); - - it('should NOT ignore case', () => { - const onFoo = spy(), - onFOO = spy(); - events.set('Foo', [onFoo]); - events.set('FOO', [onFOO]); - - inst.emit('Foo', 'Foo arg'); - inst.emit('FOO', 'FOO arg'); - - expect(onFoo).to.have.been.calledOnce.and.calledWith('Foo arg'); - expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg'); - }); - - it('should invoke * handlers', () => { - const star = spy(), - ea = { a: 'a' }, - eb = { b: 'b' }; - - events.set('*', [star]); - - inst.emit('foo', ea); - expect(star).to.have.been.calledOnce.and.calledWith('foo', ea); - star.resetHistory(); - - inst.emit('bar', eb); - expect(star).to.have.been.calledOnce.and.calledWith('bar', eb); - }); - }); -}); diff --git a/test/index_test.ts b/test/index_test.ts new file mode 100644 index 0000000..1f1d9bb --- /dev/null +++ b/test/index_test.ts @@ -0,0 +1,182 @@ +import mitt, { Emitter } from '..'; +import chai, { expect } from 'chai'; +import { spy } from 'sinon'; +import sinonChai from 'sinon-chai'; +chai.use(sinonChai); + +describe('mitt', () => { + it('should default export be a function', () => { + expect(mitt).to.be.a('function'); + }); + + it('should accept an optional event handler map', () => { + expect(() => mitt(new Map())).not.to.throw; + const map = new Map(); + const a = spy(); + const b = spy(); + map.set('foo', [a, b]); + const events = mitt(map); + events.emit('foo'); + expect(a).to.have.been.calledOnce; + expect(b).to.have.been.calledOnce; + }); +}); + +describe('mitt#', () => { + let events, inst: Emitter; + + beforeEach( () => { + events = new Map(); + inst = mitt(events); + }); + + describe('on()', () => { + it('should be a function', () => { + expect(inst) + .to.have.property('on') + .that.is.a('function'); + }); + + it('should register handler for new type', () => { + const foo = () => {}; + inst.on('foo', foo); + + expect(events.get('foo')).to.deep.equal([foo]); + }); + + it('should register handlers for any type strings', () => { + const foo = () => {}; + inst.on('constructor', foo); + + expect(events.get('constructor')).to.deep.equal([foo]); + }); + + it('should append handler for existing type', () => { + const foo = () => {}; + const bar = () => {}; + inst.on('foo', foo); + inst.on('foo', bar); + + expect(events.get('foo')).to.deep.equal([foo, bar]); + }); + + it('should NOT normalize case', () => { + const foo = () => {}; + inst.on('FOO', foo); + inst.on('Bar', foo); + inst.on('baz:baT!', foo); + + expect(events.get('FOO')).to.deep.equal([foo]); + expect(events.has('foo')).to.equal(false); + expect(events.get('Bar')).to.deep.equal([foo]); + expect(events.has('bar')).to.equal(false); + expect(events.get('baz:baT!')).to.deep.equal([foo]); + }); + + it('can take symbols for event types', () => { + const foo = () => {}; + const eventType = Symbol('eventType'); + inst.on(eventType, foo); + expect(events.get(eventType)).to.deep.equal([foo]); + }); + + // Adding the same listener multiple times should register it multiple times. + // See https://nodejs.org/api/events.html#events_emitter_on_eventname_listener + it('should add duplicate listeners', () => { + const foo = () => {}; + inst.on('foo', foo); + inst.on('foo', foo); + expect(events.get('foo')).to.deep.equal([foo, foo]); + }); + }); + + describe('off()', () => { + it('should be a function', () => { + expect(inst) + .to.have.property('off') + .that.is.a('function'); + }); + + it('should remove handler for type', () => { + const foo = () => {}; + inst.on('foo', foo); + inst.off('foo', foo); + + expect(events.get('foo')).to.be.empty; + }); + + it('should NOT normalize case', () => { + const foo = () => {}; + inst.on('FOO', foo); + inst.on('Bar', foo); + inst.on('baz:bat!', foo); + + inst.off('FOO', foo); + inst.off('Bar', foo); + inst.off('baz:baT!', foo); + + expect(events.get('FOO')).to.be.empty; + expect(events.has('foo')).to.equal(false); + expect(events.get('Bar')).to.be.empty; + expect(events.has('bar')).to.equal(false); + expect(events.get('baz:bat!')).to.have.lengthOf(1); + }); + + it('should remove only the first matching listener', () => { + const foo = () => {}; + inst.on('foo', foo); + inst.on('foo', foo); + inst.off('foo', foo); + expect(events.get('foo')).to.deep.equal([foo]); + inst.off('foo', foo); + expect(events.get('foo')).to.deep.equal([]); + }); + }); + + describe('emit()', () => { + it('should be a function', () => { + expect(inst) + .to.have.property('emit') + .that.is.a('function'); + }); + + it('should invoke handler for type', () => { + const event = { a: 'b' }; + + inst.on('foo', (one, two?) => { + expect(one).to.deep.equal(event); + expect(two).to.be.an('undefined'); + }); + + inst.emit('foo', event); + }); + + it('should NOT ignore case', () => { + const onFoo = spy(), + onFOO = spy(); + events.set('Foo', [onFoo]); + events.set('FOO', [onFOO]); + + inst.emit('Foo', 'Foo arg'); + inst.emit('FOO', 'FOO arg'); + + expect(onFoo).to.have.been.calledOnce.and.calledWith('Foo arg'); + expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg'); + }); + + it('should invoke * handlers', () => { + const star = spy(), + ea = { a: 'a' }, + eb = { b: 'b' }; + + events.set('*', [star]); + + inst.emit('foo', ea); + expect(star).to.have.been.calledOnce.and.calledWith('foo', ea); + star.resetHistory(); + + inst.emit('bar', eb); + expect(star).to.have.been.calledOnce.and.calledWith('bar', eb); + }); + }); +}); diff --git a/test/test-types-compilation.ts b/test/test-types-compilation.ts new file mode 100644 index 0000000..00510da --- /dev/null +++ b/test/test-types-compilation.ts @@ -0,0 +1,43 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unused-vars */ + +import mitt from '..'; + +const emitter = mitt(); + +/* + * Check that if on is provided a generic, it only accepts handlers of that type + */ +{ + const badHandler = (x: number) => {}; + const goodHandler = (x: string) => {}; + + // @ts-expect-error + emitter.on('foo', badHandler); + emitter.on('foo', goodHandler); +} + +/* + * Check that if off is provided a generic, it only accepts handlers of that type + */ +{ + const badHandler = (x: number) => {}; + const goodHandler = (x: string) => {}; + + // @ts-expect-error + emitter.off('foo', badHandler); + emitter.off('foo', goodHandler); +} + + +/* + * Check that if emitt is provided a generic, it only accepts event data of that type + */ +{ + interface SomeEventData { + name: string; + } + // @ts-expect-error + emitter.emit('foo', 'NOT VALID'); + emitter.emit('foo', { name: 'jack' }); +} + diff --git a/test/types.ts b/test/types.ts deleted file mode 100644 index 23334bb..0000000 --- a/test/types.ts +++ /dev/null @@ -1,20 +0,0 @@ -import mitt, { EventHandlerList, EventHandlerMap } from '..'; - -const events = mitt(); -function foo() {} -events.on('foo', foo); -events.emit('foo', 'hello'); - -// handler return type should be ignored: -events.on('foo', async e => e * 42); - -// event map type -const map = new Map([ - ['foo', [foo]] -]); -const events2 = mitt(map); -events2.emit('foo', 'hello'); - -// event map type & iterables -const map2 : EventHandlerMap = new Map(Object.entries(({ foo: [foo] }))); -mitt(map2); diff --git a/tsconfig.json b/tsconfig.json index 2610831..acab4f5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,9 +3,11 @@ "compilerOptions": { "noEmit": true, "declaration": true, - "moduleResolution": "node" + "moduleResolution": "node", + "esModuleInterop": true }, - "exclude": [ - "test" + "include": [ + "src/*.ts", + "test/*.ts", ] } -- cgit v1.2.3 From 22c5dcba10736aecb1f39ee88d9f85278108c988 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 15 Jul 2020 15:00:13 +0000 Subject: 2.1.0 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index a2c2b58..51b5cf8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mitt", - "version": "2.0.1", + "version": "2.1.0", "description": "Tiny 200b functional Event Emitter / pubsub.", "jsnext:main": "dist/mitt.es.js", "source": "src/index.ts", @@ -19,7 +19,7 @@ "build": "npm-run-all --silent clean -p bundle -s docs", "clean": "rimraf dist", "docs": "documentation readme src/index.ts --section API -q --parse-extension ts", - "release": "npm run -s build -s && npm t && git commit -m $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" + "release": "npm run -s build -s && npm t && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, "repository": "developit/mitt", "keywords": [ -- cgit v1.2.3 From 8f439b8ae54d3864c24ddf7205297031d64fbad6 Mon Sep 17 00:00:00 2001 From: iyegoroff Date: Wed, 23 Jun 2021 00:22:49 +0300 Subject: Stronger typing (#114) * improved typing * upd readme * upd readme * upd test & readme * removed magic 1 * removed ts-toolbelt dependency --- .gitignore | 1 + README.md | 18 +++++++--- package.json | 9 ++--- src/index.ts | 71 ++++++++++++++++++++++++--------------- test/index_test.ts | 25 ++++++++++---- test/test-types-compilation.ts | 75 +++++++++++++++++++++++++++++++----------- tsconfig.json | 6 ++-- 7 files changed, 139 insertions(+), 66 deletions(-) (limited to 'package.json') diff --git a/.gitignore b/.gitignore index eb570d3..bf29d4f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ package-lock.json .DS_Store .idea +.vscode diff --git a/README.md b/README.md index 08a21bf..0fe8dc4 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,17 @@ emitter.off('foo', onFoo) // unlisten ### Typescript +Set `"strict": true` in your tsconfig.json to get improved type inference for `mitt` instance methods. + ```ts import mitt from 'mitt'; -const emitter: mitt.Emitter = mitt(); + +type Events = { + foo: string + bar?: number +} + +const emitter: mitt.Emitter = mitt(); ``` ## Examples & Demos @@ -126,7 +134,7 @@ Register an event handler for the given type. #### Parameters -- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `"*"` for all events +- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `'*'` for all events - `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event ### off @@ -135,15 +143,15 @@ Remove an event handler for the given type. #### Parameters -- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `"*"` +- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `'*'` - `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove ### emit Invoke all handlers for the given type. -If present, `"*"` handlers are invoked after type-matched handlers. +If present, `'*'` handlers are invoked after type-matched handlers. -Note: Manually firing "\*" handlers is not supported. +Note: Manually firing '\*' handlers is not supported. #### Parameters diff --git a/package.json b/package.json index 51b5cf8..b34f8e9 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "scripts": { "test": "npm-run-all --silent typecheck lint mocha test-types", "mocha": "mocha test", - "test-types": "tsc test/test-types-compilation.ts --noEmit", + "test-types": "tsc test/test-types-compilation.ts --noEmit --strict", "lint": "eslint src test --ext ts --ext js", "typecheck": "tsc --noEmit", "bundle": "microbundle", @@ -78,7 +78,8 @@ "@typescript-eslint/no-explicit-any": 0, "@typescript-eslint/explicit-function-return-type": 0, "@typescript-eslint/explicit-module-boundary-types": 0, - "@typescript-eslint/no-empty-function": 0 + "@typescript-eslint/no-empty-function": 0, + "@typescript-eslint/no-non-null-assertion": 0 } }, "eslintIgnore": [ @@ -104,6 +105,6 @@ "sinon": "^9.0.2", "sinon-chai": "^3.5.0", "ts-node": "^8.10.2", - "typescript": "^3.9.3" + "typescript": "^3.9.7" } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index ae85607..00286bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,27 +2,33 @@ export type EventType = string | symbol; // An event handler can take an optional event argument // and should not return a value -export type Handler = (event?: T) => void; -export type WildcardHandler = (type: EventType, event?: any) => void; +export type Handler = (event: T) => void; +export type WildcardHandler> = ( + type: keyof T, + event: T[keyof T] +) => void; // An array of all currently registered event handlers for a type -export type EventHandlerList = Array; -export type WildCardEventHandlerList = Array; +export type EventHandlerList = Array>; +export type WildCardEventHandlerList> = Array>; // A map of event types and their corresponding event handlers. -export type EventHandlerMap = Map; +export type EventHandlerMap> = Map< + keyof Events | '*', + EventHandlerList | WildCardEventHandlerList +>; -export interface Emitter { - all: EventHandlerMap; +export interface Emitter> { + all: EventHandlerMap; - on(type: EventType, handler: Handler): void; - on(type: '*', handler: WildcardHandler): void; + on(type: Key, handler: Handler): void; + on(type: '*', handler: WildcardHandler): void; - off(type: EventType, handler: Handler): void; - off(type: '*', handler: WildcardHandler): void; + off(type: Key, handler: Handler): void; + off(type: '*', handler: WildcardHandler): void; - emit(type: EventType, event?: T): void; - emit(type: '*', event?: any): void; + emit(type: Key, event: Events[Key]): void; + emit(type: undefined extends Events[Key] ? Key : never): void; } /** @@ -30,7 +36,12 @@ export interface Emitter { * @name mitt * @returns {Mitt} */ -export default function mitt(all?: EventHandlerMap): Emitter { +export default function mitt>( + all?: EventHandlerMap +): Emitter { + type GenericEventHandler = + | Handler + | WildcardHandler; all = all || new Map(); return { @@ -42,26 +53,26 @@ export default function mitt(all?: EventHandlerMap): Emitter { /** * Register an event handler for the given type. - * @param {string|symbol} type Type of event to listen for, or `"*"` for all events + * @param {string|symbol} type Type of event to listen for, or `'*'` for all events * @param {Function} handler Function to call in response to given event * @memberOf mitt */ - on(type: EventType, handler: Handler) { - const handlers = all.get(type); + on(type: Key, handler: GenericEventHandler) { + const handlers: Array | undefined = all!.get(type); const added = handlers && handlers.push(handler); if (!added) { - all.set(type, [handler]); + all!.set(type, [handler] as EventHandlerList); } }, /** * Remove an event handler for the given type. - * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"` + * @param {string|symbol} type Type of event to unregister `handler` from, or `'*'` * @param {Function} handler Handler function to remove * @memberOf mitt */ - off(type: EventType, handler: Handler) { - const handlers = all.get(type); + off(type: Key, handler: GenericEventHandler) { + const handlers: Array | undefined = all!.get(type); if (handlers) { handlers.splice(handlers.indexOf(handler) >>> 0, 1); } @@ -69,17 +80,25 @@ export default function mitt(all?: EventHandlerMap): Emitter { /** * Invoke all handlers for the given type. - * If present, `"*"` handlers are invoked after type-matched handlers. + * If present, `'*'` handlers are invoked after type-matched handlers. * - * Note: Manually firing "*" handlers is not supported. + * Note: Manually firing '*' handlers is not supported. * * @param {string|symbol} type The event type to invoke * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler * @memberOf mitt */ - emit(type: EventType, evt: T) { - ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); }); - ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); }); + emit(type: Key, evt?: Events[Key]) { + ((all!.get(type) || []) as EventHandlerList) + .slice() + .map((handler) => { + handler(evt!); + }); + ((all!.get('*') || []) as WildCardEventHandlerList) + .slice() + .map((handler) => { + handler(type, evt!); + }); } }; } diff --git a/test/index_test.ts b/test/index_test.ts index 5c25b55..ba1dbff 100644 --- a/test/index_test.ts +++ b/test/index_test.ts @@ -1,4 +1,4 @@ -import mitt, { Emitter } from '..'; +import mitt, { Emitter, EventHandlerMap } from '..'; import chai, { expect } from 'chai'; import { spy } from 'sinon'; import sinonChai from 'sinon-chai'; @@ -15,7 +15,7 @@ describe('mitt', () => { const a = spy(); const b = spy(); map.set('foo', [a, b]); - const events = mitt(map); + const events = mitt<{ foo: undefined }>(map); events.emit('foo'); expect(a).to.have.been.calledOnce; expect(b).to.have.been.calledOnce; @@ -23,9 +23,21 @@ describe('mitt', () => { }); describe('mitt#', () => { - let events, inst: Emitter; - - beforeEach( () => { + const eventType = Symbol('eventType'); + type Events = { + foo: unknown; + constructor: unknown; + FOO: unknown; + bar: unknown; + Bar: unknown; + 'baz:bat!': unknown; + 'baz:baT!': unknown; + Foo: unknown; + [eventType]: unknown; + }; + let events: EventHandlerMap, inst: Emitter; + + beforeEach(() => { events = new Map(); inst = mitt(events); }); @@ -83,7 +95,6 @@ describe('mitt#', () => { it('can take symbols for event types', () => { const foo = () => {}; - const eventType = Symbol('eventType'); inst.on(eventType, foo); expect(events.get(eventType)).to.deep.equal([foo]); }); @@ -151,7 +162,7 @@ describe('mitt#', () => { it('should invoke handler for type', () => { const event = { a: 'b' }; - inst.on('foo', (one, two?) => { + inst.on('foo', (one, two?: unknown) => { expect(one).to.deep.equal(event); expect(two).to.be.an('undefined'); }); diff --git a/test/test-types-compilation.ts b/test/test-types-compilation.ts index 00510da..476e631 100644 --- a/test/test-types-compilation.ts +++ b/test/test-types-compilation.ts @@ -2,42 +2,77 @@ import mitt from '..'; -const emitter = mitt(); +interface SomeEventData { + name: string; +} + +const emitter = mitt<{ + foo: string; + someEvent: SomeEventData; + bar?: number; +}>(); + +const barHandler = (x?: number) => {}; +const fooHandler = (x: string) => {}; +const wildcardHandler = ( + _type: 'foo' | 'bar' | 'someEvent', + _event: string | SomeEventData | number | undefined +) => {}; /* - * Check that if on is provided a generic, it only accepts handlers of that type + * Check that 'on' args are inferred correctly */ { - const badHandler = (x: number) => {}; - const goodHandler = (x: string) => {}; + // @ts-expect-error + emitter.on('foo', barHandler); + emitter.on('foo', fooHandler); + emitter.on('bar', barHandler); // @ts-expect-error - emitter.on('foo', badHandler); - emitter.on('foo', goodHandler); + emitter.on('bar', fooHandler); + + emitter.on('*', wildcardHandler); + // fooHandler is ok, because ('foo' | 'bar' | 'someEvent') extends string + emitter.on('*', fooHandler); + // @ts-expect-error + emitter.on('*', barHandler); } /* - * Check that if off is provided a generic, it only accepts handlers of that type + * Check that 'off' args are inferred correctly */ { - const badHandler = (x: number) => {}; - const goodHandler = (x: string) => {}; + // @ts-expect-error + emitter.off('foo', barHandler); + emitter.off('foo', fooHandler); + emitter.off('bar', barHandler); // @ts-expect-error - emitter.off('foo', badHandler); - emitter.off('foo', goodHandler); -} + emitter.off('bar', fooHandler); + emitter.off('*', wildcardHandler); + // fooHandler is ok, because ('foo' | 'bar' | 'someEvent') extends string + emitter.off('*', fooHandler); + // @ts-expect-error + emitter.off('*', barHandler); +} /* - * Check that if emitt is provided a generic, it only accepts event data of that type + * Check that 'emit' args are inferred correctly */ { - interface SomeEventData { - name: string; - } - // @ts-expect-error - emitter.emit('foo', 'NOT VALID'); - emitter.emit('foo', { name: 'jack' }); -} + // @ts-expect-error + emitter.emit('someEvent', 'NOT VALID'); + emitter.emit('someEvent', { name: 'jack' }); + + // @ts-expect-error + emitter.emit('foo'); + // @ts-expect-error + emitter.emit('foo', 1); + emitter.emit('foo', 'string'); + emitter.emit('bar'); + emitter.emit('bar', 1); + // @ts-expect-error + emitter.emit('bar', 'string'); +} diff --git a/tsconfig.json b/tsconfig.json index acab4f5..563d58e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,13 +1,11 @@ { "compileOnSave": false, "compilerOptions": { + "strict": true, "noEmit": true, "declaration": true, "moduleResolution": "node", "esModuleInterop": true }, - "include": [ - "src/*.ts", - "test/*.ts", - ] + "include": ["src/*.ts", "test/*.ts"] } -- cgit v1.2.3 From 62a2351bbe57caea0e7a0fe650603ea0a07ba554 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 22 Jun 2021 17:37:46 -0400 Subject: Remove "src" from published files list (fixes #128) --- package.json | 1 - 1 file changed, 1 deletion(-) (limited to 'package.json') diff --git a/package.json b/package.json index b34f8e9..666ffe7 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ ], "license": "MIT", "files": [ - "src", "dist", "index.d.ts" ], -- cgit v1.2.3 From f3990ec5e34477d2fc21644c91a64d7a0bc004c1 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 23 Jun 2021 16:40:45 -0400 Subject: Add Package Exports configuration (#139) * Add Package Exports configuration (semver major) * Update compressed-size.yml --- .github/workflows/compressed-size.yml | 2 ++ package.json | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'package.json') diff --git a/.github/workflows/compressed-size.yml b/.github/workflows/compressed-size.yml index 958fd22..45cad2f 100644 --- a/.github/workflows/compressed-size.yml +++ b/.github/workflows/compressed-size.yml @@ -8,3 +8,5 @@ jobs: steps: - uses: actions/checkout@v2 - uses: preactjs/compressed-size-action@v2 + with: + pattern: "./dist/*.{js,mjs,cjs}" diff --git a/package.json b/package.json index 666ffe7..767bbca 100644 --- a/package.json +++ b/package.json @@ -2,20 +2,24 @@ "name": "mitt", "version": "2.1.0", "description": "Tiny 200b functional Event Emitter / pubsub.", - "jsnext:main": "dist/mitt.es.js", - "source": "src/index.ts", - "module": "dist/mitt.es.js", - "esmodules": "dist/mitt.modern.js", + "module": "dist/mitt.mjs", "main": "dist/mitt.js", + "jsnext:main": "dist/mitt.mjs", "umd:main": "dist/mitt.umd.js", + "source": "src/index.ts", "typings": "index.d.ts", + "exports": { + "import": "./dist/mitt.mjs", + "require": "./dist/mitt.js", + "default": "./dist/mitt.mjs" + }, "scripts": { "test": "npm-run-all --silent typecheck lint mocha test-types", "mocha": "mocha test", "test-types": "tsc test/test-types-compilation.ts --noEmit --strict", "lint": "eslint src test --ext ts --ext js", "typecheck": "tsc --noEmit", - "bundle": "microbundle", + "bundle": "microbundle -f es,cjs,umd", "build": "npm-run-all --silent clean -p bundle -s docs", "clean": "rimraf dist", "docs": "documentation readme src/index.ts --section API -q --parse-extension ts", -- cgit v1.2.3 From 13905bd4009e83f1876f2d31419e23cdbf4f9b48 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 23 Jun 2021 16:46:36 -0400 Subject: 3.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'package.json') diff --git a/package.json b/package.json index 767bbca..ad38c7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mitt", - "version": "2.1.0", + "version": "3.0.0", "description": "Tiny 200b functional Event Emitter / pubsub.", "module": "dist/mitt.mjs", "main": "dist/mitt.js", -- cgit v1.2.3 From 1194efba1adb575a1fdb26d7638f448faf5df2d4 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Tue, 25 Oct 2022 10:43:50 +0800 Subject: Update package.json (#167) --- package.json | 1 + 1 file changed, 1 insertion(+) (limited to 'package.json') diff --git a/package.json b/package.json index ad38c7b..ae9dc6f 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "source": "src/index.ts", "typings": "index.d.ts", "exports": { + "types": "./index.d.ts", "import": "./dist/mitt.mjs", "require": "./dist/mitt.js", "default": "./dist/mitt.mjs" -- cgit v1.2.3 From 5a17bef2d7bb833cac299dbc43d7961953461c04 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 4 Jul 2023 10:35:03 -0400 Subject: update deps --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index ae9dc6f..a989165 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "@typescript-eslint/eslint-plugin": "^3.0.1", "@typescript-eslint/parser": "^3.0.1", "chai": "^4.2.0", - "documentation": "^13.0.0", + "documentation": "^14.0.2", "eslint": "^7.1.0", "eslint-config-developit": "^1.2.0", "esm": "^3.2.25", @@ -108,7 +108,7 @@ "rimraf": "^3.0.2", "sinon": "^9.0.2", "sinon-chai": "^3.5.0", - "ts-node": "^8.10.2", - "typescript": "^3.9.7" + "ts-node": "^10.9.1", + "typescript": "^5.1.5" } } -- cgit v1.2.3 From bbba04a149f29755be637188a1f480a3ec228807 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 4 Jul 2023 13:27:38 -0400 Subject: move eslint config out of package.json and add prettier --- .eslintrc | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 46 ++++++++-------------------------------------- 2 files changed, 60 insertions(+), 38 deletions(-) create mode 100644 .eslintrc (limited to 'package.json') diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..429b835 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,52 @@ +{ + "ignorePatterns": [ + "node_modules", + "dist", + "index.d.ts" + ], + "extends": [ + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended", + "developit" + ], + "parser": "@typescript-eslint/parser", + "parserOptions": { + "sourceType": "module" + }, + "env": { + "browser": true, + "mocha": true, + "jest": false, + "es6": true + }, + "globals": { + "expect": true + }, + "rules": { + "semi": [ + 2, + "always" + ], + "brace-style": [ + 2, + "1tbs" + ], + "quotes": [ + 2, + "single" + ], + "lines-around-comment": [ + 2, + { + "allowBlockStart": true, + "allowObjectStart": true + } + ], + "jest/valid-expect": 0, + "@typescript-eslint/no-explicit-any": 0, + "@typescript-eslint/explicit-function-return-type": 0, + "@typescript-eslint/explicit-module-boundary-types": 0, + "@typescript-eslint/no-empty-function": 0, + "@typescript-eslint/no-non-null-assertion": 0 + } +} diff --git a/package.json b/package.json index a989165..78c89e0 100644 --- a/package.json +++ b/package.json @@ -54,57 +54,27 @@ "test/*_test.ts" ] }, - "eslintConfig": { - "extends": [ - "developit", - "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended" - ], - "parser": "@typescript-eslint/parser", - "parserOptions": { - "sourceType": "module" - }, - "env": { - "browser": true, - "mocha": true, - "jest": false, - "es6": true + "prettier": { + "singleQuote": true, + "trailingComma": "none" }, - "globals": { - "expect": true - }, - "rules": { - "semi": [ - 2, - "always" - ], - "jest/valid-expect": 0, - "@typescript-eslint/no-explicit-any": 0, - "@typescript-eslint/explicit-function-return-type": 0, - "@typescript-eslint/explicit-module-boundary-types": 0, - "@typescript-eslint/no-empty-function": 0, - "@typescript-eslint/no-non-null-assertion": 0 - } - }, - "eslintIgnore": [ - "dist", - "index.d.ts" - ], "devDependencies": { "@types/chai": "^4.2.11", "@types/mocha": "^7.0.2", "@types/sinon": "^9.0.4", "@types/sinon-chai": "^3.2.4", - "@typescript-eslint/eslint-plugin": "^3.0.1", - "@typescript-eslint/parser": "^3.0.1", + "@typescript-eslint/eslint-plugin": "^5.61.0", + "@typescript-eslint/parser": "^5.61.0", "chai": "^4.2.0", "documentation": "^14.0.2", - "eslint": "^7.1.0", + "eslint": "^7.32.0", "eslint-config-developit": "^1.2.0", + "eslint-plugin-compat": "^4.1.4", "esm": "^3.2.25", "microbundle": "^0.12.3", "mocha": "^8.0.1", "npm-run-all": "^4.1.5", + "prettier": "^2.8.8", "rimraf": "^3.0.2", "sinon": "^9.0.2", "sinon-chai": "^3.5.0", -- cgit v1.2.3 From 3f81679127be71dd7ad1b2bbbd5a56df81c96290 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 4 Jul 2023 13:27:50 -0400 Subject: fix TypeScript config --- package.json | 5 +++-- tsconfig.json | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'package.json') diff --git a/package.json b/package.json index 78c89e0..f6479b0 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "typings": "index.d.ts", "exports": { "types": "./index.d.ts", + "module": "./dist/mitt.mjs", "import": "./dist/mitt.mjs", "require": "./dist/mitt.js", "default": "./dist/mitt.mjs" @@ -57,7 +58,7 @@ "prettier": { "singleQuote": true, "trailingComma": "none" - }, + }, "devDependencies": { "@types/chai": "^4.2.11", "@types/mocha": "^7.0.2", @@ -79,6 +80,6 @@ "sinon": "^9.0.2", "sinon-chai": "^3.5.0", "ts-node": "^10.9.1", - "typescript": "^5.1.5" + "typescript": "^4.9.5" } } diff --git a/tsconfig.json b/tsconfig.json index 563d58e..6d1b988 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,11 @@ "noEmit": true, "declaration": true, "moduleResolution": "node", - "esModuleInterop": true + "skipLibCheck": true, + "esModuleInterop": true, }, - "include": ["src/*.ts", "test/*.ts"] + "include": [ + "src/*.ts", + "test/*.ts" + ] } -- cgit v1.2.3 From b240473b5707857ba2c6a8e6d707c28d1e39da49 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 4 Jul 2023 13:31:06 -0400 Subject: 3.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'package.json') diff --git a/package.json b/package.json index f6479b0..e51397c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mitt", - "version": "3.0.0", + "version": "3.0.1", "description": "Tiny 200b functional Event Emitter / pubsub.", "module": "dist/mitt.mjs", "main": "dist/mitt.js", -- cgit v1.2.3