Skip to content

Instantly share code, notes, and snippets.

@adoc
Created July 1, 2015 00:13
Show Gist options
  • Save adoc/b63f40656b6c3ff1cca5 to your computer and use it in GitHub Desktop.
Save adoc/b63f40656b6c3ff1cca5 to your computer and use it in GitHub Desktop.
/* Collate at the given dimension namespace.
:param Object rawData: The raw data from the RESTful api.
:param Array dimensions: Array of string namespaces that define the
nesting and collation order.
:param String collateAt: Namespace to stop "digging" and begin to
collate the data.
*/
function collateData(rawData, dimensions, collateAt, __forwardData,
__returnData) {
var dimension,
di,
index;
if (!rawData || !dimensions) {
throw "Check out function params and docs!! thx";
}
__forwardData = __forwardData || rawData['dataset'];
__returnData = __returnData || {};
// Make copy of the dimensions array. (Acquire new ref)
dimensions = dimensions.slice(0);
// Remove first element from the array.
dimension = dimensions.shift();
// Iterate all dimentional indices.
for (di in rawData[dimension]) {
index = rawData[dimension][di];
if (dimensions.length > 0 && dimension !== collateAt) {
// Dig deaper...
collateData(rawData, dimensions, collateAt,
__forwardData[index], __returnData);
} else {
// We are at the specified depth to begin to collate.
// (We still need to dig, but slightly different)
if (dimensions.length > 0) {
// Dig but now we're building our return data by
// establishing namespaces.
__returnData[index] = collateData(rawData, dimensions,
collateAt,
__forwardData[index],
__returnData[index]);
} else {
// We are at our scalar, on first iteration we assign
// the array. Then simply push our forward data.
if (!__returnData[index])
__returnData[index] = [];
__returnData[index].push(__forwardData[index]);
}
}
}
return __returnData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment