unique-stream

WebJar for unique-stream

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

unique-stream
Last Version

Last Version

2.3.1
Release Date

Release Date

Type

Type

jar
Description

Description

unique-stream
WebJar for unique-stream
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/eugeneware/unique-stream

Download unique-stream

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/unique-stream/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>unique-stream</artifactId>
    <version>2.3.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/unique-stream/
implementation 'org.webjars.npm:unique-stream:2.3.1'
// https://jarcasting.com/artifacts/org.webjars.npm/unique-stream/
implementation ("org.webjars.npm:unique-stream:2.3.1")
'org.webjars.npm:unique-stream:jar:2.3.1'
<dependency org="org.webjars.npm" name="unique-stream" rev="2.3.1">
  <artifact name="unique-stream" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='unique-stream', version='2.3.1')
)
libraryDependencies += "org.webjars.npm" % "unique-stream" % "2.3.1"
[org.webjars.npm/unique-stream "2.3.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.webjars.npm : json-stable-stringify-without-jsonify jar [1.0.1,2)
org.webjars.npm : through2-filter jar [3.0.0,4)

Project Modules

There are no modules declared in this project.

unique-stream

node.js through stream that emits a unique stream of objects based on criteria

Build Status Coverage Status

Installation

Install via npm:

$ npm install unique-stream

Examples

Dedupe a ReadStream based on JSON.stringify:

var unique = require('unique-stream')
  , Stream = require('stream');

// return a stream of 3 identical objects
function makeStreamOfObjects() {
  var s = new Stream;
  s.readable = true;
  var count = 3;
  for (var i = 0; i < 3; i++) {
    setImmediate(function () {
      s.emit('data', { name: 'Bob', number: 123 });
      --count || end();
    });
  }

  function end() {
    s.emit('end');
  }

  return s;
}

// Will only print out one object as the rest are dupes. (Uses JSON.stringify)
makeStreamOfObjects()
  .pipe(unique())
  .on('data', console.log);

Dedupe a ReadStream based on an object property:

// Use name as the key field to dedupe on. Will only print one object
makeStreamOfObjects()
  .pipe(unique('name'))
  .on('data', console.log);

Dedupe a ReadStream based on a custom function:

// Use a custom function to dedupe on. Use the 'number' field. Will only print one object.
makeStreamOfObjects()
  .pipe(function (data) {
    return data.number;
  })
  .on('data', console.log);

Dedupe multiple streams

The reason I wrote this was to dedupe multiple object streams:

var aggregator = unique();

// Stream 1
makeStreamOfObjects()
  .pipe(aggregator);

// Stream 2
makeStreamOfObjects()
  .pipe(aggregator);

// Stream 3
makeStreamOfObjects()
  .pipe(aggregator);

aggregator.on('data', console.log);

Use a custom store to record keys that have been encountered

By default a set is used to store keys encountered so far, in order to check new ones for uniqueness. You can supply your own store instead, providing it supports the add(key) and has(key) methods. This could allow you to use a persistent store so that already encountered objects are not re-streamed when node is reloaded.

var keyStore = {
  store: {},

  add: function(key) {
    this.store[key] = true;
  },

  has: function(key) {
    return this.store[key] !== undefined;
  }
};
    
makeStreamOfObjects()
  .pipe(unique('name', keyStore))
  .on('data', console.log);

Contributing

unique-stream is an OPEN Open Source Project. This means that:

Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

See the CONTRIBUTING.md file for more details.

Contributors

unique-stream is only possible due to the excellent work of the following contributors:

Eugene Ware GitHub/eugeneware
Craig Ambrose GitHub/craigambrose
Shinnosuke Watanabe GitHub/shinnn
Rouven Weßling GitHub/realityking

Versions

Version
2.3.1
2.2.1
2.0.2
1.0.0