123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- var expect = require('chai').expect
- , unique = require('..')
- , Stream = require('stream')
- , after = require('after')
- , setImmediate = global.setImmediate || process.nextTick;
- describe('unique stream', function() {
- function makeStream(type) {
- var s = new Stream();
- s.readable = true;
- var n = 10;
- var next = after(n, function () {
- setImmediate(function () {
- s.emit('end');
- });
- });
- for (var i = 0; i < n; i++) {
- var o = {
- type: type,
- name: 'name ' + i,
- number: i * 10
- };
- (function (o) {
- setImmediate(function () {
- s.emit('data', o);
- next();
- });
- })(o);
- }
- return s;
- }
- it('should be able to uniqueify objects based on JSON data', function(done) {
- var aggregator = unique();
- makeStream('a')
- .pipe(aggregator);
- makeStream('a')
- .pipe(aggregator);
- var n = 0;
- aggregator
- .on('data', function () {
- n++;
- })
- .on('end', function () {
- expect(n).to.equal(10);
- done();
- });
- });
- it('should be able to uniqueify objects based on a property', function(done) {
- var aggregator = unique('number');
- makeStream('a')
- .pipe(aggregator);
- makeStream('b')
- .pipe(aggregator);
- var n = 0;
- aggregator
- .on('data', function () {
- n++;
- })
- .on('end', function () {
- expect(n).to.equal(10);
- done();
- });
- });
- it('should be able to uniqueify objects based on a function', function(done) {
- var aggregator = unique(function (data) {
- return data.name;
- });
- makeStream('a')
- .pipe(aggregator);
- makeStream('b')
- .pipe(aggregator);
- var n = 0;
- aggregator
- .on('data', function () {
- n++;
- })
- .on('end', function () {
- expect(n).to.equal(10);
- done();
- });
- });
- it('should be able to handle uniqueness when not piped', function(done) {
- var stream = unique();
- var count = 0;
- stream.on('data', function (data) {
- expect(data).to.equal('hello');
- count++;
- });
- stream.on('end', function() {
- expect(count).to.equal(1);
- done();
- });
- stream.write('hello');
- stream.write('hello');
- stream.end();
- });
- });
|