Files
omni-tools/src/pages/tools/string/split/index.tsx

209 lines
6.0 KiB
TypeScript
Raw Normal View History

2024-06-26 09:02:05 +01:00
import { Box } from '@mui/material';
2025-02-27 02:21:08 +00:00
import React, { useRef, useState } from 'react';
2025-02-23 01:38:42 +01:00
import ToolTextInput from '@components/input/ToolTextInput';
import ToolTextResult from '@components/result/ToolTextResult';
2024-06-22 03:29:34 +01:00
import { compute, SplitOperatorType } from './service';
2025-02-23 01:38:42 +01:00
import RadioWithTextField from '@components/options/RadioWithTextField';
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
2025-02-27 02:21:08 +00:00
import ToolExamples, {
CardExampleType
} from '@components/examples/ToolExamples';
import { ToolComponentProps } from '@tools/defineTool';
import { FormikProps } from 'formik';
2025-03-09 18:06:27 +00:00
import ToolContent from '@components/ToolContent';
2024-06-19 21:18:35 +01:00
2024-06-21 20:06:07 +01:00
const initialValues = {
2024-06-21 21:59:17 +01:00
splitSeparatorType: 'symbol' as SplitOperatorType,
2024-06-21 20:24:00 +01:00
symbolValue: ' ',
regexValue: '/\\s+/',
lengthValue: '16',
2024-06-21 20:57:05 +01:00
chunksValue: '4',
outputSeparator: '\\n',
charBeforeChunk: '',
charAfterChunk: ''
};
const splitOperators: {
2024-06-21 20:06:07 +01:00
title: string;
description: string;
2024-06-21 20:57:05 +01:00
type: SplitOperatorType;
}[] = [
{
title: 'Use a Symbol for Splitting',
description:
'Character that will be used to\n' +
'break text into parts.\n' +
'(Space by default.)',
type: 'symbol'
},
2024-06-21 20:06:07 +01:00
{
title: 'Use a Regex for Splitting',
type: 'regex',
2024-06-21 20:57:05 +01:00
description:
'Regular expression that will be\n' +
2024-06-21 20:06:07 +01:00
'used to break text into parts.\n' +
'(Multiple spaces by default.)'
},
{
2024-06-21 20:57:05 +01:00
title: 'Use Length for Splitting',
description:
'Number of symbols that will be\n' + 'put in each output chunk.',
2024-06-21 20:06:07 +01:00
type: 'length'
},
{
2024-06-21 20:57:05 +01:00
title: 'Use a Number of Chunks',
description: 'Number of chunks of equal\n' + 'length in the output.',
2024-06-21 20:06:07 +01:00
type: 'chunks'
2024-06-21 20:57:05 +01:00
}
];
const outputOptions: {
description: string;
accessor: keyof typeof initialValues;
}[] = [
{
description:
'Character that will be put\n' +
'between the split chunks.\n' +
'(It\'s newline "\\n" by default.)',
accessor: 'outputSeparator'
},
{
description: 'Character before each chunk',
accessor: 'charBeforeChunk'
},
{
description: 'Character after each chunk',
accessor: 'charAfterChunk'
}
];
2024-06-21 21:59:17 +01:00
2025-02-27 02:21:08 +00:00
const exampleCards: CardExampleType<typeof initialValues>[] = [
{
title: 'Split German Numbers',
description:
'In this example, we break the text into pieces by two characters a comma and space. As a result, we get a column of numbers from 1 to 10 in German.',
sampleText: `1 - eins, 2 - zwei, 3 - drei, 4 - vier, 5 - fünf, 6 - sechs, 7 - sieben, 8 - acht, 9 - neun, 10 - zehn`,
sampleResult: `1 - eins
2 - zwei
3 - drei
4 - vier
5 - fünf
6 - sechs
7 - sieben
8 - acht
9 - neun
10 - zehn`,
sampleOptions: {
...initialValues,
symbolValue: ',',
splitSeparatorType: 'symbol',
outputSeparator: '\n'
}
},
{
title: 'Text Cleanup via a Regular Expression',
description:
'In this example, we use a super smart regular expression trick to clean-up the text. This regexp finds all non-alphabetic characters and splits the text into pieces by these non-alphabetic chars. As a result, we extract only those parts of the text that contain Latin letters and words.',
sampleText: `Finding%№1.65*;?words()is'12#easy_`,
sampleResult: `Finding
words
is
easy`,
sampleOptions: {
...initialValues,
regexValue: '[^a-zA-Z]+',
splitSeparatorType: 'regex',
outputSeparator: '\n'
}
},
{
title: 'Three-dot Output Separator',
description:
'This example splits the text by spaces and then places three dots between the words.',
sampleText: `If you started with $0.01 and doubled your money every day, it would take 27 days to become a millionaire.`,
sampleResult: `If...you...started...with...$0.01...and...doubled...your...money...every...day,...it...would...take...27...days...to...become...a...millionaire.!`,
sampleOptions: {
...initialValues,
symbolValue: '',
splitSeparatorType: 'symbol',
outputSeparator: '...'
}
}
];
export default function SplitText({ title }: ToolComponentProps) {
2024-06-21 20:57:05 +01:00
const [input, setInput] = useState<string>('');
const [result, setResult] = useState<string>('');
2025-03-09 18:06:27 +00:00
const computeExternal = (
optionsValues: typeof initialValues,
input: string
) => {
2024-06-26 07:47:17 +01:00
const {
splitSeparatorType,
outputSeparator,
charBeforeChunk,
charAfterChunk,
chunksValue,
symbolValue,
regexValue,
lengthValue
} = optionsValues;
2024-06-21 22:35:56 +01:00
2024-06-26 07:47:17 +01:00
setResult(
compute(
splitSeparatorType,
input,
symbolValue,
regexValue,
Number(lengthValue),
Number(chunksValue),
charBeforeChunk,
charAfterChunk,
outputSeparator
)
);
2024-06-21 21:59:17 +01:00
};
2024-06-21 20:06:07 +01:00
2024-06-19 21:18:35 +01:00
return (
2025-03-09 18:06:27 +00:00
<ToolContent
title={title}
input={input}
inputComponent={<ToolTextInput value={input} onChange={setInput} />}
resultComponent={<ToolTextResult title={'Text pieces'} value={result} />}
initialValues={initialValues}
getGroups={({ values, updateField }) => [
{
title: 'Split separator options',
component: splitOperators.map(({ title, description, type }) => (
<RadioWithTextField
key={type}
checked={type === values.splitSeparatorType}
title={title}
fieldName={'splitSeparatorType'}
description={description}
value={values[`${type}Value`]}
onRadioClick={() => updateField('splitSeparatorType', type)}
onTextChange={(val) => updateField(`${type}Value`, val)}
/>
))
},
{
title: 'Output separator options',
component: outputOptions.map((option) => (
<TextFieldWithDesc
key={option.accessor}
value={values[option.accessor]}
onOwnChange={(value) => updateField(option.accessor, value)}
description={option.description}
/>
))
}
]}
compute={computeExternal}
setInput={setInput}
exampleCards={exampleCards}
/>
2024-06-21 20:57:05 +01:00
);
2024-06-19 21:18:35 +01:00
}