Repository navigation
Expand file tree
/
Copy pathjasper.engine.js
More file actions
366 lines (315 loc) · 9.55 KB
/
Copy pathjasper.engine.js
File metadata and controls
366 lines (315 loc) · 9.55 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* jshint laxcomma:true */
var Jasper = (function () {
"use strict";
var adjectives
// indicators of success in answering the ask
, answered = []
, API
// attic will hold versions of the levels array that have been emptied
, attic = []
// check isType description for more details about these functions
// , isFunction = isType.bind(null, "function") // Phantom hates bind? :(
// , isString = isType.bind(null, "string") // Phantom hates bind? :(
, isFunction = apply_one(isType, "function") // taking advantage of hoisting
, isString = apply_one(isType, "string") // taking advantage of hoisting
// levels will hold all asks given to Jasper
, levels = []
// lock indicates if asks can be added or not
, lock = false
// progress is the current position of the user within the levels array
, progress = 0
;
// Phantom hates Function.prototype.bind :(
/**
* Apply the first argument of a function
*
* @param {Function} fn
* The function partially apply
*
* @param {String} type
* The type to check against
*
* @return {true|false}
*/
function apply_one (fn, type) {
return function (obj) {
return fn(type, obj);
};
}
/**
* Deterime if argument is a number
*
* @param {anything} n
*
* @return {Boolean}
*
*/
function isNumber (n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
/**
* Test an object type against a string as a regex
*
* @param {string} type
* A string to be tested against the object type
*
* @param {Object} obj
* Any object to be inspected
*
* @return {Boolean}
*/
function isType (type, obj) {
return (new RegExp(type, "i"))
.test({}.toString.call(obj));
}
/**
* Randomize a list of congratulatory adjectives
*
* @return {String}
*/
adjectives = (function () {
var length, words;
words = ("Admirable Awesome Brilliant Capital Excellent Fabulous " +
"Fantabulous Glorious Good Great Impressive Keen Magnificent " +
"Outstanding Resplendent Splendid Splendiferous Superb Superior " +
"Swanky")
.split(" ");
length = words.length;
return function () {
var indx = ~~(Math.random() * length) % length;
words = words
.slice(indx)
.concat(words.slice(0, indx));
return ("%" + ["!", "."][~~(Math.random() * 2)])
.replace("%", words[0]);
};
}());
/**
* Namespace for the API allowing for lookup in jasper_engine
*
* @type {Object}
*/
API = {
/**
* Register a new ask (challenge) with Jasper to extend the fun
*
* @param {String} intro
* An introductory (ambiguous) description of what the ask is asking
*
* @param {String} hint
* A more explicit text to describe the ask specifically
*
* @param {Function} fn
* The body of the challenge
*
* @return undefined
*/
ask: function api_ask (intro, hint, fn) {
if (!!lock) {
throw new Error("Jasper has been locked, no new asks may be added.");
}
var length = arguments.length;
if (length > 0) {
// Make second parameter optional
if (2 === length) {
fn = hint;
hint = "";
}
if (!isString(intro)) {
throw new Error("Intro text must be a String.");
}
if (!isString(hint)) {
throw new Error("Hint text must be a String.");
}
if (!isFunction(fn)) {
throw new Error("Challenge must be a Function.");
}
fn.intro = intro;
fn.hint = hint;
levels.push(fn);
} else {
// no arguments passed
// lock the implementation so no more asks can be added
lock = true;
}
}
/**
* Remove all asks to start from scratch
* This must be executed with a context of 'true' to prevent accidental call
*
* @param {true} arg
* Passing anything other than true will throw an error
*
* @param {true|falsey} atticToo
* Passing truthy will empty the attic array; testing only probably
*
* @return undefined
*/
, empty: function api_empty (arg, atticToo) {
if (true === arg) {
if (levels.length && !atticToo) {
attic.push(levels);
}
if (atticToo) {
attic = [];
}
levels = [];
lock = false;
API.restart();
} else {
throw new Error("Empty must be passed first_argument === 'true'.");
}
}
// , extras: function () {
// // global variables: help, start, hint, jasper
// // to evaluate as a call to Jasper("hint") via valueOf/toString
// }
/**
* Show the current level's intro text
*
* @return {String}
* The intro text of the current level, or the end text
*/
, help: function api_help (prepend) {
return progress < levels.length && false !== answered[progress]
? levels[progress]
.intro
.replace("#", 1 + progress)
: "\nCongratulations you're done; start over with Jasper('restart').";
}
/**
* Show the current level's hint text
*
* @param {String} prepend
* Optional addition text to add to the output to be more explanatory
*
* @return {String}
* The resulting string to be output
*/
, hint: function api_hint (prepend) {
return (prepend || "") +
levels[progress].hint +
(prepend ? "\n" + API.help() : "");
}
/**
* Start the asks/challenges over again
*
* @return {String}
* Message telling the user they are ready to go again
*/
, restart: function api_restart () {
answered = [];
progress = 0;
return "All clear; go for it!\n" + API.help();
}
/**
* Restore previously emptied asks
* This must be executed with a context of 'true' to prevent accidental call
*
* @param {true} arg
* Passing anything other than true will throw an error
*
* @return undefined
*/
, restore: function api_restore (arg) {
if (true === arg) {
if (attic.length) {
levels = attic.pop();
} else {
throw new Error("Restore called with no history to restore.");
}
return API.restart();
} else {
throw new Error("Restore must be passed first_argument === 'true'.");
}
}
/**
* Skip to an ask/challenge without completing the current on
*
* @param {Integer} num
* A positive number skips forward from the current location -
* 'progress', negative moves backward
*
* @return {String}
* The help text for the next ask/challenge
*/
, skip: function api_skip (search) {
var len;
if (void 0 === search || isNumber(search)) {
if (0 === +search) {
throw new Error("Why would you want to skip to where you are? " +
"Zero passed to Jasper('skip').");
}
// default to 'skipping' one ask forward
search = search || 1;
// if previously answered dont't penalize for skipping
answered[progress] = answered[progress] || false;
// make sure to skip in bounds
progress = (progress + ~~search) % levels.length;
} else {
throw new Error("Skip needs a number to skip to, " +
"or a string to search for; '%' doesn't work."
.replace("%", search));
}
return API.help();
}
};
/** Command Pattern - Manager
* Process user interactions and manage state
*
* @param {String} command
* API method to invoke or (attempted) answer to level
*
* @param {[anything?]} arg
* The first additional argument
*
* @return {String}
* Prompt for the user to take next steps
*
*/
function jasper_engine (command, arg) {
/*jshint validthis:true*/
var result;
// default to the 'help' util method
command = command || "help";
// run a util method if asked for or no arguments
if (API[command] || 0 === arguments.length) {
// only pass the arguments if provided; zero is a valid argument sometimes
result = (arg || 0 === arg)
? API[command].apply(this, [].slice.call(arguments, 1))
: API[command].call(this);
} else {
try {
// pass the arguments to the level function to check correctness
if (levels[progress].apply(this, arguments)) {
answered[progress] = true;
result = API.hint(adjectives() + "\nLevel " + (progress) + ": ");
progress++;
result += "\n\n" + API.help();
} else {
result = "Not quite try again.";
}
} catch (e) {
if (/not\sa\sfunction/i.test(e.message)) {
result = "Remember that time I told you to pass a function?";
} else if (/JSON/i.test(e.message)) {
result = "The JSON string needs to be properly formatted.";
} else {
result = "I'm not sure at all what you are trying to do.";
}
throw e;
}
}
return result;
}
// hints to help people find the path to enlightenment
jasper_engine.help =
jasper_engine.start =
jasper_engine.toString =
function () {
return jasper_engine();
};
console.log("Level 0: Completed by opening your browser's console.\n" +
"'Jasper' is waiting for 'start'. GL! HF!");
return jasper_engine;
}());