mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 05:59:34 +02:00
rotate tool, testCases then updated index file
This commit is contained in:
@@ -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';
|
||||
|
11
src/pages/string/rotate/index.tsx
Normal file
11
src/pages/string/rotate/index.tsx
Normal file
@@ -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 <Box>Lorem ipsum</Box>;
|
||||
}
|
13
src/pages/string/rotate/meta.ts
Normal file
13
src/pages/string/rotate/meta.ts
Normal file
@@ -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'))
|
||||
});
|
69
src/pages/string/rotate/rotate.service.test.ts
Normal file
69
src/pages/string/rotate/rotate.service.test.ts
Normal file
@@ -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);
|
||||
});
|
||||
});
|
43
src/pages/string/rotate/service.ts
Normal file
43
src/pages/string/rotate/service.ts
Normal file
@@ -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');
|
||||
}
|
Reference in New Issue
Block a user