From 5f31bae0fb6231dc274a26857b5b7778856cc78d Mon Sep 17 00:00:00 2001 From: gwer Date: Thu, 25 Oct 2018 10:24:32 +0300 Subject: [PATCH] Reduce size to 115 B. --- README.md | 2 +- index.js | 49 +++++++++++++++++++++++++------------------------ package.json | 2 +- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 96a7691..e1fb407 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Simple and tiny event emitter library for JavaScript. * No Node.js [EventEmitter] compatibility. -* Only 118 bytes (minified and gzipped). It uses [Size Limit] to control size. +* Only 115 bytes (minified and gzipped). It uses [Size Limit] to control size. * `on` method returns `unbind` function. You don’t need to save callback to variable for `removeListener`. * No aliases, just `emit` and `on` methods. diff --git a/index.js b/index.js index d77bc15..af57c0a 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,31 @@ } ).prototype = { + /** + * Calls each of the listeners registered for a given event. + * + * @param {string} event The event name. + * @param {...*} arguments The arguments for listeners. + * + * @return {undefined} + * + * @example + * ee.emit('tick', tickType, tickDuration) + * + * @alias NanoEvents#emit + * @method + */ + emit: function emit (event) { + // event variable is reused and repurposed, now it's an array of handlers + event = this.events[event] + if (!event || !event[0]) return // event[0] === Array.isArray(event) + + var args = event.slice.call(arguments, 1) + event.slice().map(function (i) { + i.apply(this, args) // this === global or window + }) + }, + /** * Add a listener for a given event. * @@ -68,29 +93,5 @@ // -1 >>> 0 === 0xFFFFFFFF, max possible array length event.splice(event.indexOf(cb) >>> 0, 1) } - }, - - /** - * Calls each of the listeners registered for a given event. - * - * @param {string} event The event name. - * @param {...*} arguments The arguments for listeners. - * - * @return {undefined} - * - * @example - * ee.emit('tick', tickType, tickDuration) - * - * @alias NanoEvents#emit - * @method - */ - emit: function emit (event) { - var list = this.events[event] - if (!list || !list[0]) return // list[0] === Array.isArray(list) - - var args = list.slice.call(arguments, 1) - list.slice().map(function (i) { - i.apply(this, args) // this === global or window - }) } } diff --git a/package.json b/package.json index 01f3fda..c70e2fd 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "size-limit": [ { "path": "index.js", - "limit": "118 B" + "limit": "115 B" } ], "jest": {