Skip to content

Commit

Permalink
refactor(forecast): overnight/% chances
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceh91 committed Nov 1, 2023
1 parent 9c4e78c commit 6049741
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/__tests__/abbreviateForecast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,23 @@ describe("Forecast Truncation", () => {
expect(abbreviateForecast("cloudy with 60 percent chance of showers. low 13.", forecastLengthWanted)).toStrictEqual(
"cloudy w/ 60% chnc of shwrs. low 13."
);
expect(
abbreviateForecast(
"30 percent chance of rain showers or flurries changing to 30 percent chance of flurries near midnight",
forecastLengthWanted
)
).toStrictEqual("30% chnc of rain shwrs/flrys until 12am");
expect(
abbreviateForecast(
"70 percent chance of rain showers or flurries changing to 30 percent chance of flurries near midnight",
forecastLengthWanted
)
).toStrictEqual("30-70% chnc of rain shwrs/flrys until 12am");
});

test("Condition developing", () => {
expect(abbreviateForecast("fog patches developing overnight", forecastLengthWanted)).toStrictEqual(
"fog patches overnight"
expect(abbreviateForecast("fog patches developing ovrngt", forecastLengthWanted)).toStrictEqual(
"fog patches ovrngt"
);
expect(abbreviateForecast("fog patches developing after 12am", forecastLengthWanted)).toStrictEqual(
"fog patches after 12am"
Expand All @@ -118,9 +130,7 @@ describe("Forecast Truncation", () => {
expect(abbreviateForecast("30% chance of showers or tstorms", forecastLengthWanted)).toStrictEqual(
"30% chnc of shwrs/tstorms"
);
expect(abbreviateForecast("rain or flurries overnight", forecastLengthWanted)).toStrictEqual(
"rain/flrys overnight"
);
expect(abbreviateForecast("rain or flurries ovrngt", forecastLengthWanted)).toStrictEqual("rain/flrys ovrngt");
});

test("Wintery weather", () => {
Expand Down
22 changes: 19 additions & 3 deletions src/lib/conditions/forecast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const abbreviateTimeOfDay = (forecast: string) =>
.replace(/morning/gi, "mrng")
.replace(/afternoon/gi, "aftn")
.replace(/evening/gi, "eve")
.replace(/overnight/gi, "ovrngt")
.replace(/midnight/gi, "12am")
.replace(/beginning/gi, "bgng")
.replace(/occasional/gi, "ocnl")
Expand Down Expand Up @@ -111,14 +112,29 @@ const abbreviatePrecipitationPredictions = (forecast: string) =>
forecast.replace(/amount (\d+) (-|to) (\d+) (cm|mm)/gi, "amount $1-$3$4");

const abbreviateChanceOfPrecipitation = (forecast: string) => {
const initialAbbreviation = forecast.replace(/chance/gi, "chnc");
if (!forecast.includes("chance")) return forecast;

// precip is interesting. lets do abbreviations first
let abbreviation = forecast.replace(/chance/gi, "chnc");
if (forecast.includes("near"))
return initialAbbreviation.replace(
abbreviation = abbreviation.replace(
/(\d+)% chnc (.+?) changing to (\d+)% chnc .+(noon|midnigh)/gi,
"$1-$3% chnc $2 until $4"
);
else abbreviation = abbreviation.replace(/(\d+)% chnc (.+?) changing to (\d+)% chnc (.+)/gi, "$1-$3% chnc $4");

// if there's no range for the chance, return now
if (!abbreviation.includes("-")) return abbreviation;

// then make sure the percentages are min-max%, or that its just one % if both numbers are the same
const [min, max, ...rest] = abbreviation.split(/-|%/g);
if (!max) return abbreviation;

return initialAbbreviation.replace(/(\d+)% chnc (.+?) changing to (\d+)% chnc (.+)/gi, "$1-$3% chnc $4");
// now return after some edits
const minNumber = Number(min);
const maxNumber = Number(max);
if (minNumber === maxNumber) return `${minNumber}% ${rest.join(" ").trim()}`;
else return `${Math.min(minNumber, maxNumber)}-${Math.max(minNumber, maxNumber)}% ${rest.join(" ").trim()}`;
};

const abbreviateShortCompassDirections = (forecast: string) =>
Expand Down

0 comments on commit 6049741

Please sign in to comment.