diff --git a/lib/index.js b/lib/index.js index 512afea..0fb341a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,7 +4,10 @@ var FsExtra = require("fs-extra") , Moment = require("moment") ; -const STORE_PATH = Ul.USER_DIR + "/.git-stats"; +// Constants +const STORE_PATH = Ul.USER_DIR + "/.git-stats" + , LEVELS = 5 + ; // Constructor var GitStats = module.exports = {}; @@ -65,30 +68,56 @@ GitStats.record = function (data, callback) { * * @name get * @function - * @param {Object} data The stats filter. **Not yet implemented**. * @param {Function} callback The callback function. * @return {undefined} */ -GitStats.get = function (data, callback) { - if (typeof data === "function") { - callback = data; - data = {}; - } +GitStats.get = function (callback) { FsExtra.readJSON(STORE_PATH, callback); }; +GitStats.iterateDays = function (data, callback) { + + if (typeof data === "function") { + callback = data; + data = undefined; + } + + // Merge the defaults + data = Ul.merge({ + end: Moment() + , start: Moment().subtract(1, "years") + , format: "MMM D, YYYY" + }, data); + + var start = data.start + , end = data.end + , cDay = null + ; + + while (!start.isAfter(end)) { + cDay = start.format(data.format); + callback(cDay); + start.add(1, "days") + } +}; + GitStats.graph = function (data, callback) { - GitStats.get(data, function (err, stats) { + + if (typeof data === "function") { + callback = data; + data = undefined; + } + + // Get commits + GitStats.get(function (err, stats) { if (err) { return callback(err); } - var year = {} - , end = Moment() - , start = Moment().subtract({ years: 1 }) - , cDay = null - , cDayObj = null + + var cDayObj = null + , year = {} ; - while (!(start > end)) { - cDay = start.format("MMM D, YYYY"); + // Iterate days + GitStats.iterateDays(data, function (cDay) { cDayObj = year[cDay] = { _: stats[cDay] || {} , c: 0 @@ -97,11 +126,51 @@ GitStats.graph = function (data, callback) { Object.keys(cDayObj._).forEach(function (c) { cDayObj.c += Object.keys(cDayObj._[c]).length; }); - - start.add(1, "days") - } + }); callback(null, year); - // ⬚ ▢ ▤ ▣ ⬛ + }); +}; + +GitStats.calendar = function (data, callback) { + GitStats.graph(data, function (err, graph) { + if (err) { return callback(err); } + var cal = { total: 0, days: {} } + , cDay = null + , days = Object.keys(graph) + , max = 0 + ; + + days.forEach(function (c) { + cDay = graph[c]; + cal.total += cDay.c; + if (cDay.c > max) { + max = cDay.c; + } + }); + + days.forEach(function (c) { + cDay = graph[c]; + cal.days[c] = { + c: cDay.c + , level: cDay.c === 0 ? 0 : LEVELS - Math.floor(max / (cDay.c + 2)) + }; + }); + + callback(null, cal); + }); +}; + +GitStats.ansiCalendar = function (data, callback) { + + if (typeof data === "function") { + callback = data; + data = undefined; + } + // ⬚ ▢ ▤ ▣ ⬛ + GitStats.calendar(data, function (err, cal) { + if (err) { return callback(err); } + + debugger }); };