Rework 'parseDuration' as a pure duration parsing

This commit is contained in:
Valentin Valls 2022-08-25 23:15:16 +02:00
parent 3315ae8382
commit ba4f7d2ceb
2 changed files with 36 additions and 37 deletions

View File

@ -231,37 +231,30 @@ const getStartDate = function (prevTime, dateFormat, str) {
}; };
/** /**
* Parse a duration as an absolute date. * Parse a string as a moment duration.
* *
* @param durationStr A string representing a duration * The string have to be compound by a value and a shorthand duration unit. For example `5d`
* @param relativeTime The moment when this duration starts * representes 5 days.
* @returns The date of the end of the duration. *
* 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 (durationStr, relativeTime) { const parseDuration = function (str) {
const durationStatement = /^(\d+(?:\.\d+)?)([wdhms]|ms)$/.exec(durationStr.trim()); const statement = /^(\d+(?:\.\d+)?)([yMwdhms]|ms)$/.exec(str.trim());
if (durationStatement !== null) { if (statement !== null) {
switch (durationStatement[2]) { return moment.duration(Number.parseFloat(statement[1]), statement[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;
}
} }
return relativeTime.toDate(); return moment.duration.invalid();
}; };
const getEndDate = function (prevTime, dateFormat, str, inclusive) { const getEndDate = function (prevTime, dateFormat, str, inclusive) {
@ -277,7 +270,12 @@ const getEndDate = function (prevTime, dateFormat, str, inclusive) {
return mDate.toDate(); return mDate.toDate();
} }
return parseDuration(str, 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;

View File

@ -6,15 +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.utc('2019-01-01')} | ${'2019-01-02T00:00:00.000Z'} ${'1d'} | ${moment.duration(1, 'd')}
${'1w'} | ${moment.utc('2019-01-01')} | ${'2019-01-08T00:00:00.000Z'} ${'2w'} | ${moment.duration(2, 'w')}
${'1ms'} | ${moment.utc('2019-01-01')} | ${'2019-01-01T00:00:00.001Z'} ${'1ms'} | ${moment.duration(1, 'ms')}
${'0.1s'} | ${moment.utc('2019-01-01')} | ${'2019-01-01T00:00:00.100Z'} ${'0.1s'} | ${moment.duration(100, 'ms')}
`('should add $diff to $date resulting in $expected', ({ diff, date, expected }) => { ${'1f'} | ${moment.duration.invalid()}
expect(ganttDb.parseDuration(diff, date).toISOString()).toEqual(expected); `('should $str resulting in $expected duration', ({ str, expected }) => {
expect(ganttDb.parseDuration(str)).toEqual(expected);
}); });
}); });