Initial commit! 🚀
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jason Miller · committed by Jason Miller · the first commit · Browse files · 6 files·+335 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+335 added−0 removed
- tests:+145 added−0 removed
- other:+190 added−0 removed
Provenance
- Commit
e77a42c- Tree
612f883- Author
Jason Miller <jasonmiller@synacor.com>- Committer
Jason Miller <jasonmiller@synacor.com>
6 files changed+335 added−0 removed
added:src/index.js+47 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647/** Mitt: Tiny (~200b) functional event emitter / pubsub. * @name mitt * @returns {Mitt} */export default function mitt(all) { // Arrays of event handlers, keyed by type all = all || {};
// Get or create a named handler list function list(type) { let t = type.toLowerCase(); return all[t] || (all[t] = []); }
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 the given event * @memberof mitt */ on(type, handler) { list(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, handler) { let e = list(type), i = e.indexOf(handler); if (~i) e.splice(i, 1); },
/** 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, event) { list('*').concat(list(type)).forEach( f => { f(event); }); } };}added:test/index.js+145 added
1+import mitt from '../src';2+import chai, { expect } from 'chai';3+import { spy } from 'sinon';4+import sinonChai from 'sinon-chai';5+chai.use(sinonChai);6+7+8+describe('mitt', () => {9+ it('should be a function', () => {10+ expect(mitt).to.be.a('function');11+ });12+13+ describe('mitt#', () => {14+ let events, inst;15+16+ beforeEach( () => {17+ events = {};18+ inst = mitt(events);19+ });20+21+ describe('on()', () => {22+ it('should be a function', () => {23+ expect(inst)24+ .to.have.property('on')25+ .that.is.a('function');26+ });27+28+ it('should register handler for new type', () => {29+ let foo = () => {};30+ inst.on('foo', foo);31+32+ expect(events).to.have.property('foo').that.deep.equals([foo]);33+ });34+35+ it('should append handler for existing type', () => {36+ let foo = () => {};37+ let bar = () => {};38+ inst.on('foo', foo);39+ inst.on('foo', foo);40+ inst.on('foo', bar);41+42+ expect(events).to.have.property('foo').that.deep.equals([foo, foo, bar]);43+ });44+45+ it('should normalize case', () => {46+ let foo = () => {};47+ inst.on('FOO', foo);48+ inst.on('Bar', foo);49+ inst.on('baz:baT!', foo);50+51+ expect(events).to.have.property('foo').that.deep.equals([foo]);52+ expect(events).to.have.property('bar').that.deep.equals([foo]);53+ expect(events).to.have.property('baz:bat!').that.deep.equals([foo]);54+ });55+ });56+57+ describe('off()', () => {58+ it('should be a function', () => {59+ expect(inst)60+ .to.have.property('off')61+ .that.is.a('function');62+ });63+64+ it('should remove handler for type', () => {65+ let foo = () => {};66+ events.foo = [foo];67+ inst.off('foo', foo);68+69+ expect(events).to.have.property('foo').that.is.empty;70+ });71+72+ it('should remove only one handler for dupes', () => {73+ let foo = () => {};74+ events.foo = [foo, foo];75+76+ inst.off('foo', foo);77+ expect(events).to.have.property('foo').that.deep.equals([foo]);78+79+ inst.off('foo', foo);80+ expect(events).to.have.property('foo').that.is.empty;81+ });82+83+ it('should normalize case', () => {84+ let foo = () => {};85+ events.foo = [foo];86+ events.bar = [foo];87+ events['baz:bat!'] = [foo];88+89+ inst.off('FOO', foo);90+ inst.off('Bar', foo);91+ inst.off('baz:baT!', foo);92+93+ expect(events).to.have.property('foo').that.is.empty;94+ expect(events).to.have.property('bar').that.is.empty;95+ expect(events).to.have.property('baz:bat!').that.is.empty;96+ });97+ });98+99+ describe('emit()', () => {100+ it('should be a function', () => {101+ expect(inst)102+ .to.have.property('emit')103+ .that.is.a('function');104+ });105+106+ it('should invoke handler for type', () => {107+ let foo = spy(),108+ event = {};109+ events.foo = [foo];110+111+ inst.emit('foo', event);112+113+ expect(foo)114+ .to.have.been.calledOnce115+ .and.calledWithExactly(event);116+ });117+118+ it('should ignore case', () => {119+ let foo = spy(),120+ event = {};121+ events.foo = [foo];122+123+ inst.emit('FOO', event);124+ inst.emit('Foo', event);125+126+ expect(foo)127+ .to.have.been.calledTwice128+ .and.always.calledWithExactly(event);129+ });130+131+ it('should invoke * handlers', () => {132+ let star = spy(),133+ event = {};134+ events['*'] = [star];135+136+ inst.emit('foo', event);137+ inst.emit('bar', event);138+139+ expect(star)140+ .to.have.been.calledTwice141+ .and.always.calledWithExactly(event);142+ });143+ });144+ });145+});added:.gitignore+5 added
1+/dist2+/test-reports3+/node_modules4+/npm-debug.log5+.DS_Storeadded:package.json+76 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576{ "name": "mitt", "amdName": "mitt", "version": "0.2.1", "description": "Tiny ~200b functional event emitter / pubsub.", "jsnext:main": "src/index.js", "main": "dist/mitt.js", "umd:main": "dist/mitt.umd.js", "scripts": { "test": "eslint src test && mocha --compilers js:babel-register test/**/*.js", "build": "npm-run-all clean -p rollup:* -p minify:* -s docs size", "clean": "rimraf dist && mkdirp dist", "rollup:cjs": "rollup -c rollup.config.js -m -f cjs -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_main", "rollup:umd": "rollup -c rollup.config.js -m -f umd -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_umd_main", "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", "size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size | pretty-bytes)\"", "release": "npm run build -s && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, "repository": "developit/mitt", "keywords": [ "events", "eventemitter", "pubsub" ], "homepage": "httsp://github.com/developit/mitt", "authors": [ "Jason Miller <jason@developit.ca>" ], "license": "MIT", "files": [ "src", "dist" ], "babel": { "presets": [ "es2015", "stage-0" ] }, "eslintConfig": { "parser": "babel-eslint", "extends": "eslint:recommended", "env": { "browser": true, "mocha": true, "es6": true }, "globals": { "expect": true } }, "devDependencies": { "babel-core": "^6.9.1", "babel-eslint": "^7.1.1", "babel-preset-es2015": "^6.9.0", "babel-preset-stage-0": "^6.5.0", "babel-register": "^6.9.0", "chai": "^3.5.0", "documentation": "^4.0.0-beta4", "eslint": "^3.13.1", "gzip-size-cli": "^1.0.0", "mkdirp": "^0.5.1", "mocha": "^3.2.0", "npm-run-all": "^2.1.1", "pretty-bytes": "^4.0.2", "rimraf": "^2.5.2", "rollup": "^0.41.4", "rollup-plugin-buble": "^0.15.0", "sinon": "^1.17.4", "sinon-chai": "^2.8.0", "strip-json-comments-cli": "^1.0.1", "uglify-js": "^2.6.2" }}added:README.md+54 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# `mitt`
[](https://www.npmjs.org/package/mitt)[](https://travis-ci.org/developit/mitt)
**Tiny (~200b) functional event emitter / pubsub.**
**It's tiny:** no dependencies and only **190 bytes** when gzipped _(250b without)_.
* * *
## Installation
```shnpm install --save mitt```
* * *
## API
### mitt
Mitt: Tiny (~200b) functional event emitter / pubsub.
Returns **Mitt**
#### on
Register an event handler for the given type.
**Parameters**
- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to the given event
#### off
Remove an event handler for the given type.
**Parameters**
- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove
#### emit
Invoke all handlers for the given type.If present, `"*"` handlers are invoked prior to type-matched handlers.
**Parameters**
- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke- `event` **\[Any]** An event object, passed to each handleradded:rollup.config.js+8 added
12345678import buble from 'rollup-plugin-buble';
export default { useStrict: false, plugins: [ buble() ]};