From f7b8d1ac072c7dd6682656c5235cf967f16003bb Mon Sep 17 00:00:00 2001 From: Valentin Valls Date: Wed, 24 Aug 2022 23:13:35 +0200 Subject: [PATCH] Create a more consistent 'parseDuration' - Remove 'durationToDate' which was not a usable function --- src/diagrams/gantt/ganttDb.js | 15 +++++++++++---- src/diagrams/gantt/ganttDb.spec.js | 10 +++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/diagrams/gantt/ganttDb.js b/src/diagrams/gantt/ganttDb.js index b69d46518..e8c75a982 100644 --- a/src/diagrams/gantt/ganttDb.js +++ b/src/diagrams/gantt/ganttDb.js @@ -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, }; diff --git a/src/diagrams/gantt/ganttDb.spec.js b/src/diagrams/gantt/ganttDb.spec.js index d07aee4bf..1d2b3724f 100644 --- a/src/diagrams/gantt/ganttDb.spec.js +++ b/src/diagrams/gantt/ganttDb.spec.js @@ -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();