Merge pull request #3360 from vallsv/feature-ms-duration

Feature decimal duration in second for gantt diagram
This commit is contained in:
Knut Sveidqvist
2022-09-01 16:23:42 +02:00
committed by GitHub
3 changed files with 42 additions and 34 deletions

View File

@@ -175,7 +175,7 @@ describe('Gantt diagram', () => {
Another task :after a1, 20ms Another task :after a1, 20ms
section Another section Another
Another another task :b1, 20, 12ms Another another task :b1, 20, 12ms
Another another another task :after b1, 24ms Another another another task :after b1, 0.024s
`, `,
{} {}
); );

View File

@@ -230,31 +230,31 @@ const getStartDate = function (prevTime, dateFormat, str) {
return new Date(); return new Date();
}; };
const durationToDate = function (durationStatement, relativeTime) { /**
if (durationStatement !== null) { * Parse a string as a moment duration.
switch (durationStatement[2]) { *
case 'ms': * The string have to be compound by a value and a shorthand duration unit. For example `5d`
relativeTime.add(durationStatement[1], 'milliseconds'); * representes 5 days.
break; *
case 's': * Shorthand unit supported are:
relativeTime.add(durationStatement[1], 'seconds'); *
break; * - `y` for years
case 'm': * - `M` for months
relativeTime.add(durationStatement[1], 'minutes'); * - `w` for weeks
break; * - `d` for days
case 'h': * - `h` for hours
relativeTime.add(durationStatement[1], 'hours'); * - `s` for seconds
break; * - `ms` for milliseconds
case 'd': *
relativeTime.add(durationStatement[1], 'days'); * @param {string} str - A string representing the duration.
break; * @returns {moment.Duration} A moment duration, including an invalid moment for invalid input string.
case 'w': */
relativeTime.add(durationStatement[1], 'weeks'); const parseDuration = function (str) {
break; const statement = /^(\d+(?:\.\d+)?)([yMwdhms]|ms)$/.exec(str.trim());
} if (statement !== null) {
return moment.duration(Number.parseFloat(statement[1]), statement[2]);
} }
// Default date - now return moment.duration.invalid();
return relativeTime.toDate();
}; };
const getEndDate = function (prevTime, dateFormat, str, inclusive) { const getEndDate = function (prevTime, dateFormat, str, inclusive) {
@@ -270,7 +270,12 @@ const getEndDate = function (prevTime, dateFormat, str, inclusive) {
return mDate.toDate(); return mDate.toDate();
} }
return durationToDate(/^([\d]+)([wdhms]|ms)$/.exec(str.trim()), moment(prevTime)); const endTime = moment(prevTime);
const duration = parseDuration(str);
if (duration.isValid()) {
endTime.add(duration);
}
return endTime.toDate();
}; };
let taskCnt = 0; let taskCnt = 0;
@@ -666,7 +671,7 @@ export default {
setLink, setLink,
getLinks, getLinks,
bindFunctions, bindFunctions,
durationToDate, parseDuration,
isInvalidDate, isInvalidDate,
}; };

View File

@@ -6,13 +6,16 @@ describe('when using the ganttDb', function () {
ganttDb.clear(); ganttDb.clear();
}); });
describe('when using relative times', function () { describe('when using duration', function () {
it.each` it.each`
diff | date | expected str | expected
${' 1d'} | ${moment('2019-01-01')} | ${moment('2019-01-02').toDate()} ${'1d'} | ${moment.duration(1, 'd')}
${' 1w'} | ${moment('2019-01-01')} | ${moment('2019-01-08').toDate()} ${'2w'} | ${moment.duration(2, 'w')}
`('should add $diff to $date resulting in $expected', ({ diff, date, expected }) => { ${'1ms'} | ${moment.duration(1, 'ms')}
expect(ganttDb.durationToDate(diff, date)).toEqual(expected); ${'0.1s'} | ${moment.duration(100, 'ms')}
${'1f'} | ${moment.duration.invalid()}
`('should $str resulting in $expected duration', ({ str, expected }) => {
expect(ganttDb.parseDuration(str)).toEqual(expected);
}); });
}); });
@@ -106,7 +109,7 @@ describe('when using the ganttDb', function () {
ganttDb.addTask('test2', 'id2,after id1,5ms'); ganttDb.addTask('test2', 'id2,after id1,5ms');
ganttDb.addSection('testa2'); ganttDb.addSection('testa2');
ganttDb.addTask('test3', 'id3,20,10ms'); 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(); const tasks = ganttDb.getTasks();