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
commit ac76fb73a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 34 deletions

View File

@ -175,7 +175,7 @@ describe('Gantt diagram', () => {
Another task :after a1, 20ms
section Another
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();
};
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;
case 'm':
relativeTime.add(durationStatement[1], 'minutes');
break;
case 'h':
relativeTime.add(durationStatement[1], 'hours');
break;
case 'd':
relativeTime.add(durationStatement[1], 'days');
break;
case 'w':
relativeTime.add(durationStatement[1], 'weeks');
break;
}
/**
* Parse a string as a moment duration.
*
* The string have to be compound by a value and a shorthand duration unit. For example `5d`
* representes 5 days.
*
* Shorthand unit supported are:
*
* - `y` for years
* - `M` for months
* - `w` for weeks
* - `d` for days
* - `h` for hours
* - `s` for seconds
* - `ms` for milliseconds
*
* @param {string} str - A string representing the duration.
* @returns {moment.Duration} A moment duration, including an invalid moment for invalid input string.
*/
const parseDuration = function (str) {
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 relativeTime.toDate();
return moment.duration.invalid();
};
const getEndDate = function (prevTime, dateFormat, str, inclusive) {
@ -270,7 +270,12 @@ const getEndDate = function (prevTime, dateFormat, str, inclusive) {
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;
@ -666,7 +671,7 @@ export default {
setLink,
getLinks,
bindFunctions,
durationToDate,
parseDuration,
isInvalidDate,
};

View File

@ -6,13 +6,16 @@ describe('when using the ganttDb', function () {
ganttDb.clear();
});
describe('when using relative times', function () {
describe('when using duration', 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()}
`('should add $diff to $date resulting in $expected', ({ diff, date, expected }) => {
expect(ganttDb.durationToDate(diff, date)).toEqual(expected);
str | expected
${'1d'} | ${moment.duration(1, 'd')}
${'2w'} | ${moment.duration(2, 'w')}
${'1ms'} | ${moment.duration(1, 'ms')}
${'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.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();