-
Notifications
You must be signed in to change notification settings - Fork 98
Игнатенко Алексей #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,63 @@ | |
* Сделано задание на звездочку | ||
* Реализовано оба метода и tryLater | ||
*/ | ||
const isStar = true; | ||
const isStar = false; | ||
|
||
const minutesPerHour = 60; | ||
const hourPerDay = 24; | ||
|
||
let weekDays = ['ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; | ||
|
||
let dayTimeStrToMinutes = (weekDay, timeStr, timeZone, bankTimeZone) => | ||
[weekDays.indexOf(weekDay) * minutesPerHour * hourPerDay, | ||
timeStr.split(':')[0] * minutesPerHour + Number(timeStr.split(':')[1]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
(bankTimeZone - (timeZone === undefined ? 0 : Number(timeZone))) * minutesPerHour | ||
].reduce((a, b) => a + b); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. какой тут смысл от reduce, когда можно просто сложить переменные? |
||
|
||
let addTimeToTwoChars = (time) => time.toString().length === 2 ? time.toString() : `0${time}`; | ||
|
||
function minutesToDayTime(minutes, bankTimeZone) { | ||
let weekDay = parseInt(minutes / (minutesPerHour * hourPerDay)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
minutes -= minutesPerHour * hourPerDay * parseInt(minutes / (minutesPerHour * hourPerDay)); | ||
weekDay = weekDays[weekDay]; | ||
let hours = parseInt(minutes / minutesPerHour); | ||
minutes -= hours * minutesPerHour; | ||
bankTimeZone = !bankTimeZone ? '' : `+${bankTimeZone}`; | ||
|
||
return { | ||
day: weekDay, | ||
hours: addTimeToTwoChars(hours), | ||
minutes: addTimeToTwoChars(minutes), | ||
tz: bankTimeZone }; | ||
} | ||
|
||
function getBankSchedule(workingHours, bankTimeZone) { | ||
let endTime = dayTimeStrToMinutes(...`СР 23:59+${bankTimeZone}`.split(/ |\+/), bankTimeZone); | ||
let endWeek = dayTimeStrToMinutes(...`ВС 23:59+${bankTimeZone}`.split(/ |\+/), bankTimeZone); | ||
|
||
return weekDays | ||
.slice(0, 3) | ||
.map((day)=> | ||
-dayTimeStrToMinutes(...`${day} ${workingHours.from}`.split(/ |\+/), bankTimeZone)) | ||
.concat( | ||
weekDays | ||
.slice(0, 3) | ||
.map((day)=> | ||
dayTimeStrToMinutes( | ||
...`${day} ${workingHours.to}`.split(/ |\+/), bankTimeZone))) | ||
.concat([endTime, -endWeek]); | ||
} | ||
|
||
function getSign(number) { | ||
let sign = isNaN(Math.sign(number)) ? 1 : Math.sign(number); | ||
if (isNaN(sign)) { | ||
sign = 1; | ||
} else if (sign === 0) { | ||
sign = -1; | ||
} | ||
|
||
return sign; | ||
} | ||
|
||
/** | ||
* @param {Object} schedule – Расписание Банды | ||
|
@@ -16,6 +72,37 @@ const isStar = true; | |
*/ | ||
function getAppropriateMoment(schedule, duration, workingHours) { | ||
console.info(schedule, duration, workingHours); | ||
let bankTimeZone = Number(workingHours.from.split('+')[1]); | ||
bankTimeZone = isNaN(bankTimeZone) ? 0 : bankTimeZone; | ||
workingHours = getBankSchedule(workingHours, bankTimeZone); | ||
let answer = [NaN].concat(Object.keys(schedule) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. такой код очень сложно читать и понимать Например, зачем [NaN] в начале? |
||
.map((friend)=>[...schedule[friend]]) | ||
.reduce((a, b)=>a.concat(b), []) | ||
.map((record)=> | ||
[ | ||
dayTimeStrToMinutes(...record.from.split(/ |\+/), bankTimeZone), | ||
-dayTimeStrToMinutes(...record.to.split(/ |\+/), bankTimeZone)]) | ||
.reduce((a, b)=>a.concat(b), []) | ||
.concat(workingHours)) | ||
.sort((a, b) => Math.abs(a) - Math.abs(b)) | ||
.reduce(function (result, time) { | ||
let lastInterval = result.intervals[result.intervals.length - 1]; | ||
let sign = getSign(time); | ||
if (!(result.count += sign)) { | ||
result.intervals.push({ from: -time, to: 0 }); | ||
} else if (result.count === 1 && (lastInterval !== undefined) && !(lastInterval.to)) { | ||
result.intervals[result.intervals.length - 1].to = time; | ||
} | ||
|
||
return result; | ||
}, { count: 0, intervals: [] }) | ||
.intervals | ||
.filter((record)=>Math.abs(record.to - record.from) >= duration) | ||
.map((record)=> { | ||
return { | ||
from: minutesToDayTime(record.from, bankTimeZone), | ||
to: minutesToDayTime(record.to, bankTimeZone) }; | ||
}); | ||
|
||
return { | ||
|
||
|
@@ -24,7 +111,7 @@ function getAppropriateMoment(schedule, duration, workingHours) { | |
* @returns {Boolean} | ||
*/ | ||
exists: function () { | ||
return false; | ||
return Boolean(answer.length); | ||
}, | ||
|
||
/** | ||
|
@@ -34,7 +121,11 @@ function getAppropriateMoment(schedule, duration, workingHours) { | |
* @returns {String} | ||
*/ | ||
format: function (template) { | ||
return template; | ||
return !answer.length | ||
? '' : template | ||
.replace('%DD', answer[0].from.day) | ||
.replace('%HH', answer[0].from.hours) | ||
.replace('%MM', answer[0].from.minutes); | ||
}, | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const MINUTES_PER_HOUR = 60;