Skip to content

Commit

Permalink
More encodings!
Browse files Browse the repository at this point in the history
Base10, and a new URL mode
  • Loading branch information
ghluka committed Apr 27, 2024
1 parent eeb9c62 commit 2769c42
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion techdemos/encode/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Mode:
<textarea id="outputText" rows="10" class="panel" spellcheck="false" style="resize:none; width:100%;"></textarea>

<!-- Encode/Decode -->
<script src="./js/main.js"></script>
<script src="./js/main.js" type="module"></script>
29 changes: 29 additions & 0 deletions techdemos/encode/js/encoders/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const encodeURIAggressive = text => "%" + text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("%").toUpperCase();
const decodeURIAggressive = text => text.split("%").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join("");

function encodeURINonDestructive(text) {
var encoded_text = encodeURIAggressive(text);

if (encoded_text.includes("%3A%2F%2F")) {
encoded_text = encoded_text.replace("%3A%2F%2F", "://");
const protocol = decodeURIAggressive(encoded_text.split("://")[0])
encoded_text = protocol + "://" + encoded_text.split("://")[1];
}

encoded_text = encoded_text.replaceAll("%2F", "/").replaceAll("%3F", "?").replaceAll("%26", "&").replaceAll("%2B", "+").replaceAll("%3D", "=");

return encoded_text;
}

function decodeURINonDestructive(text) {
if (text.includes("://")) {
const protocol = encodeURIAggressive(text.split("://")[0]);
text = protocol + "%3A%2F%2F" + text.split("://")[1];
}

text = text.replaceAll("/", "%2F").replaceAll("?", "%3F").replaceAll("&", "%26").replaceAll("+", "%2B").replaceAll("=", "%3D");

return decodeURIAggressive(text);
}

export {encodeURIAggressive, decodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive}
16 changes: 14 additions & 2 deletions techdemos/encode/js/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {encodeURIAggressive, decodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive} from "./encoders/url.js";

const mode = document.getElementById("mode");
const encode = document.getElementById("encode");
const decode = document.getElementById("decode");
const livemode = document.getElementById("livemode");
const input = document.getElementById("inputText");
const output = document.getElementById("outputText");

const codes = {
"Base64": {
encode: text => btoa(text),
Expand All @@ -13,6 +16,10 @@ const codes = {
encode: text => text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("").toUpperCase(),
decode: text => text.split(/(\w\w)/g).filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join("")
},
"Base10 (Decimal)": {
encode: text => text.split("").map(c => c.charCodeAt(0).toString(10).padStart(3, "0")).join(" ").toUpperCase(),
decode: text => text.split(" ").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 10))).join("")
},
"Base2 (Binary)": {
encode: text => text.split("").map(c => c.charCodeAt(0).toString(2).padStart(8, "0")).join(" "),
decode: text => text.split(" ").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 2))).join("")
Expand All @@ -26,10 +33,15 @@ const codes = {
decode: text => decodeURIComponent(text)
},
"URL (aggressive)": {
encode: text => "%" + text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("%").toUpperCase(),
decode: text => text.split("%").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join("")
encode: encodeURIAggressive,
decode: decodeURIAggressive
},
"URL (aggressive, non destructive)": {
encode: encodeURINonDestructive,
decode: decodeURINonDestructive
},
}

for (const code in codes) {
let element = document.createElement("option");
element.value = code;
Expand Down

0 comments on commit 2769c42

Please sign in to comment.