-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from iib0011/shuffle
Shuffle
- Loading branch information
Showing
5 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
|
||
}) |