From 8be0a1f76d8d543f476a9c4ae9d4b56763f38c14 Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Sun, 30 Jun 2024 23:17:06 +0000 Subject: [PATCH 1/3] shuffle service and testCases --- src/pages/list/shuffle/index.tsx | 11 +++ src/pages/list/shuffle/meta.ts | 13 +++ src/pages/list/shuffle/service.ts | 38 ++++++++ .../list/shuffle/shuffle.service.test.ts | 95 +++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 src/pages/list/shuffle/index.tsx create mode 100644 src/pages/list/shuffle/meta.ts create mode 100644 src/pages/list/shuffle/service.ts create mode 100644 src/pages/list/shuffle/shuffle.service.test.ts diff --git a/src/pages/list/shuffle/index.tsx b/src/pages/list/shuffle/index.tsx new file mode 100644 index 0000000..c4ba61b --- /dev/null +++ b/src/pages/list/shuffle/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 Shuffle() { + return Lorem ipsum; +} \ No newline at end of file diff --git a/src/pages/list/shuffle/meta.ts b/src/pages/list/shuffle/meta.ts new file mode 100644 index 0000000..441b9a2 --- /dev/null +++ b/src/pages/list/shuffle/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: 'Shuffle', + path: 'shuffle', + // image, + description: '', + shortDescription: '', + keywords: ['shuffle'], + component: lazy(() => import('./index')) +}); \ No newline at end of file diff --git a/src/pages/list/shuffle/service.ts b/src/pages/list/shuffle/service.ts new file mode 100644 index 0000000..f75bb90 --- /dev/null +++ b/src/pages/list/shuffle/service.ts @@ -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); +} \ No newline at end of file diff --git a/src/pages/list/shuffle/shuffle.service.test.ts b/src/pages/list/shuffle/shuffle.service.test.ts new file mode 100644 index 0000000..6b718a4 --- /dev/null +++ b/src/pages/list/shuffle/shuffle.service.test.ts @@ -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(''); + }); + + }) \ No newline at end of file From cc4b17f91f60113eeff04f179e99228cfc62e92e Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Sun, 30 Jun 2024 23:17:48 +0000 Subject: [PATCH 2/3] update index --- src/pages/list/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/list/index.ts b/src/pages/list/index.ts index 88d4a49..9bc430a 100644 --- a/src/pages/list/index.ts +++ b/src/pages/list/index.ts @@ -1,3 +1,4 @@ +import { tool as listShuffle } from './shuffle/meta'; import { tool as listSort } from './sort/meta'; export const listTools = [listSort]; From b54c0a487ec345790ec2c968fc0caf6a0435090e Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Mon, 1 Jul 2024 10:25:30 +0000 Subject: [PATCH 3/3] removing console.log in test file --- src/pages/list/shuffle/shuffle.service.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/list/shuffle/shuffle.service.test.ts b/src/pages/list/shuffle/shuffle.service.test.ts index 6b718a4..feb911a 100644 --- a/src/pages/list/shuffle/shuffle.service.test.ts +++ b/src/pages/list/shuffle/shuffle.service.test.ts @@ -18,7 +18,6 @@ describe('shuffle function', () => { splitSeparator, joinSeparator ); - console.log(result); expect(result.split(joinSeparator).length).toBe(4); });