Add next open/close day panel + refactor common funcs to util

This commit is contained in:
2018-03-19 14:00:34 +05:30
parent eb3151471a
commit 047bc7c291
11 changed files with 309 additions and 118 deletions
+1 -1
View File
@@ -1 +1 @@
export { Utils } from "./utils";
export { Utils, HolidayAnswer, OpenOrClose } from "./utils";
+100
View File
@@ -1,7 +1,107 @@
import * as moment from 'moment-timezone';
import * as Linq from 'linq';
import { Holiday, HolidayList, getWeekendHolidayOnDate } from '../Data';
export interface HolidayAnswer {
open: boolean;
holiday: Holiday | undefined;
}
export enum OpenOrClose {
Open = 'open',
Close = 'closed'
}
export class Utils {
static invertOpenOrClose(openOrClose: OpenOrClose) {
return (openOrClose === OpenOrClose.Open) ? OpenOrClose.Close : OpenOrClose.Open;
}
static createTodayDateInIndiaTZ(): moment.Moment {
return moment.tz(moment.tz.guess()).tz("Asia/Kolkata");
}
static checkSameDayInSameTZ(moment1: moment.Moment, moment2: moment.Moment, raiseErrorTZMismatch: boolean = true) {
// tslint:disable-next-line:triple-equals
if (raiseErrorTZMismatch && (moment1.tz() != moment2.tz())) {
throw new Error("Timezones don't match");
}
// tslint:disable-next-line:triple-equals
return (moment1.clone().startOf("day").format() == moment2.clone().startOf("day").format());
}
static checkSameYearInSameTZ(moment1: moment.Moment, moment2: moment.Moment, raiseErrorTZMismatch: boolean = true) {
// tslint:disable-next-line:triple-equals
if (raiseErrorTZMismatch && (moment1.tz() != moment2.tz())) {
throw new Error("Timezones don't match");
}
// tslint:disable-next-line:triple-equals
return (moment1.clone().startOf("year").format() == moment2.clone().startOf("year").format());
}
static getHolidayAnswerOnDate(xDay: moment.Moment, allHolidays: HolidayList): HolidayAnswer {
let tmpHolidayAnswer: HolidayAnswer;
{
const eventHoliday = this.getEventHolidayOnDate(xDay, allHolidays);
if (typeof eventHoliday !== "undefined") {
tmpHolidayAnswer = { open: false, holiday: eventHoliday };
} else {
tmpHolidayAnswer = { open: true, holiday: undefined };
}
}
{
const weekendHoliday = getWeekendHolidayOnDate(xDay);
if (typeof weekendHoliday !== "undefined") {
tmpHolidayAnswer = { open: false, holiday: weekendHoliday };
}
}
return tmpHolidayAnswer;
}
static getEventHolidayOnDate(xDay: moment.Moment, allHolidays: HolidayList): Holiday | undefined {
{
const matchingHoliday = Linq
.from<Holiday>(allHolidays)
.firstOrDefault(
(eachHoliday, _tmp) => Utils.checkSameDayInSameTZ(xDay, eachHoliday.Date)
);
if (matchingHoliday === null) {
return undefined;
} else {
return matchingHoliday;
}
}
}
static findNextOpenOrCloseFromDate(
xDay: moment.Moment,
allHolidays: HolidayList,
openOrClose: OpenOrClose): moment.Moment | undefined {
let currentDay = xDay.clone().add(1, "day");
while (this.checkSameYearInSameTZ(currentDay, xDay)) {
var holidayAnswerOnCurrentDay = this.getHolidayAnswerOnDate(currentDay, allHolidays);
if ((openOrClose === OpenOrClose.Open) && holidayAnswerOnCurrentDay.open) {
return currentDay;
}
if ((openOrClose === OpenOrClose.Close) && !holidayAnswerOnCurrentDay.open) {
return currentDay;
}
currentDay = currentDay.add(1, "day");
}
return undefined;
}
}