Created
July 29, 2013 15:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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