Skip to content

Commit fc18dc0

Browse files
authored
Merge pull request #208
feat: add custom URL encoder and decoder with full percent-encoding support
2 parents 38c556a + a54e8be commit fc18dc0

File tree

9 files changed

+255
-1
lines changed

9 files changed

+255
-1
lines changed

public/locales/en/string.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,31 @@
257257
"resultTitle": "Uppercase text",
258258
"shortDescription": "Convert text to uppercase",
259259
"title": "Convert to Uppercase"
260+
},
261+
"urlEncode": {
262+
"toolInfo": {
263+
"description": "Load your string and it will automatically get URL-escaped.",
264+
"shortDescription": "Quickly URL-escape a string.",
265+
"longDescription": "This tool URL-encodes a string. Special URL characters get converted to percent-sign encoding. This encoding is called percent-encoding because each character's numeric value gets converted to a percent sign followed by a two-digit hexadecimal value. The hex values are determined based on the character's codepoint value. For example, a space gets escaped to %20, a colon to %3a, a slash to %2f. Characters that are not special stay unchanged. In case you also need to convert non-special characters to percent-encoding, then we've also added an extra option that lets you do that. Select the encode-non-special-chars option to enable this behavior.",
266+
"title": "String URL encoder"
267+
},
268+
"encodingOption": {
269+
"title": "Encoding Options",
270+
"nonSpecialCharPlaceholder": "Encode non-special characters",
271+
"nonSpecialCharDescription": "If selected, then all characters in the input string will be converted to URL-encoding (not just special)."
272+
},
273+
"inputTitle": "Input String",
274+
"resultTitle": "Url-escaped String"
275+
},
276+
"urlDecode": {
277+
"toolInfo": {
278+
"description": "Load your string and it will automatically get URL-unescaped.",
279+
"shortDescription": "Quickly URL-unescape a string.",
280+
"longDescription": "This tool URL-decodes a previously URL-encoded string. URL-decoding is the inverse operation of URL-encoding. All percent-encoded characters get decoded to characters that you can understand. Some of the most well known percent-encoded values are %20 for a space, %3a for a colon, %2f for a slash, and %3f for a question mark. The two digits following the percent sign are character's char code values in hex.",
281+
"title": "String URL decoder"
282+
},
283+
284+
"inputTitle": "Input String(URL-escaped)",
285+
"resultTitle": "Output string"
260286
}
261287
}

src/pages/tools/string/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { tool as stringBase64 } from './base64/meta';
1818
import { tool as stringStatistic } from './statistic/meta';
1919
import { tool as stringCensor } from './censor/meta';
2020
import { tool as stringPasswordGenerator } from './password-generator/meta';
21+
import { tool as stringEncodeUrl } from './url-encode/meta';
22+
import { tool as StringDecodeUrl } from './url-decode/meta';
2123

2224
export const stringTools = [
2325
stringSplit,
@@ -39,5 +41,7 @@ export const stringTools = [
3941
stringBase64,
4042
stringStatistic,
4143
stringCensor,
42-
stringPasswordGenerator
44+
stringPasswordGenerator,
45+
stringEncodeUrl,
46+
StringDecodeUrl
4347
];
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { useState } from 'react';
2+
import ToolTextResult from '@components/result/ToolTextResult';
3+
import { decodeString } from './service';
4+
import ToolTextInput from '@components/input/ToolTextInput';
5+
import ToolContent from '@components/ToolContent';
6+
import { CardExampleType } from '@components/examples/ToolExamples';
7+
import { ToolComponentProps } from '@tools/defineTool';
8+
import { useTranslation } from 'react-i18next';
9+
10+
const initialValues = {};
11+
12+
const exampleCards: CardExampleType<typeof initialValues>[] = [
13+
{
14+
title: 'Decode an actual URL',
15+
description:
16+
'This example decodes a URL-encoded string back to its readable URL form.',
17+
sampleText: 'https%3A%2F%2Fomnitools.app%2F',
18+
sampleResult: 'https://omnitools.app/',
19+
sampleOptions: initialValues
20+
},
21+
{
22+
title: 'Decode All Characters',
23+
description:
24+
'This example decodes a string where every character has been URL-encoded, restoring the original readable text.',
25+
sampleText:
26+
'%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',
27+
sampleResult: "I can't believe it's not butter!",
28+
sampleOptions: initialValues
29+
}
30+
];
31+
32+
export default function DecodeString({
33+
title,
34+
longDescription
35+
}: ToolComponentProps) {
36+
const { t } = useTranslation('string');
37+
const [input, setInput] = useState<string>('');
38+
const [result, setResult] = useState<string>('');
39+
40+
function compute(_initialValues: typeof initialValues, input: string) {
41+
setResult(decodeString(input));
42+
}
43+
44+
return (
45+
<ToolContent
46+
title={title}
47+
initialValues={initialValues}
48+
getGroups={null}
49+
compute={compute}
50+
input={input}
51+
setInput={setInput}
52+
inputComponent={
53+
<ToolTextInput
54+
title={t('urlDecode.inputTitle')}
55+
value={input}
56+
onChange={setInput}
57+
/>
58+
}
59+
resultComponent={
60+
<ToolTextResult title={t('urlDecode.resultTitle')} value={result} />
61+
}
62+
toolInfo={{
63+
title: t('urlDecode.toolInfo.title', { title }),
64+
description: longDescription
65+
}}
66+
exampleCards={exampleCards}
67+
/>
68+
);
69+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineTool } from '@tools/defineTool';
2+
import { lazy } from 'react';
3+
4+
export const tool = defineTool('string', {
5+
path: 'url-decode-string',
6+
icon: 'codicon:symbol-string',
7+
8+
keywords: ['uppercase'],
9+
component: lazy(() => import('./index')),
10+
i18n: {
11+
name: 'string:urlDecode.toolInfo.title',
12+
description: 'string:urlDecode.toolInfo.description',
13+
shortDescription: 'string:urlDecode.toolInfo.shortDescription',
14+
longDescription: 'string:urlDecode.toolInfo.longDescription'
15+
}
16+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function decodeString(input: string): string {
2+
if (!input) return '';
3+
return decodeURIComponent(input);
4+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { Box } from '@mui/material';
2+
import { useState } from 'react';
3+
import ToolTextResult from '@components/result/ToolTextResult';
4+
import { GetGroupsType } from '@components/options/ToolOptions';
5+
import { encodeString } from './service';
6+
import ToolTextInput from '@components/input/ToolTextInput';
7+
import { InitialValuesType } from './types';
8+
import ToolContent from '@components/ToolContent';
9+
import { CardExampleType } from '@components/examples/ToolExamples';
10+
import { ToolComponentProps } from '@tools/defineTool';
11+
import CheckboxWithDesc from '@components/options/CheckboxWithDesc';
12+
import { useTranslation } from 'react-i18next';
13+
14+
const initialValues: InitialValuesType = {
15+
nonSpecialChar: false
16+
};
17+
18+
const exampleCards: CardExampleType<InitialValuesType>[] = [
19+
{
20+
title: 'Encode an actual URL',
21+
description:
22+
'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.',
23+
sampleText: 'https://omnitools.app/',
24+
sampleResult: 'https%3A%2F%2Fomnitools.app%2F',
25+
sampleOptions: initialValues
26+
},
27+
{
28+
title: 'Encode All Characters',
29+
description:
30+
"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.",
31+
sampleText: "I can't believe it's not butter!",
32+
sampleResult:
33+
'%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',
34+
sampleOptions: {
35+
nonSpecialChar: true
36+
}
37+
}
38+
];
39+
40+
export default function EncodeString({
41+
title,
42+
longDescription
43+
}: ToolComponentProps) {
44+
const { t } = useTranslation('string');
45+
const [input, setInput] = useState<string>('');
46+
const [result, setResult] = useState<string>('');
47+
48+
function compute(initialValues: InitialValuesType, input: string) {
49+
setResult(encodeString(input, initialValues));
50+
}
51+
52+
const getGroups: GetGroupsType<InitialValuesType> = ({
53+
values,
54+
updateField
55+
}) => [
56+
{
57+
title: t('urlEncode.encodingOption.title'),
58+
component: (
59+
<Box>
60+
<CheckboxWithDesc
61+
checked={values.nonSpecialChar}
62+
onChange={(value) => updateField('nonSpecialChar', value)}
63+
title={t('urlEncode.encodingOption.nonSpecialCharPlaceholder')}
64+
description={t(
65+
'urlEncode.encodingOption.nonSpecialCharDescription'
66+
)}
67+
/>
68+
</Box>
69+
)
70+
}
71+
];
72+
73+
return (
74+
<ToolContent
75+
title={title}
76+
initialValues={initialValues}
77+
getGroups={getGroups}
78+
compute={compute}
79+
input={input}
80+
setInput={setInput}
81+
inputComponent={
82+
<ToolTextInput
83+
title={t('urlEncode.inputTitle')}
84+
value={input}
85+
onChange={setInput}
86+
/>
87+
}
88+
resultComponent={
89+
<ToolTextResult title={t('urlEncode.resultTitle')} value={result} />
90+
}
91+
toolInfo={{
92+
title: t('urlEncode.toolInfo.title', { title }),
93+
description: longDescription
94+
}}
95+
exampleCards={exampleCards}
96+
/>
97+
);
98+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineTool } from '@tools/defineTool';
2+
import { lazy } from 'react';
3+
4+
export const tool = defineTool('string', {
5+
path: 'url-encode-string',
6+
icon: 'ic:baseline-percentage',
7+
8+
keywords: ['uppercase'],
9+
component: lazy(() => import('./index')),
10+
i18n: {
11+
name: 'string:urlEncode.toolInfo.title',
12+
description: 'string:urlEncode.toolInfo.description',
13+
shortDescription: 'string:urlEncode.toolInfo.shortDescription',
14+
longDescription: 'string:urlEncode.toolInfo.longDescription'
15+
}
16+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { InitialValuesType } from './types';
2+
3+
export function encodeString(
4+
input: string,
5+
options: InitialValuesType
6+
): string {
7+
if (!input) return '';
8+
if (!options.nonSpecialChar) {
9+
return encodeURIComponent(input);
10+
}
11+
12+
let result = '';
13+
for (const char of input) {
14+
const hex = char.codePointAt(0)!.toString(16).toUpperCase();
15+
result += '%' + hex.padStart(2, '0');
16+
}
17+
return result;
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type InitialValuesType = {
2+
nonSpecialChar: boolean;
3+
};

0 commit comments

Comments
 (0)