mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-12-29 16:16:02 +00:00
refactor: tools folder inside pages
This commit is contained in:
105
src/pages/tools/list/reverse/index.tsx
Normal file
105
src/pages/tools/list/reverse/index.tsx
Normal file
@@ -0,0 +1,105 @@
|
||||
import { Box } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import ToolTextInput from '@components/input/ToolTextInput';
|
||||
import ToolTextResult from '@components/result/ToolTextResult';
|
||||
import ToolOptions from '@components/options/ToolOptions';
|
||||
import { reverseList, SplitOperatorType } from './service';
|
||||
import ToolInputAndResult from '@components/ToolInputAndResult';
|
||||
import SimpleRadio from '@components/options/SimpleRadio';
|
||||
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
|
||||
|
||||
const initialValues = {
|
||||
splitOperatorType: 'symbol' as SplitOperatorType,
|
||||
splitSeparator: ',',
|
||||
joinSeparator: '\\n'
|
||||
};
|
||||
const splitOperators: {
|
||||
title: string;
|
||||
description: string;
|
||||
type: SplitOperatorType;
|
||||
}[] = [
|
||||
{
|
||||
title: 'Use a Symbol for Splitting',
|
||||
description: 'Delimit input list items with a character.',
|
||||
type: 'symbol'
|
||||
},
|
||||
{
|
||||
title: 'Use a Regex for Splitting',
|
||||
type: 'regex',
|
||||
description: 'Delimit input list items with a regular expression.'
|
||||
}
|
||||
];
|
||||
|
||||
export default function Reverse() {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
const compute = (optionsValues: typeof initialValues, input: any) => {
|
||||
const { splitOperatorType, splitSeparator, joinSeparator } = optionsValues;
|
||||
|
||||
setResult(
|
||||
reverseList(splitOperatorType, splitSeparator, joinSeparator, input)
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<ToolInputAndResult
|
||||
input={
|
||||
<ToolTextInput
|
||||
title={'Input list'}
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
/>
|
||||
}
|
||||
result={<ToolTextResult title={'Reversed list'} value={result} />}
|
||||
/>
|
||||
<ToolOptions
|
||||
compute={compute}
|
||||
getGroups={({ values, updateField }) => [
|
||||
{
|
||||
title: 'Splitter Mode',
|
||||
component: (
|
||||
<Box>
|
||||
{splitOperators.map(({ title, description, type }) => (
|
||||
<SimpleRadio
|
||||
key={type}
|
||||
onClick={() => updateField('splitOperatorType', type)}
|
||||
title={title}
|
||||
description={description}
|
||||
checked={values.splitOperatorType === type}
|
||||
/>
|
||||
))}
|
||||
</Box>
|
||||
)
|
||||
},
|
||||
{
|
||||
title: 'Item Separator',
|
||||
component: (
|
||||
<Box>
|
||||
<TextFieldWithDesc
|
||||
description={'Set a delimiting symbol or regular expression.'}
|
||||
value={values.splitSeparator}
|
||||
onOwnChange={(val) => updateField('splitSeparator', val)}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
},
|
||||
{
|
||||
title: 'Output List Options',
|
||||
component: (
|
||||
<Box>
|
||||
<TextFieldWithDesc
|
||||
description={'Output list item separator.'}
|
||||
value={values.joinSeparator}
|
||||
onOwnChange={(val) => updateField('joinSeparator', val)}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
]}
|
||||
initialValues={initialValues}
|
||||
input={input}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
13
src/pages/tools/list/reverse/meta.ts
Normal file
13
src/pages/tools/list/reverse/meta.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
// import image from '@assets/text.png';
|
||||
|
||||
export const tool = defineTool('list', {
|
||||
name: 'Reverse',
|
||||
path: 'reverse',
|
||||
// image,
|
||||
description: '',
|
||||
shortDescription: '',
|
||||
keywords: ['reverse'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
28
src/pages/tools/list/reverse/reverse.service.test.ts
Normal file
28
src/pages/tools/list/reverse/reverse.service.test.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { describe, expect } from 'vitest';
|
||||
import { reverseList } from './service';
|
||||
|
||||
describe('reverseList Function', () => {
|
||||
test('should reverse items split by symbol', () => {
|
||||
const input = 'apple,banana,orange';
|
||||
const result = reverseList('symbol', ',', '\n', input);
|
||||
expect(result).toBe('orange\nbanana\napple');
|
||||
});
|
||||
|
||||
test('should reverse items split by regex', () => {
|
||||
const input = 'apple banana orange';
|
||||
const result = reverseList('regex', '\\s+', '\n', input);
|
||||
expect(result).toBe('orange\nbanana\napple');
|
||||
});
|
||||
|
||||
test('should handle empty input', () => {
|
||||
const input = '';
|
||||
const result = reverseList('symbol', ',', '\n', input);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
test('should handle join separator', () => {
|
||||
const input = 'apple,banana,orange';
|
||||
const result = reverseList('symbol', ',', ', ', input);
|
||||
expect(result).toBe('orange, banana, apple');
|
||||
});
|
||||
});
|
||||
23
src/pages/tools/list/reverse/service.ts
Normal file
23
src/pages/tools/list/reverse/service.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
export function reverseList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string = '\n',
|
||||
input: string
|
||||
): string {
|
||||
let array: string[] = [];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input
|
||||
.split(new RegExp(splitSeparator))
|
||||
.filter((item) => item !== '');
|
||||
break;
|
||||
}
|
||||
|
||||
const reversedList = array.reverse();
|
||||
return reversedList.join(joinSeparator);
|
||||
}
|
||||
Reference in New Issue
Block a user