-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.js
53 lines (49 loc) · 1.46 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
function createExample(name, details) {
var body = $('body');
$('<h1>').text(name).appendTo(body);
$('<blockquote>')
.html(details.description)
.appendTo(body);
if(details.implementation) {
// kind of ugly, but removes leading spaces in the body
var functionBody = details.implementation.toString().split(/\n/);
functionBody = _.map(functionBody, function(line) {
return line.replace(/^ {8}/, '');
});
$('<pre>')
.addClass('function-body')
.text(functionBody.join('\n'))
.appendTo(body);
var validationList = $('<ul>');
for(var index = 0; index < details.validation.length; index++) {
var test = details.validation[index];
var status;
var results;
try
{
var args = _.isArray(test.input) ? test.input : [ test.input ];
results = details.implementation.apply(null, args);
if(_.isEqual(results, test.output)) {
status = 'success';
}
else {
status = 'incorrect';
}
}
catch (error)
{
status = 'error';
results = error.message;
}
var formatted = status + ': ' + JSON.stringify(test.input) + ' -> ' +
JSON.stringify(results);
if(status === 'incorrect') {
formatted += ' (expected: ' + JSON.stringify(test.output) + ')';
}
$('<li>').addClass(status)
.text(formatted)
.appendTo(validationList);
}
validationList.appendTo(body);
}
}