refactor: tools folder inside pages

This commit is contained in:
Ibrahima G. Coulibaly
2025-02-23 01:38:42 +01:00
parent 62f084eb45
commit 64936ab11f
117 changed files with 447 additions and 194 deletions

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 Wrap() {
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: 'Wrap',
path: 'wrap',
// image,
description: '',
shortDescription: '',
keywords: ['wrap'],
component: lazy(() => import('./index'))
});

View File

@@ -0,0 +1,31 @@
export type SplitOperatorType = 'symbol' | 'regex';
function wrap(array: string[], left: string, right: string): string[] {
return array.map((element) => left + element + right);
}
export function wrapList(
splitOperatorType: SplitOperatorType,
input: string,
splitSeparator: string,
joinSeparator: string,
deleteEmptyItems: boolean,
left: string = '',
right: string = ''
): string {
let array: string[];
let wrappedArray: string[];
switch (splitOperatorType) {
case 'symbol':
array = input.split(splitSeparator);
break;
case 'regex':
array = input.split(new RegExp(splitSeparator));
break;
}
if (deleteEmptyItems) {
array = array.filter(Boolean);
}
wrappedArray = wrap(array, left, right);
return wrappedArray.join(joinSeparator);
}

View File

@@ -0,0 +1,132 @@
import { describe, expect, it } from 'vitest';
import { SplitOperatorType, wrapList } from './service';
describe('wrap function', () => {
it('should return the same input if no left and right are blanked', () => {
const input: string = 'apple, pineaple, lemon, orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const deleteEmptyItems = false;
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
deleteEmptyItems
);
expect(result).toBe('apple, pineaple, lemon, orange, mango');
});
it('should append to left if defined', () => {
const input: string = 'apple, pineaple, lemon, orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const left = 'the ';
const deleteEmptyItems = false;
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
deleteEmptyItems,
left
);
expect(result).toBe(
'the apple, the pineaple, the lemon, the orange, the mango'
);
});
it('should append to right if defined', () => {
const input: string = 'apple, pineaple, lemon, orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const left = '';
const right = 'z';
const deleteEmptyItems = false;
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
deleteEmptyItems,
left,
right
);
expect(result).toBe('applez, pineaplez, lemonz, orangez, mangoz');
});
it('should append to both side if both defined', () => {
const input: string = 'apple, pineaple, lemon, orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const deleteEmptyItems = false;
const left = 'K';
const right = 'z';
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
deleteEmptyItems,
left,
right
);
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
});
it('should append to both side if both defined and not delete empty items', () => {
const input: string = 'apple, pineaple, lemon, orange, mango, ';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const deleteEmptyItems = false;
const left = 'K';
const right = 'z';
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
deleteEmptyItems,
left,
right
);
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz, Kz');
});
it('should append to both side if both defined and delete empty items', () => {
const input: string = 'apple, pineaple, lemon, , orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const deleteEmptyItems = true;
const left = 'K';
const right = 'z';
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
deleteEmptyItems,
left,
right
);
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
});
});