-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (82 loc) · 2.83 KB
/
Copy pathindex.js
File metadata and controls
92 lines (82 loc) · 2.83 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
const arrify = require('arrify');
const pAny = require('p-any');
const pify = require('pify');
const pTimeout = require('p-timeout');
const uf = require('unfuck');
const esotericRegExps = {
'ook!': '(Ook.|Ook?|Ook!)+',
'pikalang': '(pi|pipi|pichu|ka|pikachu|pikapi|pika|chu)+',
'solbofuck': '((ROCK SOLBO(\\n)?)?BW(E){1,9})+',
'triplet': '([01]{3}(\s)?)+',
// 'pogaack': '((pogack(!|\\?))|(pogaack(!|\\?))|(pogaaack(!|\\?))|(po(o)?ck!)|poock\\?)+',
// 'namelesslanguage': '([01]{4})+',
'brainfuck': '[+-<>.,\\[\\]]+'
};
const esotericCommands = {
'ook!': ['Ook. Ook.', 'Ook! Ook!', 'Ook. Ook?', 'Ook? Ook.', 'Ook! Ook?', 'Ook? Ook!', 'Ook! Ook.', 'Ook. Ook!'],
'pikalang': ['pi', 'ka', 'pipi', 'pichu', 'pika', 'chu', 'pikachu', 'pikapi'],
'solbofuck': ['BWEEEE', 'BWEEEEE', 'BWEE', 'BWEEE', 'BWEEEEEEEE', 'BWEEEEEEEEE', 'BWEEEEEE', 'BWEEEEEEE'],
'triplet': ['111', '000', '001', '100', '110', '011', '010', '101'],
// 'pogaack': ['pogaaack!', 'poock!', 'pogack!', 'pogaack!', 'pogack? ', 'pogaack?', 'pogaaack?', 'poock?'], // 9th command is 'pock!'
'brainfuck': ['+', '-', '>', '<', '[', ']', '.', ',']
};
function detectLang (text) {
for (const lang in esotericRegExps) {
var detectedLang = '';
if (esotericRegExps.hasOwnProperty(lang)) {
var regExp = new RegExp(esotericRegExps[lang], 'g');
if (regExp.test(text)) {
detectedLang = lang;
break;
}
}
}
if (detectedLang.length > 1) {
return detectedLang;
} else {
return 'Code does not correspond to any of our supported essoteric languages';
}
}
function parseToArray (text, lang) {
if (lang === 'ook!') {
return text.match(/\S+\s+\S+/g);
} else if (lang === 'namelesslanguage') {
return text.match(/.{1,4}/g);
} else if (lang === 'solbofuck') {
return text.split(' ').slice(2);
} else {
return text.split(' ');
}
};
function interpret (text) {
const detectedLang = detectLang(text);
const parsedArray = parseToArray(text, detectedLang);
const detectedLangCommands = esotericCommands[detectedLang];
const bfCommands = esotericCommands['brainfuck'];
var bf = '';
if (detectedLang.includes('does not correspond to any of our supported essoteric languages')) {
return detectedLang
} else if (detectedLang !== 'brainfuck') {
for (const i in parsedArray) {
const command = parsedArray[i];
bf += bfCommands[detectedLangCommands.indexOf(command)];
}
} else {
bf = text;
}
const compiler = uf.compiler();
var plaintext = '';
try {
plaintext = compiler.run(bf, '');
} catch (err) {
throw new Error(err.message);
}
return plaintext;
};
module.exports = (dests, opts) => {
opts = opts || {};
opts.timeout = typeof opts.timeout === 'number' ? opts.timeout : 10000;
const p = pAny(arrify(dests).map(interpret));
return pTimeout(p, opts.timeout).catch(() => 'Esoteric code could not be interpreted');
};