/
app.js
175 lines (158 loc) · 3.62 KB
/
app.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
/**
* REPL (read-evaluate-print loop)
*/
class REPL {
/**
* @constructor
*/
constructor({input, output, console}) {
this.elements = {input, output};
this._context = {};
this._history = [];
this._historyIndex = 0;
this._hijack(console);
window.addEventListener('resize', this._scroll.bind(this), false);
}
/**
* @method
*/
clear() {
this.elements.input.value = '';
this._historyIndex = 0;
}
/**
* @method
*/
prev() {
if (!this._history.length) return
this._historyIndex = Math.min(++this._historyIndex, this._history.length);
const index = this._history.length - this._historyIndex;
this.elements.input.value = this._history[index];
}
/**
* @method
*/
next() {
if (!this._history.length) return;
this._historyIndex = Math.max(--this._historyIndex, 1);
const index = this._history.length - this._historyIndex;
this.elements.input.value = this._history[index];
}
/**
* @method
*/
run() {
this._read();
this.elements.input.value = '';
this._historyIndex = 0;
}
/**
* @method
* @private
* @param {console} console Console object
*/
_hijack(console) {
for (let key in console) {
console[key] = (...args) => this._print(key, ...args);
}
}
/**
* @method
* @private
*/
_read() {
this._evaluate(this.elements.input.value);
}
/**
* @method
* @private
* @param {string} input Input string
*/
_evaluate(input) {
this._history.push(input);
this._print('echo', input);
try {
this._print('return', eval.call(this._context, input));
} catch(e) {
this._print('error', e);
}
}
/**
* Push data to output list
* @method
* @private
* @param {string} logType Console method
* @param {...*} data Data to pass to the method
*/
_print(logType, ...data) {
const formatted = this._format(logType, ...data);
const listNode = document.createElement('li');
const textNode = document.createTextNode(formatted);
listNode.classList.add(logType);
listNode.appendChild(textNode);
this.elements.output.appendChild(listNode);
this._scroll();
}
/**
* Format data as a string
* @private
* @param {string} logType Console method
* @param {...*} data Data to pass to the method
*/
_format(logType, ...data) {
if (logType === 'log' || logType === 'return') {
data = data.map(item => {
const type = this._type(item);
switch (type) {
case 'Array':
case 'Boolean':
case 'Number':
case 'Object':
case 'String':
return JSON.stringify(item);
case 'Class':
case 'Function':
return item.toString();
case 'Undefined':
return 'undefined'
default:
return `${type} {}`;
}
});
}
return data.join(', ');
}
/**
* A better typeof
* @method
* @private
* @param {*} thing Thing to find the type of
*/
_type(thing) {
const toString = Object.prototype.toString.call(thing);
return toString.replace(/\[\w+ (\w+)\]/, '$1');
}
/**
* Scroll output to the bottom
* @method
* @private
*/
_scroll() {
this.elements.output.scrollTop = this.elements.output.scrollHeight;
}
}
/**
* Expose repl instance to the global scope
*/
window.repl = new REPL({
input: document.querySelector('textarea'),
output: document.querySelector('ul'),
console: window.console
});
/**
* Register service worker
*/
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js');
}