From 9c211d67afc073591878a89cc9aa533ca72cf52c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionic=C4=83=20Biz=C4=83u?= Date: Sun, 11 Oct 2015 12:28:15 +0300 Subject: [PATCH] Improved the removeCommit function The date field is not mandatory anymore. --- lib/index.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index 86d810a..15bca0b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -14,6 +14,7 @@ var Ul = require("ul") , Typpy = require("typpy") , Exec = ChildProcess.exec , Spawn = ChildProcess.spawn + , IterateObject = require("iterate-object") ; // Constants @@ -209,7 +210,7 @@ GitStats.prototype.record = function (data, callback) { * @function * @param {Object} data The commit data containing: * - * - `date` (String|Date): The date object or a string in a format that can be parsed. + * - `date` (String|Date): The date object or a string in a format that can be parsed. If not provided, the hash object will be searched in all dates. * - `hash` (String): The commit hash. * - `_data` (Object): If this field is provided, it should be the content of the git-stats data file as object. It will be modified in-memory and then returned. * - `save` (Boolean): If `false`, the result will *not* be saved in the file. @@ -230,8 +231,7 @@ GitStats.prototype.removeCommit = function (data, callback) { } if (!/^moment|date$/.test(Typpy(data.date))) { - callback(new Error("The date field should be a string or a date object.")); - return GitStats; + data.date = null; } else if (Typpy(data.date, Date)) { data.date = Moment(data.date); } @@ -244,13 +244,19 @@ GitStats.prototype.removeCommit = function (data, callback) { function modify (err, stats) { if (err) { return callback(err); } + if (!data.date) { + debugger + IterateObject(stats.commits, function (todayObj) { + delete todayObj[data.hash]; + }); + } else { + var commits = stats.commits + , day = data.date.format(DATE_FORMAT) + , today = commits[day] = Object(commits[day]) + ; - var commits = stats.commits - , day = data.date.format(DATE_FORMAT) - , today = commits[day] = Object(commits[day]) - ; - - delete today[data.hash]; + delete today[data.hash]; + } if (data.save === false) { callback(null, stats);