mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-12-17 17:56:38 +00:00
chore:
- validations added for edge cases handling - Multiple lines processing added
This commit is contained in:
parent
55354e3cd7
commit
d9917c6b3e
@ -1,24 +1,37 @@
|
|||||||
import { InitialValuesType } from './types';
|
import { InitialValuesType } from './types';
|
||||||
|
import { humanTimeValidation } from 'utils/time';
|
||||||
|
|
||||||
export function convertTimeToDecimal(
|
export function convertTimeToDecimal(
|
||||||
input: string,
|
input: string,
|
||||||
options: InitialValuesType
|
options: InitialValuesType
|
||||||
): string {
|
): string {
|
||||||
if (!input || (!input.includes(':') && !input.includes('.'))) {
|
if (!input) return '';
|
||||||
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]);
|
const result: string[] = [];
|
||||||
let minutes = parseInt(splitTime[1]);
|
|
||||||
let seconds = splitTime[2] ? parseInt(splitTime[2]) : 0;
|
|
||||||
|
|
||||||
let decimalTime = hours + minutes / 60;
|
lines.forEach((line) => {
|
||||||
|
line = line.trim();
|
||||||
|
if (!line) return;
|
||||||
|
|
||||||
if (seconds !== 0) {
|
const { isValid, hours, minutes, seconds } = humanTimeValidation(line);
|
||||||
decimalTime += seconds / 3600;
|
|
||||||
|
if (!isValid) {
|
||||||
|
result.push('Incorrect input format use `HH:MM:(SS)` or `HH.MM.(SS )`.');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return decimalTime.toFixed(parseInt(options.decimalPlaces)).toString();
|
const decimalTime = hours + minutes / 60 + seconds / 3600;
|
||||||
|
result.push(decimalTime.toFixed(dp).toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.join('\n');
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user