time-slotter
Version:
Generate time slots between a start and end time, with or without intervals between.
11 lines (8 loc) • 6.26 kB
JavaScript
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.timeSlotter = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
(function (global){
!function(r){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).timeDrift=r()}}(function(){return function(){return function r(e,t,n){function o(a,u){if(!t[a]){if(!e[a]){var s="function"==typeof require&&require;if(!u&&s)return s(a,!0);if(i)return i(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var c=t[a]={exports:{}};e[a][0].call(c.exports,function(r){return o(e[a][1][r]||r)},c,c.exports,r,e,t,n)}return t[a].exports}for(var i="function"==typeof require&&require,a=0;a<n.length;a++)o(n[a]);return o}}()({1:[function(r,e,t){"use strict";function n(r,e){return function(r){if(Array.isArray(r))return r}(r)||function(r,e){var t=[],n=!0,o=!1,i=void 0;try{for(var a,u=r[Symbol.iterator]();!(n=(a=u.next()).done)&&(t.push(a.value),!e||t.length!==e);n=!0);}catch(r){o=!0,i=r}finally{try{n||null==u.return||u.return()}finally{if(o)throw i}}return t}(r,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}e.exports=function(r,e){if(!/^\d\d.\d\d(.\d\d)?$/.test(r))throw new Error("Time format is incorrect. It should be either 'HH:MM:SS' or 'HH:MM', where the colon can be replaced by a non-numerical character");var t=n(r.split(/[.:\- ]/).map(function(r){return Number(r)}),3),o=t[0],i=t[1],a=t[2];if(o>23||o<0)throw new Error("Hours must be between 0 and 23");if(i>59||i<0)throw new Error("Minutes must be between 0 and 59");if(null!=a&&(a>59||a<0))throw new Error("Seconds must be between 0 and 59");if(e&&("string"!=typeof e||e.length>1))throw new Error("Separator must be a single, non-numerical character");function u(r,e){if("number"!=typeof r)throw new Error("First argument of ".concat(e," method must be a number"))}function s(r,e){if("string"!=typeof r)throw new Error("Second argument of ".concat(e," method must be a string representing the unit of time"));var t=r.charAt(0).toLowerCase();if(!["h","m","s"].includes(t))throw new Error("Second argument of ".concat(e," method must be hours, minutes or seconds"));if("s"===t&&null==a)throw new Error("You can't adjust seconds if they weren't included in the original time given");return t}var f={normalize:function(r){return null==a&&r.pop(),r.map(function(r){return(r=String(r)).length<2?"0"+r:r}).join(e||":")},hasCrossedMidnight:!1,add:function(r,e){switch(u(r,"add"),this.hasCrossedMidnight=!1,s(e,"add")){case"h":this.hasCrossedMidnight=Boolean(Math.floor((o+r)/24)),o=(o+r)%24;break;case"m":var t=Math.floor((i+r)/60);i=(i+r)%60,this.add(t,"h");break;case"s":var n=Math.floor((a+r)/60);a=(a+r)%60,this.add(n,"m")}return this},subtract:function(r,e){u(r,"subtract"),this.hasCrossedMidnight=!1;var t=0;switch(s(e,"subtract")){case"h":for(var n=o-r;n<0;)t++,n=24+n;o=n,t&&(this.hasCrossedMidnight=!0);break;case"m":for(var f=i-r;f<0;)t++,f=60+f;i=f,t&&this.subtract(t,"h");break;case"s":for(var c=a-r;c<0;)t++,c=60+c;a=c,t&&this.subtract(t,"m")}return this}};return Object.defineProperty(f,"val",{get:function(){return this.normalize([o,i,a])}}),f.add=f.add.bind(f),f.subtract=f.subtract.bind(f),f}},{}]},{},[1])(1)});
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],2:[function(require,module,exports){
;var timeDrift=require("time-drift");module.exports=function(){var n,i,t,e,r,o,s,a,u,d,c,l,f=["units","spacer","spacerUnits","includeOverflow","pushToEndTime","joinOn","delimiter"],h={DURATION_TOO_LARGE:"The duration is too large to create even one time-slot within the times given",UNRECOGNISED_OPTION:function(n){return"Unrecognised '".concat(n,"' option was provided")}},O={NO_DURATION:"You must include a positive duration value as the third argument",MISSING_ARGS:"You must enter a start and an end value as the first and second arguments"},m={};function v(){o&&!l&&(c=!0),o>1&&(c=!0),m.hasCrossedMidnight&&o++}function T(){!function(n,r){v(),n!==t||i===t&&!o||(c=!0);d||(n<t&&r>t&&(c=!0),n<t&&r<t&&m.hasCrossedMidnight&&(c=!0),n>t&&r>t&&n>r&&(c=!0));c||e.push([n,r])}(m.val,m.add(n,r).val),s&&(m.add(s,a),v())}function N(){!function(n,r){v(),n!==i||i===t&&!o||(c=!0);d||(n>i&&r<i&&(c=!0),n>i&&r>i&&m.hasCrossedMidnight&&(c=!0),n>i&&r>i&&n<r&&(c=!0));c||e.unshift([r,n])}(m.val,m.subtract(n,r).val),s&&(m.subtract(s,a),v())}function p(){if(u)for(;m.val>=i&&!c;)N();else for(;m.val<=t&&!c;)T();return e}return function(v,E,I){var D=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};if(!v||!E)throw new Error(O.MISSING_ARGS);if(!I)throw new Error(O.NO_DURATION);Object.keys(D).forEach(function(n){f.includes(n)||console.warn(h.UNRECOGNISED_OPTION(n))});var R,_=D.pushToEndTime?E:v;return e=[],c=!1,o=0,m=timeDrift(_,D.delimiter),n=I,r=D.units||"m",u=D.pushToEndTime||!1,s=D.spacer,a=D.spacerUnits||"m",d=D.includeOverflow,l=!1,i=timeDrift(v,D.delimiter).val,t=timeDrift(E,D.delimiter).val,R=timeDrift(i).add(n,r),i<t?(R.val>t||R.hasCrossedMidnight)&&(c=!0,console.warn(h.DURATION_TOO_LARGE)):R.val>t&&R.hasCrossedMidnight&&(c=!0,console.warn(h.DURATION_TOO_LARGE)),e=v<E?p():function(){for(l=!0;!o;)u?N():T();return p()}(),D.joinOn?e.map(function(n){return n.join(D.joinOn)}):e}}();
},{"time-drift":1}]},{},[2])(2)
});