Unverified Commit 8ceee98e authored by jdckr's avatar jdckr Committed by GitHub
Browse files

added support for relative due and threshold date (#337)



* added support for relative due and threshold date

* added new implementation for testing

* use formatDate from helper.mjs

* cleanup

* fixed review finding

Co-authored-by: default avatarransome <11188741+ransome1@users.noreply.github.com>
parent 3cbbb37e
"use strict";
import "../../node_modules/jstodotxt/jsTodoExtensions.js";
import { convertDate } from "./date.mjs";
import { addIntervalToDate } from "./recurrences.mjs";
function RecExtension() {
this.name = "rec";
......@@ -22,21 +24,32 @@ function SugarDueExtension() {
SugarDueExtension.prototype = new TodoTxtExtension();
SugarDueExtension.prototype.parsingFunction = function (line) {
var dueDate = null;
var indexDueKeyword = line.indexOf("due:");
var relativeDateRegEx = /due:(\d+[dwm])/;
var relativeDatMatch = relativeDateRegEx.exec(line)
// Find keyword due
if (indexDueKeyword >= 0) {
var stringAfterDue = line.substr(indexDueKeyword + 4)
var words = stringAfterDue.split(" ");
var match = null;
if ( relativeDatMatch !== null) {
var dueDate = resolveRelativeDate(relativeDatMatch[1]);
// Try to parse a valid date until the end of the text
for (var i = Math.max(5, words.length); i > 0; i--) {
match = words.slice(0, i).join(" ");
dueDate = Sugar.Date.create(match, {future: true});
if (Sugar.Date.isValid(dueDate)) {
return [dueDate, line.replace("due:" + match, ''), Sugar.Date.format(dueDate, '%Y-%m-%d')];
return [dueDate, line.replace(relativeDateRegEx, ''), convertDate(dueDate)];
}
else
{
var dueDate = null;
var indexDueKeyword = line.indexOf("due:");
// Find keyword due
if (indexDueKeyword >= 0) {
var stringAfterDue = line.substr(indexDueKeyword + 4)
var words = stringAfterDue.split(" ");
var match = null;
// Try to parse a valid date until the end of the text
for (var i = Math.max(5, words.length); i > 0; i--) {
match = words.slice(0, i).join(" ");
dueDate = Sugar.Date.create(match, {future: true});
if (Sugar.Date.isValid(dueDate)) {
return [dueDate, line.replace("due:" + match, ''), Sugar.Date.format(dueDate, '%Y-%m-%d')];
}
}
}
}
......@@ -66,15 +79,34 @@ function ThresholdExtension() {
}
ThresholdExtension.prototype = new TodoTxtExtension();
ThresholdExtension.prototype.parsingFunction = function (line) {
var thresholdDate = null;
var thresholdRegex = /t:([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\s*/;
var matchThreshold = thresholdRegex.exec(line);
if ( matchThreshold !== null ) {
var datePieces = matchThreshold[1].split('-');
thresholdDate = new Date( datePieces[0], datePieces[1] - 1, datePieces[2] );
return [thresholdDate, line.replace(thresholdRegex, ''), matchThreshold[1]];
var relativeDateRegEx = /t:(\d+[dwm])/;
var relativeDatMatch = relativeDateRegEx.exec(line)
if ( relativeDatMatch !== null) {
var thresholdDate = resolveRelativeDate(relativeDatMatch[1]);
return [thresholdDate, line.replace(relativeDateRegEx, ''), convertDate(thresholdDate)];
}
else {
var thresholdDate = null;
var thresholdRegex = /t:([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\s*/;
var matchThreshold = thresholdRegex.exec(line);
if ( matchThreshold !== null ) {
var datePieces = matchThreshold[1].split('-');
thresholdDate = new Date( datePieces[0], datePieces[1] - 1, datePieces[2] );
return [thresholdDate, line.replace(thresholdRegex, ''), matchThreshold[1]];
}
}
return [null, null, null];
};
export { RecExtension, SugarDueExtension, ThresholdExtension, PriExtension };
function resolveRelativeDate(relativeDate) {
var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
var unit = relativeDate.slice(-1);
var increment = parseInt(relativeDate.slice(0, -1));
return addIntervalToDate(today, increment, unit);
}
export { RecExtension, SugarDueExtension, ThresholdExtension, resolveRelativeDate, PriExtension };
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment