weather-forecaster
Advanced tools
Comparing version 2.0.0 to 2.0.1
107
index.js
const fetch = require("node-fetch"); | ||
const { func, convert } = require("./util"); | ||
String.prototype.func = func; | ||
String.prototype.convert = convert; | ||
const { convertToEnglish, convertToFahrenheit } = require("./util"); | ||
String.prototype.convertToEnglish = convertToEnglish; | ||
String.prototype.convertToFahrenheit = convertToFahrenheit; | ||
module.exports = async (answer) => { | ||
const weather = async (answer, options = { date_format: "numeric" }) => { | ||
const query = encodeURI(answer.func()); | ||
const query = encodeURI(answer.convertToEnglish()); | ||
@@ -29,10 +29,11 @@ const location_url = `https://www.metaweather.com/api/location/search/?query=${query}`; | ||
const { consolidated_weather, title, woeid, parent } = data; | ||
let { date_format } = options; | ||
if (date_format !== "numeric" && date_format !== "long") date_format = "numeric"; | ||
const { | ||
const { consolidated_weather, title, parent } = data; | ||
const [{ | ||
applicable_date, | ||
weather_state_name, | ||
the_temp, | ||
min_temp, | ||
max_temp, | ||
humidity, | ||
@@ -44,45 +45,11 @@ weather_state_abbr, | ||
visibility, | ||
} | ||
= consolidated_weather[0]; | ||
}] | ||
= consolidated_weather; | ||
const direction_names = { | ||
N: "North", | ||
S: "South", | ||
E: "East", | ||
W: "West", | ||
NE: "Northeast", | ||
SE: "Southeast", | ||
NW: "Northwest", | ||
SW: "Southwest", | ||
NNE: "North-Northeast", | ||
NNW: "North-Northwest", | ||
SSE: "South-Southeast", | ||
SSW: "South-Southwest", | ||
ENE: "East-Northeast", | ||
ESE: "East-Southeast", | ||
WNW: "West-Northwest", | ||
WSW: "West-Southwest" | ||
}; | ||
const date_options = { year: "numeric", month: "long", day: "numeric" }; | ||
const date_ = new Date(applicable_date); | ||
const tr_numeric = date_.toLocaleDateString("tr-TR"); | ||
const en_numeric = date_.toLocaleDateString("en-EN"); | ||
const tr_long = date_.toLocaleDateString("tr-TR", date_options); | ||
const en_long = date_.toLocaleDateString("en-EN", date_options); | ||
const locale_date = { | ||
en: { | ||
numeric: en_numeric, | ||
long: en_long | ||
}, | ||
tr: { | ||
numeric: tr_numeric, | ||
long: tr_long | ||
} | ||
}; | ||
const locale_date = | ||
date_.toLocaleDateString("en-US", { year: "numeric", month: date_format, day: "numeric" }); | ||
const location = `${title}, ${parent.title}`; | ||
const location_id = woeid; | ||
@@ -96,47 +63,23 @@ const moisture = parseInt(humidity); | ||
const windspeed = { | ||
kmh: Number((wind_speed * 1.609344).toFixed(1)), | ||
mph: Number(wind_speed.toFixed(1)) | ||
}; | ||
const windspeed = Number((wind_speed * 1.609344).toFixed(1)); | ||
const direction_name = direction_names[wind_direction_compass]; | ||
const temp = the_temp.toFixed(1); | ||
const mintemp = min_temp.toFixed(1); | ||
const maxtemp = max_temp.toFixed(1); | ||
const celcius = { | ||
current: Number(temp), | ||
lowest: Number(mintemp), | ||
highest: Number(maxtemp) | ||
}; | ||
const visibility_range = Number((visibility * 1.609344).toFixed(1)); | ||
const fahrenheit = { | ||
current: Number(temp.convert()), | ||
lowest: Number(mintemp.convert()), | ||
highest: Number(maxtemp.convert()) | ||
}; | ||
const visibility_range = { | ||
km: Number((visibility * 1.609344).toFixed(1)), | ||
miles: Number(visibility.toFixed(1)) | ||
}; | ||
const json = { | ||
return { | ||
date: locale_date, | ||
location: location, | ||
location_woeid: location_id, | ||
humidity_percentage: moisture, | ||
humidity: `${moisture}%`, | ||
weather_state: state_name, | ||
weather_state_iconURL: state_iconURL, | ||
air_pressure_hPa: pressure, | ||
wind_speed: windspeed, | ||
wind_direction: direction_name, | ||
temp_celcius: celcius, | ||
temp_fahrenheit: fahrenheit, | ||
visibility_range: visibility_range, | ||
wind_speed: `${windspeed} kmph`, | ||
wind_direction: wind_direction_compass, | ||
temp: `${temp} °C`, | ||
visibility_range: `${visibility_range} km`, | ||
}; | ||
}; | ||
return json; | ||
}; | ||
module.exports = weather; | ||
module.exports.weather = weather; |
{ | ||
"name": "weather-forecaster", | ||
"version": "2.0.0", | ||
"description": "A NPM module that will allow you to easily access the current and detailed weather conditions of any city in any country you prefer!", | ||
"version": "2.0.1", | ||
"description": "A package that will help you obtain weather information", | ||
"main": "index.js", | ||
@@ -31,4 +31,4 @@ "scripts": { | ||
"dependencies": { | ||
"node-fetch": "^2.6.1" | ||
"node-fetch": "^2.6.5" | ||
} | ||
} | ||
} |
176
README.md
## weather-forecaster | ||
> **weather-forecaster** is a NPM module that will allow you to easily access the current and detailed weather conditions of any city in any country that you prefer! | ||
A package that will help you obtain weather information | ||
[![package-image]][package-url] [![install-image]][install-url] | ||
[![NPM][npm-image]][npm-url] | ||
## Installation | ||
```batch | ||
npm install weather-forecaster | ||
``` | ||
## Properties | ||
| Field | Type | Unit | Description | | ||
|:--------|:-------|:-----------|:-------| | ||
| date | object | | The date that the observation pertains (both numeric and long date formats) | | ||
| location | string | | Name of the location (City, Country)| | ||
| location_woeid | integer | | [Where On Earth ID](https://en.wikipedia.org/wiki/WOEID) | | ||
| humidity_percentage | integer | percentage | | | ||
| weather_state | string | | Name of the weather state | | ||
| weather_state_iconURL | string | | Icon of the weather state (SVG format) | | ||
| air_pressure_hPa | float | hPa | | | ||
| wind_speed | object | kmh, mph | | | ||
| wind_direction | string | [compass point](https://en.wikipedia.org/wiki/Points_of_the_compass#Compass_points) | Compass point of the wind direction | | ||
| temp_celcius | object | centigrade | | | ||
| temp_fahrenheit | object | fahrenheit | | | ||
| visibility_range | object | km, miles | Measure of the distance which the weather state can be clearly discerned | | ||
## Usage | ||
> Since this package's structure is asynchronous you have to use "async-await" or ".then()" structures when using the module. | ||
Example usages with these structures are given below. | ||
- **Getting all the data with IIFE:** | ||
```js | ||
const weather_forecast = require("weather-forecaster"); | ||
import weather from "weather-forecaster" | ||
weather("istanbul") | ||
/* | ||
{ | ||
date: '10/10/2021', | ||
location: 'Istanbul, Turkey', | ||
humidity: '80%', | ||
weather_state: 'Showers', | ||
weather_state_iconURL: 'https://www.metaweather.com/static/img/weather/s.svg', | ||
air_pressure_hPa: 1022, | ||
wind_speed: '20.4 kmph', | ||
wind_direction: 'NE', | ||
temp: '17.1 °C', | ||
visibility_range: '15 km' | ||
} | ||
*/ | ||
(async () => { | ||
// the "date_format" option is "numeric" by default, can be used as "long" for long date response | ||
const data = await weather_forecast("san francisco"); | ||
// your code.. | ||
console.log(data); | ||
})(); | ||
weather("new york", { date_format: "long" }) | ||
/* | ||
{ | ||
date: 'October 10, 2021', | ||
location: 'New York, New York', | ||
humidity: '83%', | ||
weather_state: 'Light Rain', | ||
weather_state_iconURL: 'https://www.metaweather.com/static/img/weather/lr.svg', | ||
air_pressure_hPa: 1021.5, | ||
wind_speed: '14.1 kmph', | ||
wind_direction: 'ENE', | ||
temp: '19.0 °C', | ||
visibility_range: '13.2 km' | ||
} | ||
*/ | ||
``` | ||
- **Getting all the data with .then():** | ||
```js | ||
const weather_forecast = require("weather-forecaster"); | ||
weather_forecast("istanbul").then(data => { | ||
// your code.. | ||
console.log(data); | ||
}); | ||
``` | ||
- **Getting all the data with async-await:** | ||
```js | ||
const weather_forecast = require("weather-forecaster"); | ||
async function myWeatherData(query) { | ||
const data = await weather_forecast(query); | ||
// your code.. | ||
console.log(data); | ||
}; | ||
myWeatherData("new york"); | ||
``` | ||
- **Destructuring & getting specific data:** | ||
```js | ||
const weather_forecast = require("weather-forecaster"); | ||
async function myWeatherData(query) { | ||
const data = await weather_forecast(query); | ||
/* | ||
this destructuring assignment will allow us to use | ||
these parts of the data without using the data.<name> format | ||
*/ | ||
const { weather_state, temp_celcius } = data; | ||
// in this case the expected outputs will be the same | ||
// example outputs: | ||
console.log(weather_state); // Light Cloud | ||
console.log(data.weather_state); // Light Cloud | ||
/* | ||
we can do that destructuring assignment once again with | ||
the parts that we already destructured | ||
*/ | ||
const { lowest, highest, current } = temp_celcius; | ||
const first_array = [ | ||
lowest, | ||
highest, | ||
current | ||
]; | ||
const second_array = [ | ||
temp_celcius.lowest, | ||
temp_celcius.highest, | ||
temp_celcius.current | ||
]; | ||
const third_array = [ | ||
data.temp_celcius.lowest, | ||
data.temp_celcius.highest, | ||
data.temp_celcius.current | ||
]; | ||
// in this case the expected outputs will be the same | ||
// example outputs: | ||
console.log(first_array); // [ 15.2, 23.2, 23.5 ] | ||
console.log(second_array); // [ 15.2, 23.2, 23.5 ] | ||
console.log(third_array); // [ 15.2, 23.2, 23.5 ] | ||
}; | ||
myWeatherData("new york"); | ||
``` | ||
## License | ||
> Licensed under The MIT License (MIT). | ||
> For the full copyright and license information, | ||
> please view the [LICENSE](https://github.com/ardneps/weather-forecaster/blob/master/LICENSE) file. | ||
[package-url]: http://npmjs.org/package/weather-forecaster | ||
[package-image]: https://badge.fury.io/js/weather-forecaster.svg | ||
[install-url]: https://packagephobia.com/result?p=weather-forecaster | ||
[install-image]: https://packagephobia.com/badge?p=weather-forecaster | ||
[npm-url]: https://nodei.co/npm/weather-forecaster/ | ||
[npm-image]: https://nodei.co/npm/weather-forecaster.png?downloads=true&downloadRank=true&stars=false | ||
MIT |
36
util.js
module.exports = { | ||
func() { | ||
convertToEnglish() { | ||
return this | ||
.replace('Ğ', 'g') | ||
.replace('Ü', 'u') | ||
.replace('Ş', 's') | ||
.replace('I', 'i') | ||
.replace('İ', 'i') | ||
.replace('Ö', 'o') | ||
.replace('Ç', 'c') | ||
.replace('ğ', 'g') | ||
.replace('ü', 'u') | ||
.replace('ş', 's') | ||
.replace('ı', 'i') | ||
.replace('ö', 'o') | ||
.replace('ç', 'c'); | ||
.replace("Ğ", "g") | ||
.replace("Ü", "u") | ||
.replace("Ş", "s") | ||
.replace("I", "i") | ||
.replace("İ", "i") | ||
.replace("Ö", "o") | ||
.replace("Ç", "c") | ||
.replace("ğ", "g") | ||
.replace("ü", "u") | ||
.replace("ş", "s") | ||
.replace("ı", "i") | ||
.replace("ö", "o") | ||
.replace("ç", "c"); | ||
}, | ||
convertToFahrenheit() { | ||
convert() { | ||
return ((this * 9 / 5) + 32).toFixed(1); | ||
} | ||
}; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6031
76
47
3
Updatednode-fetch@^2.6.5