Create a more consistent 'parseDuration'

- Remove 'durationToDate' which was not a usable function
This commit is contained in:
Valentin Valls 2022-08-24 23:13:35 +02:00
parent cde3a7cf70
commit f7b8d1ac07
2 changed files with 16 additions and 9 deletions

View File

@ -230,7 +230,15 @@ const getStartDate = function (prevTime, dateFormat, str) {
return new Date();
};
const durationToDate = function (durationStatement, relativeTime) {
/**
* Parse a duration as an absolute date.
*
* @param durationStr A string representing a duration
* @param relativeTime The moment when this duration starts
* @returns The date of the end of the duration.
*/
const parseDuration = function (durationStr, relativeTime) {
const durationStatement = /^([\d]+)([wdhms]|ms)$/.exec(durationStr.trim());
if (durationStatement !== null) {
switch (durationStatement[2]) {
case 'ms':
@ -253,7 +261,6 @@ const durationToDate = function (durationStatement, relativeTime) {
break;
}
}
// Default date - now
return relativeTime.toDate();
};
@ -270,7 +277,7 @@ const getEndDate = function (prevTime, dateFormat, str, inclusive) {
return mDate.toDate();
}
return durationToDate(/^([\d]+)([wdhms]|ms)$/.exec(str.trim()), moment(prevTime));
return parseDuration(str, moment(prevTime));
};
let taskCnt = 0;
@ -666,7 +673,7 @@ export default {
setLink,
getLinks,
bindFunctions,
durationToDate,
parseDuration,
isInvalidDate,
};

View File

@ -8,11 +8,11 @@ describe('when using the ganttDb', function () {
describe('when using relative times', function () {
it.each`
diff | date | expected
${' 1d'} | ${moment('2019-01-01')} | ${moment('2019-01-02').toDate()}
${' 1w'} | ${moment('2019-01-01')} | ${moment('2019-01-08').toDate()}
diff | date | expected
${'1d'} | ${moment('2019-01-01')} | ${moment('2019-01-02').toDate()}
${'1w'} | ${moment('2019-01-01')} | ${moment('2019-01-08').toDate()}
`('should add $diff to $date resulting in $expected', ({ diff, date, expected }) => {
expect(ganttDb.durationToDate(diff, date)).toEqual(expected);
expect(ganttDb.parseDuration(diff, date)).toEqual(expected);
});
});
@ -106,7 +106,7 @@ describe('when using the ganttDb', function () {
ganttDb.addTask('test2', 'id2,after id1,5ms');
ganttDb.addSection('testa2');
ganttDb.addTask('test3', 'id3,20,10ms');
ganttDb.addTask('test4', 'id4,after id3,5ms');
ganttDb.addTask('test4', 'id4,after id3,0.005s');
const tasks = ganttDb.getTasks();