[JS] Plumb.js - a lib for aggregating key/value pairs

Occasionally I work on small pieces of utilitarian code under the auspice of "Laziness is a developer virtue" theory that working longer on a library I can use down the road will definitely save me more time.

Then I use it for a single demo and never touch it again.

Plumb was one such library. I wrote it because I needed to be able to take a relatively large set of data and organize it in a way that charts and visualizations understand. What you see in a data visualization is often, depending on the design, the aggregate of results and not the single points of data.

For instance, my Speed Test demo which took average Internet speeds and displayed them around a 24 hour segmented circle to show how they fluctuated through the day. The whole point was to illustrate how the neighborhood problem of cable Internet was killing my speeds at peak hours.

To arrive at the visualization, I only needed the transformed numbers from the raw source. So out of about 1,000 points of data I only needed show 24.

To do this, I just fed the raw data into a new plumb. In this case an array of custom objects (from a Remote Action call) that included the download speed and timestamp:

speed_plumb = new plumb(speeds);

And then use the generateKey method of the plumb that set of data based on the hour array. Plumbs do this by being handed a method to determine how to form that key:

speed_plumb.generateKey(function(speed) {
            var hour = speed.TimeString__c.split(" ")[1];
            hour = hour.split(":")[0];
            hour = parseInt(hour);

            return hour;
            })

Once generated, you can ask it to use that key to create mean, max and min fields based on another field:

speed_plumb.generateKey(function(speed) {
            var hour = speed.TimeString__c.split(" ")[1];
            hour = hour.split(":")[0];
            hour = parseInt(hour);

            return hour;
            }).meanField("Download__c","DownloadSpeed")
              .maxField("Download__c","MaxDownload")
              .minField("Download__c","MinDownload")
              .meanField("Download__c","MeanDownload");

All of this is stored as an attribute "data" on the plumb and can be accessed via the key:

speed_plumb.data[12].MaxDownload

Would give you the max download speed at noon given the current set of data in the plumb.

Plumb, btw, comes from "plumb the depths" not necessarily a "tree born fruit".

You can see the code and how I used it in that demo from this recent gist I put together to show how the Speed Test component would work in Lightning.

File it under "I found it helpful once and now maybe you could too".

comments powered by Disqus