Created
June 16, 2017 13:59
-
-
Save bigslycat/a1dfcc2561a739ba29904ad87ec2e5a9 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
| /* @flow */ | |
| import moment from 'moment-timezone'; | |
| const isAcceptByArrayRules = (arr: any[], given: any) => !arr.length || arr.includes(given); | |
| const timeBlocksFilter = ( | |
| byCity: string[], | |
| byClinic: string[], | |
| bySpec: string[], | |
| byMedic: string[], | |
| byDate: { from: moment, to?: moment }[], | |
| byDay: number[], | |
| byTime: { from: [number, number], to?: [number, number] }[], | |
| ) => | |
| ({ cityId, clinicId, specId, medicId, from }: { | |
| cityId: string, | |
| clinicId: string, | |
| specId: string, | |
| medicId: string, | |
| from: moment, | |
| }) => { | |
| if (!isAcceptByArrayRules(byCity, cityId)) return false; | |
| if (!isAcceptByArrayRules(byClinic, clinicId)) return false; | |
| if (!isAcceptByArrayRules(bySpec, specId)) return false; | |
| if (!isAcceptByArrayRules(byMedic, medicId)) return false; | |
| if (byDate.length) { | |
| const result = byDate.filter(({ from: dateFrom, to: dateTo }) => { | |
| const fromValue = from.clone().startOf('day').valueOf(); | |
| if (fromValue < dateFrom.valueOf()) return false; | |
| if (dateTo && fromValue >= dateTo.valueOf()) return false; | |
| return true; | |
| }); | |
| if (!result.length) return false; | |
| } | |
| if (byDay.length && !byDay.filter(day => day === from.weekday()).length) return false; | |
| if (byTime.length) { | |
| const result = byTime.filter( | |
| ({ from: [hoursFrom, minutesFrom], to: [hoursTo, minutesTo] = [] }) => { | |
| const fromValue = from.valueOf(); | |
| const time = from.clone(); | |
| if (fromValue < time.set({ hour: hoursFrom, minute: minutesFrom }).valueOf()) { | |
| return false; | |
| } | |
| if (hoursTo && fromValue >= time.set({ hour: hoursTo, minute: minutesTo }).valueOf()) { | |
| return false; | |
| } | |
| return true; | |
| }, | |
| ); | |
| if (!result.length) return false; | |
| } | |
| return true; | |
| }; | |
| export default timeBlocksFilter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment