HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/node_modules/unique-stream/test/index.js
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();
  });
});