mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 22:19:36 +02:00
shuffle service and testCases
This commit is contained in:
11
src/pages/list/shuffle/index.tsx
Normal file
11
src/pages/list/shuffle/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 Shuffle() {
|
||||||
|
return <Box>Lorem ipsum</Box>;
|
||||||
|
}
|
13
src/pages/list/shuffle/meta.ts
Normal file
13
src/pages/list/shuffle/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('list', {
|
||||||
|
name: 'Shuffle',
|
||||||
|
path: 'shuffle',
|
||||||
|
// image,
|
||||||
|
description: '',
|
||||||
|
shortDescription: '',
|
||||||
|
keywords: ['shuffle'],
|
||||||
|
component: lazy(() => import('./index'))
|
||||||
|
});
|
38
src/pages/list/shuffle/service.ts
Normal file
38
src/pages/list/shuffle/service.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
export type SplitOperatorType = 'symbol' | 'regex';
|
||||||
|
|
||||||
|
// function that randomize the array
|
||||||
|
function shuffleArray(array: string[]): string[] {
|
||||||
|
let shuffledArray = array.slice(); // Create a copy of the array
|
||||||
|
for (let i = shuffledArray.length - 1; i > 0; i--) {
|
||||||
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
|
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
|
||||||
|
}
|
||||||
|
return shuffledArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function shuffleList(
|
||||||
|
splitOperatorType: SplitOperatorType,
|
||||||
|
input: string,
|
||||||
|
splitSeparator: string,
|
||||||
|
joinSeparator: string,
|
||||||
|
length?: number, // "?" is to handle the case the user let the input blank
|
||||||
|
) : string {
|
||||||
|
let array: string[];
|
||||||
|
let shuffledArray: string[];
|
||||||
|
switch (splitOperatorType) {
|
||||||
|
case 'symbol':
|
||||||
|
array = input.split(splitSeparator);
|
||||||
|
break;
|
||||||
|
case 'regex':
|
||||||
|
array = input.split(new RegExp(splitSeparator));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
shuffledArray = shuffleArray(array);
|
||||||
|
if (length !== undefined) {
|
||||||
|
if (length <= 0) {
|
||||||
|
throw new Error("Length value must be a positive number.");
|
||||||
|
}
|
||||||
|
return shuffledArray.slice(0, length).join(joinSeparator);
|
||||||
|
}
|
||||||
|
return shuffledArray.join(joinSeparator);
|
||||||
|
}
|
95
src/pages/list/shuffle/shuffle.service.test.ts
Normal file
95
src/pages/list/shuffle/shuffle.service.test.ts
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import { expect, describe, it } from 'vitest';
|
||||||
|
import {
|
||||||
|
shuffleList,
|
||||||
|
SplitOperatorType
|
||||||
|
} from './service';
|
||||||
|
|
||||||
|
describe('shuffle function', () => {
|
||||||
|
|
||||||
|
it('should be a 4 length list if no length value defined ', () => {
|
||||||
|
const input: string = 'apple, pineaple, lemon, orange';
|
||||||
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
|
const splitSeparator = ', ';
|
||||||
|
const joinSeparator = ' ';
|
||||||
|
|
||||||
|
const result = shuffleList(
|
||||||
|
splitOperatorType,
|
||||||
|
input,
|
||||||
|
splitSeparator,
|
||||||
|
joinSeparator
|
||||||
|
);
|
||||||
|
console.log(result);
|
||||||
|
expect(result.split(joinSeparator).length).toBe(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be a 2 length list if length value is set to 2', () => {
|
||||||
|
const input: string = 'apple, pineaple, lemon, orange';
|
||||||
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
|
const splitSeparator = ', ';
|
||||||
|
const joinSeparator = ' ';
|
||||||
|
const length = 2;
|
||||||
|
|
||||||
|
const result = shuffleList(
|
||||||
|
splitOperatorType,
|
||||||
|
input,
|
||||||
|
splitSeparator,
|
||||||
|
joinSeparator,
|
||||||
|
length
|
||||||
|
);
|
||||||
|
console.log(result);
|
||||||
|
expect(result.split(joinSeparator).length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be a 4 length list if length value is set to 99', () => {
|
||||||
|
const input: string = 'apple, pineaple, lemon, orange';
|
||||||
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
|
const splitSeparator = ', ';
|
||||||
|
const joinSeparator = ' ';
|
||||||
|
const length = 99;
|
||||||
|
|
||||||
|
const result = shuffleList(
|
||||||
|
splitOperatorType,
|
||||||
|
input,
|
||||||
|
splitSeparator,
|
||||||
|
joinSeparator,
|
||||||
|
length
|
||||||
|
);
|
||||||
|
console.log(result);
|
||||||
|
expect(result.split(joinSeparator).length).toBe(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should include a random element if length value is undefined', () => {
|
||||||
|
const input: string = 'apple, pineaple, lemon, orange';
|
||||||
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
|
const splitSeparator = ', ';
|
||||||
|
const joinSeparator = ' ';
|
||||||
|
|
||||||
|
const result = shuffleList(
|
||||||
|
splitOperatorType,
|
||||||
|
input,
|
||||||
|
splitSeparator,
|
||||||
|
joinSeparator,
|
||||||
|
length
|
||||||
|
);
|
||||||
|
console.log(result);
|
||||||
|
expect(result.split(joinSeparator)).toContain('apple');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return empty string if input is empty', () => {
|
||||||
|
const input: string = '';
|
||||||
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
|
const splitSeparator = ', ';
|
||||||
|
const joinSeparator = ' ';
|
||||||
|
|
||||||
|
const result = shuffleList(
|
||||||
|
splitOperatorType,
|
||||||
|
input,
|
||||||
|
splitSeparator,
|
||||||
|
joinSeparator,
|
||||||
|
length
|
||||||
|
);
|
||||||
|
console.log(result);
|
||||||
|
expect(result).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
})
|
Reference in New Issue
Block a user