desirae.js/lib/aggregate-core.js

125 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-01-23 09:52:41 +00:00
/*jshint -W054 */
2020-11-09 03:01:26 +00:00
(function (exports) {
"use strict";
2015-01-23 09:52:41 +00:00
2020-11-09 03:01:26 +00:00
var path = exports.path || require("path"),
months,
cores = {};
2015-01-23 09:52:41 +00:00
months = {
2020-11-09 03:01:26 +00:00
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December",
2015-01-23 09:52:41 +00:00
};
function byDate(a, b) {
a.year = parseInt(a.year, 10) || 0;
b.year = parseInt(b.year, 10) || 0;
if (a.year > b.year) {
return -1;
} else if (a.year < b.year) {
return 1;
}
a.month = parseInt(a.month, 10) || 0;
b.month = parseInt(b.month, 10) || 0;
if (a.month > b.month) {
return -1;
} else if (a.month < b.month) {
return 1;
}
a.day = parseInt(a.day, 10) || 0;
b.day = parseInt(b.day, 10) || 0;
if (a.day > b.day) {
return -1;
} else if (a.day < b.day) {
return 1;
}
if (a.hour > b.hour) {
return -1;
} else if (a.hour < b.hour) {
return 1;
}
if (a.minute > b.minute) {
return -1;
} else if (a.minute < b.minute) {
return 1;
}
if (a.title.toLowerCase() <= b.title.toLowerCase()) {
return -1;
}
return 1;
}
function collate(entities, env) {
2020-11-09 03:01:26 +00:00
var yearsArr = [];
2015-01-23 09:52:41 +00:00
entities.forEach(function (f) {
2017-05-18 20:27:17 +00:00
var set;
var yindex = 3000 - f.year;
var mindex = 12 - f.month;
var monthName = months[parseInt(f.month, 10)];
2015-01-23 09:52:41 +00:00
f.url = path.join(env.base_path, f.permalink);
if (!yearsArr[yindex]) {
yearsArr[yindex] = { year: f.year, months: [] };
}
set = yearsArr[yindex];
if (!set.months[mindex]) {
2017-05-18 20:27:17 +00:00
set.months[mindex] = {
2020-11-09 03:01:26 +00:00
month_name: monthName,
month_number: mindex,
month: monthName,
pages: [],
2017-05-18 20:27:17 +00:00
};
2015-01-23 09:52:41 +00:00
}
set = set.months[mindex];
2017-05-18 20:27:17 +00:00
f.month_name = months[parseInt(f.month, 10)];
f.month_number = parseInt(f.month, 10);
2015-01-23 09:52:41 +00:00
set.pages.push(f);
});
yearsArr = yearsArr.filter(function (y) {
if (!y) {
return false;
}
y.months = y.months.filter(function (m) {
return m && m.pages.length;
});
if (!y.months.length) {
return false;
}
return true;
});
return { years: yearsArr };
}
cores.collate = function (desi, env) {
// TODO categories
// TODO tags
desi.content.collections.sort(byDate);
desi.collated = collate(desi.content.collections, env);
};
exports.DesiraeAggregateCore = cores.DesiraeAggregateCore = cores;
2020-11-09 03:01:26 +00:00
})(("undefined" !== typeof exports && exports) || window);