-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqlobber.js
1126 lines (912 loc) · 29.6 KB
/
qlobber.js
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
# qlobber [![ci](https://github.com/davedoesdev/qlobber/actions/workflows/ci.yml/badge.svg)](https://github.com/davedoesdev/qlobber/actions/workflows/ci.yml) [![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.
__Note:__ Version 5.0.0 adds async and worker thread support when used on Node 12+.
Example:
```javascript
var assert = require('assert');
var Qlobber = require('qlobber').Qlobber;
var matcher = new Qlobber();
matcher.add('foo.*', 'it matched!');
assert.deepEqual(matcher.match('foo.bar'), ['it matched!']);
assert(matcher.test('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 assert = require('assert');
var Qlobber = require('qlobber').Qlobber;
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']]);
```
## Async Example
Same as the first example but using `await`:
```javascript
const assert = require('assert');
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
const matcher = new Qlobber.nativeString();
(async () => {
await matcher.addP('foo.*', 'it matched!');
assert.deepEqual(await matcher.matchP('foo.bar'), ['it matched!']);
assert(await matcher.testP('foo.bar', 'it matched!'));
})();
```
## Worker Thread Example
Same again but the matching is done on a separate thread:
```
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
if (isMainThread) {
const matcher = new Qlobber.nativeString();
matcher.add('foo.*', 'it matched!');
const worker = new Worker(__filename, {
workerData: matcher.state_address
});
worker.on('message', msg => {
const assert = require('assert');
assert.deepEqual(msg, [['it matched!'], true]);
});
} else {
const matcher = new Qlobber.nativeString(workerData);
parentPort.postMessage([
matcher.match('foo.bar'),
matcher.test('foo.bar', 'it matched!')
]);
}
```
## 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
npm test
```
## Lint
```shell
grunt lint
```
## Code Coverage
```shell
npm run coverage
```
[c8](https://github.com/bcoe/c8) 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).
## Native Qlobbers
The Javascript Qlobbers don't support asynchronous calls and worker threads
because Javascript values can't be shared between threads.
In order to support asynchronous calls and worker threads, a native C++
implementation is available in the
[qlobber-native](https://www.npmjs.com/package/qlobber-native) module.
Add qlobber-native as a dependency to your project and then add it to qlobber
like this:
```javascript
require('qlobber').set_native(require('qlobber-native'));
```
Note that [`set_native`](#set_nativeqlobber_native) returns qlobber's exports so you can do something like
this:
```javascript
const { Qlobber } = require('qlobber').set_native(require('qlobber-native'));
```
Note that qlobber-native requires Gnu C++ version 9+ and Boost 1.70+,
including the `boost_context` runtime library.
Once's you've added it to qlobber, the following classes will be available
alongside the Javascript classes:
- `Qlobber.nativeString`
- `Qlobber.nativeNumber`
- `QlobberDedup.nativeString`
- `QlobberDedup.nativeNumber`
- `QlobberTrue.native`
They can only hold values of a single type (currently strings or numbers).
### Asynchronous calls
The native classes support the same API as the Javascript classes but have the
following additional methods:
- `addP`
- `removeP`
- `matchP`
- `match_iterP`
- `testP`
- `clearP`
- `visitP`
- `get_restorerP`
They correspond to their namesakes but return Promises. Note that `match_iterP`
and `visitP` return async iterators.
# 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 _non-empty_ 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.
- `{Boolean} match_empty_levels` If `true` then `wilcard_one` also matches an empty word in a topic. Defaults to `false`.
- `{Boolean|Map} cache_adds` Whether to cache topics when adding topic matchers. This will make adding multiple matchers for the same topic faster at the cost of extra memory usage. Defaults to `false`. If you supply a `Map` then it will be used to cache the topics (use this to enumerate all the topics in the qlobber).
- `{Integer} cache_splits` How many `topic.split` results to cache. When you pass in a topic, it has to be split on the `separator`. Caching the results will make using the same topics multiple times faster at the cost of extra memory usage. Defaults to `0` (no caching). The number of split results cached is limited by the value you pass here.
- `{Integer} max_words` Maximum number of words to allow in a topic. Defaults to 100.
- `{Integer} max_wildcard_somes` Maximum number of `wildcard_some` words in a topic. Defaults to 3.
*/
function Qlobber (options)
{
options = options || {};
this._separator = options.separator || '.';
this._wildcard_one = options.wildcard_one || '*';
this._wildcard_some = options.wildcard_some || '#';
this._max_words = options.max_words || 100;
this._max_wildcard_somes = options.max_wildcard_somes || 3;
this._match_empty_levels = options.match_empty_levels;
this._trie = new Map();
if (options.cache_adds instanceof Map)
{
this._shortcuts = options.cache_adds;
}
else if (options.cache_adds)
{
this._shortcuts = new Map();
}
if (options.cache_splits > 0)
{
this._cache_splits = options.cache_splits;
this._split_cache = 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._iter_values = function (origin, ctx)
{
return origin[Symbol.iterator](ctx);
};
Qlobber.prototype._remove_value = function (vals, val)
{
if (val === undefined)
{
return true;
}
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
{
st = this._initial_value(val);
sub_trie.set(this._separator, st);
}
return st;
}
word = words[i];
st = sub_trie.get(word);
if (!st)
{
st = new Map();
sub_trie.set(word, st);
}
return this._add(val, i + 1, words, st);
};
Qlobber.prototype._remove = function (val, i, words, sub_trie)
{
var st, word, r;
if (i === words.length)
{
st = sub_trie.get(this._separator);
if (st && this._remove_value(st, val))
{
sub_trie.delete(this._separator);
return true;
}
return false;
}
word = words[i];
st = sub_trie.get(word);
if (!st)
{
return false;
}
r = this._remove(val, i + 1, words, st);
if (st.size === 0)
{
sub_trie.delete(word);
}
return r;
};
Qlobber.prototype._match_some = function (v, i, words, st, ctx)
{
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, ctx);
}
break;
}
}
return v;
};
Qlobber.prototype._match = function (v, i, words, sub_trie, ctx)
{
var word, st;
st = sub_trie.get(this._wildcard_some);
if (st)
{
// in the common case there will be no more levels...
v = this._match_some(v, i, words, st, ctx);
// and we'll end up matching the rest of the words:
v = this._match(v, words.length, words, st, ctx);
}
if (i === words.length)
{
st = sub_trie.get(this._separator);
if (st)
{
if (v.dest)
{
this._add_values(v.dest, v.source, ctx);
this._add_values(v.dest, st, ctx);
v = v.dest;
}
else if (v.source)
{
v.dest = v.source;
v.source = st;
}
else
{
this._add_values(v, st, ctx);
}
}
}
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, ctx);
}
}
if (word || this._match_empty_levels)
{
st = sub_trie.get(this._wildcard_one);
if (st)
{
v = this._match(v, i + 1, words, st, ctx);
}
}
}
return v;
};
Qlobber.prototype._match2 = function (v, topic, ctx)
{
var vals = this._match(
{
source: v
}, 0, this._split(topic, false), this._trie, ctx);
return vals.source || vals;
};
Qlobber.prototype._test_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)
{
if (this._test(v, j, words, st))
{
return true;
}
}
break;
}
}
return false;
};
Qlobber.prototype._test = 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...
if (this._test_some(v, i, words, st) ||
// and we'll end up matching the rest of the words:
this._test(v, words.length, words, st))
{
return true;
}
}
if (i === words.length)
{
st = sub_trie.get(this._separator);
if (st && this.test_values(st, v))
{
return true;
}
}
else
{
word = words[i];
if ((word !== this._wildcard_one) && (word !== this._wildcard_some))
{
st = sub_trie.get(word);
if (st && this._test(v, i + 1, words, st))
{
return true;
}
}
if (word || this._match_empty_levels)
{
st = sub_trie.get(this._wildcard_one);
if (st && this._test(v, i + 1, words, st))
{
return true;
}
}
}
return false;
};
Qlobber.prototype._match_some_iter = function* (i, words, st, ctx)
{
var j, w;
for (w of st.keys())
{
if (w !== this._separator)
{
for (j = i; j < words.length; j += 1)
{
yield* this._match_iter(j, words, st, ctx);
}
break;
}
}
};
Qlobber.prototype._match_iter = function* (i, words, sub_trie, ctx)
{
var word, st;
st = sub_trie.get(this._wildcard_some);
if (st)
{
// in the common case there will be no more levels...
yield* this._match_some_iter(i, words, st, ctx);
// and we'll end up matching the rest of the words:
yield* this._match_iter(words.length, words, st, ctx);
}
if (i === words.length)
{
st = sub_trie.get(this._separator);
if (st)
{
yield* this._iter_values(st, ctx);
}
}
else
{
word = words[i];
if ((word !== this._wildcard_one) && (word !== this._wildcard_some))
{
st = sub_trie.get(word);
if (st)
{
yield* this._match_iter(i + 1, words, st, ctx);
}
}
if (word || this._match_empty_levels)
{
st = sub_trie.get(this._wildcard_one);
if (st)
{
yield* this._match_iter(i + 1, words, st, ctx);
}
}
}
};
Qlobber.prototype._split_words = function (topic) {
const r = topic.split(this._separator);
if (r.length > this._max_words) {
throw new Error('too many words');
}
return r;
};
Qlobber.prototype._split = function (topic, adding) {
let r;
if (this._split_cache) {
r = this._split_cache.get(topic);
if (r === undefined) {
r = this._split_words(topic);
this._split_cache.set(topic, r);
if (this._split_cache.size > this._cache_splits) {
for (let t of this._split_cache.keys()) {
this._split_cache.delete(t);
break;
}
}
}
} else {
r = this._split_words(topic);
}
if (adding &&
(r.reduce((n, w) => n + (w === this._wildcard_some), 0) > this._max_wildcard_somes)) {
throw new Error('too many wildcard somes');
}
return r;
};
/**
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.
@return {Qlobber} The qlobber (for chaining).
*/
Qlobber.prototype.add = function (topic, val)
{
var shortcut = this._shortcuts && this._shortcuts.get(topic);
if (shortcut)
{
this._add_value(shortcut, val);
}
else
{
shortcut = this._add(val, 0, this._split(topic, true), this._trie);
if (this._shortcuts)
{
this._shortcuts.set(topic, shortcut);
}
}
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)
{
if (this._remove(val, 0, this._split(topic, false), this._trie) && this._shortcuts)
{
this._shortcuts.delete(topic);
}
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. Use a [`QlobberDedup`](#qlobberdedupoptions) if you don't want duplicates.
*/
Qlobber.prototype.match = function (topic, ctx)
{
return this._match2([], topic, ctx);
};
/**
Match a topic, returning the matches one at a time.
@return {Iterator} An iterator on the values that match the topic. There may be duplicate values, even if you use a [`QlobberDedup`](#qlobberdedupoptions).
*/
Qlobber.prototype.match_iter = function (topic, ctx)
{
return this._match_iter(0, this._split(topic, false), this._trie, ctx);
};
/**
Test whether a topic match contains a value. Faster than calling [`match`](#qlobberprototypematchtopic) and searching the result for the value. Values are tested using [`test_values`](#qlobberprototypetest_valuesvals-val).
@param {String} topic The topic to match against.
@param {Any} val The value being tested for.
@return {Boolean} Whether matching against `topic` contains `val`.
*/
Qlobber.prototype.test = function (topic, val)
{
return this._test(val, 0, this._split(topic, false), this._trie);
};
/**
Test whether values found in a match contain a value passed to [`test`](#qlobberprototypetesttopic-val). You can override this to provide a custom implementation. Defaults to using `indexOf`.
@param {Array} vals The values found while matching.
@param {Any} val The value being tested for.
@return {Boolean} Whether `vals` contains `val`.
*/
Qlobber.prototype.test_values = function (vals, val)
{
return vals.indexOf(val) >= 0;
};
/**
Reset the qlobber.
Removes all topic matchers from the qlobber.
@return {Qlobber} The qlobber (for chaining).
*/
Qlobber.prototype.clear = function ()
{
this._trie.clear();
if (this._shortcuts)
{
this._shortcuts.clear();
}
if (this._split_cache)
{
this._split_cache.clear();
}
return this;
};
// for debugging
Qlobber.prototype.get_trie = function ()
{
return this._trie;
};
/**
Visit each node in the qlobber's trie in turn.
@return {Iterator} An iterator on the trie. The iterator returns objects which, if fed (in the same order) to the function returned by [`get_restorer`](#qlobberprototypeget_restoreroptions) on a different qlobber, will build that qlobber's trie to the same state. The objects can be serialized using `JSON.stringify`, _if_ the values you store in the qlobber are also serializable.
*/
Qlobber.prototype.visit = function* ()
{
let iterators = [],
iterator = this._trie.entries(),
i = 0;
while (true)
{
if (i === 0)
{
yield { type: 'start_entries' };
}
let next = iterator.next();
if (next.done)
{
yield { type: 'end_entries' };
let prev = iterators.pop();
if (prev === undefined)
{
return;
}
[iterator, i] = prev;
continue;
}
let [key, value] = next.value;
yield { type: 'entry', key: key };
++i;
if (key === this._separator)
{
yield { type: 'start_values' };
if (value[Symbol.iterator])
{
for (let v of value)
{
yield { type: 'value', value: v };
}
}
else
{
yield { type: 'value', value: value };
}
yield { type: 'end_values' };
continue;
}
iterators.push([iterator, i]);
iterator = value.entries();
i = 0;
}
};
/**
Get a function which can restore the qlobber's trie to a state you retrieved
by calling [`visit`](#qlobberprototypevisit) on this or another qlobber.
@param {Object} [options] Options for restoring the trie.
- `{Boolean} cache_adds` Whether to cache topics when rebuilding the trie. This only applies if you also passed `cache_adds` as true in the [constructor](#qlobberoptions).
@return {Function} Function to call in order to rebuild the qlobber's trie. You should call this repeatedly with the objects you received from a call to [`visit`](#qlobberprototypevisit). If you serialized the objects, remember to deserialize them first (e.g. with `JSON.parse`)!
*/
Qlobber.prototype.get_restorer = function (options)
{
options = options || {};
let sts = [],
entry = this._trie,
path = '';
return obj =>
{
switch (obj.type)
{
case 'entry':
entry = entry || new Map();
sts.push([entry, obj.key, path]);
entry = entry.get(obj.key);
if (options.cache_adds)
{
if (path)
{
path += this._separator;
}
path += obj.key;
}
break;
case 'value':
if (entry)
{
this._add_value(entry, obj.value);
}
else
{
entry = this._initial_value(obj.value);
}
break;
case 'end_entries':
if (entry && (entry.size === 0))
{
entry = undefined;
}
/* falls through */
case 'end_values':
let prev = sts.pop();
if (prev === undefined)
{
entry = undefined;
path = '';
}
else
{
let [prev_entry, key, prev_path] = prev;
if (entry)
{
if (options.cache_adds &&
this._shortcuts &&
(obj.type === 'end_values'))
{
this._shortcuts.set(prev_path, entry);
}
prev_entry.set(key, entry);
}
entry = prev_entry;
path = prev_path;
}
break;
}
};
};
/**
Creates a new de-duplicating qlobber.
Inherits from [`Qlobber`](#qlobberoptions).
@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)
{
if (val === undefined)
{
return true;
}
vals.delete(val);
return vals.size === 0;
};
/**
Test whether values found in a match contain a value passed to [`test`](#qlobberprototypetesttopic_val). You can override this to provide a custom implementation. Defaults to using `has`.
@param {Set} vals The values found while matching ([ES6 Set](http://www.ecma-international.org/ecma-262/6.0/#sec-set-objects)).
@param {Any} val The value being tested for.
@return {Boolean} Whether `vals` contains `val`.
*/
QlobberDedup.prototype.test_values = function (vals, val)
{
return vals.has(val);
};
/**
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, ctx)
{
return this._match2(new Set(), topic, ctx);
};
/**
Creates a new qlobber which only stores the value `true`.
Whatever value you [`add`](#qlobberprototypeaddtopic-val) to this qlobber
(even `undefined`), a single, de-duplicated `true` will be stored. Use this
qlobber if you only need to test whether topics match, not about the values
they match to.
Inherits from [`Qlobber`](#qlobberoptions).
@constructor
@param {Object} [options] Same options as Qlobber.
*/
function QlobberTrue (options)
{
Qlobber.call(this, options);
}
util.inherits(QlobberTrue, Qlobber);
QlobberTrue.prototype._initial_value = function ()
{
return true;
};
QlobberTrue.prototype._add_value = function ()
{
};
QlobberTrue.prototype._remove_value = function ()
{
return true;
};
QlobberTrue.prototype._iter_values = function* ()
{
yield true;
};
/**
This override of [`test_values`](#qlobberprototypetest_valuesvals-val) always