Skip to content

Commit

Permalink
Adding preprocessing on group by categories.
Browse files Browse the repository at this point in the history
  • Loading branch information
Irene Ros committed Feb 12, 2012
1 parent 8fe24d1 commit cce2e57
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
28 changes: 20 additions & 8 deletions src/derived.js
Expand Up @@ -16,21 +16,28 @@
* group rows by values in a given column
* @param {byColumn} column by which rows will be grouped
* @param {columns} columns to be included
* @param {method} function to be applied, default addition
* @params {object} options
* method function to be applied, default addition
* preprocess - specify a normalization function for the
* byColumn values if you need to group by some kind of derivation of
* those values that are not just equality based.
*/
groupBy : function(byColumn, columns, method) {
groupBy : function(byColumn, columns, options) {

options = options || {};

// TODO: should we check type match here?
// default method is addition
method = method || function(array) {
return _.reduce(array, function(memo, num){
return memo + num;
}, 0);
};
var method = options.method || _.sum;

var d = {
_columns : []
};

if (options && options.preprocess) {
this.preprocess = options.preprocess;
}

var parser = new DS.Parsers();

// copy columns we want - just types and names. No data.
Expand All @@ -53,7 +60,12 @@

// bin all values by their categories
for(var i = 0; i < this.length; i++) {
var category = this._columns[this._columnPositionByName[byColumn]].data[i];
var category = null;
if (this.preprocess) {
category = this.preprocess(this._columns[this._columnPositionByName[byColumn]].data[i]);
} else {
category = this._columns[this._columnPositionByName[byColumn]].data[i];
}

if (_.isUndefined(categoryPositions[category])) {

Expand Down
4 changes: 2 additions & 2 deletions test/index.html
Expand Up @@ -42,9 +42,9 @@
<script src="unit/products.js"></script>
<script src="unit/views.js"></script>
<script src="unit/events.js"></script>

<script src="unit/importers.js"></script>
<script src="unit/derived.js"></script>
<script src="unit/importers.js"></script>


</head>
<body>
Expand Down
41 changes: 36 additions & 5 deletions test/unit/derived.js
Expand Up @@ -65,14 +65,45 @@ test("base group by with diff modifier", function() {
});

var groupedData = ds.groupBy("state",
["count", "anothercount"],
function(array) {
return _.reduce(array, function(memo, num){
return memo * num;
}, 1);
["count", "anothercount"], {
method : function(array) {
return _.reduce(array, function(memo, num){
return memo * num;
}, 1);
}
});

ok(_.isEqual(groupedData._columns[1].data, ["AZ", "MA"]), "states correct");
ok(_.isEqual(groupedData._columns[2].data, [6,120]), "counts correct");
ok(_.isEqual(groupedData._columns[3].data, [6000,120000]), "anothercounts correct" + groupedData._columns[3].data);
});

test("group by with preprocessing of categoeies", function() {
var ds = new DS.Dataset({
data : getData(),
strict: true
});

var groupedData = ds.groupBy("state", ["count", "anothercount"], {
preprocess : function(state) {
return state + state;
}
});

ok(_.isEqual(groupedData._columns[1].data, ["AZAZ", "MAMA"]), "states correct");
ok(_.isEqual(groupedData._columns[2].data, [6,15]), "counts correct");
ok(_.isEqual(groupedData._columns[3].data, [60,150]), "anothercounts correct" + groupedData._columns[3].data);

groupedData = ds.groupBy("state", ["count", "anothercount"], {
preprocess : function(state) {
return "A";
}
});

ok(_.isEqual(groupedData._columns[1].data, ["A"]), "states correct");
ok(_.isEqual(groupedData._columns[1].data.length, 1), "states correct");
ok(_.isEqual(groupedData._columns[2].data, [21]), "count correct");
ok(_.isEqual(groupedData._columns[3].data, [210]), "anothercounts correct" + groupedData._columns[3].data);


});

0 comments on commit cce2e57

Please sign in to comment.