Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Created June 1, 2016 19:31
Show Gist options
  • Save joshbirk/235dca81956802bd95d4b69d5e418f4b to your computer and use it in GitHub Desktop.
Save joshbirk/235dca81956802bd95d4b69d5e418f4b to your computer and use it in GitHub Desktop.
Module version of plumb
"use strict";
var plumb = (function (initial_data) {
// Properties
///////////////////////////
var raw_data = [];
var data = [];
var load = function(raw_data,data) {
this.raw_data = raw_data;
this.data = data;
};
var generateKey = function(plumber) {
this.keyPlumber = plumber;
this.data = [];
var data_plumb = this;
this.raw_data.forEach(function(record) {
data_plumb.data[data_plumb.keyPlumber(record)] = {};
});
return this;
}
var meanField = function(field,label) {
var data_plumb = this;
this.raw_data.forEach(function(record) {
if(data_plumb.data[data_plumb.keyPlumber(record)][label] === undefined) { //this assumes first run
data_plumb.data[data_plumb.keyPlumber(record)][label] = 0;
data_plumb.data[data_plumb.keyPlumber(record)]["keyCount"] = 0;
}
data_plumb.data[data_plumb.keyPlumber(record)][label] += record[field];
data_plumb.data[data_plumb.keyPlumber(record)]["keyCount"] = data_plumb.data[data_plumb.keyPlumber(record)]["keyCount"]+1;
});
this.data.forEach(function(record) {
record[label] /= record["keyCount"];
});
return this;
}
var maxField = function(field,label) {
var data_plumb = this;
this.raw_data.forEach(function(record) {
if(data_plumb.data[data_plumb.keyPlumber(record)][label] === undefined || record[field] > data_plumb.data[data_plumb.keyPlumber(record)][label]) {
data_plumb.data[data_plumb.keyPlumber(record)][label] = record[field];
}
});
return this;
}
var minField = function(field,label) {
var data_plumb = this;
this.raw_data.forEach(function(record) {
if(data_plumb.data[data_plumb.keyPlumber(record)][label] === undefined || record[field] < data_plumb.data[data_plumb.keyPlumber(record)][label]) {
data_plumb.data[data_plumb.keyPlumber(record)][label] = record[field];
}
});
return this;
}
raw_data = initial_data;
// Reveal public methods
return {
raw_data: raw_data,
data: data,
load: load,
generateKey: generateKey,
meanField: meanField,
maxField: maxField,
minField: minField
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment