Permalink
Browse files

Categories support weight

Instead of creating an array of strings for categories, wheat builds an array of objects
Each category object has 2 fields:
tag - containst the label to be displayed
weight - is a number of times category was used in all posts

That allows the renderer to display a cloud tag where more frequent categories
are bigger than less popular ones.
  • Loading branch information...
1 parent 7809b59 commit d43b4f087d4e976a698c114f0c337a35616493d6 @pirxpilot committed Nov 23, 2011
Showing with 18 additions and 6 deletions.
  1. +18 −6 lib/wheat/data.js
View
@@ -317,17 +317,29 @@ var Data = module.exports = {
},
function processCategories(err, articles) {
if (err) { callback(err); return; }
- var categories = articles.reduce(function (start, element) {
+ var cats = {}, categories = [];
+ articles.forEach(function (element) {
if (element && element.categories) {
element.categories.forEach(function(category) {
- if(start.indexOf(category) == -1) {
- start = start.concat(category);
+ if (cats[category] !== undefined) {
+ cats[category] += 1;
+ } else {
+ cats[category] = 1;
}
});
}
- return start;
- }, []);
- return categories;
+ });
+ for (c in cats) {
+ if(cats.hasOwnProperty(c) && typeof c === 'string') {
+ categories.push({
+ tag: c,
+ weight: cats[c]
+ });
+ }
+ }
+ return categories.sort(function(a, b) {
+ return a.tag.localeCompare(b.tag);
+ });
},
callback
)

0 comments on commit d43b4f0

Please sign in to comment.