From bbbd68808360bba3937c99dbb5e0b3e39aa555af Mon Sep 17 00:00:00 2001 From: Klaus Zanders Date: Mon, 9 Dec 2024 17:17:15 +0100 Subject: [PATCH] add functionality to chronic duration to parse 8:15 to 8hours 15minutes instead of 8minutes 15seconds --- frontend/src/app/shared/helpers/chronic_duration.js | 12 +++++++++--- .../src/app/shared/helpers/chronic_duration.spec.ts | 11 +++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/shared/helpers/chronic_duration.js b/frontend/src/app/shared/helpers/chronic_duration.js index 9689a5390db2..de4fac624ff2 100644 --- a/frontend/src/app/shared/helpers/chronic_duration.js +++ b/frontend/src/app/shared/helpers/chronic_duration.js @@ -143,8 +143,14 @@ function calculateFromWords(string, opts) { } // Parse 3:41:59 and return 3 hours 41 minutes 59 seconds -function filterByType(string) { - const chronoUnitsList = DURATION_UNITS_LIST.filter((v) => v !== 'weeks'); +function filterByType(string, opts) { + const chronoUnitsList = DURATION_UNITS_LIST.filter(function (value) { + if (value === "weeks") { return false } + if (opts.ignoreSecondsWhenColonSeperated && value === "seconds") { return false } + + return true + }); + if ( string .replace(/ +/g, '') @@ -196,7 +202,7 @@ function filterThroughWhiteList(string, opts) { function cleanup(string, opts) { let res = string.toLowerCase(); - res = filterByType(res); + res = filterByType(res, opts); res = res .replace(FLOAT_MATCHER, (n) => ` ${n} `) .replace(/ +/g, ' ') diff --git a/frontend/src/app/shared/helpers/chronic_duration.spec.ts b/frontend/src/app/shared/helpers/chronic_duration.spec.ts index ae44d959927f..8946fbb98484 100644 --- a/frontend/src/app/shared/helpers/chronic_duration.spec.ts +++ b/frontend/src/app/shared/helpers/chronic_duration.spec.ts @@ -151,6 +151,17 @@ describe('parseChronicDuration', () => { ); }); }); + + describe('with .ignoreSecondsWhenColonSeperated param', () => { + it('parses 8:15 to 8 hours 15 minutes when seconds are ignored', () => { + expect(parseChronicDuration('8:15', { ignoreSecondsWhenColonSeperated: true })).toBe(8 * 3600 + 15 * 60); + }); + + it('parses 8:15 to 8 minutes 15 seconds when seconds are not ignored', () => { + expect(parseChronicDuration('8:15')).toBe(8 * 60 + 15); + }); + + }); }); describe('outputChronicDuration', () => {