forked from opening-hours/opening_hours.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive_testing.js
executable file
·123 lines (110 loc) · 4.92 KB
/
interactive_testing.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
#!/usr/bin/env nodejs
var optimist = require('optimist')
.usage('Usage: $0 [optional parameters] [server_listening_ports]')
.describe('h', 'Display the usage')
// .describe('v', 'Verbose output')
.describe('f', 'File path to the opening_hours.js libary file to run the tests against.')
.describe('l', 'Locale for error/warning messages and prettified values.')
.describe('L', 'Locale used for prettifyValue')
.describe('V', 'opening_hours value. If present, interactive mode will be skipped.')
.alias('h', 'help')
// .alias('v', 'verbose')
.alias('f', 'library-file')
.alias('l', 'locale')
.alias('L', 'prettify-locale')
.alias('V', 'value')
.default('f', './opening_hours.js')
.default('l', 'en')
.default('L', 'en');
var argv = optimist.argv;
if (argv.help) {
optimist.showHelp();
process.exit(0);
}
var opening_hours = require('./' + argv['library-file']);
var readline = require('readline');
var net = require('net');
// used for sunrise, sunset and PH,SH
// https://nominatim.openstreetmap.org/reverse?format=json&lat=49.5487429714954&lon=9.81602098644987&zoom=18&addressdetails=1
var nominatimTestJSON = {"place_id":"44651229","licence":"Data \u00a9 OpenStreetMap contributors, ODbL 1.0. https:\/\/www.openstreetmap.org\/copyright","osm_type":"way","osm_id":"36248375","lat":"49.5400039","lon":"9.7937133","display_name":"K 2847, Lauda-K\u00f6nigshofen, Main-Tauber-Kreis, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, European Union","address":{"road":"K 2847","city":"Lauda-K\u00f6nigshofen","county":"Main-Tauber-Kreis","state_district":"Regierungsbezirk Stuttgart","state":"Baden-W\u00fcrttemberg","country":"Germany","country_code":"de","continent":"European Union"}};
function opening_hours_object(value) {
var oh;
var crashed = true;
var needed_nominatim_json = false;
var warnings = [];
try {
oh = new opening_hours(value, {}, { 'locale': argv.locale } );
warnings = oh.getWarnings();
if (typeof warnings !== 'object')
console.error(warnings);
// prettified = oh.prettifyValue();
crashed = false;
} catch (err) {
try {
oh = new opening_hours(value, nominatimTestJSON, { 'locale': argv.locale });
crashed = false;
needed_nominatim_json = true;
} catch (err) {
crashed = err;
}
}
var result = { 'needed_nominatim_json': needed_nominatim_json };
if (crashed) {
result.error = true;
result.eval_notes = crashed;
} else {
result.error = false;
result.eval_notes = warnings;
result.comment = oh.getComment();
result.state = oh.getState();
result.unknown = oh.getUnknown();
result.state_string = oh.getStateString();
try {
result.next_change = oh.getNextChange();
} catch (err) {
// This might throw an exception if there is no change.
}
result.rule_index = oh.getMatchingRule();
result.matching_rule = typeof result.rule_index === 'undefined'
? undefined
: oh.prettifyValue({ 'rule_index': result.rule_index, conf: { 'locale': argv['prettify-locale'] } });
result.prettified = oh.prettifyValue({ conf: { 'locale': argv['prettify-locale'] } });
result.week_stable = oh.isWeekStable();
}
return result;
}
var servers = [];
for (var i = 0; i < argv._.length; i++) {
console.log('Starting to listen on "%s"', argv._[i]);
servers[i] = net.createServer(function(socket) {
console.log("connected");
socket.on('data', function (data) {
value = data.toString();
console.log(value);
result = opening_hours_object(value);
socket.write(JSON.stringify(result, null, '\t'));
});
}).listen(argv._[i]);
}
if (typeof argv.value === 'string') {
result = opening_hours_object(argv.value);
console.log(JSON.stringify(result, null, '\t') + '\n');
} else {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', function (value) {
result = opening_hours_object(value);
console.log(JSON.stringify(result, null, '\t') + '\n');
}).on('close', function() {
for (var i = 0; i < servers.length; i++) {
servers[i].close();
}
console.log('\nBye');
process.exit(0);
});
console.info('You can enter your opening_hours like value and hit enter to evaluate. The result handed to you is represented in JSON.');
console.info('If you want to create a binding for another programing language you should use the unix socket interface which gives you full access to the API or use a native binding to NodeJS/JavaScript if one does exist.');
// Also the stdin method breaks for certain values (e.g. newlines in values).
}