Skip to content

Instantly share code, notes, and snippets.

@ryanpitts
Last active August 29, 2015 14:00
Show Gist options
  • Save ryanpitts/07d0bd506aedf88fe6b8 to your computer and use it in GitHub Desktop.
Save ryanpitts/07d0bd506aedf88fe6b8 to your computer and use it in GitHub Desktop.
census column name prefixer
// 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