UNPKG

visiting-hours

Version:

An advanced, feature rich, optimised and tiny visiting-hours library.

157 lines (118 loc) 7.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HoursIndex = void 0; var _Utils = require("./Utils"); function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var HoursIndex = /*#__PURE__*/function () { function HoursIndex(_ref) { var regularIndex = _ref.regularIndex, specialIndex = _ref.specialIndex; _classCallCheck(this, HoursIndex); _defineProperty(this, "index", void 0); _defineProperty(this, "specialIndex", void 0); if (regularIndex) this.index = regularIndex; if (specialIndex) this.specialIndex = specialIndex; } _createClass(HoursIndex, [{ key: "dateKeys", value: function dateKeys(date) { var _this$specialIndex, _this$specialIndex2, _this$specialIndex3, _this$index; var month = date.month, day = date.day, weekday = date.weekday; return { specialDate: ((_this$specialIndex = this.specialIndex) === null || _this$specialIndex === void 0 ? void 0 : _this$specialIndex["".concat(day, "/").concat(month)]) || null, leapYearDate: ((_this$specialIndex2 = this.specialIndex) === null || _this$specialIndex2 === void 0 ? void 0 : _this$specialIndex2[_Utils.Utils.leapYearKey]) || null, postLeapYearDate: ((_this$specialIndex3 = this.specialIndex) === null || _this$specialIndex3 === void 0 ? void 0 : _this$specialIndex3[_Utils.Utils.postLeapYearKey]) || null, regularDate: ((_this$index = this.index) === null || _this$index === void 0 ? void 0 : _this$index[weekday]) || null }; } }, { key: "checkSpecialDay", value: function checkSpecialDay(query) { var _query$day; var dayResult = this.checkDay(query); if (dayResult) { return dayResult; } if (typeof ((_query$day = query.day) === null || _query$day === void 0 ? void 0 : _query$day.open) === 'boolean') { return this.makeHourResult(query); } } }, { key: "checkDay", value: function checkDay(query) { var day = query.day; if (!(day === null || day === void 0 ? void 0 : day.open) && !(day === null || day === void 0 ? void 0 : day.pastMidnight)) { return this.makeHourResult(query); } return this.findHour(query); } }, { key: "findHour", value: function findHour(query) { var key = query.key, skipSoonest = query.skipSoonest, day = query.day; if (!day) return this.makeHourResult(query); // No need to check soonest, do fast lookup. if (skipSoonest) { var _day$hours; var open = (_day$hours = day.hours) === null || _day$hours === void 0 ? void 0 : _day$hours.find(function (_ref2) { var _ref3 = _slicedToArray(_ref2, 2), o = _ref3[0], c = _ref3[1]; return (key - o ^ key - c) < 0; }); return this.makeHourResult(query, open); } var soonest = null; var _iterator = _createForOfIteratorHelper(day.hours), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var hour = _step.value; var _hour = _slicedToArray(hour, 2), o = _hour[0], c = _hour[1]; // When open stop looking for soonest (because we don't care) if ((key - o ^ key - c) < 0) { return this.makeHourResult(query, hour); } // No match yet, keep track of soonest opening hours. if (o > key && (soonest === null || soonest > o)) soonest = o; } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } if (soonest === null) return this.makeHourResult(query); return this.makeHourResult(query, undefined, soonest); } }, { key: "makeHourResult", value: function makeHourResult(query, hours, soonest) { // fromTimeValues(hours, minut) return { open: !!hours, soonest: soonest ? query.makeVisitingHour(soonest) : null, match: hours ? { open: query.makeVisitingHour(hours[0]), close: query.makeVisitingHour(hours[1]) } : null }; } }]); return HoursIndex; }(); exports.HoursIndex = HoursIndex;