Skip to content

Instantly share code, notes, and snippets.

@nathanhleung
Last active January 13, 2024 23:42
Show Gist options
  • Save nathanhleung/8eebf17c1021322dd11e654c7ceade50 to your computer and use it in GitHub Desktop.
Save nathanhleung/8eebf17c1021322dd11e654c7ceade50 to your computer and use it in GitHub Desktop.
// https://www.youtube.com/watch?v=N-_XmLanPYg
const BACKSLASH = String.fromCharCode(92);
const BACKTICK = String.fromCharCode(96);
function A() {
const program = `function B(str: string) {
function escapeProgram(str: string) {
return str
.replaceAll("\`", BACKSLASH + "\`")
.replaceAll("\${", BACKSLASH + "\${");
}
function escapeStr(str: string) {
return str
.replaceAll(
BACKTICK + "\${escapeProgram(str)}" + BACKTICK,
BACKSLASH + BACKTICK + "\${escapeProgram(str)}" +
BACKSLASH + BACKTICK
);
}
function q(str: string) {
return \`// https://www.youtube.com/watch?v=N-_XmLanPYg
const BACKSLASH = String.fromCharCode(92);
const BACKTICK = String.fromCharCode(96);
function A() {
const program = \`\${escapeProgram(str)}\`;
return program;
}\`
}
const A = q(str);
console.log(A);
console.log();
console.log(escapeStr(str));
console.log();
console.log("B(A());")
}`;
return program;
}
function B(str: string) {
function escapeProgram(str: string) {
return str
.replaceAll("`", BACKSLASH + "`")
.replaceAll("${", BACKSLASH + "${");
}
function escapeStr(str: string) {
return str
.replaceAll(
BACKTICK + "${escapeProgram(str)}" + BACKTICK,
BACKSLASH + BACKTICK + "${escapeProgram(str)}" +
BACKSLASH + BACKTICK
);
}
function q(str: string) {
return `// https://www.youtube.com/watch?v=N-_XmLanPYg
const BACKSLASH = String.fromCharCode(92);
const BACKTICK = String.fromCharCode(96);
function A() {
const program = \`${escapeProgram(str)}\`;
return program;
}`
}
const A = q(str);
console.log(A);
console.log();
console.log(escapeStr(str));
console.log();
console.log("B(A());")
}
B(A());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment