Chesterkxng d9917c6b3e chore:
- validations added for edge cases handling
- Multiple lines processing added
2025-12-05 18:03:55 +01:00

38 lines
897 B
TypeScript

import { InitialValuesType } from './types';
import { humanTimeValidation } from 'utils/time';
export function convertTimeToDecimal(
input: string,
options: InitialValuesType
): string {
if (!input) return '';
const dp = parseInt(options.decimalPlaces, 10);
if (isNaN(dp) || dp < 0) {
return 'Invalid decimal places value.';
}
// Multiple lines processing
const lines = input.split('\n');
if (!lines) return '';
const result: string[] = [];
lines.forEach((line) => {
line = line.trim();
if (!line) return;
const { isValid, hours, minutes, seconds } = humanTimeValidation(line);
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');
}