From 0583c4fa25a1e04674b8e619b31d54c0a6a5d8fa Mon Sep 17 00:00:00 2001 From: Valentin Valls Date: Wed, 24 Aug 2022 22:26:39 +0200 Subject: [PATCH] Added 'ms' duration --- cypress/integration/rendering/gantt.spec.js | 18 ++++++++++++++++++ src/diagrams/gantt/ganttDb.js | 5 ++++- src/diagrams/gantt/ganttDb.spec.js | 21 +++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/cypress/integration/rendering/gantt.spec.js b/cypress/integration/rendering/gantt.spec.js index a94132942..7dc7c6cf5 100644 --- a/cypress/integration/rendering/gantt.spec.js +++ b/cypress/integration/rendering/gantt.spec.js @@ -163,6 +163,24 @@ describe('Gantt diagram', () => { ); }); + it('should handle milliseconds', () => { + imgSnapshotTest( + ` + gantt + title A Gantt Diagram + dateFormat x + axisFormat %L + section Section + A task :a1, 0, 30ms + Another task :after a1, 20ms + section Another + Another another task :b1, 20, 12ms + Another another another task :after b1, 24ms + `, + {} + ); + }); + it('should render a gantt diagram when useMaxWidth is true (default)', () => { renderGraph( ` diff --git a/src/diagrams/gantt/ganttDb.js b/src/diagrams/gantt/ganttDb.js index 6014cd9bb..b69d46518 100644 --- a/src/diagrams/gantt/ganttDb.js +++ b/src/diagrams/gantt/ganttDb.js @@ -233,6 +233,9 @@ const getStartDate = function (prevTime, dateFormat, str) { const durationToDate = function (durationStatement, relativeTime) { if (durationStatement !== null) { switch (durationStatement[2]) { + case 'ms': + relativeTime.add(durationStatement[1], 'milliseconds'); + break; case 's': relativeTime.add(durationStatement[1], 'seconds'); break; @@ -267,7 +270,7 @@ const getEndDate = function (prevTime, dateFormat, str, inclusive) { return mDate.toDate(); } - return durationToDate(/^([\d]+)([wdhms])/.exec(str.trim()), moment(prevTime)); + return durationToDate(/^([\d]+)([wdhms]|ms)$/.exec(str.trim()), moment(prevTime)); }; let taskCnt = 0; diff --git a/src/diagrams/gantt/ganttDb.spec.js b/src/diagrams/gantt/ganttDb.spec.js index 406a4d070..d07aee4bf 100644 --- a/src/diagrams/gantt/ganttDb.spec.js +++ b/src/diagrams/gantt/ganttDb.spec.js @@ -99,6 +99,27 @@ describe('when using the ganttDb', function () { } ); + it('should handle milliseconds', function () { + ganttDb.setDateFormat('x'); + ganttDb.addSection('testa1'); + ganttDb.addTask('test1', 'id1,0,20ms'); + ganttDb.addTask('test2', 'id2,after id1,5ms'); + ganttDb.addSection('testa2'); + ganttDb.addTask('test3', 'id3,20,10ms'); + ganttDb.addTask('test4', 'id4,after id3,5ms'); + + const tasks = ganttDb.getTasks(); + + expect(tasks[0].startTime.toISOString()).toEqual('1970-01-01T00:00:00.000Z'); + expect(tasks[0].endTime.toISOString()).toEqual('1970-01-01T00:00:00.020Z'); + expect(tasks[1].startTime.toISOString()).toEqual('1970-01-01T00:00:00.020Z'); + expect(tasks[1].endTime.toISOString()).toEqual('1970-01-01T00:00:00.025Z'); + expect(tasks[2].startTime.toISOString()).toEqual('1970-01-01T00:00:00.020Z'); + expect(tasks[2].endTime.toISOString()).toEqual('1970-01-01T00:00:00.030Z'); + expect(tasks[3].startTime.toISOString()).toEqual('1970-01-01T00:00:00.030Z'); + expect(tasks[3].endTime.toISOString()).toEqual('1970-01-01T00:00:00.035Z'); + }); + it('should handle relative start date based on id regardless of sections', function () { ganttDb.setDateFormat('YYYY-MM-DD'); ganttDb.addSection('testa1');