Last active
September 7, 2016 01:54
-
-
Save eob/6246108f8b501bb00de27853ea3a271a to your computer and use it in GitHub Desktop.
This file contains 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
module.exports.beforeRender = function(elem, data) { | |
console.log("Hi there, before render!"); | |
data.Grouped = groupBy( | |
data.Sheet1, | |
function(item) { | |
return item.Course; | |
}, | |
function(group) { | |
return {}; | |
}, | |
function(a, b) { | |
if (a.Name < b.Name) { | |
return -1; | |
} else if (b.Name < a.Name) { | |
return 1; | |
} else { | |
return 0; | |
} | |
}, | |
function(a, b) { | |
if (a.Special < b.Special) { | |
return -1; | |
} else if (b.Special < a.Special) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
); | |
} |
This file contains 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
/* | |
* list: A list of Javascript objects | |
* groupNameFn: A function that takes an item from the list and returns the name of the group | |
* groupDetailsFn: A function that is provided the items of the group and returns an object of details about the gorup, to be added to the group element | |
* groupSortFn: A Javascript function that sorts the groups, adorned with the details, and containing the name: property | |
* itemSortFn: A Javascript function that sorts items in the group | |
* | |
* Returns: | |
* An ordered list of groups. Each group is an object wtih properties name and items. | |
* Items is the list of members in that group. | |
* | |
* [ | |
* ] | |
*/ | |
function groupBy(list, groupNameFn, groupDetailsFn, groupSortFn, itemSortFn) { | |
var groups = {}; | |
// Generate group names | |
for (var i = 0; i < list.length; i++) { | |
var groupName = groupNameFn(list[i]); | |
if (typeof groups[groupName] == 'undefined') { | |
groups[groupName] = { | |
name: groupName, | |
items: [list[i]] | |
} | |
} else { | |
groups[groupName].items.push(list[i]) | |
} | |
} | |
// Now form the result variable | |
var ret = []; | |
for (var name in groups) { | |
ret.push(groups[name]); | |
} | |
// Add details about each group | |
if (groupDetailsFn) { | |
for (var i = 0; i < ret.length; i++) { | |
var details = groupDetailsFn(ret[i]); | |
for (var key in details) { | |
ret[i][key] = details[key]; | |
} | |
} | |
} | |
// sort the groups | |
if (groupSortFn) { | |
ret.sort(groupSortFn); | |
} | |
// sort the items in each group | |
if (itemSortFn) { | |
for (var i = 0; i < ret.length; i++) { | |
ret[i].items.sort(itemSortFn) | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment