Skip to content

Instantly share code, notes, and snippets.

@bigslycat
Created June 16, 2017 13:59
Show Gist options
  • Select an option

  • Save bigslycat/a1dfcc2561a739ba29904ad87ec2e5a9 to your computer and use it in GitHub Desktop.

Select an option

Save bigslycat/a1dfcc2561a739ba29904ad87ec2e5a9 to your computer and use it in GitHub Desktop.
/* @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