Skip to content

Commit

Permalink
Merge pull request #20 from iib0011/truncate
Browse files Browse the repository at this point in the history
rotate service and test cases; then updated index file
  • Loading branch information
iib0011 authored Jul 9, 2024
2 parents bb80845 + 6ef2bd1 commit 6a18eb3
Show file tree
Hide file tree
Showing 29 changed files with 1,158 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/pages/list/find-most-popular/find-most-popular.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expect, describe, it } from 'vitest';
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 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 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 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'
);
});
});
11 changes: 11 additions & 0 deletions src/pages/list/find-most-popular/index.tsx
Original file line number Diff line number Diff line change
@@ -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 FindMostPopular() {
return <Box>Lorem ipsum</Box>;
}
13 changes: 13 additions & 0 deletions src/pages/list/find-most-popular/meta.ts
Original file line number Diff line number Diff line change
@@ -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 most popular',
path: 'find-most-popular',
// image,
description: '',
shortDescription: '',
keywords: ['find', 'most', 'popular'],
component: lazy(() => import('./index'))
});
107 changes: 107 additions & 0 deletions src/pages/list/find-most-popular/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
export type SplitOperatorType = 'symbol' | 'regex';
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
): { [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;
}

// Function that sorts the dict created with dictMaker based on the chosen sorting method
function dictSorter(
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);
}

// Function that prepares the output of dictSorter based on the chosen display format
function displayFormater(
dict: { [key: string]: number },
displayFormat: DisplayFormat
): string[] {
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;
}

export function TopItemsList(
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;
}

// 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 !== '');
}

// Transform the array into dict
const unsortedDict = dictMaker(array, ignoreItemCase);

// Sort the list if required
const sortedDict = dictSorter(unsortedDict, sortingMethod);

// Format the output with desired format
const formattedOutput = displayFormater(sortedDict, displayFormat);

return formattedOutput.join('\n');
}
47 changes: 47 additions & 0 deletions src/pages/list/find-unique/find-unique.service.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
11 changes: 11 additions & 0 deletions src/pages/list/find-unique/index.tsx
Original file line number Diff line number Diff line change
@@ -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>;
}
13 changes: 13 additions & 0 deletions src/pages/list/find-unique/meta.ts
Original file line number Diff line number Diff line change
@@ -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'))
});
58 changes: 58 additions & 0 deletions src/pages/list/find-unique/service.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit 6a18eb3

Please sign in to comment.