wrap tool and testCases, then updated index file

This commit is contained in:
Chesterkxng
2024-07-04 09:17:52 +00:00
parent 0282706370
commit 37c520d9ca
5 changed files with 136 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
import { tool as listWrap } from './wrap/meta';
import { tool as listRotate } from './rotate/meta';
import { tool as listTruncate } from './truncate/meta';
import { tool as listShuffle } from './shuffle/meta';

View 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 Wrap() {
return <Box>Lorem ipsum</Box>;
}

View 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: 'Wrap',
path: 'wrap',
// image,
description: '',
shortDescription: '',
keywords: ['wrap'],
component: lazy(() => import('./index'))
});

View File

@@ -0,0 +1,27 @@
export type SplitOperatorType = 'symbol' | 'regex';
function wrap(array: string[], left: string, right: string): string[] {
return array.map((element) => left + element + right);
}
export function wrapList(
splitOperatorType: SplitOperatorType,
input: string,
splitSeparator: string,
joinSeparator: string,
left: string = '',
right: string = ''
): string {
let array: string[];
let wrappedArray: string[];
switch (splitOperatorType) {
case 'symbol':
array = input.split(splitSeparator);
break;
case 'regex':
array = input.split(new RegExp(splitSeparator));
break;
}
wrappedArray = wrap(array, left, right);
return wrappedArray.join(joinSeparator);
}

View File

@@ -0,0 +1,84 @@
import { expect, describe, it } from 'vitest';
import {
SplitOperatorType,
wrapList
} from './service';
describe('wrap function', () => {
it('should return the same input if no left and right are blanked', () => {
const input: string = 'apple, pineaple, lemon, orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
);
expect(result).toBe('apple, pineaple, lemon, orange, mango');
});
it('should append to left if defined', () => {
const input: string = 'apple, pineaple, lemon, orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const left = 'the ';
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
left);
expect(result).toBe('the apple, the pineaple, the lemon, the orange, the mango');
});
it('should append to right if defined', () => {
const input: string = 'apple, pineaple, lemon, orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const left = '';
const right = 'z';
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
left,
right);
expect(result).toBe('applez, pineaplez, lemonz, orangez, mangoz');
});
it('should append to both side if both defined', () => {
const input: string = 'apple, pineaple, lemon, orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const left = 'K';
const right = 'z';
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
left,
right);
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
});
})