Merge branch 'main' into truncate

This commit is contained in:
Chesterkxng
2024-07-09 21:23:35 +00:00
10 changed files with 563 additions and 221 deletions

View File

@@ -1,65 +1,105 @@
import { expect, describe, it } from 'vitest';
import { TopItemsList, SplitOperatorType, SortingMethod, DisplayFormat } from './service';
import {
TopItemsList,
SplitOperatorType,
SortingMethod,
DisplayFormat
} from './service';
describe('TopItemsList function', () => {
it('should handle sorting alphabetically ignoring case', () => {
const input = 'Apple,banana,apple,Orange,Banana,apple';
const result = TopItemsList('symbol', 'alphabetic', 'count', ',', input, false, true, false);
expect(result).toEqual(
'apple: 3\n' +
'banana: 2\n' +
'orange: 1'
);
});
it('should handle sorting alphabetically ignoring case', () => {
const input = 'Apple,banana,apple,Orange,Banana,apple';
const result = TopItemsList(
'symbol',
'alphabetic',
'count',
',',
input,
false,
true,
false
);
expect(result).toEqual('apple: 3\n' + 'banana: 2\n' + 'orange: 1');
});
it('should handle sorting by count and not ignoring case', () => {
const input = 'apple,banana,apple,orange,banana,apple,Banana';
const result = TopItemsList('symbol', 'count', 'count', ',', input, false, false, false);
expect(result).toEqual(
'apple: 3\n' +
'banana: 2\n' +
'orange: 1\n' +
'Banana: 1'
);
});
it('should handle sorting by count and not ignoring case', () => {
const input = 'apple,banana,apple,orange,banana,apple,Banana';
const result = TopItemsList(
'symbol',
'count',
'count',
',',
input,
false,
false,
false
);
expect(result).toEqual(
'apple: 3\n' + 'banana: 2\n' + 'orange: 1\n' + 'Banana: 1'
);
});
it('should handle regex split operator', () => {
const input = 'apple123banana456apple789orange012banana345apple678';
const result = TopItemsList('regex', 'count', 'count', '\\d+', input, false, false, false);
expect(result).toEqual(
'apple: 3\n' +
'banana: 2\n' +
'orange: 1'
);
});
it('should handle regex split operator', () => {
const input = 'apple123banana456apple789orange012banana345apple678';
const result = TopItemsList(
'regex',
'count',
'count',
'\\d+',
input,
false,
false,
false
);
expect(result).toEqual('apple: 3\n' + 'banana: 2\n' + 'orange: 1');
});
it('should handle percentage display format', () => {
const input = 'apple,banana,apple,orange,banana,apple';
const result = TopItemsList('symbol', 'count', 'percentage', ',', input, false, false, false);
expect(result).toEqual(
'apple: 3 (50.00%)\n' +
'banana: 2 (33.33%)\n' +
'orange: 1 (16.67%)'
);
});
it('should handle percentage display format', () => {
const input = 'apple,banana,apple,orange,banana,apple';
const result = TopItemsList(
'symbol',
'count',
'percentage',
',',
input,
false,
false,
false
);
expect(result).toEqual(
'apple: 3 (50.00%)\n' + 'banana: 2 (33.33%)\n' + 'orange: 1 (16.67%)'
);
});
it('should handle total display format', () => {
const input = 'apple,banana,apple,orange,banana,apple';
const result = TopItemsList('symbol', 'count', 'total', ',', input, false, false, false);
expect(result).toEqual(
'apple: 3 (3 / 6)\n' +
'banana: 2 (2 / 6)\n' +
'orange: 1 (1 / 6)'
);
});
it('should handle total display format', () => {
const input = 'apple,banana,apple,orange,banana,apple';
const result = TopItemsList(
'symbol',
'count',
'total',
',',
input,
false,
false,
false
);
expect(result).toEqual(
'apple: 3 (3 / 6)\n' + 'banana: 2 (2 / 6)\n' + 'orange: 1 (1 / 6)'
);
});
it('should handle trimming and ignoring empty items', () => {
const input = ' apple , banana , apple , orange , banana , apple ';
const result = TopItemsList('symbol', 'count', 'count', ',', input, true, false, true);
expect(result).toEqual(
'apple: 3\n' +
'banana: 2\n' +
'orange: 1'
);
});
});
it('should handle trimming and ignoring empty items', () => {
const input = ' apple , banana , apple , orange , banana , apple ';
const result = TopItemsList(
'symbol',
'count',
'count',
',',
input,
true,
false,
true
);
expect(result).toEqual('apple: 3\n' + 'banana: 2\n' + 'orange: 1');
});
});

View File

@@ -1,11 +1,172 @@
import { Box } from '@mui/material';
import React from 'react';
import React, { useState } from 'react';
import ToolTextInput from '../../../components/input/ToolTextInput';
import ToolTextResult from '../../../components/result/ToolTextResult';
import * as Yup from 'yup';
import ToolOptions from '../../../components/options/ToolOptions';
import {
DisplayFormat,
SortingMethod,
SplitOperatorType,
TopItemsList
} 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';
import SelectWithDesc from '../../../components/options/SelectWithDesc';
const initialValues = {
splitSeparatorType: 'symbol' as SplitOperatorType,
sortingMethod: 'alphabetic' as SortingMethod,
displayFormat: 'count' as DisplayFormat,
splitSeparator: ',',
deleteEmptyItems: false,
ignoreItemCase: false,
trimItems: 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.'
}
];
const initialValues = {};
const validationSchema = Yup.object({
// splitSeparator: Yup.string().required('The separator is required')
});
export default function FindMostPopular() {
return <Box>Lorem ipsum</Box>;
}
const [input, setInput] = useState<string>('');
const [result, setResult] = useState<string>('');
const compute = (optionsValues: typeof initialValues, input: any) => {
const {
splitSeparatorType,
splitSeparator,
displayFormat,
sortingMethod,
deleteEmptyItems,
ignoreItemCase,
trimItems
} = optionsValues;
setResult(
TopItemsList(
splitSeparatorType,
sortingMethod,
displayFormat,
splitSeparator,
input,
deleteEmptyItems,
ignoreItemCase,
trimItems
)
);
};
const validationSchema = Yup.object({
// splitSeparator: Yup.string().required('The separator is required')
});
return (
<Box>
<ToolInputAndResult
input={
<ToolTextInput
title={'Input list'}
value={input}
onChange={setInput}
/>
}
result={<ToolTextResult title={'Most popular items'} value={result} />}
/>
<ToolOptions
compute={compute}
getGroups={({ values, updateField }) => [
{
title: 'How to Extract List Items?',
component: (
<Box>
{splitOperators.map(({ title, description, type }) => (
<SimpleRadio
key={type}
onClick={() => updateField('splitSeparatorType', type)}
title={title}
description={description}
checked={values.splitSeparatorType === type}
/>
))}
<TextFieldWithDesc
description={'Set a delimiting symbol or regular expression.'}
value={values.splitSeparator}
onOwnChange={(val) => updateField('splitSeparator', val)}
/>
</Box>
)
},
{
title: 'Item comparison',
component: (
<Box>
<CheckboxWithDesc
title={'Remove empty items'}
description={'Ignore empty items from comparison.'}
checked={values.deleteEmptyItems}
onChange={(value) => updateField('deleteEmptyItems', 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={'Ignore Item Case'}
description={'Compare all list items in lowercase.'}
checked={values.ignoreItemCase}
onChange={(value) => updateField('ignoreItemCase', value)}
/>
</Box>
)
},
{
title: 'Top item output format',
component: (
<Box>
<SelectWithDesc
selected={values.displayFormat}
options={[
{ label: 'Show item percentage', value: 'percentage' },
{ label: 'Show item count', value: 'count' },
{ label: 'Show item total', value: 'total' }
]}
onChange={(value) => updateField('displayFormat', value)}
description={'How to display the most popular list items?'}
/>
<SelectWithDesc
selected={values.sortingMethod}
options={[
{ label: 'Sort Alphabetically', value: 'alphabetic' },
{ label: 'Sort by count', value: 'count' }
]}
onChange={(value) => updateField('sortingMethod', value)}
description={'Select a sorting method.'}
/>
</Box>
)
}
]}
initialValues={initialValues}
input={input}
validationSchema={validationSchema}
/>
</Box>
);
}

View File

@@ -3,105 +3,112 @@ export type DisplayFormat = 'count' | 'percentage' | 'total';
export type SortingMethod = 'count' | 'alphabetic';
// Function that takes the array as arg and returns a dict of element occurrences and handle the ignoreItemCase
function dictMaker(array: string[],
ignoreItemCase: boolean
function dictMaker(
array: string[],
ignoreItemCase: boolean
): { [key: string]: number } {
const dict: { [key: string]: number } = {};
for (const item of array) {
const key = ignoreItemCase ? item.toLowerCase() : item;
dict[key] = (dict[key] || 0) + 1;
}
return dict;
const dict: { [key: string]: number } = {};
for (const item of array) {
const key = ignoreItemCase ? item.toLowerCase() : item;
dict[key] = (dict[key] || 0) + 1;
}
return dict;
}
// Function that sorts the dict created with dictMaker based on the chosen sorting method
function dictSorter(
dict: { [key: string]: number },
sortingMethod: SortingMethod,
dict: { [key: string]: number },
sortingMethod: SortingMethod
): { [key: string]: number } {
let sortedArray: [string, number][];
switch (sortingMethod) {
case 'count':
sortedArray = Object.entries(dict).sort(([, countA], [, countB]) => countB - countA);
break;
case 'alphabetic':
sortedArray = Object.entries(dict).sort(([keyA], [keyB]) => {
return keyA.localeCompare(keyB)
});
break;
default:
sortedArray = Object.entries(dict);
break;
}
return Object.fromEntries(sortedArray);
let sortedArray: [string, number][];
switch (sortingMethod) {
case 'count':
sortedArray = Object.entries(dict).sort(
([, countA], [, countB]) => countB - countA
);
break;
case 'alphabetic':
sortedArray = Object.entries(dict).sort(([keyA], [keyB]) => {
return keyA.localeCompare(keyB);
});
break;
default:
sortedArray = Object.entries(dict);
break;
}
return Object.fromEntries(sortedArray);
}
// Function that prepares the output of dictSorter based on the chosen display format
function displayFormater(
dict: { [key: string]: number },
displayFormat: DisplayFormat
dict: { [key: string]: number },
displayFormat: DisplayFormat
): string[] {
let formattedOutput: string[] = [];
const total = Object.values(dict).reduce((acc, val) => acc + val, 0);
let formattedOutput: string[] = [];
const total = Object.values(dict).reduce((acc, val) => acc + val, 0);
switch (displayFormat) {
case 'percentage':
Object.entries(dict).forEach(([key, value]) => {
formattedOutput.push(`${key}: ${value} (${((value / total) * 100).toFixed(2)}%)`);
});
break;
case "total":
Object.entries(dict).forEach(([key, value]) => {
formattedOutput.push(`${key}: ${value} (${value} / ${total})`);
});
break;
case "count":
Object.entries(dict).forEach(([key, value]) => {
formattedOutput.push(`${key}: ${value}`);
});
break;
}
return formattedOutput;
switch (displayFormat) {
case 'percentage':
Object.entries(dict).forEach(([key, value]) => {
formattedOutput.push(
`${key}: ${value} (${((value / total) * 100).toFixed(2)}%)`
);
});
break;
case 'total':
Object.entries(dict).forEach(([key, value]) => {
formattedOutput.push(`${key}: ${value} (${value} / ${total})`);
});
break;
case 'count':
Object.entries(dict).forEach(([key, value]) => {
formattedOutput.push(`${key}: ${value}`);
});
break;
}
return formattedOutput;
}
export function TopItemsList(
splitOperatorType: SplitOperatorType,
sortingMethod: SortingMethod,
displayFormat: DisplayFormat,
splitSeparator: string,
input: string,
deleteEmptyItems: boolean,
ignoreItemCase: boolean,
trimItems: boolean
splitOperatorType: SplitOperatorType,
sortingMethod: SortingMethod,
displayFormat: DisplayFormat,
splitSeparator: string,
input: string,
deleteEmptyItems: boolean,
ignoreItemCase: boolean,
trimItems: 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;
}
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());
}
// 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 !== '');
}
// Delete empty items after initial split
if (deleteEmptyItems) {
array = array.filter((item) => item !== '');
}
// Transform the array into dict
const unsortedDict = dictMaker(array, ignoreItemCase);
// Transform the array into dict
const unsortedDict = dictMaker(array, ignoreItemCase);
// Sort the list if required
const sortedDict = dictSorter(unsortedDict, sortingMethod);
// Sort the list if required
const sortedDict = dictSorter(unsortedDict, sortingMethod);
// Format the output with desired format
const formattedOutput = displayFormater(sortedDict, displayFormat);
// Format the output with desired format
const formattedOutput = displayFormater(sortedDict, displayFormat);
return formattedOutput.join('\n');
return formattedOutput.join('\n');
}

View File

@@ -10,4 +10,15 @@ import { tool as listTruncate } from './truncate/meta';
import { tool as listShuffle } from './shuffle/meta';
import { tool as listSort } from './sort/meta';
export const listTools = [listSort];
export const listTools = [
listSort,
listUnwrap,
listReverse,
listFindUnique,
listFindMostPopular,
listGroup,
listWrap,
listRotate,
listShuffle,
listTruncate
];

View File

@@ -8,6 +8,8 @@ import { Sort, SortingMethod, 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';
import SelectWithDesc from '../../../components/options/SelectWithDesc';
const initialValues = {
splitSeparatorType: 'symbol' as SplitOperatorType,
@@ -38,8 +40,7 @@ const splitOperators: {
export default function SplitText() {
const [input, setInput] = useState<string>('');
const [result, setResult] = useState<string>('');
// const formRef = useRef<FormikProps<typeof initialValues>>(null);
const computeExternal = (optionsValues: typeof initialValues, input: any) => {
const compute = (optionsValues: typeof initialValues, input: any) => {
const {
splitSeparatorType,
joinSeparator,
@@ -80,7 +81,7 @@ export default function SplitText() {
result={<ToolTextResult title={'Sorted list'} value={result} />}
/>
<ToolOptions
compute={computeExternal}
compute={compute}
getGroups={({ values, updateField }) => [
{
title: 'Input item separator',
@@ -105,7 +106,59 @@ export default function SplitText() {
},
{
title: 'Sort method',
component: <Box></Box>
component: (
<Box>
<SelectWithDesc
selected={values.sortingMethod}
options={[
{ label: 'Sort Alphabetically', value: 'alphabetic' },
{ label: 'Sort Numerically', value: 'numeric' },
{ label: 'Sort by Length', value: 'length' }
]}
onChange={(value) => updateField('sortingMethod', value)}
description={'Select a sorting method.'}
/>
<SelectWithDesc
selected={values.increasing}
options={[
{ label: 'Increasing order', value: true },
{ label: 'Decreasing order', value: false }
]}
onChange={(value) => {
updateField('increasing', value);
}}
description={'Select a sorting order.'}
/>
<CheckboxWithDesc
title={'Case Sensitive Sort'}
description={
'Sort uppercase and lowercase items separately. Capital letters precede lowercase letters in an ascending list. (Works only in alphabetical sorting mode.)'
}
checked={values.caseSensitive}
onChange={(val) => updateField('caseSensitive', val)}
/>
</Box>
)
},
{
title: 'Sorted item properties',
component: (
<Box>
<TextFieldWithDesc
description={
'Use this symbol as a joiner between items in a sorted list.'
}
value={values.joinSeparator}
onOwnChange={(val) => updateField('joinSeparator', val)}
/>
<CheckboxWithDesc
title={'Remove duplicates'}
description={'Delete duplicate list items.'}
checked={values.removeDuplicated}
onChange={(val) => updateField('removeDuplicated', val)}
/>
</Box>
)
}
]}
initialValues={initialValues}