diff --git a/src/pages/string/index.ts b/src/pages/string/index.ts index 902c85f..db674d4 100644 --- a/src/pages/string/index.ts +++ b/src/pages/string/index.ts @@ -1,3 +1,4 @@ +import { tool as stringRotate } from './rotate/meta'; import { tool as stringQuote } from './quote/meta'; import { tool as stringRot13 } from './rot13/meta'; import { tool as stringReverse } from './reverse/meta'; diff --git a/src/pages/string/rotate/index.tsx b/src/pages/string/rotate/index.tsx new file mode 100644 index 0000000..ea7d7d5 --- /dev/null +++ b/src/pages/string/rotate/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 Rotate() { + return Lorem ipsum; +} \ No newline at end of file diff --git a/src/pages/string/rotate/meta.ts b/src/pages/string/rotate/meta.ts new file mode 100644 index 0000000..1ad7f71 --- /dev/null +++ b/src/pages/string/rotate/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: 'Rotate', + path: 'rotate', + // image, + description: '', + shortDescription: '', + keywords: ['rotate'], + component: lazy(() => import('./index')) +}); \ No newline at end of file diff --git a/src/pages/string/rotate/rotate.service.test.ts b/src/pages/string/rotate/rotate.service.test.ts new file mode 100644 index 0000000..a8b5f98 --- /dev/null +++ b/src/pages/string/rotate/rotate.service.test.ts @@ -0,0 +1,69 @@ +import { expect, describe, it } from 'vitest'; + +import { rotate, rotateString } from './service'; // Adjust the import path as necessary + +describe('rotate function', () => { + it('rotates right correctly', () => { + expect(rotate('abcdef', 2, true)).toBe('efabcd'); + expect(rotate('abcdef', 1, true)).toBe('fabcde'); + expect(rotate('abcdef', 6, true)).toBe('abcdef'); // full rotation + expect(rotate('abcdef', 7, true)).toBe('fabcde'); // beyond full rotation + }); + + it('rotates left correctly', () => { + expect(rotate('abcdef', 2, false)).toBe('cdefab'); + expect(rotate('abcdef', 1, false)).toBe('bcdefa'); + expect(rotate('abcdef', 6, false)).toBe('abcdef'); // full rotation + expect(rotate('abcdef', 7, false)).toBe('bcdefa'); // beyond full rotation + }); + + it('handles empty string', () => { + expect(rotate('', 2, true)).toBe(''); + expect(rotate('', 2, false)).toBe(''); + }); + + it('handles single character string', () => { + expect(rotate('a', 2, true)).toBe('a'); + expect(rotate('a', 2, false)).toBe('a'); + }); +}); + +describe('rotateString function', () => { + it('rotates single-line string right', () => { + expect(rotateString('abcdef', 2, true, false)).toBe('efabcd'); + }); + + it('rotates single-line string left', () => { + expect(rotateString('abcdef', 2, false, false)).toBe('cdefab'); + }); + + it('rotates multi-line string right', () => { + const input = 'abcdef\nghijkl'; + const expected = 'efabcd\nklghij'; + expect(rotateString(input, 2, true, true)).toBe(expected); + }); + + it('rotates multi-line string left', () => { + const input = 'abcdef\nghijkl'; + const expected = 'cdefab\nijklgh'; + expect(rotateString(input, 2, false, true)).toBe(expected); + }); + + it('handles empty string in multi-line mode', () => { + expect(rotateString('', 2, true, true)).toBe(''); + }); + + it('handles single character string in multi-line mode', () => { + expect(rotateString('a', 2, true, true)).toBe('a'); + }); + + it('handles single character string in single-line mode', () => { + expect(rotateString('a', 2, true, false)).toBe('a'); + }); + + it('handles string with multiple empty lines', () => { + const input = 'abcdef\n\nxyz'; + const expected = 'efabcd\n\nyzx'; + expect(rotateString(input, 2, true, true)).toBe(expected); + }); +}); diff --git a/src/pages/string/rotate/service.ts b/src/pages/string/rotate/service.ts new file mode 100644 index 0000000..f6b11db --- /dev/null +++ b/src/pages/string/rotate/service.ts @@ -0,0 +1,43 @@ +export function rotate(input: string, step: number, right: boolean): string { + const array: string[] = input.split(''); + const length = array.length; + + if (length === 0) { + return input; // Return early if the input is an empty string + } + + // Normalize the step to be within the bounds of the array length + const normalizedPositions = ((step % length) + length) % length; + + if (right) { + // Rotate right + return array + .slice(-normalizedPositions) + .concat(array.slice(0, -normalizedPositions)) + .join(''); + } else { + // Rotate left + return array + .slice(normalizedPositions) + .concat(array.slice(0, normalizedPositions)) + .join(''); + } +} + +export function rotateString( + input: string, + step: number, + right: boolean, + multiLine: boolean +) { + const result: string[] = []; + if (multiLine) { + const array: string[] = input.split('\n'); + for (const word of array) { + result.push(rotate(word, step, right)); + } + } else { + result.push(rotate(input, step, right)); + } + return result.join('\n'); +}