diff --git a/src/pages/list/index.ts b/src/pages/list/index.ts index 17134e5..388e39c 100644 --- a/src/pages/list/index.ts +++ b/src/pages/list/index.ts @@ -1,3 +1,4 @@ +import { tool as listUnwrap } from './unwrap/meta'; import { tool as listReverse } from './reverse/meta'; import { tool as listFindUnique } from './find-unique/meta'; import { tool as listFindMostPopular } from './find-most-popular/meta'; diff --git a/src/pages/list/unwrap/index.tsx b/src/pages/list/unwrap/index.tsx new file mode 100644 index 0000000..43db753 --- /dev/null +++ b/src/pages/list/unwrap/index.tsx @@ -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 Lorem ipsum; +} \ No newline at end of file diff --git a/src/pages/list/unwrap/meta.ts b/src/pages/list/unwrap/meta.ts new file mode 100644 index 0000000..a65c5b5 --- /dev/null +++ b/src/pages/list/unwrap/meta.ts @@ -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')) +}); \ No newline at end of file diff --git a/src/pages/list/unwrap/service.ts b/src/pages/list/unwrap/service.ts new file mode 100644 index 0000000..d66a107 --- /dev/null +++ b/src/pages/list/unwrap/service.ts @@ -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); +} diff --git a/src/pages/list/unwrap/unwrap.service.test.ts b/src/pages/list/unwrap/unwrap.service.test.ts new file mode 100644 index 0000000..29e12c3 --- /dev/null +++ b/src/pages/list/unwrap/unwrap.service.test.ts @@ -0,0 +1,70 @@ +import { expect, describe, 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"); + }); +}); \ No newline at end of file