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

hash-join

v0.0.0

Published

hash joins on streaming document collections

Downloads

4

Readme

hash-join

compute https://en.wikipedia.org/wiki/Hash_join on streaming document collections

browser support

build status

example

hashing with functions

var join = require('hash-join');
var r = join(
    function (doc) { return doc.type === 'test' && doc.id },
    function (doc) { return doc.type === 'output' && doc.test }
);

r.reduce(function (acc, test, output) {
    var t = acc[test.id];
    if (!t) t = acc[test.id] = { commit : test.commit };
    
    if (!t.output) t.output = [];
    t.output.push(output.value);
    
    return acc;
}, {});

r.on('result', function (res) {
    console.dir(res);
});

r.insert({ type : 'output', test : 'abc123', value : 'beep ' });
r.insert({ type : 'output', test : 'abc123', value : 'boop\n' });
r.insert({
    type : 'test',
    id : 'abc123',
    commit : 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
});
$ node example/hash.js
{ abc123: 
   { commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
     output: [ 'beep ' ] } }
{ abc123: 
   { commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
     output: [ 'beep ', 'boop\n' ] } }

using key path shortcuts

var join = require('hash-join');
var r = join('test', [ 'output', 'test' ]);

r.reduce(function (acc, test, output) {
    var t = acc[test.id];
    if (!t) t = acc[test.id] = { commit : test.commit };
    
    if (!t.output) t.output = [];
    t.output.push(output.value);
    
    return acc;
}, {});

r.on('result', function (res) {
    console.dir(res);
});

r.insert({ type : 'output', test : 'abc123', value : 'beep ' });
r.insert({ type : 'output', test : 'abc123', value : 'boop\n' });
r.insert({
    type : 'test',
    id : 'abc123',
    commit : 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
});
$ node example/reduce.js
{ abc123: 
   { commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
     output: [ 'beep ' ] } }
{ abc123: 
   { commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
     output: [ 'beep ', 'boop\n' ] } }

using crdt

var join = require('hash-join');
var r = join('test', [ 'output', 'test' ]);

r.reduce(function (acc, test, output) {
    var t = acc[test.id];
    if (!t) t = acc[test.id] = { commit : test.commit };
    
    if (!t.output) t.output = [];
    t.output.push(output.value);
    
    return acc;
}, {});

r.on('result', function (res) {
    console.dir(res);
});

var doc = new(require('crdt').Doc);
doc.on('add', function (x) { r.insert(x.state) });

doc.add({ type : 'output', test : 'abc123', value : 'beep ' });
doc.add({ type : 'output', test : 'abc123', value : 'boop\n' });
doc.add({
    type : 'test',
    id : 'abc123',
    commit : 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
});
$ node example/reduce_crdt.js
{ abc123: 
   { commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
     output: [ 'beep ' ] } }
{ abc123: 
   { commit: 'd8180b0778dd7a145b46f92b9a2d77db916debc5',
     output: [ 'beep ', 'boop\n' ] } }

methods

var join = require('hash-join')

var r = join(a, b)

Create a new 2-way join r from the hashing functions a and b.

Hashing functions should return falsy values if they don't match and otherwise should return the string id they should hash into.

If a or b is an array, they are treated as a path through the document to obtain the hashed element starting at the document's 'type' field and deeply traversing for the rest of the arguments. For example ['beep','boop','x',2] would resolve to the string "ghi" for this document:

{
  "type": "beep",
  "boop": { "x" : [ "abc", "def", "ghi" ] },
  "z" : 55
}

If a or b is a string, they are converted to [a,'id'] or [b,'id'] accordingly so that you can more tersely express a foreign key relation on the "id" field.

r.insert(doc)

Insert a document into the join r.

r.reduce(fn, init)

Compute a reduce over all the pairs that resolve to the same hash id with fn(acc, a, b) for an accumulator acc and the objects a and b that match the hashing functions supplied to or inferred from join() in the same order.

fn(acc, a, b) should return the new accumulator value acc. Each time a new value is derived, the 'result' event fires with the new accumulator result.

The accumulator starts off with the value given by init.

events

r.on('result', function (res) {})

Whenever a new result value is obtained from the reduce(), this event fires with the new accumulator value.

r.on('pair', function (a, b) {})

Whenever a pair is matched, this event fires with each item in the pair in the order specified to join().

install

With npm do:

npm install hash-join

license

MIT