rotate service and test cases; then updated index file

This commit is contained in:
Chesterkxng 2024-07-03 23:09:25 +00:00
commit 0282706370
5 changed files with 183 additions and 0 deletions

View file

@ -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';

View 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>;
}

View file

@ -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'))
});

View file

@ -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.');
});
})

View file

@ -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.")
}