Skip to content

Commit

Permalink
Merge pull request #18 from iib0011/shuffle
Browse files Browse the repository at this point in the history
Shuffle
  • Loading branch information
iib0011 authored Jul 1, 2024
2 parents d6fc974 + b54c0a4 commit 9db2d10
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pages/list/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { tool as listShuffle } from './shuffle/meta';
import { tool as listSort } from './sort/meta';

export const listTools = [listSort];
11 changes: 11 additions & 0 deletions src/pages/list/shuffle/index.tsx
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions src/pages/list/shuffle/meta.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions src/pages/list/shuffle/service.ts
Original file line number Diff line number Diff line change
@@ -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);
}
94 changes: 94 additions & 0 deletions src/pages/list/shuffle/shuffle.service.test.ts
Original file line number Diff line number Diff line change
@@ -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('');
});

})

0 comments on commit 9db2d10

Please sign in to comment.