Skip to content

Fix #4184: Added fractional time parsing when creating a new task #4255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/app/features/tasks/short-syntax.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,44 @@ describe('shortSyntax', () => {
});
});

it('', () => {
const t = {
...TASK,
title: 'Fun title whatever 1.5h',
};
const r = shortSyntax(t, CONFIG);
expect(r).toEqual({
newTagTitles: [],
remindAt: null,
projectId: undefined,
taskChanges: {
title: 'Fun title whatever',
timeEstimate: 5400000,
},
});
});

it('', () => {
const t = {
...TASK,
title: 'Fun title whatever 1.5h/2.5h',
};
const r = shortSyntax(t, CONFIG);
expect(r).toEqual({
newTagTitles: [],
remindAt: null,
projectId: undefined,
taskChanges: {
title: 'Fun title whatever',
// timeSpent: 7200000,
timeSpentOnDay: {
[getWorklogStr()]: 5400000,
},
timeEstimate: 9000000,
},
});
});

it('should ignore time short syntax when disabled', () => {
const t = {
...TASK,
Expand Down
22 changes: 13 additions & 9 deletions src/app/features/tasks/short-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type DueChanges = {
plannedAt?: number;
};

const SHORT_SYNTAX_TIME_REG_EX = / t?(([0-9]+(m|h|d)+)? *\/ *)?([0-9]+(m|h|d)+)/;
const SHORT_SYNTAX_TIME_REG_EX =
/(?:\s|^)t?((\d+(?:\.\d+)?[mhd])(?:\s*\/\s*(\d+(?:\.\d+)?[mhd]))?(?=\s|$))/i;
// NOTE: should come after the time reg ex is executed so we don't have to deal with those strings too

const CH_PRO = '+';
Expand Down Expand Up @@ -317,25 +318,28 @@ const parseTimeSpentChanges = (task: Partial<TaskCopy>): Partial<Task> => {
}

const matches = SHORT_SYNTAX_TIME_REG_EX.exec(task.title);

if (matches && matches.length >= 3) {
const full = matches[0];
const timeSpent = matches[2];
const timeEstimate = matches[4];

const timeSpent = matches[2]; // First part (before slash)
const timeEstimate = matches[3]; // Second part (after slash)
// If no slash, use the single value as timeEstimate only
const hasSlashFormat = matches[3] !== undefined;
return {
...(timeSpent
...(hasSlashFormat && timeSpent
? {
timeSpentOnDay: {
...(task.timeSpentOnDay || {}),
[getWorklogStr()]: stringToMs(timeSpent),
},
}
: {}),
timeEstimate: stringToMs(timeEstimate),
title: task.title.replace(full, ''),
...(timeEstimate
? { timeEstimate: stringToMs(timeEstimate) }
: timeSpent
? { timeEstimate: stringToMs(timeSpent) }
: {}),
title: task.title.replace(full, '').trim(),
};
}

return {};
};
18 changes: 18 additions & 0 deletions src/app/ui/duration/string-to-ms.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ export const stringToMs = (strValue: string, args?: any): number => {
return 0;
}

// First try to parse simple formats like "1.5h", "30m", etc.
const simpleFormatMatch = strValue.trim().match(/^([0-9]*\.?[0-9]+)([smhd])$/i);
if (simpleFormatMatch) {
const amount = parseFloat(simpleFormatMatch[1]);
const unit = simpleFormatMatch[2].toLowerCase();

switch (unit) {
case 's':
return amount * 1000;
case 'm':
return amount * 1000 * 60;
case 'h':
return amount * 1000 * 60 * 60;
case 'd':
return amount * 1000 * 60 * 60 * 24;
}
}

let d: number | undefined;
let h: number | undefined;
let m: number | undefined;
Expand Down
Loading