From b309921d892fabef063ecb34ee2891913b229d01 Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Wed, 10 Jul 2024 14:34:17 +0000 Subject: [PATCH] create palindrome tool, testCases and undated index file --- .../create-palindrome.service.test.ts | 66 +++++++++++++++++++ src/pages/string/create-palindrome/index.tsx | 11 ++++ src/pages/string/create-palindrome/meta.ts | 13 ++++ src/pages/string/create-palindrome/service.ts | 35 ++++++++++ src/pages/string/index.ts | 1 + 5 files changed, 126 insertions(+) create mode 100644 src/pages/string/create-palindrome/create-palindrome.service.test.ts create mode 100644 src/pages/string/create-palindrome/index.tsx create mode 100644 src/pages/string/create-palindrome/meta.ts create mode 100644 src/pages/string/create-palindrome/service.ts diff --git a/src/pages/string/create-palindrome/create-palindrome.service.test.ts b/src/pages/string/create-palindrome/create-palindrome.service.test.ts new file mode 100644 index 0000000..ddd712b --- /dev/null +++ b/src/pages/string/create-palindrome/create-palindrome.service.test.ts @@ -0,0 +1,66 @@ +import { expect, describe, it } from 'vitest'; + import { createPalindromeList, createPalindrome } from './service'; + +describe('createPalindrome', () => { + test('should create palindrome by reversing the entire string', () => { + const input = 'hello'; + const result = createPalindrome(input, true); + expect(result).toBe('helloolleh'); + }); + + test('should create palindrome by reversing the string excluding the last character', () => { + const input = 'hello'; + const result = createPalindrome(input, false); + expect(result).toBe('hellolleh'); + }); + + test('should return an empty string if input is empty', () => { + const input = ''; + const result = createPalindrome(input, true); + expect(result).toBe(''); + }); +}); + +describe('createPalindromeList', () => { + test('should create palindrome for single-line input', () => { + const input = 'hello'; + const result = createPalindromeList(input, true, false); + expect(result).toBe('helloolleh'); + }); + + test('should create palindrome for single-line input considering trailing spaces', () => { + const input = 'hello '; + const result = createPalindromeList(input, true, false); + expect(result).toBe('hello olleh'); + }); + + test('should create palindrome for single-line input ignoring trailing spaces if lastChar is set to false', () => { + const input = 'hello '; + const result = createPalindromeList(input, true, false); + expect(result).toBe('hello olleh'); + }); + + test('should create palindrome for multi-line input', () => { + const input = 'hello\nworld'; + const result = createPalindromeList(input, true, true); + expect(result).toBe('helloolleh\nworlddlrow'); + }); + + test('should create palindrome for no multi-line input', () => { + const input = 'hello\nworld\n'; + const result = createPalindromeList(input, true, false); + expect(result).toBe('hello\nworld\n\ndlrow\nolleh'); + }); + + test('should handle multi-line input with lastChar set to false', () => { + const input = 'hello\nworld'; + const result = createPalindromeList(input, false, true); + expect(result).toBe('hellolleh\nworldlrow'); + }); + + test('should return an empty string if input is empty', () => { + const input = ''; + const result = createPalindromeList(input, true, false); + expect(result).toBe(''); + }); +}); \ No newline at end of file diff --git a/src/pages/string/create-palindrome/index.tsx b/src/pages/string/create-palindrome/index.tsx new file mode 100644 index 0000000..da75227 --- /dev/null +++ b/src/pages/string/create-palindrome/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 CreatePalindrome() { + return Lorem ipsum; +} \ No newline at end of file diff --git a/src/pages/string/create-palindrome/meta.ts b/src/pages/string/create-palindrome/meta.ts new file mode 100644 index 0000000..5b21620 --- /dev/null +++ b/src/pages/string/create-palindrome/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: 'Create palindrome', + path: 'create-palindrome', + // image, + description: '', + shortDescription: '', + keywords: ['create', 'palindrome'], + component: lazy(() => import('./index')) +}); \ No newline at end of file diff --git a/src/pages/string/create-palindrome/service.ts b/src/pages/string/create-palindrome/service.ts new file mode 100644 index 0000000..1f87c6d --- /dev/null +++ b/src/pages/string/create-palindrome/service.ts @@ -0,0 +1,35 @@ +import { reverseString } from 'utils/string' + +export function createPalindrome( + input: string, + lastChar: boolean // only checkbox is need here to handle it [instead of two combo boxes] +) { + if (!input) return ''; + let result: string; + let reversedString: string; + + // reverse the whole input if lastChar enabled + reversedString = lastChar ? reverseString(input) : reverseString(input.slice(0, -1)); + result = input.concat(reversedString); + return result; +} + +export function createPalindromeList( + input: string, + lastChar: boolean, + multiLine: boolean +): string { + if (!input) return ''; + let array: string[]; + let result: string[] = []; + + if (!multiLine) return createPalindrome(input, lastChar); + else { + array = input.split('\n'); + for (const word of array) { + result.push(createPalindrome(word, lastChar)); + } + } + return result.join('\n'); + +} \ No newline at end of file diff --git a/src/pages/string/index.ts b/src/pages/string/index.ts index ccc5a01..b588dee 100644 --- a/src/pages/string/index.ts +++ b/src/pages/string/index.ts @@ -1,3 +1,4 @@ +import { tool as stringCreatePalindrome } from './create-palindrome/meta'; import { tool as stringPalindrome } from './palindrome/meta'; import { tool as stringToMorse } from './to-morse/meta'; import { tool as stringSplit } from './split/meta';