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

View File

@@ -0,0 +1,69 @@
export type SplitOperatorType = 'symbol' | 'regex';
function leftUnwrap(
row: string,
left: string = '',
multiLevel: boolean
): string {
if (left === '') return row; // Prevent infinite loop if left is an empty string
while (row.startsWith(left)) {
row = row.slice(left.length);
if (!multiLevel) {
break;
}
}
return row;
}
function rightUnwrap(
row: string,
right: string = '',
multiLevel: boolean
): string {
if (right === '') return row; // Prevent infinite loop if right is an empty string
while (row.endsWith(right)) {
row = row.slice(0, row.length - right.length);
if (!multiLevel) {
break;
}
}
return row;
}
export function unwrapList(
splitOperatorType: SplitOperatorType,
input: string,
splitSeparator: string,
joinSeparator: string,
deleteEmptyItems: boolean,
multiLevel: boolean,
trimItems: boolean,
left: string = '',
right: string = ''
): string {
let array: string[];
let unwrappedArray: 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);
}
// for each element of array unwrap left side then right side and push the result to a final array
for (let row of array) {
row = leftUnwrap(row, left, multiLevel);
row = rightUnwrap(row, right, multiLevel);
unwrappedArray.push(row);
}
// trim items if needed
if (trimItems) {
unwrappedArray = unwrappedArray.map((item) => item.trim());
}
return unwrappedArray.join(joinSeparator);
}

View File

@@ -0,0 +1,170 @@
import { describe, expect, it } from 'vitest';
import { unwrapList } from './service';
describe('unwrapList function', () => {
it('should unwrap elements correctly with symbol split', () => {
const input = '##Hello##\n##World##';
const result = unwrapList(
'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
});
it('should unwrap elements correctly with regex split', () => {
const input = '##Hello##||##World##';
const result = unwrapList(
'regex',
input,
'\\|\\|',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
});
it('should handle multiple levels of unwrapping', () => {
const input = '###Hello###';
const result = unwrapList(
'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello');
});
it('should handle single level of unwrapping', () => {
const input = '###Hello###';
const result = unwrapList(
'symbol',
input,
'\n',
' ',
true,
false,
true,
'#',
'#'
);
expect(result).toBe('##Hello##');
});
it('should delete empty items', () => {
const input = '##Hello##\n\n##World##';
const result = unwrapList(
'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
});
it('should keep empty items if deleteEmptyItems is false', () => {
const input = '##Hello##\n\n##World##';
const result = unwrapList(
'symbol',
input,
'\n',
' ',
false,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
});
it('should trim items', () => {
const input = '## Hello ##\n## World ##';
const result = unwrapList(
'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
});
it('should handle no left or right unwrapping', () => {
const input = 'Hello\nWorld';
const result = unwrapList('symbol', input, '\n', ' ', true, true, true);
expect(result).toBe('Hello World');
});
it('should handle mixed levels of unwrapping', () => {
const input = '###Hello##\n#World###';
const result = unwrapList(
'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
});
it('should handle complex regex split', () => {
const input = '##Hello##||###World###||####Test####';
const result = unwrapList(
'regex',
input,
'\\|\\|',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World Test');
});
it('should handle different joinSeparator', () => {
const input = '##Hello##\n##World##';
const result = unwrapList(
'symbol',
input,
'\n',
'-',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello-World');
});
});