While building a weather app using Environment Canada's RSS feed, I needed a way to extract weather data from the XML. A JSON API was not available, and I found the XML API unwieldy. So, I developed a human-centric, JavaScript-friendly API: a simple JavaScript object with intuitive, straight-forward properties for accessing the weather data.
▸ Get current conditions
▸ Get forecast for a specific date
▸ Get weekly forecast
▸ Get hourly forecast
▸ Narrow weekly and hourly forecasts to just temperatures or precipitation or winds
▸ JSDoc documentation
▸ Jest tested
One intuitive feature I wanted was the ability to narrow weekly and hourly forecasts to just temperatures or precipitation or winds. And I wanted to do this by chaining.
weather.weekly
yields an array of detailed objects for every day of the week,
but weather.weekly.temperatures
should yield an array of simplified objects with just the day of the week and the temperature for that day.
Similarly, for weather.weekly.precipitation
and weather.weekly.winds
.
To accomplish this, I extended the Array class with getters that could yield the appropriately narrowed arrays.
This way, weather.weekly
yields a detailed array that can be handled like a regular array.
But this array also possesses getters that can yield further arrays.