diff --git a/src/pages/tools/string/url-encode/index.tsx b/src/pages/tools/string/url-encode/index.tsx new file mode 100644 index 0000000..27d81f5 --- /dev/null +++ b/src/pages/tools/string/url-encode/index.tsx @@ -0,0 +1,98 @@ +import { Box } from '@mui/material'; +import { useState } from 'react'; +import ToolTextResult from '@components/result/ToolTextResult'; +import { GetGroupsType } from '@components/options/ToolOptions'; +import { encodeString } from './service'; +import ToolTextInput from '@components/input/ToolTextInput'; +import { InitialValuesType } from './types'; +import ToolContent from '@components/ToolContent'; +import { CardExampleType } from '@components/examples/ToolExamples'; +import { ToolComponentProps } from '@tools/defineTool'; +import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; +import { useTranslation } from 'react-i18next'; + +const initialValues: InitialValuesType = { + nonSpecialChar: false +}; + +const exampleCards: CardExampleType[] = [ + { + title: 'Encode an actual URL', + description: + 'This example URL-encodes a string that also happens to be a valid web link. Special characters in this example are a colon, slash, question mark and equals sign.', + sampleText: 'https://omnitools.app/', + sampleResult: 'https%3A%2F%2Fomnitools.app%2F', + sampleOptions: initialValues + }, + { + title: 'Encode All Characters', + description: + "In this example, we've enabled the option that encodes absolutely all characters in a string to URL-encoding. This option makes non-special characters, such as letters get encoded to their hex codes prefixed by a percent sign.", + sampleText: "I can't believe it's not butter!", + sampleResult: + '%49%20%63%61%6E%27%74%20%62%65%6C%69%65%76%65%20%69%74%27%73%20%6E%6F%74%20%62%75%74%74%65%72%21', + sampleOptions: { + nonSpecialChar: true + } + } +]; + +export default function EncodeString({ + title, + longDescription +}: ToolComponentProps) { + const { t } = useTranslation('string'); + const [input, setInput] = useState(''); + const [result, setResult] = useState(''); + + function compute(initialValues: InitialValuesType, input: string) { + setResult(encodeString(input, initialValues)); + } + + const getGroups: GetGroupsType = ({ + values, + updateField + }) => [ + { + title: t('urlEncode.encodingOption.title'), + component: ( + + updateField('nonSpecialChar', value)} + title={t('urlEncode.encodingOption.nonSpecialCharPlaceholder')} + description={t( + 'urlEncode.encodingOption.nonSpecialCharDescription' + )} + /> + + ) + } + ]; + + return ( + + } + resultComponent={ + + } + toolInfo={{ + title: t('urlEncode.toolInfo.title', { title }), + description: longDescription + }} + exampleCards={exampleCards} + /> + ); +} diff --git a/src/pages/tools/string/url-encode/meta.ts b/src/pages/tools/string/url-encode/meta.ts new file mode 100644 index 0000000..3774fca --- /dev/null +++ b/src/pages/tools/string/url-encode/meta.ts @@ -0,0 +1,16 @@ +import { defineTool } from '@tools/defineTool'; +import { lazy } from 'react'; + +export const tool = defineTool('string', { + path: 'url-encode-string', + icon: 'ic:baseline-percentage', + + keywords: ['uppercase'], + component: lazy(() => import('./index')), + i18n: { + name: 'string:urlEncode.toolInfo.title', + description: 'string:urlEncode.toolInfo.description', + shortDescription: 'string:urlEncode.toolInfo.shortDescription', + longDescription: 'string:urlEncode.toolInfo.longDescription' + } +}); diff --git a/src/pages/tools/string/url-encode/service.ts b/src/pages/tools/string/url-encode/service.ts new file mode 100644 index 0000000..207fb30 --- /dev/null +++ b/src/pages/tools/string/url-encode/service.ts @@ -0,0 +1,18 @@ +import { InitialValuesType } from './types'; + +export function encodeString( + input: string, + options: InitialValuesType +): string { + if (!input) return ''; + if (!options.nonSpecialChar) { + return encodeURIComponent(input); + } + + let result = ''; + for (const char of input) { + const hex = char.codePointAt(0)!.toString(16).toUpperCase(); + result += '%' + hex.padStart(2, '0'); + } + return result; +} diff --git a/src/pages/tools/string/url-encode/types.ts b/src/pages/tools/string/url-encode/types.ts new file mode 100644 index 0000000..2d4d3f0 --- /dev/null +++ b/src/pages/tools/string/url-encode/types.ts @@ -0,0 +1,3 @@ +export type InitialValuesType = { + nonSpecialChar: boolean; +};