Skip to content

Instantly share code, notes, and snippets.

@garbados
Created July 29, 2013 15:26
Show Gist options
  • Save garbados/6105137 to your computer and use it in GitHub Desktop.
Save garbados/6105137 to your computer and use it in GitHub Desktop.
example of using commonjs to separate transformation logic from the map function itself
{
views: {
gender: {
map: function(doc){
var lib = require('/views/lib/transform')
, key_value = lib.gender(doc);
emit(key_value[0], key_value[1]);
}
},
lib: {
transform: {
exports.gender = function(doc){
// the default column is "gender"
var key = 'gender'
, value;
// for different datasets, we write different rules
if(doc.dataset_id === '...'){
// let's say this dataset uses "gender" as the column
// but uses 0 and 1 as values, where 1 is male and 0 is female
switch(doc[key]){
case 0:
value = 'M';
break;
case 1:
value = 'F';
break;
}
} else if(doc.dataset_id === '...'){
// this dataset uses "sex" as the column
value = doc.sex;
} else {
// if no special rules, just grab the value
value = doc[key];
}
return [key, value];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment