From 028270637026ec699ce88eda71b2599e6c0f2dd8 Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Wed, 3 Jul 2024 23:09:25 +0000 Subject: [PATCH] rotate service and test cases; then updated index file --- src/pages/list/index.ts | 1 + src/pages/list/rotate/index.tsx | 11 ++ src/pages/list/rotate/meta.ts | 13 +++ src/pages/list/rotate/rotate.service.test.ts | 110 +++++++++++++++++++ src/pages/list/rotate/service.ts | 48 ++++++++ 5 files changed, 183 insertions(+) create mode 100644 src/pages/list/rotate/index.tsx create mode 100644 src/pages/list/rotate/meta.ts create mode 100644 src/pages/list/rotate/rotate.service.test.ts create mode 100644 src/pages/list/rotate/service.ts diff --git a/src/pages/list/index.ts b/src/pages/list/index.ts index c3e441d..c359bb0 100644 --- a/src/pages/list/index.ts +++ b/src/pages/list/index.ts @@ -1,3 +1,4 @@ +import { tool as listRotate } from './rotate/meta'; import { tool as listTruncate } from './truncate/meta'; import { tool as listShuffle } from './shuffle/meta'; import { tool as listSort } from './sort/meta'; diff --git a/src/pages/list/rotate/index.tsx b/src/pages/list/rotate/index.tsx new file mode 100644 index 0000000..ea7d7d5 --- /dev/null +++ b/src/pages/list/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/list/rotate/meta.ts b/src/pages/list/rotate/meta.ts new file mode 100644 index 0000000..506b51e --- /dev/null +++ b/src/pages/list/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('list', { + name: 'Rotate', + path: 'rotate', + // image, + description: '', + shortDescription: '', + keywords: ['rotate'], + component: lazy(() => import('./index')) +}); \ No newline at end of file diff --git a/src/pages/list/rotate/rotate.service.test.ts b/src/pages/list/rotate/rotate.service.test.ts new file mode 100644 index 0000000..9b68933 --- /dev/null +++ b/src/pages/list/rotate/rotate.service.test.ts @@ -0,0 +1,110 @@ +import { expect, describe, it } from 'vitest'; +import { + SplitOperatorType, + rotateList +} from './service'; + +describe('rotate function', () => { + it('should rotate right side if right is set to true', () => { + const input: string = 'apple, pineaple, lemon, orange, mango'; + const splitOperatorType: SplitOperatorType = 'symbol'; + const splitSeparator = ', '; + const joinSeparator = ' '; + const step = 1; + const right = true; + + const result = rotateList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + right, + + step); + + expect(result).toBe('mango apple pineaple lemon orange'); + + }); + + it('should rotate left side if right is set to true', () => { + const input: string = 'apple, pineaple, lemon, orange, mango'; + const splitOperatorType: SplitOperatorType = 'symbol'; + const splitSeparator = ', '; + const joinSeparator = ' '; + const step = 1; + const right = false; + + const result = rotateList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + right, + + step); + + expect(result).toBe('pineaple lemon orange mango apple'); + + }); + + it('should rotate left side with 2 step if right is set to true', () => { + const input: string = 'apple, pineaple, lemon, orange, mango'; + const splitOperatorType: SplitOperatorType = 'symbol'; + const splitSeparator = ', '; + const joinSeparator = ' '; + const step = 2; + const right = false; + + const result = rotateList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + right, + + step); + + expect(result).toBe('lemon orange mango apple pineaple'); + + }); + + it('should raise an error if step is negative', () => { + const input: string = 'apple, pineaple, lemon, orange, mango'; + const splitOperatorType: SplitOperatorType = 'symbol'; + const splitSeparator = ', '; + const joinSeparator = ' '; + const step = -2; + const right = false; + + expect(() => { + rotateList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + right, + step); + }).toThrowError('Rotation step must be greater than zero.'); + + }); + + it('should raise an error if step is undefined', () => { + const input: string = 'apple, pineaple, lemon, orange, mango'; + const splitOperatorType: SplitOperatorType = 'symbol'; + const splitSeparator = ', '; + const joinSeparator = ' '; + const right = false; + + expect(() => { + rotateList( + splitOperatorType, + input, + splitSeparator, + joinSeparator, + right); + }).toThrowError('Rotation step contains non-digits.'); + + }); + + +}) \ No newline at end of file diff --git a/src/pages/list/rotate/service.ts b/src/pages/list/rotate/service.ts new file mode 100644 index 0000000..9a27957 --- /dev/null +++ b/src/pages/list/rotate/service.ts @@ -0,0 +1,48 @@ +import { isNumber } from 'utils/string'; +export type SplitOperatorType = 'symbol' | 'regex'; + +function rotateArray( + array: string[], + step: number, + right: boolean): string[] { + const length = array.length; + + // 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)); + } else { + // Rotate left + return array.slice(normalizedPositions).concat(array.slice(0, normalizedPositions)); + } +} + +export function rotateList( + splitOperatorType: SplitOperatorType, + input: string, + splitSeparator: string, + joinSeparator: string, + right: boolean, + step?: number, +): string { + let array: string[]; + let rotatedArray: string[]; + switch (splitOperatorType) { + case 'symbol': + array = input.split(splitSeparator); + break; + case 'regex': + array = input.split(new RegExp(splitSeparator)); + break; + } + if (step !== undefined) { + if (step <= 0) { + throw new Error("Rotation step must be greater than zero."); + } + rotatedArray = rotateArray(array, step, right); + return rotatedArray.join(joinSeparator); + } + throw new Error("Rotation step contains non-digits.") +} \ No newline at end of file