diff --git a/src/pages/string/extract-substring/extract-substring.service.test.ts b/src/pages/string/extract-substring/extract-substring.service.test.ts new file mode 100644 index 0000000..ecacc55 --- /dev/null +++ b/src/pages/string/extract-substring/extract-substring.service.test.ts @@ -0,0 +1,57 @@ +import { expect, describe, it } from 'vitest'; +import { extractSubstring } from './service'; + +describe('extractSubstring', () => { + it('should extract a substring from single-line input', () => { + const input = 'hello world'; + const result = extractSubstring(input, 1, 4, false, false); + expect(result).toBe('hell'); + }); + + it('should extract and reverse a substring from single-line input', () => { + const input = 'hello world'; + const result = extractSubstring(input, 1, 5, false, true); + expect(result).toBe('olleh'); + }); + + it('should extract substrings from multi-line input', () => { + const input = 'hello\nworld'; + const result = extractSubstring(input, 1, 5, true, false); + expect(result).toBe('hello\nworld'); + }); + + it('should extract and reverse substrings from multi-line input', () => { + const input = 'hello\nworld'; + const result = extractSubstring(input, 1, 4, true, true); + expect(result).toBe('lleh\nlrow'); + }); + + it('should handle empty input', () => { + const input = ''; + const result = extractSubstring(input, 1, 5, false, false); + expect(result).toBe(''); + }); + + it('should handle start and length out of bounds', () => { + const input = 'hello'; + const result = extractSubstring(input, 10, 5, false, false); + expect(result).toBe(''); + }); + + it('should handle negative start and length', () => { + expect(() => extractSubstring('hello', -1, 5, false, false)).toThrow("Start index must be greater than zero."); + expect(() => extractSubstring('hello', 1, -5, false, false)).toThrow("Length value must be greater than or equal to zero."); + }); + + it('should handle zero length', () => { + const input = 'hello'; + const result = extractSubstring(input, 1, 0, false, false); + expect(result).toBe(''); + }); + + it('should work', () => { + const input = 'je me nomme king\n22 est mon chiffre'; + const result = extractSubstring(input, 12, 7, true, false); + expect(result).toBe(' king\nchiffre'); + }); +}); \ No newline at end of file diff --git a/src/pages/string/extract-substring/index.tsx b/src/pages/string/extract-substring/index.tsx new file mode 100644 index 0000000..be249e8 --- /dev/null +++ b/src/pages/string/extract-substring/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 ExtractSubstring() { + return Lorem ipsum; +} \ No newline at end of file diff --git a/src/pages/string/extract-substring/meta.ts b/src/pages/string/extract-substring/meta.ts new file mode 100644 index 0000000..30e8f74 --- /dev/null +++ b/src/pages/string/extract-substring/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('string', { + name: 'Extract substring', + path: 'extract-substring', + // image, + description: '', + shortDescription: '', + keywords: ['extract', 'substring'], + component: lazy(() => import('./index')) +}); \ No newline at end of file diff --git a/src/pages/string/extract-substring/service.ts b/src/pages/string/extract-substring/service.ts new file mode 100644 index 0000000..c06a10f --- /dev/null +++ b/src/pages/string/extract-substring/service.ts @@ -0,0 +1,36 @@ +import { reverseString } from 'utils/string' + +export function extractSubstring( + input: string, + start: number, + length: number, + multiLine: boolean, + reverse: boolean +): string { + if (!input) return ''; + // edge Cases + if (start <= 0) throw new Error("Start index must be greater than zero."); + if (length < 0) throw new Error("Length value must be greater than or equal to zero."); + if (length === 0) return ''; + + let array: string[]; + let result: string[] = []; + + const extract = (str: string, start: number, length: number): string => { + const end = start - 1 + length; + if (start - 1 >= str.length) return ''; + return str.substring(start - 1, Math.min(end, str.length)); + }; + + if (!multiLine) { + result.push(extract(input, start, length)); + } + else { + array = input.split('\n'); + for (const word of array) { + result.push(extract(word, start, length)); + } + } + result = reverse ? result.map(word => reverseString(word)) : result; + return result.join('\n'); +} \ No newline at end of file diff --git a/src/pages/string/index.ts b/src/pages/string/index.ts index b588dee..1ea0627 100644 --- a/src/pages/string/index.ts +++ b/src/pages/string/index.ts @@ -1,3 +1,4 @@ +import { tool as stringExtractSubstring } from './extract-substring/meta'; import { tool as stringCreatePalindrome } from './create-palindrome/meta'; import { tool as stringPalindrome } from './palindrome/meta'; import { tool as stringToMorse } from './to-morse/meta';