Improved the removeCommit function

The date field is not mandatory anymore.
This commit is contained in:
Ionică Bizău 2015-10-11 12:28:15 +03:00
parent e8b643a718
commit 9c211d67af

View file

@ -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])
;
delete today[data.hash];
}
if (data.save === false) {
callback(null, stats);