From d9917c6b3e5ca87846c8765f22f31fc9f830b42d Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Fri, 5 Dec 2025 18:03:55 +0100 Subject: [PATCH] chore: - validations added for edge cases handling - Multiple lines processing added --- .../time/convert-time-to-decimal/service.ts | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/pages/tools/time/convert-time-to-decimal/service.ts b/src/pages/tools/time/convert-time-to-decimal/service.ts index f7b8dab..770ae18 100644 --- a/src/pages/tools/time/convert-time-to-decimal/service.ts +++ b/src/pages/tools/time/convert-time-to-decimal/service.ts @@ -1,24 +1,37 @@ import { InitialValuesType } from './types'; +import { humanTimeValidation } from 'utils/time'; export function convertTimeToDecimal( input: string, options: InitialValuesType ): string { - if (!input || (!input.includes(':') && !input.includes('.'))) { - return ''; + if (!input) return ''; + + const dp = parseInt(options.decimalPlaces, 10); + if (isNaN(dp) || dp < 0) { + return 'Invalid decimal places value.'; } - let splitTime = input.split(/[.:]/); + // Multiple lines processing + const lines = input.split('\n'); + if (!lines) return ''; - let hours = parseInt(splitTime[0]); - let minutes = parseInt(splitTime[1]); - let seconds = splitTime[2] ? parseInt(splitTime[2]) : 0; + const result: string[] = []; - let decimalTime = hours + minutes / 60; + lines.forEach((line) => { + line = line.trim(); + if (!line) return; - if (seconds !== 0) { - decimalTime += seconds / 3600; - } + const { isValid, hours, minutes, seconds } = humanTimeValidation(line); - return decimalTime.toFixed(parseInt(options.decimalPlaces)).toString(); + if (!isValid) { + result.push('Incorrect input format use `HH:MM:(SS)` or `HH.MM.(SS )`.'); + return; + } + + const decimalTime = hours + minutes / 60 + seconds / 3600; + result.push(decimalTime.toFixed(dp).toString()); + }); + + return result.join('\n'); }