<commit_before>[![Build Status](https://travis-ci.org/Orgun109uk/weighted-emitter.svg)](https://travis-ci.org/Orgun109uk/weighted-emitter)
[![Build Status](https://david-dm.org/orgun109uk/weighted-emitter.png)](https://david-dm.org/orgun109uk/weighted-emitter)
[![npm version](https://badge.fury.io/js/weighted-emitter.svg)](http://badge.fury.io/js/weighted-emitter)

[![NPM](https://nodei.co/npm/weighted-emitter.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/weighted-emitter/)

# Weighted Emitter

This utility is a basic event emitter, and allows sorting the listener callbacks by weight.

### Installation
```sh
$ npm install weighted-emitter
```

### Usage

```js
const WeightedEmitter = require('weighted-emitter');
const emitter = new WeightedEmitter();

emitter.on('event', function (next) {
    if (!this.result) {
        this.result = [];
    }

    this.result.push('world');
    next();
});

emitter.on('event', function (next) {
    if (!this.result) {
        this.result = [];
    }

    this.result.push(this.args.message1);
});

emitter.emit('event', {message1: 'hello'}, function (err, result) {
    console.info(result);
    /**
     * Result = ['hello', 'world'];
     */
});
```

Listeners can also be removed;

```js
emitter.un('event', callback);
// Or to remove all listeners of an event;
emitter.un('event');
```

## Testing
A mocha test suite has been provided and can be run by:
```sh
$ npm test
```<commit_msg>Updated missing weight in the README.<commit_after>[![Build Status](https://travis-ci.org/Orgun109uk/weighted-emitter.svg)](https://travis-ci.org/Orgun109uk/weighted-emitter)
[![Build Status](https://david-dm.org/orgun109uk/weighted-emitter.png)](https://david-dm.org/orgun109uk/weighted-emitter)
[![npm version](https://badge.fury.io/js/weighted-emitter.svg)](http://badge.fury.io/js/weighted-emitter)

[![NPM](https://nodei.co/npm/weighted-emitter.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/weighted-emitter/)

# Weighted Emitter

This utility is a basic event emitter, and allows sorting the listener callbacks by weight.

### Installation
```sh
$ npm install weighted-emitter
```

### Usage

```js
const WeightedEmitter = require('weighted-emitter');
const emitter = new WeightedEmitter();

emitter.on('event', function (next) {
    if (!this.result) {
        this.result = [];
    }

    this.result.push('world');
    next();
});

emitter.on('event', function (next) {
    if (!this.result) {
        this.result = [];
    }

    this.result.push(this.args.message1);
});

emitter.emit('event', {message1: 'hello'}, function (err, result) {
    console.info(result);
    /**
     * Result = ['hello', 'world'];
     */
}, -10);
```

Listeners can also be removed;

```js
emitter.un('event', callback);
// Or to remove all listeners of an event;
emitter.un('event');
```

## Testing
A mocha test suite has been provided and can be run by:
```sh
$ npm test
```
{'fraction_non_alphanumeric': 0.1883665877578627, 'fraction_numerical': 0.010145417653026716, 'mean_word_length': 4.126516464471404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4274675', 'n_tokens_mistral': 1077, 'n_tokens_neox': 965, 'n_words': 198}