UNPKG

snow-forecast-sfr

Version:

An NPM module that scrapes snow-forecast for the relevant resort and returns its information.

52 lines (51 loc) 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const TimeUtil = { times: ['AM', 'PM', 'night'], days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], currentDayOffset: 0, MIN_DAY_STRING_LEN: 4, MAX_INDEX_CNT: 17, getTime: function (timePeriodIndex, startDay, index) { const timeDiff = (timePeriodIndex + index) % this.times.length; const dayDiff = this.getDayOffset(startDay, timePeriodIndex, index); if (index === this.MAX_INDEX_CNT) { this.currentDayOffset = 0; } return this.days[dayDiff] + ' ' + this.times[timeDiff]; }, getTimeOffset: function (startTime) { return this.times.indexOf(startTime); }, getDayOffset: function (startDay, timePeriodIndex, index) { const firstDayPos = this.days.indexOf(this.getCorrectDay(startDay)); if (((timePeriodIndex + index) / this.times.length) % 1 === 0 && timePeriodIndex + index !== 0) { this.currentDayOffset++; } return (firstDayPos + this.currentDayOffset) % this.days.length; }, getDay: function (lastUpdateDate, timePeriodIndex, index) { const offset = Math.floor((timePeriodIndex + index) / this.times.length); const date = new Date(lastUpdateDate.trim()); if (offset > 0) { date.setDate(date.getDate() + offset); } return date.toDateString(); }, getCorrectDay: function (day) { if (day.length > this.MIN_DAY_STRING_LEN) { return day.split(" ")[0].trim(); } return this.days.find(currentDay => currentDay.includes(day)); }, fixIssueDateFormat: function (issuedDate) { const tmpDateArray = issuedDate.split(" "); const tmpDate = tmpDateArray.join('').match(/(\d+|[^\d]+)/g); return tmpDate[0] + tmpDate[1].trim() + ' ' + tmpDate[2] + ' ' + tmpDate[3].trim() + ' ' + tmpDate[4]; }, getPrevDay: function (startDay) { const index = this.days.indexOf(startDay); return index === 0 ? this.days[6] : this.days[index - 1]; } }; exports.default = TimeUtil;