Skip to content

Commit

Permalink
Add template code for inclusion of relative dates
Browse files Browse the repository at this point in the history
Setup the basic stuff for #4, once the module can be used, it can be
immediately re-used here.
  • Loading branch information
Siddharth Kannan committed Jun 12, 2016
1 parent 6c0b3ca commit 4edae77
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ exports.MSG_ERR_CREDIT_DEBIT_TRANS = "Input is not enough or could not be parsed
exports.MSG_ERR_STASH_TRANS = 'Input is not enough or could not be parsed\nCommand should look like: wallet stash 500';
exports.MSG_INVALID_OPTION = 'Invalid option. Run wallet --help to learn about available options';
exports.MSG_INVALID_DATE = 'Date should be formatted as yyyy-mm-dd. Support for other formats coming soon.';

// TODO: This will be replaced by the module relative-date-reverse
// the module will take a relative date string as input and return a list,
// the first element will be true if the string could be parsed into a date,
// and in this case, the second element will be the Date object
exports.parseRelativeDate = function (relativeDateString) {
return [false, ''];
};
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
module.exports = function (input, opts) {
var Consts = require('./consts.js');
var parseRelativeDate = Consts.parseRelativeDate;

var expenseObject = {
reason: input[2],
Expand All @@ -17,7 +18,12 @@ module.exports = function (input, opts) {
var dateRe = /(\d{4})-(\d{2})-(\d{2})/;
var matchObject = expenseObject.date.match(dateRe);
if (!matchObject) {
throw new Error(Consts.MSG_INVALID_DATE);
var relativeDate = parseRelativeDate(expenseObject.date);
if (relativeDate[0]) {
expenseObject.date = require('date-format').asString('yyyy-MM-dd', relativeDate[1]);
} else {
throw new Error(Consts.MSG_INVALID_DATE);
}
}
} else {
expenseObject.date = require('date-format').asString('yyyy-MM-dd', new Date());
Expand Down

0 comments on commit 4edae77

Please sign in to comment.