Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove negative lookbehind workaround #88

Merged
merged 1 commit into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 9 additions & 29 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,17 @@ export abstract class AbstractParser {
BECMG = "BECMG";
RMK = "RMK";

// Safari does not currently support negative lookbehind
// #TOKENIZE_REGEX = /\s((?=\d\/\dSM)(?<!\s\d\s)|(?!\d\/\dSM))|=/;
#INTENSITY_REGEX = /^(-|\+|VC)/;
#CAVOK = "CAVOK";
#commonSupplier = new CommandSupplier();
static #TOKENIZE_REGEX = /\s((?=\d\/\dSM)(?<!\s(P|M)?\d\s)|(?!\d\/\dSM))|=/;
static #INTENSITY_REGEX = /^(-|\+|VC)/;
static #CAVOK = "CAVOK";
static #commonSupplier = new CommandSupplier();

constructor(protected locale: Locale) {}

parseWeatherCondition(input: string): IWeatherCondition | undefined {
let intensity: Intensity | undefined;
if (input.match(this.#INTENSITY_REGEX)) {
const match = input.match(this.#INTENSITY_REGEX)?.[0];
if (input.match(AbstractParser.#INTENSITY_REGEX)) {
const match = input.match(AbstractParser.#INTENSITY_REGEX)?.[0];
if (match) {
intensity = match as Intensity;
input = input.slice(match.length);
Expand Down Expand Up @@ -228,26 +227,7 @@ export abstract class AbstractParser {
* @returns List of tokens
*/
tokenize(input: string) {
// Missing safari support. If added in the future, put this back
// return input.split(this.#TOKENIZE_REGEX).filter((v) => v);

// Hack for safari below...
const splitRegex = /\s|=/;
const smRegex = /^\d\/\dSM$/;
const digitRegex = /^(P|M)?\d$/;

// return input.split(this.#TOKENIZE_REGEX).filter((v) => v);
const splitted = input.split(splitRegex);

for (let i = 0; i < splitted.length; i++) {
if (digitRegex.test(splitted[i])) {
if (splitted[i + 1] && smRegex.test(splitted[i + 1])) {
splitted.splice(i, 2, `${splitted[i]} ${splitted[i + 1]}`);
}
}
}

return splitted.filter((t) => t);
return input.split(AbstractParser.#TOKENIZE_REGEX).filter((v) => v);
}

/**
Expand All @@ -260,7 +240,7 @@ export abstract class AbstractParser {
abstractWeatherContainer: IAbstractWeatherContainer,
input: string,
): boolean {
if (input === this.#CAVOK) {
if (input === AbstractParser.#CAVOK) {
abstractWeatherContainer.cavok = true;
abstractWeatherContainer.visibility = {
indicator: ValueIndicator.GreaterThan,
Expand All @@ -278,7 +258,7 @@ export abstract class AbstractParser {
return true;
}

const command = this.#commonSupplier.get(input);
const command = AbstractParser.#commonSupplier.get(input);

if (command) {
try {
Expand Down
Loading