-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
212 lines (185 loc) · 5.76 KB
/
index.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
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
var fs = require('fs'),
path = require('path'),
readline = require('readline'),
exec = require('child_process').exec,
_ = require('lodash'),
colors = require('colors'),
async = require('async');
function run(datafile, str, callback) {
exec("jq '" + str + "' " + datafile, function (err, stdout, stderr) {
var errorMessage = err && err.message;
callback(errorMessage || stderr, stdout);
});
}
function clearscreen() {
// ref: http://stackoverflow.com/questions/8813142
process.stdout.write('\u001B[2J\u001B[0;0f');
}
function divider() {
process.stdout.write('\n\n--------------------------------\n\n');
}
function isEqual(a, b) {
// If string representation is equal then return true.
if (_.isEqual(a, b)) {
return true;
}
// If a and b are convertable to objects, return comparison of objects.
try {
return _.isEqual(JSON.parse(a), JSON.parse(b));
} catch {}
// Otherwise prepare functions for comparison of streams.
// Given opening and closing chars of items (curly brackets for objects,
// square brackets for arrays) return function that converts string to
// array of strings that are convertable to json.
var to_stream = function (opening_char, closing_char) {
var separator = closing_char + '\n' + opening_char;
return (str) =>
str
.slice(1, -2)
.split(separator)
.map((x) => opening_char + x + closing_char);
};
// Create function converting string to array of strings holding objects.
var to_object_stream = to_stream('{', '}');
// Create function converting string to array of strings holding arrays.
var to_array_stream = to_stream('[', ']');
// Given function from above, return function that compares items one by one.
var compare_as_stream = (streamer) => (_a, _b) =>
_.zip(streamer(a), streamer(b)).map((tuple) =>
_.isEqual(JSON.parse(tuple[0]), JSON.parse(tuple[1])),
);
// Try to convert string to stream of objects. If the conversion succeeds,
// return result of comparison.
try {
return compare_as_stream(to_object_stream)(a, b).every((x) => x == true);
} catch {}
// Try to convert string to stream of arrays. If the conversion succeeds,
// return result of comparison.
try {
return compare_as_stream(to_array_stream)(a, b).every((x) => x == true);
} catch {}
// If all attempts failed, suppose a and b are not equal
return false;
}
function runOne(problem, callback) {
var datafile = path.resolve(__dirname, 'data/' + problem.dataset + '.json');
solution = problem.solution;
var dataset = fs.readFileSync(datafile);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
var writeAndPrompt = function (what) {
process.stdout.write(what + '\n');
rl.prompt();
};
var helpMessage = function () {
writeAndPrompt(
[
'Enter your answer or one of the following:',
' * help? show this help message',
' * prompt? show the original challenge prompt',
' * data? show the challenge data set',
].join('\n'),
);
};
var problemPrompt = function () {
writeAndPrompt(
[
'Given: '.bold.white +
" '" +
problem.dataset +
'\' (type "data?" to view)',
'Challenge: '.bold.white + problem.prompt + '\n',
].join('\n'),
);
};
divider();
problemPrompt();
rl.on('line', function (answer) {
switch (answer) {
case '?':
case 'help?':
helpMessage();
break;
case 'prompt?':
problemPrompt();
break;
case 'data?':
writeAndPrompt(dataset);
break;
default:
async.parallel(
{
actual: _.partial(run, datafile, answer),
expected: _.partial(run, datafile, solution),
},
function (err, results) {
if (err) {
process.stderr.write(err.red);
return rl.prompt();
}
if (isEqual(results.expected, results.actual)) {
rl.close();
process.stdout.write('\nYour answer was correct:\n');
process.stdout.write(results.actual.green + '\n');
callback(null);
} else {
process.stdout.write('\nExpected:\n');
process.stdout.write(results.expected.green + '\n');
process.stdout.write('\nYour answer:\n');
process.stdout.write(results.actual.yellow + '\n');
rl.prompt();
}
},
);
}
});
}
function show(problem, callback) {
async.map(
[
path.resolve(__dirname, 'problems', problem, 'README.md'),
path.resolve(__dirname, 'problems', problem, 'problem.json'),
],
fs.readFile,
function (err, results) {
if (err) throw err;
var problems = JSON.parse(results[1]);
clearscreen();
// Print README
process.stdout.write(results[0]);
process.stdout.write(
'type "data?" to see dataset or "help?" for more options',
);
// do problem
async.mapSeries(problems, runOne, callback);
},
);
}
fs.readFile(path.resolve(__dirname, 'menu.json'), function (err, result) {
var lesson = process.argv[process.argv.length - 1],
problems = JSON.parse(result);
var success = function (lesson) {
process.stdout.write(
['\u2605'.yellow + ' "' + lesson + '" completed with a gold star!'].join(
'\n',
) + '\n\n',
);
};
var usage = function () {
process.stdout.write(
['Run jq-tutorial with one of the following:']
.concat(problems)
.join('\n * ') + '\n\n',
);
};
if (problems.indexOf(lesson) == -1) {
usage();
} else {
show(lesson, function (err, result) {
if (err) throw err;
success(lesson);
});
}
});