Skip to content

Commit

Permalink
Replaced regex with a more strict version
Browse files Browse the repository at this point in the history
  • Loading branch information
fkrauthan committed Jan 2, 2025
1 parent 2e57aa2 commit 3df97c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/command/metar/RunwayCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ICommand } from "../metar";
export class RunwayCommand implements ICommand {
#genericRegex = /^(R\d{2}\w?\/)/;
#runwayMaxRangeRegex = /^R(\d{2}\w?)\/(\d{4})V(\d{3,4})([UDN])?(FT)?/;
#runwayRegex = /^R(\d{2}\w?)\/([MP])?(\d{4})([UDN])?(FT)?(\/([UDN]))?$/;
#runwayRegex = /R(\d{2}\w?)\/([MP])?(\d{4})(?:([UDN])|(FT)(?:\/([UDN]))?)$/;
#runwayDepositRegex = /^R(\d{2}\w?)\/([/\d])([/\d])(\/\/|\d{2})(\/\/|\d{2})$/;

canParse(input: string): boolean {
Expand Down Expand Up @@ -43,9 +43,10 @@ export class RunwayCommand implements ICommand {

const indicator = matches[2] ? as(matches[2], ValueIndicator) : undefined;
const trend = (() => {
if (matches[6]) return as(matches[6], RunwayInfoTrend);
if (matches[4]) return as(matches[4], RunwayInfoTrend);
if (matches[7]) return as(matches[7], RunwayInfoTrend);
})();

const unit = matches[5]
? as(matches[5], RunwayInfoUnit)
: RunwayInfoUnit.Meters;
Expand Down
23 changes: 23 additions & 0 deletions tests/command/metar/RunwayCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,27 @@ describe("RunwayCommand", () => {
});
});
})();

(() => {
const code = "R36/4000FT/D"; // runway info north america style
const metar = { runwaysInfo: [] } as unknown as IMetar;

describe(code, () => {
test("canParse", () => {
expect(command.canParse(code)).toBe(true);
});

test("parse", () => {
command.execute(metar, code);

expect(metar.runwaysInfo).toHaveLength(1);
expect(metar.runwaysInfo[0]).toEqual({
name: "36",
minRange: 4000,
unit: RunwayInfoUnit.Feet,
trend: RunwayInfoTrend.Decreasing,
});
});
});
})();
});

0 comments on commit 3df97c1

Please sign in to comment.