-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
192 lines (157 loc) · 4.39 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
const lookupManufacturer = require('flight-recorder-manufacturers/lookup');
const RE_SEEYOU = /^(\d)([1-9a-c])([1-9a-v])_([\da-z]{1,3})\.igc$/i;
const RE_STREPLA_PREFIX = /^([\da-z]{1,3})_(.*)$/i;
const RE_SHORT = /^(\d)([1-9a-c])([1-9a-v])([\da-z])([\da-z]{3})([\da-z]).*\.igc$/i;
const RE_LONG = /^(\d{4}-\d{2}-\d{2})(?:-([\da-z]{3})-([\da-z]{3,})-(\d{2})|_flight_(\d+))?.*\.igc$/i;
const RE_FULL_DATE = /^(\d{4}_\d{2}_\d{2})_\d{2}_\d{2}_\d{2}.*\.igc$/i;
const RE_SHORT_DATE = /^(19\d{2}|20\d{2})[\.-_]?(\d{2})[\.-_]?(\d{2}).*\.igc$/i;
const RE_IGC_DROID = /^igcdroid_(\d{4})_([a-z]{3})_(\d{2}).*\.igc$/i;
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'.split('');
const MONTHS = {
jan: '01',
feb: '02',
mar: '03',
apr: '04',
may: '05',
jun: '06',
jul: '07',
aug: '08',
sep: '09',
oct: '10',
nov: '11',
dec: '12',
};
const PARSERS = [
parseShort,
parseLong,
parseSeeyou,
parseStrepla,
parseShortDate,
parseFullDate,
parseIGCDroid,
];
module.exports = function parse(filename, maxYear) {
if (arguments.length === 1) {
maxYear = (new Date()).getUTCFullYear();
}
for (let parser of PARSERS) {
let result = parser(filename, maxYear);
if (result) {
return result;
}
}
return null;
};
function parseShort(filename, maxYear) {
let match = filename.match(RE_SHORT);
if (!match) {
return null;
}
let callsign = null;
let date = charsToDate(match[1], match[2], match[3], maxYear);
let manufacturer = lookupManufacturer(match[4]);
let loggerId = match[5].toUpperCase();
let numFlight = charToNumber(match[6]);
return { callsign, date, manufacturer, loggerId, numFlight };
}
function parseSeeyou(filename, maxYear) {
let match = filename.match(RE_SEEYOU);
if (!match) {
return null;
}
let callsign = match[4] || null;
let date = charsToDate(match[1], match[2], match[3], maxYear);
let manufacturer = null;
let loggerId = null;
let numFlight = null;
return { callsign, date, manufacturer, loggerId, numFlight };
}
function parseStrepla(filename, maxYear) {
let match = filename.match(RE_STREPLA_PREFIX);
if (!match) {
return null;
}
let callsign = match[1];
let result = parseLong(match[2]) || parseShort(match[2], maxYear);
if (result) {
result.callsign = callsign;
}
return result;
}
function parseIGCDroid(filename) {
let match = filename.match(RE_IGC_DROID);
if (!match) {
return null;
}
let month = MONTHS[match[2]];
if (!month) {
return null;
}
let callsign = null;
let date = `${match[1]}-${month}-${match[3]}`;
let manufacturer = null;
let loggerId = null;
let numFlight = null;
return { callsign, date, manufacturer, loggerId, numFlight };
}
function parseLong(filename) {
let match = filename.match(RE_LONG);
if (!match) {
return null;
}
let callsign = null;
let date = match[1];
let manufacturer = match[2] ? lookupManufacturer(match[2]) : null;
let loggerId = match[3] ? match[3].toUpperCase() : null;
let numFlight = null;
if (match[4]) {
numFlight = parseInt(match[4], 10);
} else if (match[5]) {
numFlight = parseInt(match[5], 10);
}
return { callsign, date, manufacturer, loggerId, numFlight };
}
function parseFullDate(filename) {
let match = filename.match(RE_FULL_DATE);
if (!match) {
return null;
}
let callsign = null;
let date = match[1].replace(/_/g, '-');
let manufacturer = null;
let loggerId = null;
let numFlight = null;
return { callsign, date, manufacturer, loggerId, numFlight };
}
function parseShortDate(filename) {
let match = filename.match(RE_SHORT_DATE);
if (!match) {
return null;
}
let callsign = null;
let date = `${match[1]}-${match[2]}-${match[3]}`;
let manufacturer = null;
let loggerId = null;
let numFlight = null;
return { callsign, date, manufacturer, loggerId, numFlight };
}
function charsToDate(y, m, d, maxYear) {
let yearDigit = charToNumber(y);
let monthDigit = charToNumber(m);
let dayDigit = charToNumber(d);
let yearDiff = (maxYear % 10) - yearDigit;
if (yearDiff < 0) {
yearDiff += 10;
}
let year = maxYear - yearDiff;
let month = `${monthDigit < 10 ? '0' : ''}${monthDigit}`;
let day = `${dayDigit < 10 ? '0' : ''}${dayDigit}`;
return `${year}-${month}-${day}`;
}
function charToNumber(char) {
let index = CHARS.indexOf(char.toLowerCase());
if (index === -1) {
throw new Error(`Unknown character: ${char}`);
}
return index;
}