From 98806d3688924e8272a81770c49fc3f132387e87 Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Tue, 9 Jul 2024 16:22:59 +0000 Subject: [PATCH] duplicate tool service and testCases, then updated index file --- .../list/duplicate/duplicate.service.test.ts | 62 +++++++++++++++++ src/pages/list/duplicate/index.tsx | 11 +++ src/pages/list/duplicate/meta.ts | 13 ++++ src/pages/list/duplicate/service.ts | 69 +++++++++++++++++++ src/pages/list/index.ts | 1 + 5 files changed, 156 insertions(+) create mode 100644 src/pages/list/duplicate/duplicate.service.test.ts create mode 100644 src/pages/list/duplicate/index.tsx create mode 100644 src/pages/list/duplicate/meta.ts create mode 100644 src/pages/list/duplicate/service.ts diff --git a/src/pages/list/duplicate/duplicate.service.test.ts b/src/pages/list/duplicate/duplicate.service.test.ts new file mode 100644 index 0000000..c1554e6 --- /dev/null +++ b/src/pages/list/duplicate/duplicate.service.test.ts @@ -0,0 +1,62 @@ +import { expect, describe, it } from 'vitest'; +import { duplicateList } from './service'; + +describe('duplicateList function', () => { + it('should duplicate elements correctly with symbol split', () => { + const input = "Hello World"; + const result = duplicateList('symbol', ' ', ' ', input, true, false, 2); + expect(result).toBe("Hello World Hello World"); + }); + + it('should duplicate elements correctly with regex split', () => { + const input = "Hello||World"; + const result = duplicateList('regex', '\\|\\|', ' ', input, true, false, 2); + expect(result).toBe("Hello World Hello World"); + }); + + it('should handle fractional duplication', () => { + const input = "Hello World"; + const result = duplicateList('symbol', ' ', ' ', input, true, false, 1.5); + expect(result).toBe("Hello World Hello"); + }); + + it('should handle reverse option correctly', () => { + const input = "Hello World"; + const result = duplicateList('symbol', ' ', ' ', input, true, true, 2); + expect(result).toBe("Hello World World Hello"); + }); + + it('should handle concatenate option correctly', () => { + const input = "Hello World"; + const result = duplicateList('symbol', ' ', ' ', input, false, false, 2); + expect(result).toBe("Hello Hello World World"); + }); + + it('should handle interweaving option correctly', () => { + const input = "Hello World"; + const result = duplicateList('symbol', ' ', ' ', input, false, false, 2); + expect(result).toBe("Hello Hello World World"); + }); + + it('should throw an error for negative copies', () => { + expect(() => duplicateList('symbol', ' ', ' ', "Hello World", true, false, -1)).toThrow("Number of copies cannot be negative"); + }); + + it('should handle interweaving option correctly 2', () => { + const input = "je m'appelle king"; + const result = duplicateList('symbol', ' ', ', ', input, false, true, 2.1); + expect(result).toBe("je, king, m'appelle, m'appelle, king, je"); + }); + + it('should handle interweaving option correctly 3', () => { + const input = "je m'appelle king"; + const result = duplicateList('symbol', ' ', ', ', input, false, true, 1); + expect(result).toBe("je, m'appelle, king"); + }); + + it('should handle interweaving option correctly 3', () => { + const input = "je m'appelle king"; + const result = duplicateList('symbol', ' ', ', ', input, true, true, 2.7); + expect(result).toBe("je, m'appelle, king, king, m'appelle, je, king, m'appelle"); + }); +}); \ No newline at end of file diff --git a/src/pages/list/duplicate/index.tsx b/src/pages/list/duplicate/index.tsx new file mode 100644 index 0000000..c833d45 --- /dev/null +++ b/src/pages/list/duplicate/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 Duplicate() { + return Lorem ipsum; +} \ No newline at end of file diff --git a/src/pages/list/duplicate/meta.ts b/src/pages/list/duplicate/meta.ts new file mode 100644 index 0000000..89f9fe1 --- /dev/null +++ b/src/pages/list/duplicate/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: 'Duplicate', + path: 'duplicate', + // image, + description: '', + shortDescription: '', + keywords: ['duplicate'], + component: lazy(() => import('./index')) +}); \ No newline at end of file diff --git a/src/pages/list/duplicate/service.ts b/src/pages/list/duplicate/service.ts new file mode 100644 index 0000000..c1c2bbd --- /dev/null +++ b/src/pages/list/duplicate/service.ts @@ -0,0 +1,69 @@ +export type SplitOperatorType = 'symbol' | 'regex'; + +function interweave( + array1: string[], + array2: string[]) { + const result: string[] = []; + const maxLength = Math.max(array1.length, array2.length); + + for (let i = 0; i < maxLength; i++) { + if (i < array1.length) result.push(array1[i]); + if (i < array2.length) result.push(array2[i]); + } + return result; +} +function duplicate( + input: string[], + concatenate: boolean, + reverse: boolean, + copy?: number +) { + if (copy) { + if (copy > 0) { + let result: string[] = []; + let toAdd: string[] = []; + let WholePart: string[] = []; + let fractionalPart: string[] = []; + const whole = Math.floor(copy); + const fractional = copy - whole; + if (!reverse) { + WholePart = concatenate ? Array(whole).fill(input).flat() : Array(whole - 1).fill(input).flat(); + fractionalPart = input.slice(0, Math.floor(input.length * fractional)); + toAdd = WholePart.concat(fractionalPart); + result = concatenate ? WholePart.concat(fractionalPart) : interweave(input, toAdd); + } else { + WholePart = Array(whole - 1).fill(input).flat().reverse() + fractionalPart = input.slice().reverse().slice(0, Math.floor(input.length * fractional)); + toAdd = WholePart.concat(fractionalPart); + result = concatenate ? input.concat(toAdd) : interweave(input, toAdd); + } + + return result; + } + throw new Error("Number of copies cannot be negative"); + } + throw new Error("Number of copies must be a valid number"); +} + +export function duplicateList( + splitOperatorType: SplitOperatorType, + splitSeparator: string, + joinSeparator: string, + input: string, + concatenate: boolean, + reverse: boolean, + copy?: number +): string { + let array: string[]; + let result: string[]; + switch (splitOperatorType) { + case 'symbol': + array = input.split(splitSeparator); + break; + case 'regex': + array = input.split(new RegExp(splitSeparator)).filter(item => item !== ''); + break; + } + result = duplicate(array, concatenate, reverse, copy); + return result.join(joinSeparator); +} \ No newline at end of file diff --git a/src/pages/list/index.ts b/src/pages/list/index.ts index 388e39c..79b8491 100644 --- a/src/pages/list/index.ts +++ b/src/pages/list/index.ts @@ -1,3 +1,4 @@ +import { tool as listDuplicate } from './duplicate/meta'; import { tool as listUnwrap } from './unwrap/meta'; import { tool as listReverse } from './reverse/meta'; import { tool as listFindUnique } from './find-unique/meta';