Last active
August 29, 2015 14:00
-
-
Save ryanpitts/07d0bd506aedf88fe6b8 to your computer and use it in GitHub Desktop.
census column name prefixer
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
// A user may need to view a column name in isolation, with no context | |
// as to its level of indent. Prefixed column names allow this: | |
// | |
// Female | |
// Car, truck or van | |
// Carpooled | |
// In 2-person carpool | |
// | |
// to be represented as: | |
// | |
// Female: Car, truck or van: Carpooled: In 2-person carpool | |
comparison.prefixColumnNames = function(columns) { | |
// store prefixPieces as an object with keys/values, because not all | |
// tables apply indents in a standard, orderly or predictable fashion. | |
// because some tables have a first non-total column with indent > 1, | |
// we need to seed this with empty slots for later concatenation. | |
var prefixPieces = {'0':'', '1':'', '2':''}, | |
prefixName, | |
indentAdd; | |
_.each(columns, function(v) { | |
// strip occasional end chars to prep names for concatenation | |
prefixName = (v.name.slice(-1) == ':') ? v.name.slice(0, -1) : v.name; | |
prefixName = (prefixName.slice(-3) == ' --') ? prefixName.slice(0, -3) : prefixName; | |
// add name piece to proper slot, | |
// allowing for weird subhead columns with null indents | |
prefixPieces[v.indent || 0] = prefixName; | |
// compile a prefixed name that makes sense regardless of context | |
v.prefixed_name = _.values(prefixPieces).slice(0, v.indent+1).filter(function(n){return n}).join(': '); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment