diff --git a/src/pages/string/join/index.tsx b/src/pages/string/join/index.tsx index d7bc4e9..ccb143d 100644 --- a/src/pages/string/join/index.tsx +++ b/src/pages/string/join/index.tsx @@ -17,7 +17,7 @@ import { mergeText } from './service'; import { CustomSnackBarContext } from '../../../contexts/CustomSnackBarContext'; const initialValues = { - joinCharacter: ' ', + joinCharacter: '', deleteBlank: true, deleteTrailing: true }; @@ -29,6 +29,7 @@ const validationSchema = Yup.object().shape({ }); const mergeOptions = { + placeholder: 'Join Character', description: 'Symbol that connects broken\n' + 'pieces of text. (Space by default.)\n', accessor: 'joinCharacter' as keyof typeof initialValues @@ -41,29 +42,33 @@ const blankTrailingOptions: { }[] = [ { title: 'Delete Blank Lines', - description: "Delete lines that don't have\n" + 'text symbols.\n', + description: "Delete lines that don't have\n text symbols.\n", accessor: 'deleteBlank' }, { title: 'Delete Trailing Spaces', - description: 'Remove spaces and tabs at\n' + 'the end of the lines.\n', + description: 'Remove spaces and tabs at\n the end of the lines.\n', accessor: 'deleteTrailing' } ]; const InputWithDesc = ({ + placeholder, description, value, onChange }: { + placeholder: string; description: string; value: string; onChange: (value: string) => void; }) => { return ( - + onChange(event.target.value)} /> @@ -116,6 +121,7 @@ export default function JoinText() { useEffect(() => { try { + console.log('Form values:', values['joinCharacter']); setResult(mergeText(input, deleteBlank, deleteTrailing, joinCharacter)); } catch (exception: unknown) { if (exception instanceof Error) @@ -123,6 +129,7 @@ export default function JoinText() { } }, [values, input]); + console.log('deleteBlank', deleteBlank); return null; }; @@ -153,8 +160,11 @@ export default function JoinText() { Text Merged Options setFieldValue('joinCharacter', value)} + placeholder={mergeOptions.placeholder} + value={values['joinCharacter']} + onChange={(value) => + setFieldValue(mergeOptions.accessor, value) + } description={mergeOptions.description} /> diff --git a/src/pages/string/join/service.ts b/src/pages/string/join/service.ts index d67f677..a6b9f27 100644 --- a/src/pages/string/join/service.ts +++ b/src/pages/string/join/service.ts @@ -6,11 +6,29 @@ export function mergeText( ): string { const lines = text.split('\n'); - const processedLines = lines - .map((line) => - deleteTrailingSpaces ? line.replace(/ |\r\n|\n|\r/gm, '') : line - ) - .filter((line) => !deleteBlankLines || line.trim() !== ''); + let processedLines = lines; + if (deleteBlankLines) { + lines.map((line) => + deleteTrailingSpaces + ? line + // .split(' ') + // .join('') + // .replace(/|\r\n|\n|\r/gm, '') + .trimEnd() + : line + ); + } else { + lines; + } + + if (deleteBlankLines) { + processedLines = lines.filter( + (line) => !deleteBlankLines || line.trim() !== '' + ); + } else { + lines; + } + return processedLines.join(joinCharacter); } @@ -21,7 +39,4 @@ Another line with trailing spaces Final line without trailing spaces`; export const mergedTextWithBlankLines: string = mergeText(text, false); -console.log('With blank lines:\n', mergedTextWithBlankLines); - export const mergedTextWithoutBlankLines: string = mergeText(text, true); -console.log('Without blank lines:\n', mergedTextWithoutBlankLines);