Skip to content

Commit

Permalink
randomize case tool, testCases and updated index file
Browse files Browse the repository at this point in the history
  • Loading branch information
Chesterkxng committed Jul 10, 2024
1 parent 2bb8158 commit c48b285
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pages/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { tool as stringRandomizeCase } from './randomize-case/meta';
import { tool as stringUppercase } from './uppercase/meta';
import { tool as stringExtractSubstring } from './extract-substring/meta';
import { tool as stringCreatePalindrome } from './create-palindrome/meta';
Expand Down
11 changes: 11 additions & 0 deletions src/pages/string/randomize-case/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 RandomizeCase() {
return <Box>Lorem ipsum</Box>;
}
13 changes: 13 additions & 0 deletions src/pages/string/randomize-case/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('string', {
name: 'Randomize case',
path: 'randomize-case',
// image,
description: '',
shortDescription: '',
keywords: ['randomize', 'case'],
component: lazy(() => import('./index'))
});
48 changes: 48 additions & 0 deletions src/pages/string/randomize-case/randomize-case.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect, describe, it } from 'vitest';
import { randomizeCase } from './service';

describe('randomizeCase', () => {
it('should randomize the case of each character in the string', () => {
const input = 'hello world';
const result = randomizeCase(input);

// Ensure the output length is the same
expect(result).toHaveLength(input.length);

// Ensure each character in the input string appears in the result
for (let i = 0; i < input.length; i++) {
const inputChar = input[i];
const resultChar = result[i];

if (/[a-zA-Z]/.test(inputChar)) {
expect([inputChar.toLowerCase(), inputChar.toUpperCase()]).toContain(resultChar);
} else {
expect(inputChar).toBe(resultChar);
}
}
});

it('should handle an empty string', () => {
const input = '';
const result = randomizeCase(input);
expect(result).toBe('');
});

it('should handle a string with numbers and symbols', () => {
const input = '123 hello! @world';
const result = randomizeCase(input);

// Ensure the output length is the same
expect(result).toHaveLength(input.length);

// Ensure numbers and symbols remain unchanged
for (let i = 0; i < input.length; i++) {
const inputChar = input[i];
const resultChar = result[i];

if (!/[a-zA-Z]/.test(inputChar)) {
expect(inputChar).toBe(resultChar);
}
}
});
});
6 changes: 6 additions & 0 deletions src/pages/string/randomize-case/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function randomizeCase(input: string): string {
return input
.split('')
.map(char => (Math.random() < 0.5 ? char.toLowerCase() : char.toUpperCase()))
.join('');
}

0 comments on commit c48b285

Please sign in to comment.