Chore: parse parseDuration values as numbers (#42826)
* Chore: parseDuration values as numbers * Clean up RefreshPicker labels
This commit is contained in:
@@ -18,12 +18,12 @@ describe('Duration util', () => {
|
||||
describe('parseDuration', () => {
|
||||
it('parses a duration string', () => {
|
||||
const durationString = '3M 5d 20m';
|
||||
expect(parseDuration(durationString)).toEqual({ months: '3', days: '5', minutes: '20' });
|
||||
expect(parseDuration(durationString)).toEqual({ months: 3, days: 5, minutes: 20 });
|
||||
});
|
||||
|
||||
it('strips out non valid durations', () => {
|
||||
const durationString = '3M 6v 5b 4m';
|
||||
expect(parseDuration(durationString)).toEqual({ months: '3', minutes: '4' });
|
||||
expect(parseDuration(durationString)).toEqual({ months: 3, minutes: 4 });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -35,19 +35,25 @@ export function intervalToAbbreviatedDurationString(interval: Interval, includeS
|
||||
/**
|
||||
* parseDuration parses duration string into datefns Duration object
|
||||
*
|
||||
* @param duration - string to convert. For example '2m', '5h 20s'
|
||||
* @param durationString - string to convert. For example '2m', '5h 20s'
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export function parseDuration(duration: string): Duration {
|
||||
return duration.split(' ').reduce<Duration>((acc, value) => {
|
||||
export function parseDuration(durationString: string): Duration {
|
||||
return durationString.split(' ').reduce<Duration>((acc, value) => {
|
||||
const match = value.match(/(\d+)(.+)/);
|
||||
if (match === null || match.length !== 3) {
|
||||
|
||||
const rawLength = match?.[1];
|
||||
const unit = match?.[2];
|
||||
|
||||
if (!(rawLength && unit)) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
const key = Object.entries(durationMap).find(([_, abbreviations]) => abbreviations?.includes(match[2]))?.[0];
|
||||
return !key ? acc : { ...acc, [key]: match[1] };
|
||||
const mapping = Object.entries(durationMap).find(([_, abbreviations]) => abbreviations?.includes(match[2]));
|
||||
const length = parseInt(rawLength, 10);
|
||||
|
||||
return mapping ? { ...acc, [mapping[0]]: length } : acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user