-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.min.js
9 lines (7 loc) · 4.88 KB
/
weather.min.js
1
2
3
4
5
6
7
8
9
/* Name = weather.js
* Author = Noah H. Smith <[email protected]> (http://noazark.github.com/)
* Version = 0.1.0
* License = MIT
* About = A conclusive JavaScript weather library.
*/
var isModule="undefined"!=typeof module&&module.exports;isModule&&(http=require("http"),URL=require("url"));var Weather={Utils:{}};Weather.VERSION="0.1.0",Weather.LANGUAGE="en";var jsonp=Weather.Utils.jsonp=function(a,b){return new Promise(function(b,c){var d="_"+Math.round(1e4*Math.random()),e="jsonp_callback_"+d,f=document.getElementsByTagName("head")[0]||document.body||document.documentElement,g=document.createElement("script"),h=a+"&callback="+e;window[e]=function(a){delete window[e];var c=document.getElementById(d);c.parentNode.removeChild(c),b(a)},g.src=h,g.id=d,g.addEventListener("error",c),f.appendChild(g)})};Weather.getApiKey=function(){return Weather.APIKEY},Weather.setApiKey=function(a){Weather.APIKEY=a},Weather.getLanguage=function(){return Weather.LANGUAGE},Weather.setLanguage=function(a){Weather.LANGUAGE=a},Weather.kelvinToFahrenheit=function(a){return 1.8*this.kelvinToCelsius(a)+32},Weather.kelvinToCelsius=function(a){return a-273.15},Weather.getCurrent=function(a,b){var c="https://api.openweathermap.org/data/2.5/weather?q="+encodeURIComponent(a)+"&cnt=1";return Weather.LANGUAGE&&(c=c+"&lang="+Weather.LANGUAGE),Weather.APIKEY?c=c+"&APPID="+Weather.APIKEY:console.log("WARNING: You must provide an OpenWeatherMap API key."),this._getJSON(c,function(a){b(new Weather.Current(a))})},Weather.getCurrentByCityId=function(a,b){var c="https://api.openweathermap.org/data/2.5/weather?id="+encodeURIComponent(a)+"&cnt=1";return Weather.LANGUAGE&&(c=c+"&lang="+Weather.LANGUAGE),Weather.APIKEY?c=c+"&APPID="+Weather.APIKEY:console.log("WARNING: You must provide an OpenWeatherMap API key."),this._getJSON(c,function(a){b(new Weather.Current(a))})},Weather.getCurrentByLatLong=function(a,b,c){var d="https://api.openweathermap.org/data/2.5/weather?lat="+encodeURIComponent(a)+"&lon="+encodeURIComponent(b)+"&cnt=1";return Weather.LANGUAGE&&(d=d+"&lang="+Weather.LANGUAGE),Weather.APIKEY?d=d+"&APPID="+Weather.APIKEY:console.log("WARNING: You must provide an OpenWeatherMap API key."),this._getJSON(d,function(a){c(new Weather.Current(a))})},Weather.getForecast=function(a,b){var c="https://api.openweathermap.org/data/2.5/forecast?q="+encodeURIComponent(a)+"&cnt=1";return Weather.LANGUAGE&&(c=c+"&lang="+Weather.LANGUAGE),Weather.APIKEY?c=c+"&APPID="+Weather.APIKEY:console.log("WARNING: You must provide an OpenWeatherMap API key."),this._getJSON(c,function(a){b(new Weather.Forecast(a))})},Weather.getForecastByCityId=function(a,b){var c="https://api.openweathermap.org/data/2.5/forecast?id="+encodeURIComponent(a)+"&cnt=1";return Weather.LANGUAGE&&(c=c+"&lang="+Weather.LANGUAGE),Weather.APIKEY?c=c+"&APPID="+Weather.APIKEY:console.log("WARNING: You must provide an OpenWeatherMap API key."),this._getJSON(c,function(a){b(new Weather.Forecast(a))})},Weather.getForecastByLatLong=function(a,b,c){var d="https://api.openweathermap.org/data/2.5/forecast?lat="+encodeURIComponent(a)+"&lon="+encodeURIComponent(b)+"&cnt=1";return Weather.LANGUAGE&&(d=d+"&lang="+Weather.LANGUAGE),Weather.APIKEY?d=d+"&APPID="+Weather.APIKEY:console.log("WARNING: You must provide an OpenWeatherMap API key."),this._getJSON(d,function(a){c(new Weather.Forecast(a))})},Weather._getJSON=function(a,b){if(isModule)return http.get(URL.parse(a),function(a){return b(a.body)});jsonp(a).then(b)};var maxBy=Weather.Utils.maxBy=function(a,b){var c,d=function(a,d){var e=b(d);return(null===a||e>c)&&(c=e,a=d),a};return a.reduce(d,null)},minBy=Weather.Utils.minBy=function(a,b){var c,d=function(a,d){var e=b(d);return(null===a||e<c)&&(c=e,a=d),a};return a.reduce(d,null)},isOnDate=Weather.Utils.isOnDate=function(a,b){var c=a.getYear()===b.getYear(),d=a.getMonth()===b.getMonth(),e=a.getDate()===b.getDate();return c&&d&&e};Weather.Forecast=function(a){this.data=a},Weather.Forecast.prototype.startAt=function(){return new Date(1e3*minBy(this.data.list,function(a){return a.dt}).dt)},Weather.Forecast.prototype.endAt=function(){return new Date(1e3*maxBy(this.data.list,function(a){return a.dt}).dt)},Weather.Forecast.prototype.day=function(a){return new Weather.Forecast(this._filter(a))},Weather.Forecast.prototype.low=function(){if(0!==this.data.list.length){return minBy(this.data.list,function(a){return a.main.temp_min}).main.temp_min}},Weather.Forecast.prototype.high=function(){if(0!==this.data.list.length){return maxBy(this.data.list,function(a){return a.main.temp_max}).main.temp_max}},Weather.Forecast.prototype._filter=function(a){return{list:this.data.list.filter(function(b){var c=1e3*b.dt;if(isOnDate(new Date(c),a))return b})}},Weather.Current=function(a){this.data=a},Weather.Current.prototype.temperature=function(){return this.data.main.temp},Weather.Current.prototype.conditions=function(){return this.data.weather[0].description},isModule?module.exports=Weather:window.Weather=Weather;