forked from lowbyteproductions/JavaScript-Is-Weird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweird.js
62 lines (52 loc) · 2.02 KB
/
weird.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const zero = '+[]';
const one = '+!![]';
const number = n => {
if (n === 0) return zero;
return Array.from({length: n}, () => one).join(' + ');
}
// C
const map = {};
const fromString = s =>s.split('').map(x => {
if (!(x in map)) {
const charCode = x.charCodeAt(0);
return `([]+[])[${fromString('constructor')}][${fromString('fromCharCode')}](${number(charCode)})`;
}
return map[x];
}).join('+');
map.a = `(+{}+[])[${number(1)}]`;
map.b = `({}+[])[${number(2)}]`;
map.o = `({}+[])[${number(1)}]`;
map.e = `({}+[])[${number(4)}]`;
map.c = `({}+[])[${number(5)}]`;
map.t = `({}+[])[${number(6)}]`;
map[' '] = `({}+[])[${number(7)}]`;
map.f = `(![]+[])[${number(0)}]`;
map.s = `(![]+[])[${number(3)}]`;
map.r = `(!![]+[])[${number(1)}]`;
map.u = `(!![]+[])[${number(2)}]`;
map.i = `((+!![]/+[])+[])[${number(3)}]`;
map.n = `((+!![]/+[])+[])[${number(4)}]`;
map.S = `([]+([]+[])[${fromString('constructor')}])[${number(9)}]`;
map.g = `([]+([]+[])[${fromString('constructor')}])[${number(14)}]`;
map.p = `([]+(/-/)[${fromString('constructor')}])[${number(14)}]`;
map['\\'] = `(/\\\\/+[])[${number(1)}]`;
map.d = `(${number(13)})[${fromString('toString')}](${number(14)})`;
map.h = `(${number(17)})[${fromString('toString')}](${number(18)})`;
map.m = `(${number(22)})[${fromString('toString')}](${number(23)})`;
map.C = `((()=>{})[${fromString('constructor')}](${fromString('return escape')})()(${map['\\']}))[${number(2)}]`;
const compile = code => `(()=>{})[${fromString('constructor')}](${fromString(code)})()`;
const fs = require('fs');
// Check if a filename is provided as a command line argument
if (process.argv.length < 3) {
console.error('Usage: node weird.js <filename>');
process.exit(1); // Exit with an error code
}
const filename = process.argv[2]; // Get the filename from the command line arguments
// Use fs.readFile to read the content of the file
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading the file: ${err.message}`);
} else {
console.log(compile(data));
}
});