Input
Output
/*
Backspace is replaced with \b
Newline is replaced with \n
Tab is replaced with \t
Carriage return is replaced with \r
Form feed is replaced with \f
Double quote is replaced with \"
Backslash is replaced with \\
*/
const $ = document.querySelector.bind(document);
const on = (e, evt, fn) => e.addEventListener(evt, fn);
let quote = '"';
const encodeR = {
"\b": "\\b",
"\n": "\\n",
"\t": "\\t",
"\r": "\\r",
"\f": "\\f",
"\"": "\\\"",
"\\": "\\\\",
};
const decodeR = {
"\\b": "\b",
"\\n": "\n",
"\\t": "\t",
"\\r": "\r",
"\\f": "\f",
"\\\"": "\"",
"\\\\": "\\"
};
const switchQuote = () => {
const useSingleQuote = $("#chkSingleQuote").checked;
quote = useSingleQuote ? "'" : '"';
if (quote === "'") {
encodeR["\'"] = "\\\'";
delete encodeR["\""];
decodeR["\\\'"] = "\'";
delete decodeR["\\\""];
} else {
encodeR["\""] = "\\\"";
delete encodeR["\'"];
decodeR["\\\""] = "\"";
delete decodeR["\\\'"];
}
}
const encode = (text) => {
let res = "";
for (const c of String(text)) {
if (c in encodeR) {
res += encodeR[c];
} else {
res += c;
}
}
return res;
};
const decode = (text) => {
return String(text).replace(/(\\.)/gm, (group) => {
if (group in decodeR) {
return decodeR[group];
}
return group;
});
};
const buildString = (text) => {
var lines = text.split('\n');
return lines
.map(l => '\t' + quote + encode(l) + quote)
.join(",\n");
};
const buildRust = (text) => {
var lines = text.split('\n');
const body = lines
.map(l => '\t' + quote + encode(l) + quote)
.join(",\n");
return "// Javascript / Rust\nlet sql = [\n" + body + "\n].join(\"\\n\");";
};
// reverse buildstring
const stringPart = /"(.*?[^\\])"/gm;
const stringPartSq = /'(.*?[^\\])'/gm;
const getMatches = (re, str) => {
let match, matches = [];
while (match = re.exec(str)) {
matches.push(match);
}
return matches;
};
const reverseBuildString = (text) => {
const regEx = quote === "'" ? stringPartSq : stringPart;
const lines = text.split('\n');
const resultLines = [];
for (const line of lines) {
const str = getMatches(regEx, line).map(m => decode(m[1])).join("").trim();
if (str.length > 0) resultLines.push(str);
}
return resultLines.join("\n");
};
function copyStringToClipboard(string) {
function handler(event) {
event.clipboardData.setData('text/plain', string);
event.preventDefault();
document.removeEventListener('copy', handler, true);
}
document.addEventListener('copy', handler, true);
document.execCommand('copy');
}
on(window, "DOMContentLoaded", () => {
const btnEncode = $("#btnEncode");
const btnDecode = $("#btnDecode");
const btnBuildString = $("#btnBuildString");
const btnReverseBuildString = $("#btnReverseBuildString");
if (!btnEncode || !btnDecode || !btnBuildString || !btnReverseBuildString) {
return;
}
const prefix = "// C#\nvar sql = string.Join(Environment.NewLine, new string[] {\n";
const postfix = "\n});";
const getEditor = (id) => {
const api = window.NielsAceFrontend;
if (!api || typeof api.getEditor !== "function") {
return null;
}
return api.getEditor(id);
};
const bindApp = (inputEditor, outputEditor) => {
if (!inputEditor || !outputEditor) {
return;
}
const readInput = () => String(inputEditor.getValue());
const writeOutput = (value) => {
outputEditor.setValue(value, -1);
};
outputEditor.setReadOnly(true);
function run(fn) {
return () => {
switchQuote();
const result = fn();
writeOutput(result);
copyStringToClipboard(result);
};
}
btnEncode.addEventListener("click", run(() => encode(readInput().trim())));
btnDecode.addEventListener("click", run(() => decode(readInput().trim())));
btnBuildString.addEventListener("click", run(() => {
const source = readInput().trim();
const csharpOutput = prefix + buildString(source) + postfix;
const rustOutput = buildRust(source);
return csharpOutput + "\n\n" + rustOutput;
}));
btnReverseBuildString.addEventListener("click", run(() => reverseBuildString(readInput().trim())));
};
const tryInit = (retriesLeft) => {
const inputEditor = getEditor("aceInputEditor");
const outputEditor = getEditor("aceOutputEditor");
if (inputEditor && outputEditor) {
bindApp(inputEditor, outputEditor);
return;
}
if (retriesLeft <= 0) {
return;
}
window.setTimeout(() => tryInit(retriesLeft - 1), 100);
};
tryInit(20);
});274050cookie-checkEncode/Decode javascript/C# strings