npm package: @funjs/emitter
Description: Simple and fast functional event emitter...
Author: Mauro Junior
Latest version: 1.3.0

[![travis build](https://img.shields.io/travis/fun-js/emitter.svg?style=flat)](https://travis-ci.org/fun-js/emitter)
[![Dependency Status](https://david-dm.org/fun-js/emitter.svg?theme=shields.io)](https://david-dm.org/fun-js/emitter)
[![devDependency Status](https://david-dm.org/fun-js/emitter/dev-status.svg?theme=shields.io)](https://david-dm.org/fun-js/route-parser#info=devDependencies)
[![Codecov](https://img.shields.io/codecov/c/github/fun-js/emitter.svg)]()
[![MIT License](https://img.shields.io/github/license/fun-js/emitter.svg?style=flat)](http://opensource.org/licenses/MIT)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat)](https://github.com/semantic-release/semantic-release)

## What is it?

A simple fast and functional route parser, for Javascript in Node and the browser. Its api is inspired by [EventEmitter2](https://github.com/asyncly/EventEmitter2), but is implemented in a more functional way, don't rely in 'this' keyword.


## How do I install it?

```Shell
yarn add @funjs/emitter
or
npm install --save @funjs/emitter
```

## How do I use it?

```javascript
const Emitter = require('@funjs/emitter');
const emitter = Emitter();

emitter.on('users/:action=(insert|update)/:id', ({ action, id }, data) => {
  console.log(`action: ${action}`);
  console.log(`id: ${id}`);
  console.log(`data: ${JSON.stringify(data)}`);
});

emitter.emit('users/insert/1', { id: 1, name: 'Joe', age: 33 });

// action: insert
// id: 1
// data: {"id":1,"name":"Joe","age":33}

```

## Splat operator return the section position as key, starting by 1:

```javascript
const Emitter = require('@funjs/emitter');
const emitter = Emitter();

emitter.on('*/*/:id', (e, data) => {
  console.log(`event: ${JSON.stringify(e)}`);
  console.log(`data: ${JSON.stringify(data)}`);
});

emitter.emit('users/insert/1', { id: 1, name: 'Joe', age: 33 });

// event: {"0":"users","1":"insert","id":"1"}
// data: {"id":1,"name":"Joe","age":33}

```

## Also works with custom delimiter and named section symbols?

```javascript
const Emitter = require('@funjs/emitter');
const emitter = Emitter({ delimiter: '.', namedSection: '$' });

emitter.on('users.$action=(insert|update).id', ({ action, id }, data) => {
  console.log(`action: ${action}`);
  console.log(`id: ${id}`);
  console.log(`data: ${JSON.stringify(data)}`);
});

emitter.emit('users.insert.1', { id: 1, name: 'Joe', age: 33 });

// action: insert
// id: 1
// data: {"id":1,"name":"Joe","age":33}

```

## What can I use in my events routes?

| Example         | Description          |
| --------------- | -------- |
| `:name`         |  a named parameter to capture from the route up to `/`, `?`, or end of string  |
| `*`        |  a splat to capture from the route up to `?` or end of string |
| <code>:name=(a&#124;b&#124;c)</code>  |  a named parameter group that doesn't have to be part of the query. Can contain nested optional groups, params, and splats
| anything else   | free form literals |

Some examples:

* `/some/:thing`
* `/users/:id/comments/:comment/rating/:rating`
* `/*/foo/*`
* `/books/:section=(Romance|Horror)/:title`


## TODO:

- [x] Basic API
- [x] Customizables delimeters and named segment symbols
- [ ] Reverse Matching
- [ ] Implement all features of EventEmitter2
- [ ] Emplement a "RethinkDBish" query API