feat: convert-hours-to-days

This commit is contained in:
Chesterkxng
2025-03-26 01:32:09 +00:00
parent 243c7d45a2
commit fadd503133
5 changed files with 226 additions and 1 deletions

View File

@@ -0,0 +1,40 @@
import { expect, describe, it } from 'vitest';
import { convertHoursToDays } from './service';
describe('convertHoursToDays', () => {
it('should convert hours to days with default accuracy', () => {
const input = '48';
const result = convertHoursToDays(input, '1', false);
expect(result).toBe('2');
});
it('should convert hours to days with specified accuracy', () => {
const input = '50';
const result = convertHoursToDays(input, '2', false);
expect(result).toBe('2.08');
});
it('should append "days" postfix when daysFlag is true', () => {
const input = '72';
const result = convertHoursToDays(input, '1', true);
expect(result).toBe('3 days');
});
it('should handle multiple lines of input', () => {
const input = '24\n48\n72';
const result = convertHoursToDays(input, '1', true);
expect(result).toBe('1 days\n2 days\n3 days');
});
it('should handle invalid input gracefully', () => {
const input = 'abc';
const result = convertHoursToDays(input, '1', false);
expect(result).toBe('');
});
it('should handle empty input', () => {
const input = '';
const result = convertHoursToDays(input, '1', false);
expect(result).toBe('');
});
});

View File

@@ -0,0 +1,136 @@
import { Box } from '@mui/material';
import React, { useState } from 'react';
import ToolContent from '@components/ToolContent';
import { ToolComponentProps } from '@tools/defineTool';
import ToolTextInput from '@components/input/ToolTextInput';
import ToolTextResult from '@components/result/ToolTextResult';
import { GetGroupsType } from '@components/options/ToolOptions';
import { CardExampleType } from '@components/examples/ToolExamples';
import CheckboxWithDesc from '@components/options/CheckboxWithDesc';
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
import { convertHoursToDays } from './service';
const initialValues = {
daysFlag: false,
accuracy: '1'
};
type InitialValuesType = typeof initialValues;
const exampleCards: CardExampleType<InitialValuesType>[] = [
{
title: 'Hours to Integer Days',
description:
'In this example, we convert ten hour values to ten day values. Each input hour is divisible by 24 without a remainder, so all converted output values are full days. To better communicate the time units, we use the word "hours" in the input data and also add the word "days" to the output data.',
sampleText: `24 hours
48 hours
72 hours
96 hours
120 hours
144 hours
168 hours
192 hours
216 hours
240 hours`,
sampleResult: `1 day
2 days
3 days
4 days
5 days
6 days
7 days
8 days
9 days
10 days`,
sampleOptions: { daysFlag: true, accuracy: '2' }
},
{
title: 'Decimal Days',
description:
'In this example, we convert five decimal fraction day values to hours. Conversion of partial days is similar to the conversion of full days they are all multiplied by 24. We turn off the option that appends the "hours" string after the converted values and get only the numerical hour values in the output.',
sampleText: `1 hr
100 hr
9999 hr
12345 hr
333333 hr`,
sampleResult: `0.0417 days
4.1667 days
416.625 days
514.375 days
13888.875 days`,
sampleOptions: { daysFlag: true, accuracy: '4' }
},
{
title: 'Partial Hours',
description:
'In the modern Gregorian calendar, a common year has 365 days and a leap year has 366 days. This makes the true average length of a year to be 365.242199 days. In this example, we load this number in the input field and convert it to the hours. It turns out that there 8765.812776 hours in an average year.',
sampleText: `0.5
0.01
0.99`,
sampleResult: `0.02083333
0.00041667
0.04125`,
sampleOptions: { daysFlag: false, accuracy: '8' }
}
];
export default function ConvertDaysToHours({
title,
longDescription
}: ToolComponentProps) {
const [input, setInput] = useState<string>('');
const [result, setResult] = useState<string>('');
const compute = (optionsValues: typeof initialValues, input: string) => {
setResult(
convertHoursToDays(input, optionsValues.accuracy, optionsValues.daysFlag)
);
};
const getGroups: GetGroupsType<InitialValuesType> | null = ({
values,
updateField
}) => [
{
title: 'Day Value Accuracy',
component: (
<Box>
<TextFieldWithDesc
description={
'If the calculated days is a decimal number, then how many digits should be left after the decimal point?.'
}
value={values.accuracy}
onOwnChange={(val) => updateField('accuracy', val)}
type={'text'}
/>
</Box>
)
},
{
title: 'Days Postfix',
component: (
<Box>
<CheckboxWithDesc
onChange={(val) => updateField('daysFlag', val)}
checked={values.daysFlag}
title={'Append Days Postfix'}
description={'Display numeric day values with the postfix "days".'}
/>
</Box>
)
}
];
return (
<ToolContent
title={title}
input={input}
inputComponent={<ToolTextInput value={input} onChange={setInput} />}
resultComponent={<ToolTextResult value={result} />}
initialValues={initialValues}
getGroups={getGroups}
setInput={setInput}
compute={compute}
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
exampleCards={exampleCards}
/>
);
}

View File

@@ -0,0 +1,15 @@
import { defineTool } from '@tools/defineTool';
import { lazy } from 'react';
export const tool = defineTool('time', {
path: 'convert-hours-to-days',
name: 'Convert Hours to Days',
icon: 'mdi:hours-24',
description:
'With this browser-based application, you can calculate how many days there are in the given number of hours. Given one or more hour values in the input, it converts them into days via the simple math formula: days = hours/24. It works with arbitrary large hour values and you can also customize the decimal day precision.',
shortDescription: 'Convert hours to days easily.',
keywords: ['convert', 'hours', 'days'],
longDescription:
"This is a quick online utility for converting hours to days. To figure out the number of days in the specified hours, the program divides them by 24. For example, if the input hours value is 48, then by doing 48/24, it finds that there are 2 days, or if the hours value is 120, then it's 120/24 = 5 days. If the hours value is not divisible by 24, then the number of days is displayed as a decimal number. For example, 36 hours is 36/24 = 1.5 days and 100 hours is approximately 4.167 days. You can specify the precision of the decimal fraction calculation in the options. You can also enable the option that adds the postfix 'days' to all the output values. Timeabulous!",
component: lazy(() => import('./index'))
});

View File

@@ -0,0 +1,33 @@
import { containsOnlyDigits } from '@utils/string';
function compute(input: string, accuracy: number) {
if (!containsOnlyDigits(input)) {
return '';
}
const hours = parseFloat(input);
const days = (hours / 24).toFixed(accuracy);
return parseFloat(days);
}
export function convertHoursToDays(
input: string,
accuracy: string,
daysFlag: boolean
): string {
if (!containsOnlyDigits(accuracy)) {
throw new Error('Accuracy contains non digits.');
}
const result: string[] = [];
const lines = input.split('\n');
lines.forEach((line) => {
const parts = line.split(' ');
const hours = parts[0]; // Extract the number before the space
const days = compute(hours, Number(accuracy));
result.push(daysFlag ? `${days} days` : `${days}`);
});
return result.join('\n');
}

View File

@@ -1,3 +1,4 @@
import { tool as daysDoHours } from './convert-days-to-hours/meta';
import { tool as hoursToDays } from './convert-hours-to-days/meta';
export const timeTools = [daysDoHours];
export const timeTools = [daysDoHours, hoursToDays];