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:
110
src/pages/tools/list/find-unique/find-unique.service.test.ts
Normal file
110
src/pages/tools/list/find-unique/find-unique.service.test.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { describe, expect } from 'vitest';
|
||||
|
||||
import { findUniqueCompute } from './service';
|
||||
|
||||
describe('TopItemsList Function', () => {
|
||||
test('should return unique items ignoring case sensitivity', () => {
|
||||
const input = 'apple,banana,Apple,orange,Banana,apple';
|
||||
const result = findUniqueCompute(
|
||||
'symbol',
|
||||
',',
|
||||
'\n',
|
||||
input,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
true
|
||||
);
|
||||
expect(result).toBe('orange');
|
||||
});
|
||||
|
||||
test('should return unique items considering case sensitivity', () => {
|
||||
const input = 'apple,banana,Apple,orange,Banana,apple';
|
||||
const result = findUniqueCompute(
|
||||
'symbol',
|
||||
',',
|
||||
'\n',
|
||||
input,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true
|
||||
);
|
||||
expect(result).toBe('banana\nApple\norange\nBanana');
|
||||
});
|
||||
|
||||
test('should return all unique items ignoring case sensitivity', () => {
|
||||
const input = 'apple,banana,Apple,orange,Banana,apple';
|
||||
const result = findUniqueCompute(
|
||||
'symbol',
|
||||
',',
|
||||
'\n',
|
||||
input,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false
|
||||
);
|
||||
expect(result).toBe('apple\nbanana\norange');
|
||||
});
|
||||
|
||||
test('should return all unique items considering case sensitivity', () => {
|
||||
const input = 'apple,banana,Apple,orange,Banana,apple';
|
||||
const result = findUniqueCompute(
|
||||
'symbol',
|
||||
',',
|
||||
'\n',
|
||||
input,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
false
|
||||
);
|
||||
expect(result).toBe('apple\nbanana\nApple\norange\nBanana');
|
||||
});
|
||||
|
||||
test('should handle empty items deletion', () => {
|
||||
const input = 'apple,,banana, ,orange';
|
||||
const result = findUniqueCompute(
|
||||
'symbol',
|
||||
',',
|
||||
'\n',
|
||||
input,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false
|
||||
);
|
||||
expect(result).toBe('apple\nbanana\norange');
|
||||
});
|
||||
|
||||
test('should handle trimming items', () => {
|
||||
const input = ' apple , banana , orange ';
|
||||
const result = findUniqueCompute(
|
||||
'symbol',
|
||||
',',
|
||||
'\n',
|
||||
input,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
);
|
||||
expect(result).toBe(' apple \n banana \n orange ');
|
||||
});
|
||||
|
||||
test('should handle regex split', () => {
|
||||
const input = 'apple banana orange';
|
||||
const result = findUniqueCompute(
|
||||
'regex',
|
||||
'\\s+',
|
||||
'\n',
|
||||
input,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
);
|
||||
expect(result).toBe('apple\nbanana\norange');
|
||||
});
|
||||
});
|
||||
158
src/pages/tools/list/find-unique/index.tsx
Normal file
158
src/pages/tools/list/find-unique/index.tsx
Normal file
@@ -0,0 +1,158 @@
|
||||
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 { findUniqueCompute, SplitOperatorType } from './service';
|
||||
import ToolInputAndResult from '@components/ToolInputAndResult';
|
||||
import SimpleRadio from '@components/options/SimpleRadio';
|
||||
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
|
||||
import CheckboxWithDesc from '@components/options/CheckboxWithDesc';
|
||||
|
||||
const initialValues = {
|
||||
splitOperatorType: 'symbol' as SplitOperatorType,
|
||||
splitSeparator: ',',
|
||||
joinSeparator: '\\n',
|
||||
deleteEmptyItems: true,
|
||||
caseSensitive: false,
|
||||
trimItems: true,
|
||||
absolutelyUnique: false
|
||||
};
|
||||
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 FindUnique() {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
const compute = (optionsValues: typeof initialValues, input: any) => {
|
||||
const {
|
||||
splitOperatorType,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
trimItems,
|
||||
caseSensitive,
|
||||
absolutelyUnique
|
||||
} = optionsValues;
|
||||
|
||||
setResult(
|
||||
findUniqueCompute(
|
||||
splitOperatorType,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
input,
|
||||
deleteEmptyItems,
|
||||
trimItems,
|
||||
caseSensitive,
|
||||
absolutelyUnique
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<ToolInputAndResult
|
||||
input={
|
||||
<ToolTextInput
|
||||
title={'Input list'}
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
/>
|
||||
}
|
||||
result={<ToolTextResult title={'Unique items'} value={result} />}
|
||||
/>
|
||||
<ToolOptions
|
||||
compute={compute}
|
||||
getGroups={({ values, updateField }) => [
|
||||
{
|
||||
title: 'Input List Delimiter',
|
||||
component: (
|
||||
<Box>
|
||||
{splitOperators.map(({ title, description, type }) => (
|
||||
<SimpleRadio
|
||||
key={type}
|
||||
onClick={() => updateField('splitOperatorType', type)}
|
||||
title={title}
|
||||
description={description}
|
||||
checked={values.splitOperatorType === type}
|
||||
/>
|
||||
))}
|
||||
<TextFieldWithDesc
|
||||
description={'Set a delimiting symbol or regular expression.'}
|
||||
value={values.splitSeparator}
|
||||
onOwnChange={(val) => updateField('splitSeparator', val)}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
},
|
||||
{
|
||||
title: 'Output List Delimiter',
|
||||
component: (
|
||||
<Box>
|
||||
<TextFieldWithDesc
|
||||
value={values.joinSeparator}
|
||||
onOwnChange={(value) => updateField('joinSeparator', value)}
|
||||
/>
|
||||
<CheckboxWithDesc
|
||||
title={'Trim top list items'}
|
||||
description={
|
||||
'Remove leading and trailing spaces before comparing items'
|
||||
}
|
||||
checked={values.trimItems}
|
||||
onChange={(value) => updateField('trimItems', value)}
|
||||
/>
|
||||
<CheckboxWithDesc
|
||||
title={'Skip empty items'}
|
||||
description={
|
||||
"Don't include the empty list items in the output."
|
||||
}
|
||||
checked={values.deleteEmptyItems}
|
||||
onChange={(value) => updateField('deleteEmptyItems', value)}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
},
|
||||
{
|
||||
title: 'Unique Item Options',
|
||||
component: (
|
||||
<Box>
|
||||
<CheckboxWithDesc
|
||||
title={'Find Absolutely Unique Items'}
|
||||
description={
|
||||
'Display only those items of the list that exist in a single copy.'
|
||||
}
|
||||
checked={values.absolutelyUnique}
|
||||
onChange={(value) => updateField('absolutelyUnique', value)}
|
||||
/>
|
||||
<CheckboxWithDesc
|
||||
title={'Case Sensitive Items'}
|
||||
description={
|
||||
'Output items with different case as unique elements in the list.'
|
||||
}
|
||||
checked={values.caseSensitive}
|
||||
onChange={(value) => updateField('caseSensitive', value)}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
]}
|
||||
initialValues={initialValues}
|
||||
input={input}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
13
src/pages/tools/list/find-unique/meta.ts
Normal file
13
src/pages/tools/list/find-unique/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: 'Find unique',
|
||||
path: 'find-unique',
|
||||
// image,
|
||||
description: '',
|
||||
shortDescription: '',
|
||||
keywords: ['find', 'unique'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
64
src/pages/tools/list/find-unique/service.ts
Normal file
64
src/pages/tools/list/find-unique/service.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
// Function that builds the unique items array handling caseSensitive and absolutelyUnique options
|
||||
function uniqueListBuilder(
|
||||
array: string[],
|
||||
caseSensitive: boolean,
|
||||
absolutelyUnique: boolean
|
||||
): string[] {
|
||||
const dict: { [key: string]: number } = {};
|
||||
for (const item of array) {
|
||||
const key = caseSensitive ? item : item.toLowerCase();
|
||||
dict[key] = (dict[key] || 0) + 1;
|
||||
}
|
||||
if (absolutelyUnique) {
|
||||
for (const [key, value] of Object.entries(dict)) {
|
||||
if (value > 1) {
|
||||
delete dict[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return Object.keys(dict);
|
||||
}
|
||||
|
||||
export function findUniqueCompute(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string = '\n',
|
||||
input: string,
|
||||
deleteEmptyItems: boolean,
|
||||
trimItems: boolean,
|
||||
caseSensitive: boolean,
|
||||
absolutelyUnique: boolean
|
||||
): 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;
|
||||
}
|
||||
|
||||
// Trim items if required
|
||||
if (trimItems) {
|
||||
array = array.map((item) => item.trim());
|
||||
}
|
||||
|
||||
// Delete empty items after initial split
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter((item) => item !== '');
|
||||
}
|
||||
|
||||
// Format the output with desired format
|
||||
const uniqueListItems = uniqueListBuilder(
|
||||
array,
|
||||
caseSensitive,
|
||||
absolutelyUnique
|
||||
);
|
||||
|
||||
return uniqueListItems.join(joinSeparator);
|
||||
}
|
||||
Reference in New Issue
Block a user