0.1.1 • Published 9 years ago
match-event v0.1.1
match-event
Extend an event emitter to use string and regex patterns to listen for matching events.
Install with npm
$ npm i match-event --save
Usage
Example using only the component-emitter and a simple object.
var Emitter = require('component-emitter');
var matchEvent = require('match-event');
var app = Emitter({});
matchEvent()(app);
app.on('foo', /\.json$/, function (fp, file) {
console.log(arguments);
});
app.emit('foo', 'package.json', {path: 'package.json'});
Example using base-methods and adding match-event
in the constructor.
var Base = require('base-methods');
// Create an app inheriting from Base
// component-emitter is used in base-methods so
// it's not needed here.
function MyApp() {
Base.call(this);
// use match-event as a plugin
this.use(matchEvent());
}
Base.extend(MyApp);
var app = new MyApp();
app.on('foo', /\.json$/, function (fp, file) {
console.log(arguments);
});
app.emit('foo', 'package.json', {path: 'package.json'});
Example using base-methods and adding match-event
to an instance of MyApp
.
var Base = require('base-methods');
// Create an app inheriting from Base
// component-emitter is used in base-methods so
// it's not needed here.
function MyApp() {
Base.call(this);
}
Base.extend(MyApp);
var app = new MyApp();
app.use(matchEvent());
app.on('foo', /\.json$/, function (fp, file) {
console.log(arguments);
});
app.emit('foo', 'package.json', {path: 'package.json'});
API
Register an event listener that will only be called when a string is passed that matches the given regex or string literal.
Params
event
{String}: Event name to pass to the emitteron
method.re
{String|RegExp}: String or RegExp pattern to match.fn
{Function}: Event listener function to pass to the emitteron
method.returns
{Object}: Returnthis
for chaining.
Related projects
- base-methods: Starter for creating a node.js application with a handful of common methods, like
set
,get
,… more | homepage - component-emitter: Event emitter | homepage
- emitter-only: Extend event emitters to allow registering listeners only once. | homepage
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Author
Brian Woodward
License
Copyright © 2015 Brian Woodward Released under the MIT license.
This file was generated by verb-cli on November 03, 2015.