From 0064476c51dcd39d84cfe301d2aa00b0ccda09c1 Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Thu, 11 Jul 2024 00:01:59 +0000 Subject: [PATCH] reverse tool, testCases then updated index file --- src/pages/string/index.ts | 1 + src/pages/string/reverse/index.tsx | 11 ++++ src/pages/string/reverse/meta.ts | 13 +++++ .../string/reverse/reverse.service.test.ts | 52 +++++++++++++++++++ src/pages/string/reverse/service.ts | 31 +++++++++++ 5 files changed, 108 insertions(+) create mode 100644 src/pages/string/reverse/index.tsx create mode 100644 src/pages/string/reverse/meta.ts create mode 100644 src/pages/string/reverse/reverse.service.test.ts create mode 100644 src/pages/string/reverse/service.ts diff --git a/src/pages/string/index.ts b/src/pages/string/index.ts index ea2d978..f7eed37 100644 --- a/src/pages/string/index.ts +++ b/src/pages/string/index.ts @@ -1,3 +1,4 @@ +import { tool as stringReverse } from './reverse/meta'; import { tool as stringRandomizeCase } from './randomize-case/meta'; import { tool as stringUppercase } from './uppercase/meta'; import { tool as stringExtractSubstring } from './extract-substring/meta'; diff --git a/src/pages/string/reverse/index.tsx b/src/pages/string/reverse/index.tsx new file mode 100644 index 0000000..e017afe --- /dev/null +++ b/src/pages/string/reverse/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 Reverse() { + return Lorem ipsum; +} \ No newline at end of file diff --git a/src/pages/string/reverse/meta.ts b/src/pages/string/reverse/meta.ts new file mode 100644 index 0000000..886b604 --- /dev/null +++ b/src/pages/string/reverse/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('string', { + name: 'Reverse', + path: 'reverse', + // image, + description: '', + shortDescription: '', + keywords: ['reverse'], + component: lazy(() => import('./index')) +}); \ No newline at end of file diff --git a/src/pages/string/reverse/reverse.service.test.ts b/src/pages/string/reverse/reverse.service.test.ts new file mode 100644 index 0000000..0e6ed45 --- /dev/null +++ b/src/pages/string/reverse/reverse.service.test.ts @@ -0,0 +1,52 @@ +import { expect, describe, it } from 'vitest'; +import { stringReverser } from './service'; + +describe('stringReverser', () => { + it('should reverse a single-line string', () => { + const input = 'hello world'; + const result = stringReverser(input, false, false, false); + expect(result).toBe('dlrow olleh'); + }); + + it('should reverse each line in a multi-line string', () => { + const input = 'hello\nworld'; + const result = stringReverser(input, true, false, false); + expect(result).toBe('olleh\ndlrow'); + }); + + it('should remove empty items if emptyItems is true', () => { + const input = 'hello\n\nworld'; + const result = stringReverser(input, true, true, false); + expect(result).toBe('olleh\ndlrow'); + }); + + it('should trim each line if trim is true', () => { + const input = ' hello \n world '; + const result = stringReverser(input, true, false, true); + expect(result).toBe('olleh\ndlrow'); + }); + + it('should handle empty input', () => { + const input = ''; + const result = stringReverser(input, false, false, false); + expect(result).toBe(''); + }); + + it('should handle a single line with emptyItems and trim', () => { + const input = ' hello world '; + const result = stringReverser(input, false, true, true); + expect(result).toBe('dlrow olleh'); + }); + + it('should handle a single line with emptyItems and non trim', () => { + const input = ' hello world '; + const result = stringReverser(input, false, true, false); + expect(result).toBe(' dlrow olleh '); + }); + + it('should handle a multi line with emptyItems and non trim', () => { + const input = ' hello\n\n\n\nworld '; + const result = stringReverser(input, true, true, false); + expect(result).toBe('olleh \n dlrow'); + }); +}); \ No newline at end of file diff --git a/src/pages/string/reverse/service.ts b/src/pages/string/reverse/service.ts new file mode 100644 index 0000000..c8c68a6 --- /dev/null +++ b/src/pages/string/reverse/service.ts @@ -0,0 +1,31 @@ +import { reverseString } from 'utils/string'; + +export function stringReverser( + input: string, + multiLine: boolean, + emptyItems: boolean, + trim: boolean +) { + let array: string[] = []; + let result: string[] = []; + + // split the input in multiLine mode + if (multiLine) { + array = input.split('\n'); + } + else { + array.push(input); + } + + // handle empty items + if (emptyItems){ + array = array.filter(Boolean); + } + // Handle trim + if (trim) { + array = array.map(line => line.trim()); + } + + result = array.map(element => reverseString(element)); + return result.join('\n'); +} \ No newline at end of file