fix: style

This commit is contained in:
Ibrahima G. Coulibaly
2024-07-09 23:38:29 +01:00
parent e4c726c7b5
commit 7b021d0770
36 changed files with 1107 additions and 955 deletions

View File

@@ -5,43 +5,106 @@ import { TopItemsList } from './service';
describe('TopItemsList Function', () => {
test('should return unique items ignoring case sensitivity', () => {
const input = 'apple,banana,Apple,orange,Banana,apple';
const result = TopItemsList('symbol', ',', '\n', input, true, true, false, true);
const result = TopItemsList(
'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 = TopItemsList('symbol', ',', '\n', input, true, true, true, true);
const result = TopItemsList(
'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 = TopItemsList('symbol', ',', '\n', input, true, true, false, false);
const result = TopItemsList(
'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 = TopItemsList('symbol', ',', '\n', input, true, true, true, false);
const result = TopItemsList(
'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 = TopItemsList('symbol', ',', '\n', input, true, true, false, false);
const result = TopItemsList(
'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 = TopItemsList('symbol', ',', '\n', input, false, false, false, false);
const result = TopItemsList(
'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 = TopItemsList('regex', '\\s+', '\n', input, false, false, false, false);
const result = TopItemsList(
'regex',
'\\s+',
'\n',
input,
false,
false,
false,
false
);
expect(result).toBe('apple\nbanana\norange');
});
});

View File

@@ -8,4 +8,4 @@ const validationSchema = Yup.object({
});
export default function FindUnique() {
return <Box>Lorem ipsum</Box>;
}
}

View File

@@ -10,4 +10,4 @@ export const tool = defineTool('list', {
shortDescription: '',
keywords: ['find', 'unique'],
component: lazy(() => import('./index'))
});
});

View File

@@ -2,57 +2,63 @@ 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
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;
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];
}
}
if (absolutelyUnique) {
for (const [key, value] of Object.entries(dict)) {
if (value > 1) {
delete dict[key];
}
}
}
return Object.keys(dict);
}
return Object.keys(dict);
}
export function TopItemsList(
splitOperatorType: SplitOperatorType,
splitSeparator: string,
joinSeparator: string = '\n',
input: string,
deleteEmptyItems: boolean,
trimItems: boolean,
caseSensitive: boolean,
absolutelyUnique: boolean
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;
}
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 !== '');
}
// Format the output with desired format
const uniqueListItems = uniqueListBuilder(array, caseSensitive, absolutelyUnique);
// Format the output with desired format
const uniqueListItems = uniqueListBuilder(
array,
caseSensitive,
absolutelyUnique
);
return uniqueListItems.join(joinSeparator);
return uniqueListItems.join(joinSeparator);
}