refactor: tools folder inside pages

This commit is contained in:
Ibrahima G. Coulibaly
2025-02-23 01:38:42 +01:00
parent 62f084eb45
commit 64936ab11f
117 changed files with 447 additions and 194 deletions

View File

@@ -0,0 +1,76 @@
// Import necessary modules and functions
import { describe, expect, it } from 'vitest';
import { listOfIntegers } from './service';
// Define test cases for the listOfIntegers function
describe('listOfIntegers function', () => {
it('should generate a list of integers with comma separator', () => {
const initialValue = 1;
const step = 2;
const count = 5;
const separator = ', ';
const result = listOfIntegers(initialValue, count, step, separator);
expect(result).toBe('1, 3, 5, 7, 9');
});
it('should generate a list of integers with dash separator', () => {
const initialValue = 0;
const step = 3;
const count = 4;
const separator = ' - ';
const result = listOfIntegers(initialValue, count, step, separator);
expect(result).toBe('0 - 3 - 6 - 9');
});
it('should handle negative initial value and step', () => {
const initialValue = -10;
const step = -2;
const count = 5;
const separator = ' ';
const result = listOfIntegers(initialValue, count, step, separator);
expect(result).toBe('-10 -12 -14 -16 -18');
});
it('should handle negative initial value and positive step', () => {
const initialValue = -10;
const step = 2;
const count = 5;
const separator = ' ';
const result = listOfIntegers(initialValue, count, step, separator);
expect(result).toBe('-10 -8 -6 -4 -2');
});
it('should float value', () => {
const initialValue = -10;
const step = 2.5;
const count = 5;
const separator = ' ';
const result = listOfIntegers(initialValue, count, step, separator);
expect(result).toBe('-10 -7.5 -5 -2.5 0');
});
it('should generate a constant sequence if the step is 0', () => {
const initialValue = 1;
const step = 0;
const count = 5;
const separator = ' ';
const result = listOfIntegers(initialValue, count, step, separator);
expect(result).toBe('1 1 1 1 1');
});
it('should generate a constant sequence if the step is 0', () => {
const initialValue = 1;
const step = 0;
const count = 5;
const separator = ' ';
const result = listOfIntegers(initialValue, count, step, separator);
expect(result).toBe('1 1 1 1 1');
});
});

View File

@@ -0,0 +1,79 @@
import { Box } from '@mui/material';
import React, { useState } from 'react';
import ToolTextResult from '@components/result/ToolTextResult';
import ToolOptions from '@components/options/ToolOptions';
import { listOfIntegers } from './service';
import ToolInputAndResult from '@components/ToolInputAndResult';
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
const initialValues = {
firstValue: '1',
numberOfNumbers: '10',
step: '1',
separator: '\\n'
};
export default function SplitText() {
const [result, setResult] = useState<string>('');
return (
<Box>
<ToolInputAndResult
result={<ToolTextResult title={'Total'} value={result} />}
/>
<ToolOptions
getGroups={({ values, updateField }) => [
{
title: 'Arithmetic sequence option',
component: (
<Box>
<TextFieldWithDesc
description={'Start sequence from this number.'}
value={values.firstValue}
onOwnChange={(val) => updateField('firstValue', val)}
type={'number'}
/>
<TextFieldWithDesc
description={'Increase each element by this amount'}
value={values.step}
onOwnChange={(val) => updateField('step', val)}
type={'number'}
/>
<TextFieldWithDesc
description={'Number of elements in sequence.'}
value={values.numberOfNumbers}
onOwnChange={(val) => updateField('numberOfNumbers', val)}
type={'number'}
/>
</Box>
)
},
{
title: 'Separator',
component: (
<TextFieldWithDesc
description={
'Separate elements in the arithmetic sequence by this character.'
}
value={values.separator}
onOwnChange={(val) => updateField('separator', val)}
/>
)
}
]}
compute={(optionsValues) => {
const { firstValue, numberOfNumbers, separator, step } =
optionsValues;
setResult(
listOfIntegers(
Number(firstValue),
Number(numberOfNumbers),
Number(step),
separator
)
);
}}
initialValues={initialValues}
/>
</Box>
);
}

View File

@@ -0,0 +1,14 @@
import { defineTool } from '@tools/defineTool';
import { lazy } from 'react';
// import image from '@assets/text.png';
export const tool = defineTool('number', {
name: 'Generate numbers',
path: 'generate',
shortDescription: 'Quickly calculate a list of integers in your browser',
// image,
description:
'Quickly calculate a list of integers in your browser. To get your list, just specify the first integer, change value and total count in the options below, and this utility will generate that many integers',
keywords: ['generate'],
component: lazy(() => import('./index'))
});

View File

@@ -0,0 +1,13 @@
export function listOfIntegers(
first_value: number,
number_of_numbers: number,
step: number,
separator: string
) {
const result: number[] = [];
for (let i: number = 0; i < number_of_numbers; i++) {
const value: number = first_value + i * step;
result.push(value);
}
return result.join(separator);
}