mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-12-29 16:16:02 +00:00
refactor: tools folder inside pages
This commit is contained in:
66
src/pages/tools/list/duplicate/duplicate.service.test.ts
Normal file
66
src/pages/tools/list/duplicate/duplicate.service.test.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { describe, expect, 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"
|
||||
);
|
||||
});
|
||||
});
|
||||
11
src/pages/tools/list/duplicate/index.tsx
Normal file
11
src/pages/tools/list/duplicate/index.tsx
Normal file
@@ -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 <Box>Lorem ipsum</Box>;
|
||||
}
|
||||
13
src/pages/tools/list/duplicate/meta.ts
Normal file
13
src/pages/tools/list/duplicate/meta.ts
Normal file
@@ -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'))
|
||||
});
|
||||
81
src/pages/tools/list/duplicate/service.ts
Normal file
81
src/pages/tools/list/duplicate/service.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user