Repository navigation
Expand file tree
/
Copy pathexample.html
More file actions
232 lines (194 loc) · 4.36 KB
/
Copy pathexample.html
File metadata and controls
232 lines (194 loc) · 4.36 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Verse Demo</title>
<style type="text/css">
#terminal > div {
font-family: monospace;
white-space: pre-wrap;
font-size: 13px;
}
#logs {
height: 70px;
border-bottom: 1px solid black;
position: relative;
}
#log-items {
width: 100%;
position: absolute;
bottom: 0;
max-height: 100%;
overflow-y: auto;
}
#screen {
height: 280px;
border-bottom: 1px solid black;
}
#input {
height: 70px;
}
</style>
</head>
<body>
<div id="terminal">
<div id="logs"><div id="log-items"></div></div>
<div id="screen"></div>
<div id="input"></div>
</div>
<script type="text/javascript" src=".build-tmp/verse.js"></script>
<script>
let app
/* DOM STUFF */
document.body.addEventListener('keydown', event => {
if (event.key === 'Escape') {
app.interrupt()
} else {
app.receiveKeydown(event)
}
})
function View() {
let $logContainer = document.getElementById('logs')
let $logs = document.getElementById('log-items')
let $screen = document.getElementById('screen')
let $input = document.getElementById('input')
return {
log,
screen,
input,
hideScreen,
}
function log(message) {
let newLogElement = document.createElement('div')
newLogElement.innerText = message
$logs.appendChild(newLogElement)
$logs.scrollTop = 999999
}
function screen(lines) {
$screen.innerText = lines.join('\n')
showScreen()
}
function hideScreen() {
$screen.style.height = 0
$logContainer.style.height = '350px'
}
function showScreen() {
$screen.style.height = '280px'
$logContainer.style.height = '70px'
}
function input(lines) {
$input.innerText = lines.join('\n')
}
}
function viewTestResults(results) {
terminal.innerText =
'TESTS FAILED:\n' +
results.failures.join('\n')
}
/* TESTS */
function test_last_empty() {
_expect(last(1, []), Array.isArray)
_expect(last(1, []).length, isExactly, 0)
}
function test_last_zeroReturnsEmpty() {
_expect(last(0, [5]).length, isExactly, 0)
}
function test_last_oneElement() {
_expect(last(1, [5]).length, isExactly, 1)
_expect(last(1, [5])[0], isExactly, 5)
}
function test_last_requestedLengthExceedsInputLength() {
_expect(last(2, [5]).length, isExactly, 1)
}
function test_last_getsTheLastOfMultipleElements() {
_expect(last(1, [5, 6]).length, isExactly, 1)
_expect(last(1, [5, 6])[0], isExactly, 6)
}
/* MAIN */
function main() {
window.view = View()
view.hideScreen()
app = App(window)
}
/* STATE */
function getStateType() {
return {}
}
/* SAGAS */
function *init() {
console.log('init')
yield GuessingGame
}
function *GuessingGame(tell) {
let secret = Math.ceil(Math.random() * 100)
console.log('GuessingGame')
yield log("I'm thinking of a number between 1 and 100")
console.log("I'm thinking of a number between 1 and 100")
yield function*() {
let number = yield getNumber('Enter your guess')
if (number > secret) {
yield log(number + ' is too high!')
yield retry()
} else if (number < secret) {
yield log(number + ' is too low!')
yield retry()
} else {
yield log('You got it!')
}
}
}
/* EFFECTS */
function getNumber(prompt) {
return function *() {
let entered = ''
let cursor = '_'
yield startInputDisplay(state => {
console.log('input display')
return [
prompt,
'> ' + entered + cursor
]
})
yield startTimer(0.4, () => {
console.log('cursor timer')
cursor = cursor ? '' : '_'
})
yield function*() {
let c = yield waitForChar()
if (c === 'Enter') return
entered += c
yield retry()
}
if (isNaN(entered)) {
yield log("That's not a number. Try again.")
yield retry()
}
return +entered
}
}
/* REDUCERS */
function reducer(state, action) {
return {}
}
/* LIBRARY */
function last(n, array) {
if (n > array.length) return array
return array.slice(array.length - n)
}
function dropLast(n, array) {
if (n > array.length) return []
return array.slice(0, array.length - n)
}
function reduceByDelegates(state, action, reducerMap) {
let result = {}
for (let key in reducerMap) {
if (has(key, reducerMap)) {
result[key] = reducerMap[key](state[key], action)
}
}
return result
}
main()
</script>
</body>
</html>