Export Mitt types for TS consumers (#101)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Export Mitt types for TS consumers * Add rudimentary tests for exported TS types * Run tests against the generated output instead of src
authored by Jack Franklin · committed by GitHub · parent 59cc3d1 · Browse files · 5 files·+63 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+63 added−11 removed
- tests:+37 added−3 removed
- other:+26 added−8 removed
Provenance
- Commit
eb2be7c- Tree
1e17bf5- Author
Jack Franklin <jacktfranklin@chromium.org>- Committer
GitHub <noreply@github.com>- Parent
59cc3d1
5 files changed+63 added−11 removed
modified:src/index.ts+6 added-6 removed
11234565678910910111213131415161761 unmodified linestype EventType = string | symbol;export type EventType = string | symbol;
// An event handler can take an optional event argument// and should not return a valuetype Handler = (event?: any) => void;type WildcardHandler= (type: EventType, event?: any) => voidexport type Handler = (event?: any) => void;export type WildcardHandler= (type: EventType, event?: any) => void
// An array of all currently registered event handlers for a typetype EventHandlerList = Array<Handler>;type WildCardEventHandlerList = Array<WildcardHandler>;export type EventHandlerList = Array<Handler>;export type WildCardEventHandlerList = Array<WildcardHandler>;
// A map of event types and their corresponding event handlers.type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
export interface Emitter { on(type: EventType, handler: Handler): void; on(type: '*', handler: WildcardHandler): void;61 unmodified linesmodified:test/index.js+17 added-3 removed
1-import mitt from '../src';1+import mitt from '..';2 import chai, { expect } from 'chai';3 import { spy } from 'sinon';4 import sinonChai from 'sinon-chai';5 chai.use(sinonChai);6 7+describe('mitt', () => {7-it('should default export be a function', () => {8- expect(mitt).to.be.a('function');8+ it('should default export be a function', () => {9+ expect(mitt).to.be.a('function');10+ });11+12+ it('should accept an optional event handler map', () => {13+ expect(() => mitt(new Map())).not.to.throw;14+ const map = new Map();15+ const a = spy();16+ const b = spy();17+ map.set('foo', [a, b]);18+ const events = mitt(map);19+ events.emit('foo');20+ expect(a).to.have.been.calledOnce;21+ expect(b).to.have.been.calledOnce;22+ });23 });24 25 describe('mitt#', () => {26 let events, inst;156 unmodified linesadded:test/types.ts+20 added
1234567891011121314151617181920import 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 typeconst map = new Map<string, EventHandlerList>([ ['foo', [foo]]]);const events2 = mitt(map);events2.emit('foo', 'hello');
// event map type & iterablesconst map2 : EventHandlerMap = new Map(Object.entries(({ foo: [foo] })));mitt(map2);modified:package.json+9 added-2 removed
8 unmodified lines9101112131314151516171819202 unmodified lines23242526272829303116 unmodified lines4849505152535455562 unmodified lines59606162636465666768697071727374757618 unmodified lines8 unmodified lines "main": "dist/mitt.js", "umd:main": "dist/mitt.umd.js", "typings": "dist/index.d.ts", "scripts": { "test": "npm-run-all --silent typecheck lint testonly", "testonly": "mocha --require esm --require ts-node/register test/**/*.js", "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", "docs": "documentation readme src/index.ts --section API -q --parse-extension ts",2 unmodified lines "repository": "developit/mitt", "keywords": [ "events", "eventemitter", "emitter", "pubsub" ], "homepage": "https://github.com/developit/mitt", "authors": [16 unmodified lines }, "env": { "browser": true, "mocha": true, "jest": false, "es6": true }, "globals": { "expect": true2 unmodified lines "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 } }, "eslintIgnore": [ "dist" ], "devDependencies": { "@types/chai": "^4.2.11", "@types/mocha": "^7.0.2", "@types/sinon": "^9.0.4",18 unmodified linesadded:tsconfig.json+11 added
1234567891011{ "compileOnSave": false, "compilerOptions": { "noEmit": true, "declaration": true, "moduleResolution": "node" }, "exclude": [ "test" ]}