forked from opening-hours/opening_hours.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex_search.js
100 lines (88 loc) · 3.03 KB
/
regex_search.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
var opening_hours = require('./opening_hours.js');
var fs = require('fs');
var readline = require('readline');
var colors = require('colors');
var page_width = 20;
var args = process.argv.splice(2);
var json_file = args[0];
if (typeof json_file === 'undefined') {
// json_file = 'export.opening_hours.json';
json_file = 'export.opening_hours:kitchen.json';
// console.log('Please specify the expored JSON file form taginfo as parameter.');
// return;
}
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
fs.readFile(json_file, 'utf8', function (err, json) {
if (err) {
console.log('Error: ' + err);
return;
}
var json = JSON.parse(json);
rl.setPrompt('regex search> ');
rl.prompt();
rl.on('line', function(line) {
if (line.match(/^\s*$/))
process.exit(0);
console.log('Say what? I might have heard `' + line + '`');
var user_re_ok = false;
try {
var user_re = new RegExp('^(.*?)(' + line + ')(.*)$', 'i');
user_re_ok = true;
} catch (err) {
console.log('Your regular expression did not compile: ' + err);
}
if (user_re_ok) {
matched = [];
for (var i = 0; i < json.data.length; i++) {
var res = json.data[i].value.match(user_re);
if (res)
matched.push([json.data[i].value, json.data[i].count, res]);
}
if (matched === 0) {
console.log('Did not match any value with regular expression: ' + line)
} else {
matched = matched.sort(Comparator);
var total_in_use = 0;
for (var i = 0; i < matched.length; i++) {
total_in_use += matched[i][1];
}
console.log('Matched '.green + matched.length + ' different value' + (matched.length === 1 ? '' : 's')
+ (matched.length !== 1 ? ', total in use: ' + total_in_use : ''));
if (matched.length < page_width) {
print_values(matched);
} else {
rl.question('Print values? ', function(answer) {
if (answer.match(/^y/i))
print_values(matched);
else
rl.prompt();
});
}
}
}
console.log();
rl.prompt();
}).on('close', function() {
console.log('\n\nBye');
process.exit(0);
});
});
function print_values(matched) {
for (var i = 0; i < matched.length; i++) {
if (i !== 0 && i % page_width === 0) {
}
var value = matched[i][0];
var count = matched[i][1];
var res = matched[i][2];
console.log('Matched (count: '+ count +'): ' + res[1] + res[2].blue + res[3])
}
}
// helper functions
function Comparator(a,b){
if (a[1] > b[1]) return -1;
if (a[1] < b[1]) return 1;
return 0;
}