fix: misc

This commit is contained in:
Ibrahima G. Coulibaly
2024-06-27 18:55:20 +01:00
parent 1903d316e0
commit dcac733acf
7 changed files with 383 additions and 349 deletions

View File

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

View File

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

View File

@@ -1,117 +1,123 @@
import { isNumber } from "utils/string";
import { isNumber } from 'utils/string';
export type SortingMethod = 'numeric' | 'alphabetic' | 'length';
export type SplitOperatorType = 'symbol' | 'regex';
// utils function that choose the way of numeric sorting mixed types of array
function customNumericSort(a: string, b: string, increasing: boolean): number {
const formattedA = isNumber(a) ? Number(a) : a;
const formattedB = isNumber(b) ? Number(b) : b;
if (typeof formattedA === 'number' && typeof formattedB === 'number') {
let result: number = increasing ? (formattedA - formattedB) : (formattedB - formattedA);
return result;
} else if (typeof formattedA === 'string' && typeof formattedB === 'string') {
return formattedA.localeCompare(formattedB); // Lexicographical comparison for strings
} else if (typeof formattedA === 'number' && typeof formattedB === 'string') {
return -1; // Numbers before strings
} else {
return 1; // Strings after numbers
}
const formattedA = isNumber(a) ? Number(a) : a;
const formattedB = isNumber(b) ? Number(b) : b;
if (typeof formattedA === 'number' && typeof formattedB === 'number') {
return increasing ? formattedA - formattedB : formattedB - formattedA;
} else if (typeof formattedA === 'string' && typeof formattedB === 'string') {
return formattedA.localeCompare(formattedB); // Lexicographical comparison for strings
} else if (typeof formattedA === 'number' && typeof formattedB === 'string') {
return -1; // Numbers before strings
} else {
return 1; // Strings after numbers
}
}
export function numericSort(
array: string[], // array we build after parsing the input
increasing: boolean,
joinSeparator: string,
removeDuplicated: boolean // the value if the checkbox has been selected 1 else 0
array: string[], // array we build after parsing the input
increasing: boolean,
joinSeparator: string,
removeDuplicated: boolean // the value if the checkbox has been selected 1 else 0
) {
array.sort((a, b) => customNumericSort(a, b, increasing));
if (removeDuplicated) {
array = array.filter((item, index) => array.indexOf(item) === index);
}
return array.join(joinSeparator);
array.sort((a, b) => customNumericSort(a, b, increasing));
if (removeDuplicated) {
array = array.filter((item, index) => array.indexOf(item) === index);
}
return array.join(joinSeparator);
}
// utils function that choose the way of numeric sorting mixed types of array
function customLengthSort(a: string, b: string, increasing: boolean): number {
let result: number = increasing ? (a.length - b.length) : (b.length - a.length);
return result;
return increasing ? a.length - b.length : b.length - a.length;
}
export function lengthSort(
array: string[], // array we build after parsing the input
increasing: boolean, // select value has to be increasing for increasing order and decreasing for decreasing order
joinSeparator: string,
removeDuplicated: boolean // the value if the checkbox has been selected 1 else 0
array: string[], // array we build after parsing the input
increasing: boolean, // select value has to be increasing for increasing order and decreasing for decreasing order
joinSeparator: string,
removeDuplicated: boolean // the value if the checkbox has been selected 1 else 0
) {
array.sort((a, b) => customLengthSort(a, b, increasing));
if (removeDuplicated) {
array = array.filter((item, index) => array.indexOf(item) === index);
}
return array.join(joinSeparator);
array.sort((a, b) => customLengthSort(a, b, increasing));
if (removeDuplicated) {
array = array.filter((item, index) => array.indexOf(item) === index);
}
return array.join(joinSeparator);
}
// Utils function that chooses the way of alphabetic sorting mixed types of array
function customAlphabeticSort(a: string, b: string, caseSensitive: boolean): number {
if (!caseSensitive) {
// Case-insensitive comparison
return a.toLowerCase().localeCompare(b.toLowerCase());
} else {
// Case-sensitive comparison
return a.charCodeAt(0) - b.charCodeAt(0);
}
function customAlphabeticSort(
a: string,
b: string,
caseSensitive: boolean
): number {
if (!caseSensitive) {
// Case-insensitive comparison
return a.toLowerCase().localeCompare(b.toLowerCase());
} else {
// Case-sensitive comparison
return a.charCodeAt(0) - b.charCodeAt(0);
}
}
export function alphabeticSort(
array: string[], // array we build after parsing the input
increasing: boolean, // select value has to be "increasing" for increasing order and "decreasing" for decreasing order
joinSeparator: string,
removeDuplicated: boolean, // the value if the checkbox has been selected 1 else 0
caseSensitive: boolean // the value if the checkbox has been selected 1 else 0
)
{
array.sort((a, b) => customAlphabeticSort(a, b, caseSensitive));
if (!increasing){
array.reverse();
}
if (removeDuplicated) {
array = array.filter((item, index) => array.indexOf(item) === index);
}
return array.join(joinSeparator);
}
// main function
export function Sort(
sortingMethod: SortingMethod,
splitOperatorType: SplitOperatorType,
input: string,
increasing: boolean,
splitSeparator: string,
joinSeparator: string,
removeDuplicated: boolean,
caseSensitive: boolean
array: string[], // array we build after parsing the input
increasing: boolean, // select value has to be "increasing" for increasing order and "decreasing" for decreasing order
joinSeparator: string,
removeDuplicated: boolean, // the value if the checkbox has been selected 1 else 0
caseSensitive: boolean // the value if the checkbox has been selected 1 else 0
) {
let array: string[];
switch(splitOperatorType) {
case 'symbol':
array = input.split(splitSeparator);
break;
case 'regex':
array = input.split(new RegExp(splitSeparator));
break;
}
let result : string;
switch(sortingMethod) {
case 'numeric':
result = numericSort(array, increasing, joinSeparator, removeDuplicated);
break;
case 'length':
result = lengthSort(array, increasing, joinSeparator, removeDuplicated);
break;
case 'alphabetic':
result = alphabeticSort(array, increasing, joinSeparator, removeDuplicated, caseSensitive);
break;
}
return result;
array.sort((a, b) => customAlphabeticSort(a, b, caseSensitive));
if (!increasing) {
array.reverse();
}
if (removeDuplicated) {
array = array.filter((item, index) => array.indexOf(item) === index);
}
return array.join(joinSeparator);
}
// main function
export function Sort(
sortingMethod: SortingMethod,
splitOperatorType: SplitOperatorType,
input: string,
increasing: boolean,
splitSeparator: string,
joinSeparator: string,
removeDuplicated: boolean,
caseSensitive: boolean
) {
let array: string[];
switch (splitOperatorType) {
case 'symbol':
array = input.split(splitSeparator);
break;
case 'regex':
array = input.split(new RegExp(splitSeparator));
break;
}
let result: string;
switch (sortingMethod) {
case 'numeric':
result = numericSort(array, increasing, joinSeparator, removeDuplicated);
break;
case 'length':
result = lengthSort(array, increasing, joinSeparator, removeDuplicated);
break;
case 'alphabetic':
result = alphabeticSort(
array,
increasing,
joinSeparator,
removeDuplicated,
caseSensitive
);
break;
}
return result;
}

View File

@@ -1,39 +1,92 @@
// Import necessary modules and functions
import { describe, it, expect } from 'vitest';
import {
alphabeticSort, lengthSort, numericSort, Sort,
SplitOperatorType, SortingMethod
alphabeticSort,
lengthSort,
numericSort,
Sort,
SplitOperatorType,
SortingMethod
} from './service';
// Define test cases for the numericSort function
describe('numericSort function', () => {
it('should sort a list in increasing order with comma separator not removeduplicated elements', () => {
const array: string[] = ['9', '8', '7', '4', '2', '2', '5'];
const increasing: boolean = true;
const separator = ', ';
const removeDuplicated: boolean = false;
it('should sort a list in increasing order with comma separator not removeduplicated elements', () => {
const array: string[] = ['9', '8', '7', '4', '2', '2', '5'];
const increasing: boolean = true;
const separator = ', ';
const removeDuplicated: boolean = false;
const result = numericSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('2, 2, 4, 5, 7, 8, 9');
});
const result = numericSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('2, 2, 4, 5, 7, 8, 9');
});
it('should sort a list in decreasing order with " - " separator and remove duplicated elements', () => {
const array: string[] = ['2', '4', '4', '9', '6', '6', '7'];
const increasing: boolean = false;
const separator = ' - ';
const removeDuplicated: boolean = true;
it('should sort a list in decreasing order with " - " separator and remove duplicated elements', () => {
const array: string[] = ['2', '4', '4', '9', '6', '6', '7'];
const increasing: boolean = false;
const separator = ' - ';
const removeDuplicated: boolean = true;
const result = numericSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('9 - 7 - 6 - 4 - 2');
});
it('should sort a list with numbers and characters and remove duplicated elements', () => {
const array: string[] = ['d', 'd', 'n', 'p', 'h', 'h', '6', '9', '7', '5'];
const increasing: boolean = true;
const separator = ' ';
const removeDuplicated: boolean = true;
const result = numericSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('5 6 7 9 d h n p');
});
// Define test cases for the lengthSort function
describe('lengthSort function', () => {
it('should sort a list of number by length in increasing order with comma separator ', () => {
const array: string[] = ['415689521', '3', '126', '12', '1523'];
const increasing: boolean = true;
const separator = ', ';
const removeDuplicated: boolean = false;
const result = lengthSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('3, 12, 126, 1523, 415689521');
});
it('should sort a list with numbers and characters and remove duplicated elements', () => {
const array: string[] = ['d', 'd', 'n', 'p', 'h', 'h', '6', '9', '7', '5'];
const increasing: boolean = true;
const separator = ' ';
const removeDuplicated: boolean = true;
it('should sort a list of number by length in increasing order and remove duplicated elements ', () => {
const array: string[] = [
'415689521',
'3',
'3',
'126',
'12',
'12',
'1523'
];
const increasing: boolean = true;
const separator = ', ';
const removeDuplicated: boolean = true;
const result = lengthSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('3, 12, 126, 1523, 415689521');
});
it('should sort a mixed array by length in increasing order ', () => {
const array: string[] = [
'ddd',
'd',
'nfg',
'p',
'h',
'h',
'6555',
'9',
'7',
'5556'
];
const increasing: boolean = true;
const separator = ' ';
const removeDuplicated: boolean = true;
const result = lengthSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('d p h 9 7 ddd nfg 6555 5556');
@@ -44,7 +97,7 @@ describe('numericSort function', () => {
describe('alphabeticSort function', () => {
// NON CASE SENSITIVE TEST
it('should sort a list of string in increasing order with comma separator ', () => {
const array: any[] = ['apple', 'pineaple', 'lemon', 'orange'];
const array: string[] = ['apple', 'pineaple', 'lemon', 'orange'];
const increasing: boolean = true;
const separator = ', ';
const removeDuplicated: boolean = false;
@@ -60,47 +113,31 @@ describe('numericSort function', () => {
expect(result).toBe('apple, lemon, orange, pineaple');
});
// Define test cases for the lengthSort function
describe('lengthSort function', () => {
it('should sort a list of number by length in increasing order with comma separator ', () => {
const array: string[] = ['415689521', '3', '126', '12', '1523'];
const increasing: boolean = true;
const separator = ', ';
const removeDuplicated: boolean = false;
const result = lengthSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('3, 12, 126, 1523, 415689521');
});
it('should sort a list of number by length in increasing order and remove duplicated elements ', () => {
const array: string[] = ['415689521', '3', '3', '126', '12', '12', '1523'];
const increasing: boolean = true;
const separator = ', ';
const removeDuplicated: boolean = true;
const result = lengthSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('3, 12, 126, 1523, 415689521');
});
it('should sort a mixed array by length in increasing order ', () => {
const array: string[] = ['ddd', 'd', 'nfg', 'p', 'h', 'h', '6555', '9', '7', '5556'];
const increasing: boolean = true;
const separator = ' ';
const removeDuplicated: boolean = true;
const result = lengthSort(array, increasing, separator, removeDuplicated);
expect(result).toBe('d p h 9 7 ddd nfg 6555 5556');
});
it('should sort a list of string in decreasing order with comma separator ', () => {
const array: string[] = ['apple', 'pineaple', 'lemon', 'orange'];
const increasing: boolean = false;
const separator = ', ';
const removeDuplicated: boolean = false;
const caseSensitive: boolean = false;
const result = alphabeticSort(
array,
increasing,
separator,
removeDuplicated,
caseSensitive
);
expect(result).toBe('pineaple, orange, lemon, apple');
});
it('should sort a list of string and symbols (uppercase and lower) in increasing order with comma separator ', () => {
const array: any[] = [
const array: string[] = [
'Apple',
'pineaple',
'lemon',
'Orange',
1,
9,
'1',
'9',
'@',
'+'
];
@@ -120,13 +157,13 @@ describe('numericSort function', () => {
});
it('should sort a list of string and symbols (uppercase and lower) in decreasing order with comma separator ', () => {
const array: any[] = [
const array: string[] = [
'Apple',
'pineaple',
'lemon',
'Orange',
1,
9,
'1',
'9',
'@',
'+'
];
@@ -145,150 +182,80 @@ describe('numericSort function', () => {
expect(result).toBe('pineaple Orange lemon Apple 9 1 + @');
});
// Define test cases for the alphabeticSort function
describe('alphabeticSort function', () => {
// NON CASE SENSITIVE TEST
it('should sort a list of string in increasing order with comma separator ', () => {
const array: string[] = ['apple', 'pineaple', 'lemon', 'orange'];
const increasing: boolean = true;
const separator = ', ';
const removeDuplicated: boolean = false;
const caseSensitive: boolean = false;
// CASE SENSITIVE TEST
it('should sort a list of string (uppercase) in decreasing order with comma separator ', () => {
const array: string[] = ['Apple', 'Pineaple', 'Lemon', 'Orange'];
const increasing: boolean = false;
const separator = ' ';
const removeDuplicated: boolean = false;
const caseSensitive: boolean = true;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('apple, lemon, orange, pineaple');
});
it('should sort a list of string in decreasing order with comma separator ', () => {
const array: string[] = ['apple', 'pineaple', 'lemon', 'orange'];
const increasing: boolean = false;
const separator = ', ';
const removeDuplicated: boolean = false;
const caseSensitive: boolean = false;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('pineaple, orange, lemon, apple');
});
it('should sort a list of string and symbols (uppercase and lower) in increasing order with comma separator ', () => {
const array: string[] = ['Apple', 'pineaple', 'lemon', 'Orange', '1', '9', '@', '+'];
const increasing: boolean = true;
const separator = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = false;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('@ + 1 9 Apple lemon Orange pineaple');
});
it('should sort a list of string and symbols (uppercase and lower) in decreasing order with comma separator ', () => {
const array: string[] = ['Apple', 'pineaple', 'lemon', 'Orange', '1', '9', '@', '+'];
const increasing: boolean = false;
const separator = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = false;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('pineaple Orange lemon Apple 9 1 + @');
});
// CASE SENSITIVE TEST
it('should sort a list of string (uppercase) in decreasing order with comma separator ', () => {
const array: string[] = ['Apple', 'Pineaple', 'Lemon', 'Orange'];
const increasing: boolean = false;
const separator = ' ';
const removeDuplicated: boolean = false;
const caseSensitive: boolean = true;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('Pineaple Orange Lemon Apple');
});
it('should sort a list of string (uppercase and lowercase) in increasing order with comma separator ', () => {
const array: string[] = ['Apple', 'pineaple', 'lemon', 'Orange', '1', '9'];
const increasing: boolean = true;
const separator = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('1 9 Apple Orange lemon pineaple');
});
it('should sort a list of string (uppercase and lower) in decreasing order with comma separator ', () => {
const array: string[] = ['Apple', 'pineaple', 'lemon', 'Orange', '1', '9'];
const increasing: boolean = false;
const separator = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('pineaple lemon Orange Apple 9 1');
});
it('should sort a list of string and symbols (uppercase and lower) in decreasing order with comma separator ', () => {
const array: string[] = ['Apple', 'pineaple', 'lemon', 'Orange', '1', '9', '@', '+'];
const increasing: boolean = true;
const separator = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('+ 1 9 @ Apple Orange lemon pineaple');
});
it('should sort a list of string and symbols (uppercase and lower) in decreasing order with comma separator ', () => {
const array: string[] = ['Apple', 'pineaple', 'lemon', 'Orange', '1', '9', '@', '+'];
const increasing: boolean = false;
const separator = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = alphabeticSort(array, increasing, separator, removeDuplicated, caseSensitive);
expect(result).toBe('pineaple lemon Orange Apple @ 9 1 +');
});
const result = alphabeticSort(
array,
increasing,
separator,
removeDuplicated,
caseSensitive
);
expect(result).toBe('Pineaple Orange Lemon Apple');
});
// Define test cases for the lengthSort function
describe('main function', () => {
it('should do everything alph', () => {
const sortingMethod: SortingMethod = 'alphabetic';
const splitOperatorType : SplitOperatorType = 'symbol';
const input: string = 'Apple pineaple lemon Orange 1 9 @ +';
const increasing: boolean = true;
const splitSeparator: string = ' ';
const joinSeparator: string = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = Sort(sortingMethod, splitOperatorType, input, increasing, splitSeparator, joinSeparator, removeDuplicated, caseSensitive);
expect(result).toBe('+ 1 9 @ Apple Orange lemon pineaple');
});
it('should do everything numeric', () => {
const sortingMethod: SortingMethod = 'numeric';
const splitOperatorType : SplitOperatorType = 'symbol';
const input: string = '1 6 9 4 6 7 3 5 8';
const increasing: boolean = true;
const splitSeparator: string = ' ';
const joinSeparator: string = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = Sort(sortingMethod, splitOperatorType, input, increasing, splitSeparator, joinSeparator, removeDuplicated, caseSensitive);
expect(result).toBe('1 3 4 5 6 7 8 9');
});
it('should sort a list of string and symbols (uppercase and lower) in decreasing order with comma separator ', () => {
const array: any[] = [
it('should sort a list of string (uppercase and lowercase) in increasing order with comma separator ', () => {
const array: string[] = [
'Apple',
'pineaple',
'lemon',
'Orange',
1,
9,
'1',
'9'
];
const increasing: boolean = true;
const separator = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = alphabeticSort(
array,
increasing,
separator,
removeDuplicated,
caseSensitive
);
expect(result).toBe('1 9 Apple Orange lemon pineaple');
});
it('should sort a list of string (uppercase and lower) in decreasing order with comma separator ', () => {
const array: string[] = [
'Apple',
'pineaple',
'lemon',
'Orange',
'1',
'9'
];
const increasing: boolean = false;
const separator = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = alphabeticSort(
array,
increasing,
separator,
removeDuplicated,
caseSensitive
);
expect(result).toBe('pineaple lemon Orange Apple 9 1');
});
it('should sort a list of string and symbols (uppercase and lower) in decreasing order with comma separator ', () => {
const array: string[] = [
'Apple',
'pineaple',
'lemon',
'Orange',
'1',
'9',
'@',
'+'
];
@@ -308,13 +275,13 @@ describe('numericSort function', () => {
});
it('should sort a list of string and symbols (uppercase and lower) in decreasing order with comma separator ', () => {
const array: any[] = [
const array: string[] = [
'Apple',
'pineaple',
'lemon',
'Orange',
1,
9,
'1',
'9',
'@',
'+'
];
@@ -332,5 +299,54 @@ describe('numericSort function', () => {
);
expect(result).toBe('pineaple lemon Orange Apple @ 9 1 +');
});
});
// Define test cases for the lengthSort function
describe('main function', () => {
it('should do everything alph', () => {
const sortingMethod: SortingMethod = 'alphabetic';
const splitOperatorType: SplitOperatorType = 'symbol';
const input: string = 'Apple pineaple lemon Orange 1 9 @ +';
const increasing: boolean = true;
const splitSeparator: string = ' ';
const joinSeparator: string = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = Sort(
sortingMethod,
splitOperatorType,
input,
increasing,
splitSeparator,
joinSeparator,
removeDuplicated,
caseSensitive
);
expect(result).toBe('+ 1 9 @ Apple Orange lemon pineaple');
});
it('should do everything numeric', () => {
const sortingMethod: SortingMethod = 'numeric';
const splitOperatorType: SplitOperatorType = 'symbol';
const input: string = '1 6 9 4 6 7 3 5 8';
const increasing: boolean = true;
const splitSeparator: string = ' ';
const joinSeparator: string = ' ';
const removeDuplicated: boolean = true;
const caseSensitive: boolean = true;
const result = Sort(
sortingMethod,
splitOperatorType,
input,
increasing,
splitSeparator,
joinSeparator,
removeDuplicated,
caseSensitive
);
expect(result).toBe('1 3 4 5 6 7 8 9');
});
});
});

View File

@@ -5,4 +5,4 @@ export function capitalizeFirstLetter(string: string | undefined) {
export function isNumber(number: any) {
return !isNaN(parseFloat(number)) && isFinite(number);
}
}