diff --git a/src/pages/list/index.ts b/src/pages/list/index.ts index c359bb0..a98cbb6 100644 --- a/src/pages/list/index.ts +++ b/src/pages/list/index.ts @@ -1,3 +1,4 @@ +import { tool as listWrap } from './wrap/meta'; import { tool as listRotate } from './rotate/meta'; import { tool as listTruncate } from './truncate/meta'; import { tool as listShuffle } from './shuffle/meta'; diff --git a/src/pages/list/wrap/index.tsx b/src/pages/list/wrap/index.tsx new file mode 100644 index 0000000..ff4c040 --- /dev/null +++ b/src/pages/list/wrap/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 Wrap() { + return Lorem ipsum; +} \ No newline at end of file diff --git a/src/pages/list/wrap/meta.ts b/src/pages/list/wrap/meta.ts new file mode 100644 index 0000000..5a394d0 --- /dev/null +++ b/src/pages/list/wrap/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: 'Wrap', + path: 'wrap', + // image, + description: '', + shortDescription: '', + keywords: ['wrap'], + component: lazy(() => import('./index')) +}); \ No newline at end of file diff --git a/src/pages/list/wrap/service.ts b/src/pages/list/wrap/service.ts new file mode 100644 index 0000000..d7bd8d5 --- /dev/null +++ b/src/pages/list/wrap/service.ts @@ -0,0 +1,27 @@ +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, + 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; + } + wrappedArray = wrap(array, left, right); + return wrappedArray.join(joinSeparator); +} diff --git a/src/pages/list/wrap/wrap.service.test.ts b/src/pages/list/wrap/wrap.service.test.ts new file mode 100644 index 0000000..cd20a8e --- /dev/null +++ b/src/pages/list/wrap/wrap.service.test.ts @@ -0,0 +1,84 @@ +import { expect, describe, 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 result = wrapList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + ); + + 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 result = wrapList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + 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 result = wrapList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + 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 left = 'K'; + const right = 'z'; + + const result = wrapList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + left, + right); + + expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz'); + + }); + + +}) \ No newline at end of file