Merge pull request #18 from iib0011/shuffle

Shuffle
This commit is contained in:
Ibrahima G. Coulibaly
2024-07-01 11:24:13 +01:00
committed by GitHub
5 changed files with 157 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
import { tool as listShuffle } from './shuffle/meta';
import { tool as listSort } from './sort/meta';
export const listTools = [listSort];

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 Shuffle() {
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: 'Shuffle',
path: 'shuffle',
// image,
description: '',
shortDescription: '',
keywords: ['shuffle'],
component: lazy(() => import('./index'))
});

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

View File

@@ -0,0 +1,94 @@
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
);
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('');
});
})