mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 14:09:31 +02:00
quote tool, testCases and updated indexfile
This commit is contained in:
11
src/pages/string/quote/index.tsx
Normal file
11
src/pages/string/quote/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 Quote() {
|
||||||
|
return <Box>Lorem ipsum</Box>;
|
||||||
|
}
|
13
src/pages/string/quote/meta.ts
Normal file
13
src/pages/string/quote/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('string', {
|
||||||
|
name: 'Quote',
|
||||||
|
path: 'quote',
|
||||||
|
// image,
|
||||||
|
description: '',
|
||||||
|
shortDescription: '',
|
||||||
|
keywords: ['quote'],
|
||||||
|
component: lazy(() => import('./index'))
|
||||||
|
});
|
62
src/pages/string/quote/quote.service.test.ts
Normal file
62
src/pages/string/quote/quote.service.test.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { expect, describe, it } from 'vitest';
|
||||||
|
import { quote, stringQuoter } from './service';
|
||||||
|
|
||||||
|
describe('quote function', () => {
|
||||||
|
it('quotes a word with single quotes', () => {
|
||||||
|
expect(quote('Hello', "'", "'", false)).toBe("'Hello'");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('quotes a word with double quotes', () => {
|
||||||
|
expect(quote('World', '"', '"', true)).toBe('"World"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not re-quote already quoted word', () => {
|
||||||
|
expect(quote('"Goodbye"', '"', '"', false)).toBe('"Goodbye"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles empty word when emptyQuoting is true', () => {
|
||||||
|
expect(quote('', "'", "'", false)).toBe("''");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles empty word when emptyQuoting is false', () => {
|
||||||
|
expect(quote('', "'", "'", false)).toBe("''"); // Replace with expected behavior
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('stringQuoter function', () => {
|
||||||
|
it('quotes a multi-line input with single quotes', () => {
|
||||||
|
const input = 'Hello\nWorld\n';
|
||||||
|
const expected = "'Hello'\n'World'\n''";
|
||||||
|
expect(stringQuoter(input, "'", "'", false, true, true)).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles empty lines when emptyQuoting is true', () => {
|
||||||
|
const input = 'Hello\n\nWorld';
|
||||||
|
const expected = "'Hello'\n''\n'World'";
|
||||||
|
expect(stringQuoter(input, "'", "'", false, true, true)).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not quote empty lines when emptyQuoting is false', () => {
|
||||||
|
const input = 'Hello\n\nWorld';
|
||||||
|
const expected = "'Hello'\n\n'World'";
|
||||||
|
expect(stringQuoter(input, "'", "'", false, false, true)).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('quotes a single-line input with double quotes', () => {
|
||||||
|
const input = 'Hello';
|
||||||
|
const expected = '"Hello"';
|
||||||
|
expect(stringQuoter(input, '"', '"', true, true, false)).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles empty input', () => {
|
||||||
|
const input = '';
|
||||||
|
const expected = '';
|
||||||
|
expect(stringQuoter(input, "'", "'", false, true, false)).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles spaces input', () => {
|
||||||
|
const input = ' ';
|
||||||
|
const expected = "' '";
|
||||||
|
expect(stringQuoter(input, "'", "'", false, true, false)).toBe(expected);
|
||||||
|
});
|
||||||
|
});
|
57
src/pages/string/quote/service.ts
Normal file
57
src/pages/string/quote/service.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
export function quote(
|
||||||
|
word: string,
|
||||||
|
leftQuote: string,
|
||||||
|
rightQuote: string,
|
||||||
|
doubleQuotation: boolean
|
||||||
|
): string {
|
||||||
|
const array: string[] = word.split('');
|
||||||
|
|
||||||
|
// Check if double quotation is enabled and adjust accordingly
|
||||||
|
if (doubleQuotation) {
|
||||||
|
array.unshift(leftQuote);
|
||||||
|
array.push(rightQuote);
|
||||||
|
} else {
|
||||||
|
// Check if the word is already quoted correctly
|
||||||
|
if (array[0] === leftQuote && array[array.length - 1] === rightQuote) {
|
||||||
|
return word;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append quotes if not already quoted
|
||||||
|
array.unshift(leftQuote);
|
||||||
|
array.push(rightQuote);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array.join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stringQuoter(
|
||||||
|
input: string,
|
||||||
|
leftQuote: string,
|
||||||
|
rightQuote: string,
|
||||||
|
doubleQuotation: boolean,
|
||||||
|
emptyQuoting: boolean,
|
||||||
|
multiLine: boolean
|
||||||
|
) {
|
||||||
|
if (!input) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
let arrayOfString: string[] = [];
|
||||||
|
const result: string[] = [];
|
||||||
|
if (multiLine) {
|
||||||
|
arrayOfString = input.split('\n');
|
||||||
|
} else {
|
||||||
|
arrayOfString.push(input);
|
||||||
|
}
|
||||||
|
for (const word of arrayOfString) {
|
||||||
|
if (word === '') {
|
||||||
|
if (emptyQuoting) {
|
||||||
|
result.push(quote(word, leftQuote, rightQuote, doubleQuotation));
|
||||||
|
} else {
|
||||||
|
result.push(word);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result.push(quote(word, leftQuote, rightQuote, doubleQuotation));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.join('\n');
|
||||||
|
}
|
Reference in New Issue
Block a user