Last active
October 24, 2019 14:15
-
-
Save wisniewski94/533a2f53f6338d226d9f2322ce95f731 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from "./httpService"; | |
import _ from "lodash"; | |
class Weather { | |
constructor(location, dayTime) { | |
if (_.isString(location) == false || _.isString(dayTime) == false) { | |
throw new Error("Invalid parameters"); | |
} | |
this.location = location; | |
this.dayTime = dayTime; | |
this.times = _.chain([ | |
[0, 5, "night"], | |
[6, 12, "morning"], | |
[13, 18, "afternoon"], | |
[18, 23, "evening"], | |
[0, 23, "day"] | |
]) | |
.filter(o => o.filter(el => el === this.dayTime).length > 0) | |
.flatten() | |
.filter(_.isNumber) | |
.value(); | |
if (this.times.length < 1) { | |
throw new Error("Please provide correct day time"); | |
} | |
} | |
tracker = []; | |
async insert() { | |
const options = { params: { q: this.location, cnt: 5 } }; | |
const { data } = await http.get("/forecast", options); | |
this.tracker = _.union(this.tracker, [...data.list]); | |
return this.tracker; | |
} | |
get unixTodayRange() { | |
const today = new Date(); | |
const y = today.getFullYear(); | |
const m = today.getMonth(); | |
const d = today.getDate(); | |
const range = [ | |
Date.UTC(y, m, d, this.times[0]) / 1000, | |
Date.UTC(y, m, d, this.times[1]) / 1000 | |
]; | |
const array = _.chain(this.tracker) | |
.filter(element => _.inRange(element.dt, range[0], range[1])) | |
.value(); | |
return array; | |
} | |
showMin(key) { | |
const array = this.unixTodayRange; | |
return _.minBy(array, chr => { | |
return chr.main[key]; | |
}); | |
} | |
showMax(key) { | |
const array = this.unixTodayRange; | |
return _.maxBy(array, chr => { | |
return chr.main[key]; | |
}); | |
} | |
showMean(key) { | |
const array = this.unixTodayRange; | |
return _.meanBy(array, o => { | |
return o.main[key]; | |
}); | |
} | |
showMode(key) { | |
const range = this.unixTodayRange; | |
const array = _.chain(range) | |
.map(el => el.main[key]) | |
.value(); | |
return _.chain(array) | |
.countBy() | |
.invertBy() | |
.toPairs() | |
.max(_.head) | |
.last() | |
.value(); | |
} | |
} | |
export default Weather; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment