From ebc365cb2ee2f4007ba5794eb5cdf09d2a51543f Mon Sep 17 00:00:00 2001 From: freemountain Date: Thu, 11 Feb 2016 11:07:54 +0100 Subject: [PATCH 1/5] use default handler on mismatch --- lib/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/index.js b/lib/index.js index 0cb7279..b3f0d46 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,10 +34,15 @@ function match (obj) { return function (e) { var chord = eventKey(e) var fn = obj[chord] + var defaultFn = obj.default; if (isFunction(fn)) { return fn(e) } + + if (isFunction(defaultFn)) { + return defaultFn(e); + } } } From 1b2daa2b6d2d8e595cab3cf67e7c1844b017c2a4 Mon Sep 17 00:00:00 2001 From: freemountain Date: Thu, 11 Feb 2016 11:23:40 +0100 Subject: [PATCH 2/5] add test case --- test/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/index.js b/test/index.js index 2a049c9..abdbd8e 100644 --- a/test/index.js +++ b/test/index.js @@ -19,6 +19,21 @@ test('should work', function (t) { t.end() }) +test('default handler', function (t) { + var descriptor = { + 'ctrl+shift+enter': function() { + return 'combination' + }, + 'default': function() { + return 'default' + } + } + + t.equal(ev(descriptor)(event('ctrl+shift+enter')), 'combination') + t.equal(ev(descriptor)(event('x')), 'default') + t.end() +}) + /** * Helpers */ From 7e50ca38c63a9620736f7df5c80f0bfb0cbd6b4c Mon Sep 17 00:00:00 2001 From: freemountain Date: Thu, 11 Feb 2016 11:41:50 +0100 Subject: [PATCH 3/5] add example --- Readme.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index ba8c956..c148ffc 100644 --- a/Readme.md +++ b/Readme.md @@ -14,7 +14,7 @@ Syntax sugar for events handlers. Kind of like the excellent, [classnames](https ## Usage -This module gives you some special syntax to make your event handlers more declarative and functional. You can create handlers for only certain keypresses, or easily attach multiple handlers to a single event. +This module gives you some special syntax to make your event handlers more declarative and functional. You can create handlers for only certain keypresses, or easily attach multiple handlers to a single event. This example calls `updateText` with every keydown event, but also calls `submit` only when enter is pressed: @@ -37,6 +37,17 @@ function render () { ``` This will close the input and submit when `enter` is pressed, it will also update the text on every normal keydown. +If your descriptor is an object, the 'default' key will match every event: + +```js +var ev = require('@f/event-handler') + +function render () { + return +} +``` + +This will submit the input when `enter` is pressed and update the text on every __other__ keydown. ## Return values From 8d4a1b11a80d9c7d7c6113a363fec859b7d281df Mon Sep 17 00:00:00 2001 From: freemountain Date: Thu, 11 Feb 2016 11:44:26 +0100 Subject: [PATCH 4/5] fix example #2 --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index c148ffc..1bea449 100644 --- a/Readme.md +++ b/Readme.md @@ -32,7 +32,7 @@ You may pass an array, object, or just a plain function, and you may also do any var ev = require('@f/event-handler') function render () { - return + return } ``` From f2f91bb66f0d71b4c5d96d20e6b8148ad9777ee0 Mon Sep 17 00:00:00 2001 From: freemountain Date: Thu, 11 Feb 2016 11:45:59 +0100 Subject: [PATCH 5/5] remove semicolons --- lib/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index b3f0d46..bee2fd5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,14 +34,14 @@ function match (obj) { return function (e) { var chord = eventKey(e) var fn = obj[chord] - var defaultFn = obj.default; + var defaultFn = obj.default if (isFunction(fn)) { return fn(e) } if (isFunction(defaultFn)) { - return defaultFn(e); + return defaultFn(e) } } }