# all files / qlobber/lib/ qlobber.js

100% Statements 116/116
100% Branches 51/51
100% Functions 22/22
100% Lines 116/116

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428

    1×

    1×

    27×

    27×
    27×
    27×
    27×

    1×

    165×

    1×

    360000×

    1×

    1013×

    1013×

    121013×

    1×

    27×

    27×

    26×

    27×

    1×

    4321147×

    4321147×

    720333×

    720333×

    720001×

    332×

    720333×

    3600814×
    3600814×

    3600814×

    429×
    429×

    3600814×

    1×

    222×

    222×

    66×

    66×

    60×

    66×

    156×
    156×

    156×

    2×

    154×

    154×

    68×

    1×

    1944×

    1944×

    2348×

    520×

    928×

    520×

    1944×

    1×

    3840×

    3840×

    3840×

    1944×

    1944×

    3840×

    2230×

    2230×

    2040×

    103×
    103×
    103×

    1937×

    115×
    115×

    1822×

    1610×

    1610×

    1606×

    1606×

    320×

    1610×

    1582×

    1582×

    515×

    3840×

    1×

    133×

    133×

    1×

    720333×
    720333×

    1×

    68×
    68×

    1×

    66×

    1×

    2×
    2×

    1×

    9×

    1×

    14×

    1×

    1×

    167×

    1×

    360001×

    1×

    1015×

    121015×

    1×

    27×
    27×

    1×

    67×

    1×
    1×

     /**
    # qlobber&nbsp;&nbsp;&nbsp;[![Build Status](https://travis-ci.org/davedoesdev/qlobber.png)](https://travis-ci.org/davedoesdev/qlobber) [![Coverage Status](https://coveralls.io/repos/davedoesdev/qlobber/badge.png?branch=master)](https://coveralls.io/r/davedoesdev/qlobber?branch=master) [![NPM version](https://badge.fury.io/js/qlobber.png)](http://badge.fury.io/js/qlobber)

    Node.js globbing for amqp-like topics.

    Example:

    ```javascript
    var Qlobber = require('qlobber').Qlobber;
    var matcher = new Qlobber();
    matcher.add('foo.*', 'it matched!');
    assert.deepEqual(matcher.match('foo.bar'), ['it matched!']);
    ```

    The API is described [here](#tableofcontents).

    qlobber is implemented using a trie, as described in the RabbitMQ blog posts [here](http://www.rabbitmq.com/blog/2010/09/14/very-fast-and-scalable-topic-routing-part-1/) and [here](http://www.rabbitmq.com/blog/2011/03/28/very-fast-and-scalable-topic-routing-part-2/).

    ## Installation

    ```shell
    npm install qlobber
    ```

    ## Another Example

    A more advanced example using topics from the [RabbitMQ topic tutorial](http://www.rabbitmq.com/tutorials/tutorial-five-python.html):

    ```javascript
    var matcher = new Qlobber();
    matcher.add('*.orange.*', 'Q1');
    matcher.add('*.*.rabbit', 'Q2');
    matcher.add('lazy.#', 'Q2');
    assert.deepEqual(['quick.orange.rabbit',
                      'lazy.orange.elephant',
                      'quick.orange.fox',
                      'lazy.brown.fox',
                      'lazy.pink.rabbit',
                      'quick.brown.fox',
                      'orange',
                      'quick.orange.male.rabbit',
                      'lazy.orange.male.rabbit'].map(function (topic)
                      {
                          return matcher.match(topic).sort();
                      }),
                     [['Q1', 'Q2'],
                      ['Q1', 'Q2'],
                      ['Q1'],
                      ['Q2'],
                      ['Q2', 'Q2'],
                      [],
                      [],
                      [],
                      ['Q2']]);
    ```

    ## Licence

    [MIT](LICENCE)

    ## Tests

    qlobber passes the [RabbitMQ topic tests](https://github.com/rabbitmq/rabbitmq-server/blob/master/src/rabbit_tests.erl) (I converted them from Erlang to Javascript).

    To run the tests:

    ```shell
    grunt test
    ```

    ## Lint

    ```shell
    grunt lint
    ```

    ## Code Coverage

    ```shell
    grunt coverage
    ```

    [Instanbul](http://gotwarlost.github.io/istanbul/) results are available [here](http://rawgit.davedoesdev.com/davedoesdev/qlobber/master/coverage/lcov-report/index.html).

    Coveralls page is [here](https://coveralls.io/r/davedoesdev/qlobber).

    ## Benchmarks

    ```shell
    grunt bench
    ```

    qlobber is also benchmarked in [ascoltatori](https://github.com/mcollina/ascoltatori).

    # API
    */

    /*jslint node: true, nomen: true */
    "use strict";

    var util = require('util');

    /**
    Creates a new qlobber.

    @constructor
    @param {Object} [options] Configures the qlobber. Use the following properties:

      - `{String} separator` The character to use for separating words in topics. Defaults to '.'. MQTT uses '/' as the separator, for example.

      - `{String} wildcard_one` The character to use for matching exactly one word in a topic. Defaults to '*'. MQTT uses '+', for example.

      - `{String} wildcard_some` The character to use for matching zero or more words in a topic. Defaults to '#'. MQTT uses '#' too.
    */
    function Qlobber (options)
    {
        options = options || {};

        this._separator = options.separator || '.';
        this._wildcard_one = options.wildcard_one || '*';
        this._wildcard_some = options.wildcard_some || '#';
        this._trie = new Map();
    }

    Qlobber.prototype._initial_value = function (val)
    {
        return [val];
    };

    Qlobber.prototype._add_value = function (vals, val)
    {
        vals[vals.length] = val;
    };

    Qlobber.prototype._add_values = function (dest, origin)
    {
        var i, destLength = dest.length, originLength = origin.length;

        for (i = 0; i < originLength; i += 1)
        {
            dest[destLength + i] = origin[i];
        }
    };

    Qlobber.prototype._remove_value = function (vals, val)
    {
        var index = vals.lastIndexOf(val);

        if (index >= 0)
        {
            vals.splice(index, 1);
        }

        return vals.length === 0;
    };

    Qlobber.prototype._add = function (val, i, words, sub_trie)
    {
        var st, word;

        if (i === words.length)
        {
            st = sub_trie.get(this._separator);

            if (st)
            {
                this._add_value(st, val);
            }
            else
            {
                sub_trie.set(this._separator, this._initial_value(val));
            }

            return;
        }

        word = words[i];
        st = sub_trie.get(word);

        if (!st)
        {
            st = new Map();
            sub_trie.set(word, st);
        }

        this._add(val, i + 1, words, st);
    };

    Qlobber.prototype._remove = function (val, i, words, sub_trie)
    {
        var st, word;

        if (i === words.length)
        {
            st = sub_trie.get(this._separator);

            if (st &&
                ((val === undefined) ||
                 this._remove_value(st, val)))
            {
                sub_trie.delete(this._separator);
            }

            return;
        }

        word = words[i];
        st = sub_trie.get(word);

        if (!st)
        {
            return;
        }

        this._remove(val, i + 1, words, st);

        if (st.size === 0)
        {
            sub_trie.delete(word);
        }
    };

    Qlobber.prototype._some = function (v, i, words, st)
    {
        var j, w;

        for (w of st.keys())
        {
            if (w !== this._separator)
            {
                for (j = i; j < words.length; j += 1)
                {
                    v = this._match(v, j, words, st);
                }
                break;
            }
        }

        return v;
    };

    Qlobber.prototype._match = function (v, i, words, sub_trie)
    {
        var word, st;

        st = sub_trie.get(this._wildcard_some);

        if (st)
        {
            // in the common case there will be no more levels...
            v = this._some(v, i, words, st);
            // and we'll end up matching the rest of the words:
            v = this._match(v, words.length, words, st);
        }

        if (i === words.length)
        {
            st = sub_trie.get(this._separator);

            if (st)
            {
                if (v.dest)
                {
                    this._add_values(v.dest, v.source);
                    this._add_values(v.dest, st);
                    v = v.dest;
                }
                else if (v.source)
                {
                    v.dest = v.source;
                    v.source = st;
                }
                else
                {
                    this._add_values(v, st);
                }
            }
        }
        else
        {
            word = words[i];

            if ((word !== this._wildcard_one) && (word !== this._wildcard_some))
            {
                st = sub_trie.get(word);

                if (st)
                {
                    v = this._match(v, i + 1, words, st);
                }
            }

            if (word)
            {
                st = sub_trie.get(this._wildcard_one);

                if (st)
                {
                    v = this._match(v, i + 1, words, st);
                }
            }
        }

        return v;
    };

    Qlobber.prototype._match2 = function (v, topic)
    {
        var vals = this._match(
        {
            source: v
        }, 0, topic.split(this._separator), this._trie);

        return vals.source || vals;
    };

    /**
    Add a topic matcher to the qlobber.

    Note you can match more than one value against a topic by calling `add` multiple times with the same topic and different values.

    @param {String} topic The topic to match against.
    @param {Any} val The value to return if the topic is matched. `undefined` is not supported.
    @return {Qlobber} The qlobber (for chaining).
    */
    Qlobber.prototype.add = function (topic, val)
    {
        this._add(val, 0, topic.split(this._separator), this._trie);
        return this;
    };

    /**
    Remove a topic matcher from the qlobber.

    @param {String} topic The topic that's being matched against.
    @param {Any} [val] The value that's being matched. If you don't specify `val` then all matchers for `topic` are removed.
    @return {Qlobber} The qlobber (for chaining).
    */
    Qlobber.prototype.remove = function (topic, val)
    {
        this._remove(val, 0, topic.split(this._separator), this._trie);
        return this;
    };

    /**
    Match a topic.

    @param {String} topic The topic to match against.
    @return {Array} List of values that matched the topic. This may contain duplicates.
    */
    Qlobber.prototype.match = function (topic)
    {
        return this._match2([], topic);
    };

    /**
    Reset the qlobber.

    Removes all topic matchers from the qlobber.

    @return {Qlobber} The qlobber (for chaining).
    */
    Qlobber.prototype.clear = function ()
    {
        this._trie = new Map();
        return this;
    };

    // for debugging
    Qlobber.prototype.get_trie = function ()
    {
        return this._trie;
    };

    /**
    Creates a new de-duplicating qlobber.

    Inherits from Qlobber.

    @constructor
    @param {Object} [options] Same options as Qlobber.
    */
    function QlobberDedup (options)
    {
        Qlobber.call(this, options);
    }

    util.inherits(QlobberDedup, Qlobber);

    QlobberDedup.prototype._initial_value = function (val)
    {
        return new Set().add(val);
    };

    QlobberDedup.prototype._add_value = function (vals, val)
    {
        vals.add(val);
    };

    QlobberDedup.prototype._add_values = function (dest, origin)
    {
        origin.forEach(function (val)
        {
            dest.add(val);
        });
    };

    QlobberDedup.prototype._remove_value = function (vals, val)
    {
        vals.delete(val);
        return vals.size === 0;
    };

    /**
    Match a topic.

    @param {String} topic The topic to match against.
    @return {Set} [ES6 Set](http://www.ecma-international.org/ecma-262/6.0/#sec-set-objects) of values that matched the topic.
    */
    QlobberDedup.prototype.match = function (topic)
    {
        return this._match2(new Set(), topic);
    };

    exports.Qlobber = Qlobber;
    exports.QlobberDedup = QlobberDedup;