forked from mathiasbynens/mothereff.in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheff.js
122 lines (107 loc) · 3.7 KB
/
eff.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(function(window, document) {
var textarea = document.getElementsByTagName('textarea')[0];
var characters = document.getElementById('characters');
var pre = document.getElementsByTagName('pre')[0];
var output = document.getElementById('output');
var permalink = document.getElementById('permalink');
var dds = document.getElementsByTagName('dd');
var before = dds[0];
var after = dds[1];
var ratio = dds[2];
var regexNotBrainfuck = /[^\+\-<>\[\],\.]/g;
var regexNumberGroup = /(?=(?:\d{3})+$)(?!\b)/g;
// https://mathiasbynens.be/notes/localstorage-pattern
var storage = (function() {
var uid = new Date;
var storage;
var result;
try {
(storage = window.localStorage).setItem(uid, uid);
result = storage.getItem(uid) == uid;
storage.removeItem(uid);
return result && storage;
} catch (exception) {}
}());
var characterReferences;
// Taken from https://mths.be/punycode
function ucs2decode(string) {
var output = [],
counter = 0,
length = string.length,
value,
extra;
while (counter < length) {
value = string.charCodeAt(counter++);
if ((value & 0xF800) == 0xD800) {
extra = string.charCodeAt(counter++);
if ((value & 0xFC00) != 0xD800 || (extra & 0xFC00) != 0xDC00) {
throw Error('Illegal UCS-2 sequence');
}
value = ((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000;
}
output.push(value);
}
return output;
}
function encode(string) {
// URL-encode some more characters to avoid issues when using permalink URLs in Markdown
return encodeURIComponent(string).replace(/['()_*]/g, function(character) {
return '%' + character.charCodeAt().toString(16);
});
}
function text(el, str) {
if (str == null) {
return el.innerText || el.textContent;
}
el.innerText != null && (el.innerText = str);
el.textContent != null && (el.textContent = str);
}
function formatNumber(number, unit) {
return (number == 0 ? '0' : String(number).replace(regexNumberGroup, ',')) + ' ' + unit + (number == 1 ? '' : 's');
}
function charCount(string) {
return ucs2decode(string).length;
}
function byteCount(string) {
return ~-encodeURI(string).split(/%..|./).length; // https://gist.github.com/1010324
}
function update() {
var value = textarea.value,
result = value.replace(regexNotBrainfuck, ''),
originalCharacterCount = charCount(value),
originalByteCount = byteCount(value),
resultingByteCount = byteCount(result); // byte and character counts are the same at this point
text(output, result || '[no output]');
pre.className = resultingByteCount ? '' : 'fail';
text(before, formatNumber(originalByteCount, 'byte') + (originalCharacterCount == originalByteCount ? '' : ' (assuming UTF-8); ' + formatNumber(originalCharacterCount, 'character')));
text(after, formatNumber(resultingByteCount, 'byte'));
text(ratio, (originalByteCount ? ((originalByteCount - resultingByteCount) / originalByteCount * 100) : 0).toFixed(2) + '%');
permalink.hash = encode(value);
storage && (storage.brainfuck = value);
};
// https://mathiasbynens.be/notes/oninput
textarea.onkeyup = update;
textarea.oninput = function() {
textarea.onkeyup = null;
update();
};
if (storage) {
storage.brainfuck && (textarea.value = storage.brainfuck);
update();
}
window.onhashchange = function() {
textarea.value = decodeURIComponent(location.hash.slice(1));
update();
};
if (location.hash) {
window.onhashchange();
}
}(this, document));
// Google Analytics
window._gaq = [['_setAccount', 'UA-6065217-60'], ['_trackPageview']];
(function(d, t) {
var g = d.createElement(t);
var s = d.getElementsByTagName(t)[0];
g.src = 'https://www.google-analytics.com/ga.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));