Skip to content

Commit 4104270

Browse files
Sofia Lucasjohannesjo
Sofia Lucas
authored andcommitted
Fix #4184: Added fractional time parsing when creating a new task
Fixed the issue where you could not insert 1.5h or any decimal value when inputing time stamp in the quick add method of creating a task. To test execute project with instructions given in the repository and add a new task (shift+A) and write the title of a new task such as "task 1.5h" or "task 1.5h/3.5h". Also added test cases for this fix.
1 parent e7412d5 commit 4104270

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

Diff for: src/app/features/tasks/short-syntax.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,44 @@ describe('shortSyntax', () => {
171171
});
172172
});
173173

174+
it('', () => {
175+
const t = {
176+
...TASK,
177+
title: 'Fun title whatever 1.5h',
178+
};
179+
const r = shortSyntax(t, CONFIG);
180+
expect(r).toEqual({
181+
newTagTitles: [],
182+
remindAt: null,
183+
projectId: undefined,
184+
taskChanges: {
185+
title: 'Fun title whatever',
186+
timeEstimate: 5400000,
187+
},
188+
});
189+
});
190+
191+
it('', () => {
192+
const t = {
193+
...TASK,
194+
title: 'Fun title whatever 1.5h/2.5h',
195+
};
196+
const r = shortSyntax(t, CONFIG);
197+
expect(r).toEqual({
198+
newTagTitles: [],
199+
remindAt: null,
200+
projectId: undefined,
201+
taskChanges: {
202+
title: 'Fun title whatever',
203+
// timeSpent: 7200000,
204+
timeSpentOnDay: {
205+
[getWorklogStr()]: 5400000,
206+
},
207+
timeEstimate: 9000000,
208+
},
209+
});
210+
});
211+
174212
it('should ignore time short syntax when disabled', () => {
175213
const t = {
176214
...TASK,

Diff for: src/app/features/tasks/short-syntax.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type DueChanges = {
1919
plannedAt?: number;
2020
};
2121

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

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

319320
const matches = SHORT_SYNTAX_TIME_REG_EX.exec(task.title);
320-
321321
if (matches && matches.length >= 3) {
322322
const full = matches[0];
323-
const timeSpent = matches[2];
324-
const timeEstimate = matches[4];
325-
323+
const timeSpent = matches[2]; // First part (before slash)
324+
const timeEstimate = matches[3]; // Second part (after slash)
325+
// If no slash, use the single value as timeEstimate only
326+
const hasSlashFormat = matches[3] !== undefined;
326327
return {
327-
...(timeSpent
328+
...(hasSlashFormat && timeSpent
328329
? {
329330
timeSpentOnDay: {
330331
...(task.timeSpentOnDay || {}),
331332
[getWorklogStr()]: stringToMs(timeSpent),
332333
},
333334
}
334335
: {}),
335-
timeEstimate: stringToMs(timeEstimate),
336-
title: task.title.replace(full, ''),
336+
...(timeEstimate
337+
? { timeEstimate: stringToMs(timeEstimate) }
338+
: timeSpent
339+
? { timeEstimate: stringToMs(timeSpent) }
340+
: {}),
341+
title: task.title.replace(full, '').trim(),
337342
};
338343
}
339-
340344
return {};
341345
};

Diff for: src/app/ui/duration/string-to-ms.pipe.ts

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ export const stringToMs = (strValue: string, args?: any): number => {
55
return 0;
66
}
77

8+
// First try to parse simple formats like "1.5h", "30m", etc.
9+
const simpleFormatMatch = strValue.trim().match(/^([0-9]*\.?[0-9]+)([smhd])$/i);
10+
if (simpleFormatMatch) {
11+
const amount = parseFloat(simpleFormatMatch[1]);
12+
const unit = simpleFormatMatch[2].toLowerCase();
13+
14+
switch (unit) {
15+
case 's':
16+
return amount * 1000;
17+
case 'm':
18+
return amount * 1000 * 60;
19+
case 'h':
20+
return amount * 1000 * 60 * 60;
21+
case 'd':
22+
return amount * 1000 * 60 * 60 * 24;
23+
}
24+
}
25+
826
let d: number | undefined;
927
let h: number | undefined;
1028
let m: number | undefined;

0 commit comments

Comments
 (0)