diff --git a/lib/index.js b/lib/index.js index 5baf206..e7305bf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -161,6 +161,8 @@ GitStats.prototype.record = 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; + } else if (Typpy(data.date, Date)) { + data.date = Moment(data.date); } if (typeof data.hash !== "string" || !data.hash) { @@ -203,6 +205,77 @@ GitStats.prototype.record = function (data, callback) { return self; }; +/** + * removeCommit + * Deletes a specifc commit from the history. + * + * @name record + * @function + * @param {Object} data The commit data containing: + * + * - `date` (String|Date): The date object or a string in a format that can be parsed. + * - `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. + * + * @param {Function} callback The callback function. + * @return {GitStats} The `GitStats` instance. + */ +GitStats.prototype.removeCommit = function (data, callback) { + + var self = this; + + // Validate data + callback = callback || function (err) { if (err) throw err; }; + data = Object(data); + + if (typeof data.date === "string") { + data.date = new Moment(new Date(data.date)); + } + + if (!/^moment|date$/.test(Typpy(data.date))) { + callback(new Error("The date field should be a string or a date object.")); + return GitStats; + } else if (Typpy(data.date, Date)) { + data.date = Moment(data.date); + } + + if (typeof data.hash !== "string" || !data.hash) { + callback(new Error("Invalid hash.")); + return GitStats; + } + + function modify (err, stats) { + + if (err) { return callback(err); } + + 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); + } else { + self.save(stats, callback); + } + + return stats; + } + + // Check if we have input data + if (data._data) { + return modify(null, data._data); + } else { + // Get stats + self.get(modify); + } + + return self; +}; + /** * get * Gets the git stats.