npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

diff-stream

v0.0.6

Published

Compare the contents of two object streams, and produce a new stream consisting of the differences

Downloads

2

Readme

diff-stream

Compare two ordered object streams and produce a new stream consisting of the differences.

example

  var diffStream = require('diff-stream');

  var fromArray = require('read-stream').array;
  var stream1 = fromArray([{id: 1, name: 'albert'}, {id: 2, name: 'bob'}, {id: 3, name: 'cathy'}]);
  var stream2 = fromArray([{id: 1, name: 'albert'}, {id: 2, name: 'joe'}, {id: 4, name: 'thomas'}, {id: 5, name: 'xavier'}]);

  diffStream(stream1, stream2).pipe(process.stdout);

produces a stream of tuples:

  [{id: 2, name: 'bob'}, {id: 2, name: 'joe'}]
  [{id: 3, name: 'cathy'}, null]
  [null, {id: 4, name: 'thomas'}]
  [null, {id: 5, name: 'xavier'}]

This can be used, for example, to compare rows from streaming database queries, or long streams of JSON objects such as those produced by JSONStream, without having to actually buffer large amounts of data into memory.

Similar to the behavior of the ubiquitous diff command:

  • Identical objects are not output
  • Corresponding but differing objects are output as [obj1, obj2]
  • Objects that only exist in stream1 are output as [obj1, null]
  • Objects that only exist in stream2 are output as [null, obj2]

diffStream(stream1, stream2, [compare])

Compare the readable streams stream1 and stream2, returning a new readable stream, which consists of tuple pairs.

compare

The optional compare parameter may be a string key, used to determine the order of the objets. This defaults to id, which is a common key used to order objects in an object stream, such as results from a database query.

compare may also be a function, like the following:

  diffStream(stream1, stream1, function(obj1, obj2) {
    if (obj1.id < obj2.id)
      this.left();
    else if (obj1.id > obj2.id)
      this.right();
    else if (obj1.id == obj2.id && obj1.name == obj2.name)
      this.equal();
    else
      this.notEqual();
  }).pipe(process.stdout);

The function is called with pairs of objects from each stream. The function should call left, right, equal, or notEqual, to produce the corresponding output, and advance either stream1, or stream2, or both.

  • left will output [obj1, null], and advance stream1
  • right will output [null, obj2], and advance stream2
  • equal will output nothing, and advance both streams
  • notEqual will output [obj1, obj2], and advance both streams

ordering

Note, it is important that the two object streams be ordered, and that this ordering matches the compare method as described above. If the streams consist of unordered objects, then pairs will not be aligned appropriately.

license

MIT