UNPKG

shiftly

Version:

Calculate common public safety shift schedules.

105 lines (87 loc) 4 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShiftConfiguration = undefined; var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _momentTimezone = require('moment-timezone'); var _momentTimezone2 = _interopRequireDefault(_momentTimezone); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function reverse(s) { var o = []; for (var i = 0, len = s.length; i <= len; i += 1) { o.push(s.charAt(len - i)); } return o.join(''); } var ShiftConfiguration = exports.ShiftConfiguration = function () { // eslint-disable-line import/prefer-default-export /** * Create a ShiftConfiguration. * @param {string} pattern - The department's shift pattern. * @param {string} timeZone - The department's timezone. * @param {string} shiftStart - The start time of the department's shift. * @param {string} firstDay - The first day of the pattern (assumed to be in local tz). */ function ShiftConfiguration() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, pattern = _ref.pattern, _ref$timeZone = _ref.timeZone, timeZone = _ref$timeZone === undefined ? 'US/Eastern' : _ref$timeZone, _ref$shiftStart = _ref.shiftStart, shiftStart = _ref$shiftStart === undefined ? '0800' : _ref$shiftStart, _ref$firstDay = _ref.firstDay, firstDay = _ref$firstDay === undefined ? '2016-10-30' : _ref$firstDay; _classCallCheck(this, ShiftConfiguration); this.pattern = pattern; this.timeZone = timeZone; this.shiftStart = shiftStart; this.firstDay = firstDay; this.shiftStartDate = _momentTimezone2.default.tz(this.shiftStart, 'hmm', this.timeZone); this.patternStart = _momentTimezone2.default.tz(this.firstDay, this.timeZone).startOf('day'); } _createClass(ShiftConfiguration, [{ key: 'normalize', value: function normalize(incomingDate) { return (0, _momentTimezone2.default)(incomingDate).tz(this.timeZone); } }, { key: 'reversePattern', value: function reversePattern() { return '' + this.pattern.charAt(0) + reverse(this.pattern.substring(1, this.pattern.length)); } }, { key: 'beforeShiftChange', value: function beforeShiftChange(date) { return date.hours() < this.shiftStartDate.hours(); } }, { key: 'daysFromPatternStart', value: function daysFromPatternStart(start) { return start.startOf('day').diff(this.patternStart.startOf('day'), 'days'); } }, { key: 'calculateShift', value: function calculateShift(date) { var momentDate = this.normalize(date); var checkDate = this.beforeShiftChange(momentDate) ? momentDate.subtract(1, 'days') : momentDate; var pattern = this.pattern; var daysFromStart = this.daysFromPatternStart(checkDate); if (daysFromStart < 0) { pattern = this.reversePattern(); } var patternLength = this.pattern.length; var mod = Math.abs(daysFromStart) % patternLength; var shift = null; for (var i = 0, len = patternLength; i < len; i += 1) { if (i === mod) { shift = pattern[i].toUpperCase(); break; } } return shift; } }]); return ShiftConfiguration; }();