Skip to content

Instantly share code, notes, and snippets.

@godhand4826
Last active March 16, 2024 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godhand4826/a5d04e96d4993bf722ce69d837b4d87b to your computer and use it in GitHub Desktop.
Save godhand4826/a5d04e96d4993bf722ce69d837b4d87b to your computer and use it in GitHub Desktop.
A jsFuck transpiler written in 20 lines.
let dict = {};
const digit = (d) => '(+[])' + '+(+!![])'.repeat(d);
const number = (n) => `+(${n.toString().split('').map(d => `[${digit(+d)}]`).join('+')})`;
const fill = (s) => eval(s).split('').forEach((c, i) => (dict[c] ??= `(${s})[${number(i)}]`));
const word = (s) => s.split('').map(c => dict[c] ?? c.is.not.exist.in.dict).join('+');
// at -> constructor -> return btoa -> toString -> fromCharCode -> *
fill('[]+![]'); // "false"
fill('[]+!![]'); // "true"
fill(`[]+[][${word('at')}]`); // "function at() { [native code] }"
fill(`[]+(+[])[${word('constructor')}]`); // "function Number() { [native code] }"
fill(`[][${word('at')}][${word('constructor')}](${word('return btoa')})()(${word(' ')})`); // "ICA="
fill(`[]+([]+[])[${word('constructor')}]`); // "function String() { [native code] }"
fill(`(${number(17)})[${word('toString')}](${number(36)})`); // "h"
const char = (c) => (dict[c] ??= `([]+[])[${word('constructor')}][${word('fromCharCode')}](${number(c.charCodeAt(0))})`);
const string = (s) => s.split('').map(char).join('+');
const transpile = (code) => `[][${word('at')}][${word('constructor')}](${string(code)})()`;
const run = (code) => eval(transpile(code));
run("console.log(('b' + 'a' + + 'a' + 'a').toLowerCase());");
// process.stdin
// .once('data', () => process.stdout.write(`[][${word('at')}][${word('constructor')}](`))
// .on('data', (data) => process.stdout.write(`${string(data.toString())}+`))
// .once('end', () => process.stdout.write(`${digit(0)})()`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment