From c7d80c6616c1bbc4cbbe688ac3c758edceabc16b Mon Sep 17 00:00:00 2001 From: Edouard Cunibil Date: Fri, 6 Dec 2024 12:26:01 +0100 Subject: [PATCH] feat: improve durations input format #3736 --- src/app/ui/duration/string-to-ms.pipe.ts | 32 +++++++++++++++++------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/app/ui/duration/string-to-ms.pipe.ts b/src/app/ui/duration/string-to-ms.pipe.ts index 594cf1c8874..99bff53e8fe 100644 --- a/src/app/ui/duration/string-to-ms.pipe.ts +++ b/src/app/ui/duration/string-to-ms.pipe.ts @@ -9,26 +9,40 @@ export const stringToMs = (strValue: string, args?: any): number => { let h: number | undefined; let m: number | undefined; let s: number | undefined; + let previousLastChar: string | undefined; - const arrValue = strValue.split(' '); + // Add spaces after letters to ease further splitting. + strValue = strValue.replace(/([a-zA-Z]+)\s*/g, '$1 '); + // Replace commas by dots to allow using them as float separator. + strValue = strValue.replace(',', '.'); + + const arrValue = strValue.trim().split(' '); arrValue.forEach((val: string) => { if (val.length > 0) { - const lastChar = val.slice(-1); - const amount = parseInt(val.slice(0, val.length - 1), 10); + const lastChar = val.slice(-1).toLowerCase(); + const amount = parseFloat(val); if (lastChar === 's') { s = amount; - } - if (lastChar === 'm') { + } else if (lastChar === 'm') { m = amount; - } - if (lastChar === 'h') { + } else if (lastChar === 'h') { h = amount; - } - if (lastChar === 'd') { + } else if (lastChar === 'd') { d = amount; + } else { + if (previousLastChar === 's') { + // Don't track milliseconds. + } else if (previousLastChar === 'm') { + s = amount; + } else if (previousLastChar === 'h') { + m = amount; + } else if (previousLastChar === 'd') { + h = amount; + } } + previousLastChar = lastChar; } });