Skip to content

Commit

Permalink
add functionality to chronic duration to parse 8:15 to 8hours 15minut…
Browse files Browse the repository at this point in the history
…es instead of 8minutes 15seconds
  • Loading branch information
klaustopher committed Dec 10, 2024
1 parent 178e8ca commit bbbd688
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions frontend/src/app/shared/helpers/chronic_duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')
Expand Down Expand Up @@ -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, ' ')
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/app/shared/helpers/chronic_duration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit bbbd688

Please sign in to comment.