Skip to content

Commit f266f06

Browse files
committed
Add URI encoding and decoding
1 parent 879d3a1 commit f266f06

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@
6161
</tr>
6262
</table>
6363

64+
<table>
65+
<tr>
66+
<th colspan="2">URI</th>
67+
</tr>
68+
<tr>
69+
<td>URI Encoding</td>
70+
<td id="uri_encoded_out"></td>
71+
</tr>
72+
<tr>
73+
<td>Component Encoding</td>
74+
<td id="uri_component_encoded_out"></td>
75+
</tr>
76+
<tr>
77+
<td>URI Decoding</td>
78+
<td id="uri_decoded_out"></td>
79+
</tr>
80+
<tr>
81+
<td>Component Decoding</td>
82+
<td id="uri_component_decoded_out"></td>
83+
</tr>
84+
</table>
85+
6486
<table id="chars"></table>
6587

6688
<footer class="faded">

js/script.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ function calculateAndPopulate(input) {
5454
const b64_decoding = b64Decode(input);
5555
setElementContentById("b64d_out", b64_decoding);
5656

57+
const uri_encoding = uriEncode(input);
58+
setElementContentById("uri_encoded_out", uri_encoding);
59+
60+
const uri_component_encoding = uriEncodeComponent(input);
61+
setElementContentById("uri_component_encoded_out", uri_component_encoding);
62+
63+
const uri_decoding = uriDecode(input);
64+
setElementContentById("uri_decoded_out", uri_decoding);
65+
66+
const uri_component_decoding = uriDecodeComponent(input);
67+
setElementContentById("uri_component_decoded_out", uri_component_decoding);
68+
5769
// Add character table to DOM
5870
character_table.innerHTML = characterTable(input);
5971
}
@@ -99,6 +111,30 @@ function b64Decode(input) {
99111
}
100112
}
101113

114+
// URL encoding/decoding
115+
function uriEncodeComponent(input) {
116+
return encodeURIComponent(input);
117+
}
118+
function uriDecodeComponent(input) {
119+
try {
120+
return decodeURIComponent(input);
121+
} catch (e) {
122+
console.error(e);
123+
return "Incompatible string";
124+
}
125+
}
126+
function uriEncode(input) {
127+
return encodeURI(input);
128+
}
129+
function uriDecode(input) {
130+
try {
131+
return decodeURI(input);
132+
} catch (e) {
133+
console.error(e);
134+
return "Incompatible string";
135+
}
136+
}
137+
102138
// Character table
103139
function characterTable(input) {
104140
if (precievedLength(input) > 512) {

0 commit comments

Comments
 (0)