mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 14:09:31 +02:00
Merge pull request #19 from iib0011/truncate
truncate tool and testCases
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { tool as listTruncate } from './truncate/meta';
|
||||
import { tool as listShuffle } from './shuffle/meta';
|
||||
import { tool as listSort } from './sort/meta';
|
||||
|
||||
|
11
src/pages/list/truncate/index.tsx
Normal file
11
src/pages/list/truncate/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 Truncate() {
|
||||
return <Box>Lorem ipsum</Box>;
|
||||
}
|
13
src/pages/list/truncate/meta.ts
Normal file
13
src/pages/list/truncate/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: 'Truncate',
|
||||
path: 'truncate',
|
||||
// image,
|
||||
description: '',
|
||||
shortDescription: '',
|
||||
keywords: ['truncate'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
30
src/pages/list/truncate/service.ts
Normal file
30
src/pages/list/truncate/service.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
export function truncateList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
end: boolean,
|
||||
length?: number,
|
||||
): string {
|
||||
let array: string[];
|
||||
let truncatedArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
if (length !== undefined) {
|
||||
if (length < 0) {
|
||||
throw new Error("Length value must be a positive number.")
|
||||
}
|
||||
truncatedArray = end ? array.slice(0, length) : array.slice(array.length - length, array.length);
|
||||
return truncatedArray.join(joinSeparator);
|
||||
}
|
||||
throw new Error("Length value isn't a value number.");
|
||||
|
||||
}
|
185
src/pages/list/truncate/truncate.service.test.ts
Normal file
185
src/pages/list/truncate/truncate.service.test.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
|
||||
import {
|
||||
SplitOperatorType,
|
||||
truncateList,
|
||||
} from './service';
|
||||
|
||||
describe('truncate function', () => {
|
||||
it('should remove at the end (one element) if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
|
||||
expect(result).toBe('apple pineaple lemon');
|
||||
|
||||
});
|
||||
|
||||
it('should return 3 elements from the start if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
|
||||
expect(result).toBe('apple pineaple lemon');
|
||||
|
||||
});
|
||||
|
||||
it('should return 3 elements from the start if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
|
||||
expect(result).toBe('apple pineaple lemon');
|
||||
|
||||
});
|
||||
|
||||
it('should return 3 elements from the end if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 3;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
|
||||
expect(result).toBe('lemon orange mango');
|
||||
|
||||
});
|
||||
|
||||
it('should return a void string if length is set to 0', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 0;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
|
||||
expect(result).toBe('');
|
||||
|
||||
});
|
||||
|
||||
it('should return an element (first) string if length is set to 1 and end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 1;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
|
||||
expect(result).toBe('apple');
|
||||
|
||||
});
|
||||
|
||||
it('should return an element (last) string if length is set to 1 and end is set to false', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 1;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
|
||||
expect(result).toBe('mango');
|
||||
|
||||
});
|
||||
|
||||
it('should throw an error if the length value is negative', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = -5;
|
||||
|
||||
expect(() => {
|
||||
truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length)
|
||||
}).toThrow("Length value must be a positive number.");
|
||||
});
|
||||
|
||||
it('should throw an error if the length value is left blank', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
|
||||
expect(() => {
|
||||
truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end)
|
||||
}).toThrow("Length value isn't a value number.");
|
||||
});
|
||||
|
||||
})
|
Reference in New Issue
Block a user