find-unique tool and testcases the updated index file

This commit is contained in:
Chesterkxng
2024-07-07 20:48:04 +00:00
parent 375a10b366
commit b81de605a1
5 changed files with 130 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
import { expect, describe, it } from 'vitest';
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);
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);
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);
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);
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);
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);
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);
expect(result).toBe('apple\nbanana\norange');
});
});

View File

@@ -0,0 +1,11 @@
import { Box } from '@mui/material';
import React from 'react';
import * as Yup from 'yup';
const initialValues = {};
const validationSchema = Yup.object({
// splitSeparator: Yup.string().required('The separator is required')
});
export default function FindUnique() {
return <Box>Lorem ipsum</Box>;
}

View 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'))
});

View File

@@ -0,0 +1,58 @@
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 TopItemsList(
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);
}

View File

@@ -1,3 +1,4 @@
import { tool as listFindUnique } from './find-unique/meta';
import { tool as listFindMostPopular } from './find-most-popular/meta';
import { tool as listGroup } from './group/meta';
import { tool as listWrap } from './wrap/meta';