metar-taf-parser 0.0.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -135,6 +135,8 @@ class WindVariationCommand {
135
135
  wind.maxVariation = +matches[2];
136
136
  }
137
137
  execute(container, windString) {
138
+ if (!container.wind)
139
+ throw new errors_1.InvalidWeatherStatementError();
138
140
  this.parseWindVariation(container.wind, windString);
139
141
  return true;
140
142
  }
@@ -158,7 +158,7 @@ class HourlyPressureCommand extends Command {
158
158
  const matches = code.match(__classPrivateFieldGet(this, _HourlyPressureCommand_regex, "f"));
159
159
  if (!matches)
160
160
  throw new errors_1.UnexpectedParseError("Match not found");
161
- remark.push(`${(0, i18n_1._)(`Remark.Barometer.${+matches[1]}`, this.locale)} ${(0, i18n_1.format)((0, i18n_1._)("Remark.Pressure.Tendency", this.locale), +matches[1])}`);
161
+ remark.push(`${(0, i18n_1._)(`Remark.Barometer.${+matches[1]}`, this.locale)} ${(0, i18n_1.format)((0, i18n_1._)("Remark.Pressure.Tendency", this.locale), +matches[2] / 10)}`);
162
162
  return [code.replace(__classPrivateFieldGet(this, _HourlyPressureCommand_regex, "f"), "").trim(), remark];
163
163
  }
164
164
  }
@@ -2,5 +2,8 @@ export declare function degreesToCardinal(input: number | string): string;
2
2
  export declare function convertVisibility(input: string): string;
3
3
  export declare function convertTemperature(input: string): number;
4
4
  export declare function convertInchesMercuryToPascal(input: number): number;
5
- export declare function convertTemperatureRemarks(sign: string, temperature: string): number;
5
+ /**
6
+ * Converts number `.toFixed(1)` before outputting to match python implementation
7
+ */
8
+ export declare function convertTemperatureRemarks(sign: string, temperature: string): string;
6
9
  export declare function convertPrecipitationAmount(amount: string): number;
@@ -44,11 +44,14 @@ function convertInchesMercuryToPascal(input) {
44
44
  return 33.8639 * input;
45
45
  }
46
46
  exports.convertInchesMercuryToPascal = convertInchesMercuryToPascal;
47
+ /**
48
+ * Converts number `.toFixed(1)` before outputting to match python implementation
49
+ */
47
50
  function convertTemperatureRemarks(sign, temperature) {
48
51
  const temp = +temperature / 10;
49
52
  if (sign === "0")
50
- return temp;
51
- return -temp;
53
+ return temp.toFixed(1);
54
+ return (-temp).toFixed(1);
52
55
  }
53
56
  exports.convertTemperatureRemarks = convertTemperatureRemarks;
54
57
  function convertPrecipitationAmount(amount) {
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { IMetar } from "./model/model";
1
+ import { IMetar, ITAF } from "./model/model";
2
2
  import { Locale } from "./commons/i18n";
3
- export interface IMetarTAFParserOptions {
4
- locale?: Locale;
5
- }
6
- export declare function parseMetar(metar: string, options: IMetarTAFParserOptions): IMetar;
7
3
  export { Locale } from "./commons/i18n";
8
4
  export * from "./commons/errors";
9
5
  export * from "./model/model";
10
6
  export * from "./model/enum";
7
+ export interface IMetarTAFParserOptions {
8
+ locale?: Locale;
9
+ }
10
+ export declare function parseMetar(metar: string, options?: IMetarTAFParserOptions): IMetar;
11
+ export declare function parseTAF(taf: string, options?: IMetarTAFParserOptions): ITAF;
package/dist/index.js CHANGED
@@ -17,14 +17,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.parseMetar = void 0;
20
+ exports.parseTAF = exports.parseMetar = void 0;
21
21
  const parser_1 = require("./parser/parser");
22
22
  const errors_1 = require("./commons/errors");
23
23
  const en_1 = __importDefault(require("./locale/en"));
24
+ __exportStar(require("./commons/errors"), exports);
25
+ __exportStar(require("./model/model"), exports);
26
+ __exportStar(require("./model/enum"), exports);
24
27
  function parseMetar(metar, options) {
28
+ return parse(metar, options, parser_1.MetarParser);
29
+ }
30
+ exports.parseMetar = parseMetar;
31
+ function parseTAF(taf, options) {
32
+ return parse(taf, options, parser_1.TAFParser);
33
+ }
34
+ exports.parseTAF = parseTAF;
35
+ function parse(report, options, parser) {
25
36
  const lang = (options === null || options === void 0 ? void 0 : options.locale) || en_1.default;
26
37
  try {
27
- return new parser_1.MetarParser(lang).parse(metar);
38
+ return new parser(lang).parse(report);
28
39
  }
29
40
  catch (e) {
30
41
  if (e instanceof errors_1.ParseError)
@@ -32,7 +43,3 @@ function parseMetar(metar, options) {
32
43
  throw new errors_1.InvalidWeatherStatementError(e);
33
44
  }
34
45
  }
35
- exports.parseMetar = parseMetar;
36
- __exportStar(require("./commons/errors"), exports);
37
- __exportStar(require("./model/model"), exports);
38
- __exportStar(require("./model/enum"), exports);
@@ -55,12 +55,12 @@ export interface ICloud {
55
55
  type?: CloudType;
56
56
  }
57
57
  export interface IAbstractWeatherContainer {
58
- wind: IWind;
58
+ wind?: IWind;
59
59
  visibility?: Visibility;
60
- verticalVisibility: number;
61
- windShear: IWindShear;
62
- cavok: boolean;
63
- remark: string;
60
+ verticalVisibility?: number;
61
+ windShear?: IWindShear;
62
+ cavok?: boolean;
63
+ remark?: string;
64
64
  remarks: string[];
65
65
  clouds: ICloud[];
66
66
  weatherConditions: IWeatherCondition[];
@@ -78,7 +78,7 @@ export interface ITime {
78
78
  }
79
79
  export interface IAbstractWeatherCode extends IAbstractWeatherContainer, ITime {
80
80
  day: number;
81
- airport: IAirport;
81
+ airport?: IAirport;
82
82
  message: string;
83
83
  station: string;
84
84
  trends: IAbstractTrend[];
@@ -93,10 +93,10 @@ export interface IMetar extends IAbstractWeatherCode {
93
93
  trends: IMetarTrend[];
94
94
  }
95
95
  export interface ITAF extends IAbstractWeatherCode {
96
- validity: IAbstractValidity;
97
- maxTemperature: number;
98
- minTemperature: number;
99
- amendment: boolean;
96
+ validity: IValidity;
97
+ maxTemperature?: ITemperatureDated;
98
+ minTemperature?: ITemperatureDated;
99
+ amendment?: boolean;
100
100
  trends: ITAFTrend[];
101
101
  }
102
102
  export interface IAbstractTrend extends IAbstractWeatherContainer {
@@ -109,13 +109,15 @@ export interface IMetarTrend extends IAbstractTrend {
109
109
  times: IMetarTrendTime[];
110
110
  }
111
111
  export interface ITAFTrend extends IAbstractTrend {
112
- probability: number;
113
- validity: IAbstractValidity;
112
+ probability?: number;
113
+ validity?: IFMValidity;
114
114
  }
115
- export interface IValidity extends IAbstractValidity {
115
+ export interface IEndValidity {
116
116
  endHour: number;
117
117
  endDay: number;
118
118
  }
119
- export interface IFMValidity extends IAbstractValidity {
120
- startMinutes: number;
119
+ export interface IValidity extends IAbstractValidity, IEndValidity {
120
+ }
121
+ export interface IFMValidity extends IAbstractValidity, Partial<IEndValidity> {
122
+ startMinutes?: number;
121
123
  }
@@ -1,4 +1,5 @@
1
- import { IAbstractWeatherContainer, IMetar, IMetarTrend, ITemperatureDated, IValidity, IWeatherCondition } from "../model/model";
1
+ import { IAbstractWeatherContainer, IMetar, IMetarTrend, ITAF, ITAFTrend, ITemperatureDated, IValidity, IWeatherCondition } from "../model/model";
2
+ import { WeatherChangeType } from "../model/enum";
2
3
  import { Locale } from "../commons/i18n";
3
4
  /**
4
5
  * Parses the temperature in a TAF
@@ -18,10 +19,12 @@ export declare function parseValidity(input: string): IValidity;
18
19
  */
19
20
  export declare abstract class AbstractParser {
20
21
  #private;
22
+ protected locale: Locale;
21
23
  FM: string;
22
24
  TEMPO: string;
23
25
  BECMG: string;
24
26
  RMK: string;
27
+ constructor(locale: Locale);
25
28
  parseWeatherCondition(input: string): IWeatherCondition;
26
29
  /**
27
30
  * Parses the message into different tokens
@@ -39,10 +42,8 @@ export declare abstract class AbstractParser {
39
42
  }
40
43
  export declare class MetarParser extends AbstractParser {
41
44
  #private;
42
- private locale;
43
45
  AT: string;
44
46
  TL: string;
45
- constructor(locale: Locale);
46
47
  /**
47
48
  * Parses a trend of a metar
48
49
  * @param index the index starting the trend in the list
@@ -58,6 +59,43 @@ export declare class MetarParser extends AbstractParser {
58
59
  */
59
60
  parse(input: string): IMetar;
60
61
  }
62
+ /**
63
+ * Parser for TAF messages
64
+ */
65
+ export declare class TAFParser extends AbstractParser {
66
+ #private;
67
+ TAF: string;
68
+ PROB: string;
69
+ TX: string;
70
+ TN: string;
71
+ /**
72
+ * the message to parse
73
+ * @param input
74
+ * @returns a TAF object
75
+ * @throws ParseError if the message is invalid
76
+ */
77
+ parse(input: string): ITAF;
78
+ /**
79
+ * Format the message as a multiple line code so each line can be parsed
80
+ * @param tafCode The base message
81
+ * @returns a list of string representing the lines of the message
82
+ */
83
+ extractLinesTokens(tafCode: string): string[][];
84
+ /**
85
+ * Parses the tokens of the line and updates the TAF object
86
+ * @param taf TAF object to update
87
+ * @param lineTokens the array of tokens representing a line
88
+ */
89
+ parseLine(taf: ITAF, lineTokens: string[]): void;
90
+ /**
91
+ * Parses a trend of the TAF
92
+ * @param index the index at which the array should be parsed
93
+ * @param line The array of string containing the line
94
+ * @param trend The trend object to update
95
+ */
96
+ parseTrend(index: number, line: string[], trend: ITAFTrend): void;
97
+ makeEmptyTAFTrend(type: WeatherChangeType): ITAFTrend;
98
+ }
61
99
  export declare class RemarkParser {
62
100
  #private;
63
101
  private locale;
@@ -27,9 +27,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
27
27
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
28
28
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
29
29
  };
30
- var _AbstractParser_INTENSITY_REGEX, _AbstractParser_CAVOK, _AbstractParser_commonSupplier, _MetarParser_commandSupplier, _RemarkParser_supplier;
30
+ var _AbstractParser_INTENSITY_REGEX, _AbstractParser_CAVOK, _AbstractParser_commonSupplier, _MetarParser_commandSupplier, _TAFParser_validityPattern, _RemarkParser_supplier;
31
31
  Object.defineProperty(exports, "__esModule", { value: true });
32
- exports.RemarkParser = exports.MetarParser = exports.AbstractParser = exports.parseValidity = exports.parseTemperature = void 0;
32
+ exports.RemarkParser = exports.TAFParser = exports.MetarParser = exports.AbstractParser = exports.parseValidity = exports.parseTemperature = void 0;
33
33
  const remark_1 = require("../command/remark");
34
34
  const model_1 = require("../model/model");
35
35
  const converter = __importStar(require("../commons/converter"));
@@ -109,7 +109,8 @@ function parseFromValidity(input) {
109
109
  * Base parser.
110
110
  */
111
111
  class AbstractParser {
112
- constructor() {
112
+ constructor(locale) {
113
+ this.locale = locale;
113
114
  this.FM = "FM";
114
115
  this.TEMPO = "TEMPO";
115
116
  this.BECMG = "BECMG";
@@ -200,9 +201,8 @@ class AbstractParser {
200
201
  exports.AbstractParser = AbstractParser;
201
202
  _AbstractParser_INTENSITY_REGEX = new WeakMap(), _AbstractParser_CAVOK = new WeakMap(), _AbstractParser_commonSupplier = new WeakMap();
202
203
  class MetarParser extends AbstractParser {
203
- constructor(locale) {
204
- super();
205
- this.locale = locale;
204
+ constructor() {
205
+ super(...arguments);
206
206
  this.AT = "AT";
207
207
  this.TL = "TL";
208
208
  _MetarParser_commandSupplier.set(this, new metar_1.CommandSupplier());
@@ -294,6 +294,144 @@ class MetarParser extends AbstractParser {
294
294
  }
295
295
  exports.MetarParser = MetarParser;
296
296
  _MetarParser_commandSupplier = new WeakMap();
297
+ /**
298
+ * Parser for TAF messages
299
+ */
300
+ class TAFParser extends AbstractParser {
301
+ constructor() {
302
+ super(...arguments);
303
+ this.TAF = "TAF";
304
+ this.PROB = "PROB";
305
+ this.TX = "TX";
306
+ this.TN = "TN";
307
+ _TAFParser_validityPattern.set(this, /^\d{4}\/\d{4}$/);
308
+ }
309
+ /**
310
+ * the message to parse
311
+ * @param input
312
+ * @returns a TAF object
313
+ * @throws ParseError if the message is invalid
314
+ */
315
+ parse(input) {
316
+ let amendment;
317
+ const lines = this.extractLinesTokens(input);
318
+ if (lines[0][0] !== this.TAF)
319
+ throw new errors_1.InvalidWeatherStatementError('TAF report must begin with string "TAF"');
320
+ let index = 1;
321
+ if (lines[0][1] === this.TAF)
322
+ index = 2;
323
+ if (lines[0][index] === "AMD") {
324
+ amendment = true;
325
+ index += 1;
326
+ }
327
+ const station = lines[0][index];
328
+ index += 1;
329
+ const time = parseDeliveryTime(lines[0][index]);
330
+ index += 1;
331
+ const validity = parseValidity(lines[0][index]);
332
+ const taf = Object.assign(Object.assign({ station,
333
+ amendment }, time), { validity, message: input, trends: [], remarks: [], clouds: [], weatherConditions: [] });
334
+ for (let i = index + 1; i < lines[0].length; i++) {
335
+ const token = lines[0][i];
336
+ if (token == this.RMK)
337
+ parseRemark(taf, lines[0], i, this.locale);
338
+ else if (token.startsWith(this.TX))
339
+ taf.maxTemperature = parseTemperature(token);
340
+ else if (token.startsWith(this.TN))
341
+ taf.minTemperature = parseTemperature(token);
342
+ else
343
+ this.generalParse(taf, token);
344
+ }
345
+ // Handle the other lines
346
+ for (let i = 1; i < lines.length; i++) {
347
+ this.parseLine(taf, lines[i]);
348
+ }
349
+ return taf;
350
+ }
351
+ /**
352
+ * Format the message as a multiple line code so each line can be parsed
353
+ * @param tafCode The base message
354
+ * @returns a list of string representing the lines of the message
355
+ */
356
+ extractLinesTokens(tafCode) {
357
+ const singleLine = tafCode.replace(/\n/g, " ");
358
+ const cleanLine = singleLine.replace(/\s{2,}/g, " ");
359
+ const lines = joinProbIfNeeded(cleanLine
360
+ .replace(/\s(?=PROB\d{2}\sTEMPO|TEMPO|BECMG|FM|PROB)/g, "\n")
361
+ .split(/\n/));
362
+ // TODO cleanup
363
+ function joinProbIfNeeded(ls) {
364
+ for (let i = 0; i < ls.length; i++) {
365
+ if (/PROB\d{2}/.test(ls[i]) && /TEMPO/.test(ls[i + 1])) {
366
+ ls.splice(i, 2, `${ls[i]} ${ls[i + 1]}`);
367
+ i--;
368
+ }
369
+ }
370
+ return ls;
371
+ }
372
+ const linesToken = lines.map(this.tokenize);
373
+ if (linesToken.length > 1) {
374
+ const lastLine = linesToken[lines.length - 1];
375
+ const temperatures = lastLine.filter((l) => l.startsWith(this.TX) || l.startsWith(this.TN));
376
+ if (temperatures.length) {
377
+ linesToken[0] = linesToken[0].concat(temperatures);
378
+ linesToken[lines.length - 1] = lastLine.filter((l) => !l.startsWith(this.TX) && !l.startsWith(this.TN));
379
+ }
380
+ }
381
+ return linesToken;
382
+ }
383
+ /**
384
+ * Parses the tokens of the line and updates the TAF object
385
+ * @param taf TAF object to update
386
+ * @param lineTokens the array of tokens representing a line
387
+ */
388
+ parseLine(taf, lineTokens) {
389
+ let index = 1;
390
+ let trend;
391
+ if (lineTokens[0].startsWith(this.FM)) {
392
+ trend = Object.assign(Object.assign({}, this.makeEmptyTAFTrend(enum_1.WeatherChangeType.FM)), { validity: parseFromValidity(lineTokens[0]) });
393
+ }
394
+ else if (lineTokens[0].startsWith(this.PROB)) {
395
+ trend = this.makeEmptyTAFTrend(enum_1.WeatherChangeType.PROB);
396
+ if (lineTokens.length > 1 && lineTokens[1] === this.TEMPO) {
397
+ trend = this.makeEmptyTAFTrend(enum_1.WeatherChangeType[lineTokens[1]]);
398
+ index = 2;
399
+ }
400
+ trend.probability = +lineTokens[0].slice(4);
401
+ }
402
+ else {
403
+ trend = this.makeEmptyTAFTrend(enum_1.WeatherChangeType[lineTokens[0]]);
404
+ }
405
+ this.parseTrend(index, lineTokens, trend);
406
+ taf.trends.push(trend);
407
+ }
408
+ /**
409
+ * Parses a trend of the TAF
410
+ * @param index the index at which the array should be parsed
411
+ * @param line The array of string containing the line
412
+ * @param trend The trend object to update
413
+ */
414
+ parseTrend(index, line, trend) {
415
+ for (let i = index; i < line.length; i++) {
416
+ if (line[i] === this.RMK)
417
+ parseRemark(trend, line, i, this.locale);
418
+ else if (__classPrivateFieldGet(this, _TAFParser_validityPattern, "f").test(line[i]))
419
+ trend.validity = parseValidity(line[i]);
420
+ else
421
+ super.generalParse(trend, line[i]);
422
+ }
423
+ }
424
+ makeEmptyTAFTrend(type) {
425
+ return {
426
+ type,
427
+ remarks: [],
428
+ clouds: [],
429
+ weatherConditions: [],
430
+ };
431
+ }
432
+ }
433
+ exports.TAFParser = TAFParser;
434
+ _TAFParser_validityPattern = new WeakMap();
297
435
  class RemarkParser {
298
436
  constructor(locale) {
299
437
  this.locale = locale;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metar-taf-parser",
3
- "version": "0.0.2",
3
+ "version": "1.0.0",
4
4
  "description": "Parse METAR and TAF files",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -11,14 +11,14 @@
11
11
  "url": "https://github.com/aeharding/metar-taf-parser.git"
12
12
  },
13
13
  "scripts": {
14
- "build": "yarn clean; tsc && tsc-alias",
14
+ "build": "yarn clean; tsc --project tsconfig.build.json && tsc-alias",
15
15
  "clean": "rm -rf ./dist",
16
16
  "start": "node --experimental-specifier-resolution=node --loader ts-node/esm lib",
17
17
  "check-types": "tsc --noEmit",
18
18
  "check-formatting": "prettier --check '**/*.{js,json,css,md,scss,tsx,ts}'",
19
19
  "watch": "watch 'yarn build' lib",
20
- "test": "jest",
21
- "test-watch": "jest --watch",
20
+ "test": "jest --coverage",
21
+ "test-watch": "jest --watch --coverage",
22
22
  "prepublishOnly": "yarn build"
23
23
  },
24
24
  "files": [