diff --git a/dist/dush.common.js b/dist/dush.common.js index 309d3a7..4e106c8 100644 --- a/dist/dush.common.js +++ b/dist/dush.common.js @@ -31,7 +31,7 @@ * @api public */ -module.exports = function dush () { +function dush () { var all = Object.create(null); var app = { /** @@ -269,4 +269,6 @@ module.exports = function dush () { }; return app -}; +} + +module.exports = dush; diff --git a/dist/dush.es.js b/dist/dush.es.js index f18321a..eefc5fb 100644 --- a/dist/dush.es.js +++ b/dist/dush.es.js @@ -29,7 +29,7 @@ * @api public */ -module.exports = function dush () { +function dush () { var all = Object.create(null); var app = { /** @@ -267,4 +267,6 @@ module.exports = function dush () { }; return app -}; +} + +export default dush; diff --git a/dist/dush.umd.js b/dist/dush.umd.js index 859fb84..fbf8496 100644 --- a/dist/dush.umd.js +++ b/dist/dush.umd.js @@ -1,2 +1,2 @@ -!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){module.exports=function(){var n=Object.create(null),e={all:n,use:function(n){return n(e)||e},on:function(t,o,i){function r(){r.called||(e.off(t,r),o.apply(void 0,arguments),r.called=!0)}var u=n[t]||(n[t]=[]),f=i?r:o;return f.sourceString=o.toString(),u.push(f),e},once:function(n,t){return e.on(n,t,!0),e},off:function(t,o){if(o&&n[t]){var i=o.toString();n[t]=n[t].filter(function(n){return n.sourceString!==i})}else n[t]=[];return e},emit:function(t){if("*"!==t){var o=[].slice.call(arguments);(n[t]||[]).map(function(n){n.apply(void 0,o.slice(1))}),(n["*"]||[]).map(function(n){n.apply(void 0,o)})}return e}};return e}}); +!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.dush=e()}(this,function(){function n(){var n=Object.create(null),e={all:n,use:function(n){return n(e)||e},on:function(t,o,i){function r(){r.called||(e.off(t,r),o.apply(void 0,arguments),r.called=!0)}var u=n[t]||(n[t]=[]),f=i?r:o;return f.sourceString=o.toString(),u.push(f),e},once:function(n,t){return e.on(n,t,!0),e},off:function(t,o){if(o&&n[t]){var i=o.toString();n[t]=n[t].filter(function(n){return n.sourceString!==i})}else n[t]=[];return e},emit:function(t){if("*"!==t){var o=[].slice.call(arguments);(n[t]||[]).map(function(n){n.apply(void 0,o.slice(1))}),(n["*"]||[]).map(function(n){n.apply(void 0,o)})}return e}};return e}return n}); //# sourceMappingURL=dush.umd.js.map diff --git a/dist/dush.umd.js.gz b/dist/dush.umd.js.gz index ecf1e6b..80503c8 100644 Binary files a/dist/dush.umd.js.gz and b/dist/dush.umd.js.gz differ diff --git a/dist/dush.umd.js.map b/dist/dush.umd.js.map index e089a77..c2d6cbc 100644 --- a/dist/dush.umd.js.map +++ b/dist/dush.umd.js.map @@ -1 +1 @@ -{"version":3,"file":"dush.umd.js","sources":["../src/index.js"],"sourcesContent":["/*!\n * dush \n *\n * Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)\n * Released under the MIT license.\n */\n\n'use strict'\n\n/**\n * > A constructor function that returns an object\n * with a few methods.\n *\n * See [JSBin Example](http://jsbin.com/mepemeluhi/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const dush = require('dush')\n * const emitter = dush()\n *\n * console.log(emitter.all) // => {}\n * console.log(emitter.on) // => Function\n * console.log(emitter.once) // => Function\n * console.log(emitter.off) // => Function\n * console.log(emitter.emit) // => Function\n * ```\n *\n * @name dush()\n * @return {Object} methods\n * @api public\n */\n\nmodule.exports = function dush () {\n let all = Object.create(null)\n const app = {\n /**\n * > An listeners map of all registered events\n * and their listeners. A key/value store, where 1) value\n * is an array of event listeners for the key and 2) key\n * is the name of the event.\n *\n * See [JSBin Example](http://jsbin.com/zuwayalovi/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter.on('foo', () => {})\n * emitter.on('foo', () => {})\n * emitter.on('bar', () => {})\n *\n * console.log(emitter.all)\n * // => { foo: [Function, Function], bar: [Functon] }\n * ```\n *\n * @name .all\n * @type {Object} `all` a key/value store of all events and their listeners\n * @api public\n */\n\n all,\n\n /**\n * > Invokes `plugin` function immediately, which is passed\n * with `app` instance. You can use it for adding more methods\n * or properties to the instance. Useful if you want to make\n * dush to work with DOM for example.\n *\n * ```js\n * const app = dush()\n *\n * app.on('hi', (str) => {\n * console.log(str) // => 'Hello World!!'\n * })\n *\n * app.use((app) => {\n * app.foo = 'bar'\n * app.hello = (place) => app.emit('hi', `Hello ${place}!!`)\n * })\n *\n * console.log(app.foo) // => 'bar'\n * app.hello('World')\n * ```\n *\n * @name .use\n * @param {Function} `plugin` A function passed with `(app)` signature\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n use (plugin) {\n const ret = plugin(app)\n return ret || app\n },\n\n /**\n * > Add `handler` for `name` event.\n *\n * See [JSBin Example](http://jsbin.com/xeketuruto/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter\n * .on('hi', (place) => {\n * console.log(`hello ${place}!`) // => 'hello world!'\n * })\n * .on('hi', (place) => {\n * console.log(`hi ${place}, yeah!`) // => 'hi world, yeah!'\n * })\n *\n * emitter.emit('hi', 'world')\n * ```\n *\n * @name .on\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n on (name, handler, once) {\n let e = all[name] || (all[name] = [])\n\n function func () {\n if (!func.called) {\n app.off(name, func)\n handler.apply(undefined, arguments)\n func.called = true\n }\n }\n\n var fn = once ? func : handler\n fn.sourceString = handler.toString()\n\n e.push(fn)\n return app\n },\n\n /**\n * > Add `handler` for `name` event that\n * will be called only one time.\n *\n * See [JSBin Example](http://jsbin.com/teculorima/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n * let called = 0\n *\n * emitter.once('foo', () => {\n * console.log('called only once')\n * called++\n * })\n *\n * emitter\n * .emit('foo', 111)\n * .emit('foo', 222)\n * .emit('foo', 333)\n *\n * console.log(called) // => 1\n * ```\n *\n * @name .once\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n once (name, handler) {\n app.on(name, handler, true)\n return app\n },\n\n /**\n * > Remove `handler` for `name` event. If `handler` not\n * passed will remove **all** listeners for that `name` event.\n *\n * See [JSBin Example](http://jsbin.com/nujucoquvi/3/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * const handler = () => {\n * console.log('not called')\n * }\n *\n * emitter.on('foo', handler)\n * emitter.off('foo', handler)\n *\n * emitter.on('foo', (abc) => {\n * console.log('called', abc) // => 'called 123'\n * })\n * emitter.emit('foo', 123)\n *\n * // or removing all listeners of `foo`\n * emitter.off('foo')\n * emitter.emit('foo')\n * ```\n *\n * @name .off\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n off (name, handler) {\n if (handler && all[name]) {\n const fnStr = handler.toString()\n all[name] = all[name].filter((func) => func.sourceString !== fnStr)\n } else {\n all[name] = []\n }\n\n return app\n },\n\n /**\n * > Invoke all handlers for given `name` event.\n * If present, `'*'` listeners are invoked too with `(type, ...rest)` signature,\n * where the `type` argument is a string representing the name of the\n * called event; and all of the rest arguments.\n *\n * See [JSBin Example](http://jsbin.com/muqujavolu/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter.on('foo', (a, b, c) => {\n * console.log(`${a}, ${b}, ${c}`) // => 1, 2, 3\n * })\n *\n * emitter.on('*', (name, a, b, c) => {\n * console.log(`name is: ${name}`)\n * console.log(`rest args are: ${a}, ${b}, ${c}`)\n * })\n *\n * emitter.emit('foo', 1, 2, 3)\n * emitter.emit('bar', 555)\n * ```\n *\n * @name .emit\n * @param {String} `name` The name of the event to invoke\n * @param {any} `args` Any number of arguments of any type of value, passed to each listener\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n emit (name) {\n if (name !== '*') {\n var args = [].slice.call(arguments);\n (all[name] || []).map((handler) => { handler.apply(undefined, args.slice(1)) });\n (all['*'] || []).map((handler) => { handler.apply(undefined, args) })\n }\n\n return app\n }\n }\n\n return app\n}\n"],"names":["module","exports","let","all","Object","create","app","use","plugin","on","name","handler","once","func","called","off","apply","undefined","arguments","e","fn","sourceString","toString","push","const","fnStr","filter","emit","args","slice","call","map"],"mappings":"0IAiCAA,OAAOC,QAAU,WACfC,GAAIC,GAAMC,OAAOC,OAAO,MAClBC,GA2BJH,IAAAA,EA8BAI,aAAKC,GAEH,MADYA,GAAOF,IACLA,GA+BhBG,YAAIC,EAAMC,EAASC,GAGjB,QAASC,KACFA,EAAKC,SACRR,EAAIS,IAAIL,EAAMG,GACdF,EAAQK,MAAMC,OAAWC,WACzBL,EAAKC,QAAS,GANlBZ,GAAIiB,GAAIhB,EAAIO,KAAUP,EAAIO,OAUtBU,EAAKR,EAAOC,EAAOF,CAIvB,OAHAS,GAAGC,aAAeV,EAAQW,WAE1BH,EAAEI,KAAKH,GACAd,GAmCTM,cAAMF,EAAMC,GAEV,MADAL,GAAIG,GAAGC,EAAMC,GAAS,GACfL,GAsCTS,aAAKL,EAAMC,GACT,GAAIA,GAAWR,EAAIO,GAAO,CACxBc,GAAMC,GAAQd,EAAQW,UACtBnB,GAAIO,GAAQP,EAAIO,GAAMgB,OAAO,SAACb,SAASA,GAAKQ,eAAiBI,QAE7DtB,GAAIO,KAGN,OAAOJ,IAoCTqB,cAAMjB,GACJ,GAAa,MAATA,EAAc,CAChB,GAAIkB,MAAUC,MAAMC,KAAKZ,YACxBf,EAAIO,QAAaqB,IAAI,SAACpB,GAAcA,EAAQK,MAAMC,OAAWW,EAAKC,MAAM,OACxE1B,EAAI,UAAY4B,IAAI,SAACpB,GAAcA,EAAQK,MAAMC,OAAWW,KAG/D,MAAOtB,IAIX,OAAOA"} \ No newline at end of file +{"version":3,"file":"dush.umd.js","sources":["../src/index.js"],"sourcesContent":["/*!\n * dush \n *\n * Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)\n * Released under the MIT license.\n */\n\n'use strict'\n\n/**\n * > A constructor function that returns an object\n * with a few methods.\n *\n * See [JSBin Example](http://jsbin.com/mepemeluhi/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const dush = require('dush')\n * const emitter = dush()\n *\n * console.log(emitter.all) // => {}\n * console.log(emitter.on) // => Function\n * console.log(emitter.once) // => Function\n * console.log(emitter.off) // => Function\n * console.log(emitter.emit) // => Function\n * ```\n *\n * @name dush()\n * @return {Object} methods\n * @api public\n */\n\nexport default function dush () {\n let all = Object.create(null)\n const app = {\n /**\n * > An listeners map of all registered events\n * and their listeners. A key/value store, where 1) value\n * is an array of event listeners for the key and 2) key\n * is the name of the event.\n *\n * See [JSBin Example](http://jsbin.com/zuwayalovi/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter.on('foo', () => {})\n * emitter.on('foo', () => {})\n * emitter.on('bar', () => {})\n *\n * console.log(emitter.all)\n * // => { foo: [Function, Function], bar: [Functon] }\n * ```\n *\n * @name .all\n * @type {Object} `all` a key/value store of all events and their listeners\n * @api public\n */\n\n all,\n\n /**\n * > Invokes `plugin` function immediately, which is passed\n * with `app` instance. You can use it for adding more methods\n * or properties to the instance. Useful if you want to make\n * dush to work with DOM for example.\n *\n * ```js\n * const app = dush()\n *\n * app.on('hi', (str) => {\n * console.log(str) // => 'Hello World!!'\n * })\n *\n * app.use((app) => {\n * app.foo = 'bar'\n * app.hello = (place) => app.emit('hi', `Hello ${place}!!`)\n * })\n *\n * console.log(app.foo) // => 'bar'\n * app.hello('World')\n * ```\n *\n * @name .use\n * @param {Function} `plugin` A function passed with `(app)` signature\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n use (plugin) {\n const ret = plugin(app)\n return ret || app\n },\n\n /**\n * > Add `handler` for `name` event.\n *\n * See [JSBin Example](http://jsbin.com/xeketuruto/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter\n * .on('hi', (place) => {\n * console.log(`hello ${place}!`) // => 'hello world!'\n * })\n * .on('hi', (place) => {\n * console.log(`hi ${place}, yeah!`) // => 'hi world, yeah!'\n * })\n *\n * emitter.emit('hi', 'world')\n * ```\n *\n * @name .on\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n on (name, handler, once) {\n let e = all[name] || (all[name] = [])\n\n function func () {\n if (!func.called) {\n app.off(name, func)\n handler.apply(undefined, arguments)\n func.called = true\n }\n }\n\n var fn = once ? func : handler\n fn.sourceString = handler.toString()\n\n e.push(fn)\n return app\n },\n\n /**\n * > Add `handler` for `name` event that\n * will be called only one time.\n *\n * See [JSBin Example](http://jsbin.com/teculorima/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n * let called = 0\n *\n * emitter.once('foo', () => {\n * console.log('called only once')\n * called++\n * })\n *\n * emitter\n * .emit('foo', 111)\n * .emit('foo', 222)\n * .emit('foo', 333)\n *\n * console.log(called) // => 1\n * ```\n *\n * @name .once\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n once (name, handler) {\n app.on(name, handler, true)\n return app\n },\n\n /**\n * > Remove `handler` for `name` event. If `handler` not\n * passed will remove **all** listeners for that `name` event.\n *\n * See [JSBin Example](http://jsbin.com/nujucoquvi/3/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * const handler = () => {\n * console.log('not called')\n * }\n *\n * emitter.on('foo', handler)\n * emitter.off('foo', handler)\n *\n * emitter.on('foo', (abc) => {\n * console.log('called', abc) // => 'called 123'\n * })\n * emitter.emit('foo', 123)\n *\n * // or removing all listeners of `foo`\n * emitter.off('foo')\n * emitter.emit('foo')\n * ```\n *\n * @name .off\n * @param {String} `name` Type of event to listen for, or `'*'` for all events\n * @param {Function} `handler` Function to call in response to given event\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n off (name, handler) {\n if (handler && all[name]) {\n const fnStr = handler.toString()\n all[name] = all[name].filter((func) => func.sourceString !== fnStr)\n } else {\n all[name] = []\n }\n\n return app\n },\n\n /**\n * > Invoke all handlers for given `name` event.\n * If present, `'*'` listeners are invoked too with `(type, ...rest)` signature,\n * where the `type` argument is a string representing the name of the\n * called event; and all of the rest arguments.\n *\n * See [JSBin Example](http://jsbin.com/muqujavolu/edit?js,console).\n *\n * **Example**\n *\n * ```js\n * const emitter = dush()\n *\n * emitter.on('foo', (a, b, c) => {\n * console.log(`${a}, ${b}, ${c}`) // => 1, 2, 3\n * })\n *\n * emitter.on('*', (name, a, b, c) => {\n * console.log(`name is: ${name}`)\n * console.log(`rest args are: ${a}, ${b}, ${c}`)\n * })\n *\n * emitter.emit('foo', 1, 2, 3)\n * emitter.emit('bar', 555)\n * ```\n *\n * @name .emit\n * @param {String} `name` The name of the event to invoke\n * @param {any} `args` Any number of arguments of any type of value, passed to each listener\n * @return {Object} The `dush` instance for chaining\n * @api public\n */\n\n emit (name) {\n if (name !== '*') {\n var args = [].slice.call(arguments);\n (all[name] || []).map((handler) => { handler.apply(undefined, args.slice(1)) });\n (all['*'] || []).map((handler) => { handler.apply(undefined, args) })\n }\n\n return app\n }\n }\n\n return app\n}\n"],"names":["dush","let","all","Object","create","app","use","plugin","on","name","handler","once","func","called","off","apply","undefined","arguments","e","fn","sourceString","toString","push","const","fnStr","filter","emit","args","slice","call","map"],"mappings":"mKAiCA,QAAwBA,KACtBC,GAAIC,GAAMC,OAAOC,OAAO,MAClBC,GA2BJH,IAAAA,EA8BAI,aAAKC,GAEH,MADYA,GAAOF,IACLA,GA+BhBG,YAAIC,EAAMC,EAASC,GAGjB,QAASC,KACFA,EAAKC,SACRR,EAAIS,IAAIL,EAAMG,GACdF,EAAQK,MAAMC,OAAWC,WACzBL,EAAKC,QAAS,GANlBZ,GAAIiB,GAAIhB,EAAIO,KAAUP,EAAIO,OAUtBU,EAAKR,EAAOC,EAAOF,CAIvB,OAHAS,GAAGC,aAAeV,EAAQW,WAE1BH,EAAEI,KAAKH,GACAd,GAmCTM,cAAMF,EAAMC,GAEV,MADAL,GAAIG,GAAGC,EAAMC,GAAS,GACfL,GAsCTS,aAAKL,EAAMC,GACT,GAAIA,GAAWR,EAAIO,GAAO,CACxBc,GAAMC,GAAQd,EAAQW,UACtBnB,GAAIO,GAAQP,EAAIO,GAAMgB,OAAO,SAACb,SAASA,GAAKQ,eAAiBI,QAE7DtB,GAAIO,KAGN,OAAOJ,IAoCTqB,cAAMjB,GACJ,GAAa,MAATA,EAAc,CAChB,GAAIkB,MAAUC,MAAMC,KAAKZ,YACxBf,EAAIO,QAAaqB,IAAI,SAACpB,GAAcA,EAAQK,MAAMC,OAAWW,EAAKC,MAAM,OACxE1B,EAAI,UAAY4B,IAAI,SAACpB,GAAcA,EAAQK,MAAMC,OAAWW,KAG/D,MAAOtB,IAIX,OAAOA"} \ No newline at end of file diff --git a/src/index.js b/src/index.js index de9148e..2112075 100644 --- a/src/index.js +++ b/src/index.js @@ -31,7 +31,7 @@ * @api public */ -module.exports = function dush () { +export default function dush () { let all = Object.create(null) const app = { /**