match-event

0.1.1 • Public • Published

match-event NPM version Build Status

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 emitter on method.
  • re {String|RegExp}: String or RegExp pattern to match.
  • fn {Function}: Event listener function to pass to the emitter on method.
  • returns {Object}: Return this for chaining.

Related projects

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.

Readme

Keywords

none

Package Sidebar

Install

npm i match-event

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • doowb