UNPKG

@undercroft/timespan

Version:

When dealing with time durations or intervals in JavaScript, it's often useful to have a reliable way to represent and manipulate them. The `Timespan` class provides a convenient and powerful solution for working with timespans in JavaScript.

3 lines (2 loc) 13 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Timespan=t()}(this,(function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var t,n;var r=function(){if(n)return t;function e(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function r(t){if(Array.isArray(t))return e(t)}function i(e,t,n){return t=a(t),c(e,h()?Reflect.construct(t,n||[],a(e).constructor):t.apply(e,n))}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function s(e,t,n){return t&&u(e.prototype,t),n&&u(e,n),e}function a(e){return a=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},a(e)}function l(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&f(e,t)}function c(e,t){return!t||"object"!==y(t)&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function f(e,t){return f=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},f(e,t)}function d(e){return r(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||m(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function y(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e}function m(t,n){if(t){if("string"==typeof t)return e(t,n);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(r):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?e(t,n):void 0}}function h(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(e){}return(h=function(){return!!e})()}n=1;var v=Object.defineProperty,b=Object.getOwnPropertyDescriptor,g=Object.getOwnPropertyNames,p=Object.prototype.hasOwnProperty,U={};!function(e,t){for(var n in t)v(e,n,{get:t[n],enumerable:!0})}(U,{Timespan:function(){return L},default:function(){return N}}),t=function(e){return function(e,t,n,r){var i=!0,o=!1,u=void 0;if(t&&"object"==(void 0===t?"undefined":y(t))||"function"==typeof t)try{for(var s,a=function(){var i=s.value;!p.call(e,i)&&i!==n&&v(e,i,{get:function(){return t[i]},enumerable:!(r=b(t,i))||r.enumerable})},l=g(t)[Symbol.iterator]();!(i=(s=l.next()).done);i=!0)a()}catch(e){o=!0,u=e}finally{try{i||null==l.return||l.return()}finally{if(o)throw u}}return e}(v({},"__esModule",{value:!0}),e)}(U);var w={AllowedCharactersRegexPattern:/^[a-zA-Z0-9\s]+$/,CenturyDivisor:100,DaysPerWeek:7,DaysPerYear:365,HoursPerDay:24,InputRegexPattern:/(\d{0,10})([^\s\d]+)/g,InputRegexUnitIndex:2,InputRegexValueIndex:1,LeapYearDivisor:4,MaxInputStringLength:75,MillisecondsPerSecond:1e3,MinutesPerHour:60,MonthsPerYear:12,QuadricentennialDivisor:400,SecondsPerMinute:60},T=function(){function e(){o(this,e)}return s(e,[{key:"isLeapYear",value:function(t){return e.isDivisibleByLeapYearDivisor(t)&&!e.isDivisibleByCenturyDivisor(t)||e.isDivisibleByQuadricentennialDivisor(t)}}],[{key:"isDivisibleByLeapYearDivisor",value:function(e){return e%w.LeapYearDivisor==0}},{key:"isDivisibleByCenturyDivisor",value:function(e){return e%w.CenturyDivisor==0}},{key:"isDivisibleByQuadricentennialDivisor",value:function(e){return e%w.QuadricentennialDivisor==0}}]),e}(),P=function(e){function t(){var e;return o(this,t),(e=i(this,t,arguments)).default="day",e.plural="days",e.abbreviation="d",e.millisecondsPerUnit=w.HoursPerDay*w.MinutesPerHour*w.SecondsPerMinute*w.MillisecondsPerSecond,e.aliases=["day","days","dys","dy","d"],e}return l(t,e),s(t,[{key:"between",value:function(e,t){var n=e.getTime(),r=t.getTime(),i=Math.floor((r-n)/this.millisecondsPerUnit);return Math.floor(i)}},{key:"add",value:function(e,t){return new Date(t.getTime()+e*this.millisecondsPerUnit)}}]),t}(T),M=function(e){function t(){var e;return o(this,t),(e=i(this,t,arguments)).default="hour",e.plural="hours",e.abbreviation="h",e.millisecondsPerUnit=w.MinutesPerHour*w.SecondsPerMinute*w.MillisecondsPerSecond,e.aliases=["hours","hour","hrs","hr","h"],e}return l(t,e),s(t,[{key:"between",value:function(e,t){return Math.floor((t.getTime()-e.getTime())/this.millisecondsPerUnit)}},{key:"add",value:function(e,t){return new Date(t.getTime()+e*this.millisecondsPerUnit)}}]),t}(T),D=function(e){function t(){var e;return o(this,t),(e=i(this,t,arguments)).default="millisecond",e.plural="milliseconds",e.abbreviation="ms",e.millisecondsPerUnit=1,e.aliases=["milliseconds","millisecond","msec","mss","ms"],e}return l(t,e),s(t,[{key:"between",value:function(e,t){return t.getTime()-e.getTime()}},{key:"add",value:function(e,t){return new Date(t.getTime()+e)}}]),t}(T),C=function(e){function t(){var e;return o(this,t),(e=i(this,t,arguments)).default="minute",e.plural="minutes",e.abbreviation="m",e.millisecondsPerUnit=w.SecondsPerMinute*w.MillisecondsPerSecond,e.aliases=["minutes","minute","mins","min","m"],e}return l(t,e),s(t,[{key:"between",value:function(e,t){return Math.floor((t.getTime()-e.getTime())/this.millisecondsPerUnit)}},{key:"add",value:function(e,t){return new Date(t.getTime()+e*this.millisecondsPerUnit)}}]),t}(T),k=function(e){function t(){var e;return o(this,t),(e=i(this,t,arguments)).default="month",e.plural="months",e.abbreviation="M",e.millisecondsPerUnit=-1,e.aliases=["months","month","mos","mo","M"],e}return l(t,e),s(t,[{key:"between",value:function(e,t){var n=e.getUTCFullYear(),r=e.getUTCMonth(),i=e.getUTCDate(),o=t.getUTCFullYear(),u=t.getUTCMonth(),s=t.getUTCDate(),a=(o-n)*w.MonthsPerYear+(u-r);return s<i&&a--,Math.floor(a)}},{key:"add",value:function(e,t){var n=new Date(t),r=n.getUTCHours(),i=n.getUTCMinutes(),o=n.getUTCSeconds(),u=n.getUTCMilliseconds();return n.setUTCHours(0,0,0,0),n.setUTCMonth(n.getUTCMonth()+e),n.setUTCHours(r,i,o,u),n}}]),t}(T),S=function(e){function t(){var e;return o(this,t),(e=i(this,t,arguments)).default="second",e.plural="seconds",e.abbreviation="s",e.millisecondsPerUnit=1e3,e.aliases=["seconds","second","secs","sec","s"],e}return l(t,e),s(t,[{key:"between",value:function(e,t){return Math.floor((t.getTime()-e.getTime())/this.millisecondsPerUnit)}},{key:"add",value:function(e,t){return new Date(t.getTime()+e*this.millisecondsPerUnit)}}]),t}(T),x=function(e){function t(){var e;return o(this,t),(e=i(this,t,arguments)).default="week",e.plural="weeks",e.abbreviation="w",e.millisecondsPerUnit=w.DaysPerWeek*w.HoursPerDay*w.MinutesPerHour*w.SecondsPerMinute*w.MillisecondsPerSecond,e.aliases=["w","wk","wks","week","weeks"],e}return l(t,e),s(t,[{key:"between",value:function(e,t){return Math.floor((t.getTime()-e.getTime())/this.millisecondsPerUnit)}},{key:"add",value:function(e,t){var n=e*this.millisecondsPerUnit,r=t.getTime()+n;return new Date(r)}}]),t}(T),I=function(e){function t(){var e;return o(this,t),(e=i(this,t,arguments)).default="year",e.plural="years",e.abbreviation="y",e.millisecondsPerUnit=w.DaysPerYear*w.HoursPerDay*w.MinutesPerHour*w.SecondsPerMinute*w.MillisecondsPerSecond,e.aliases=["years","year","yrs","yr","y"],e}return l(t,e),s(t,[{key:"between",value:function(e,t){var n=e.getUTCFullYear();return t.getUTCFullYear()-n}},{key:"add",value:function(e,n){var r=n.getUTCFullYear()+e,i=n.getUTCMonth()===t.february&&n.getUTCDate()===t.february29,o=new Date(n.getTime());return o.setUTCFullYear(r),i&&!this.isLeapYear(r)&&(o.setUTCMonth(t.february),o.setUTCDate(t.february28)),o}}]),t}(T);function O(e){return function(e,t){return null!=t&&"undefined"!=typeof Symbol&&t[Symbol.hasInstance]?!!t[Symbol.hasInstance](e):e instanceof t}(e,Date)&&!isNaN(e.getTime())}function Y(e){if(!O(e))throw new Error("Invalid date input.")}function j(e){return _.includes(e)}function H(e){if(!j(e))throw new Error("Invalid date unit: ".concat(e,"."))}function F(e,t){if(!function(e,t){return e.getTime()<=t.getTime()}(e,t))throw new Error("Start date cannot be greater than end date.")}I.february28=28,I.february29=29,I.february=1;var E=[new I,new k,new x,new P,new M,new C,new S,new D],R=function(){var e={},t=!0,n=!1,r=void 0,i=!0,o=!1,u=void 0;try{for(var s,a=E[Symbol.iterator]();!(i=(s=a.next()).done);i=!0){var l=s.value;try{for(var c,f=l.aliases[Symbol.iterator]();!(t=(c=f.next()).done);t=!0){e[c.value]=l}}catch(e){n=!0,r=e}finally{try{t||null==f.return||f.return()}finally{if(n)throw r}}}}catch(e){o=!0,u=e}finally{try{i||null==a.return||a.return()}finally{if(o)throw u}}return e}(),_=function(){var e=new Set,t=!0,n=!1,r=void 0;try{for(var i,o=E[Symbol.iterator]();!(t=(i=o.next()).done);t=!0){i.value.aliases.forEach((function(t){return e.add(t)}))}}catch(e){n=!0,r=e}finally{try{t||null==o.return||o.return()}finally{if(n)throw r}}return d(e)}();function A(e){return H(e),R[e]}var B=function(){function e(t,n){var r=this;o(this,e),this.toTimeframe=function(){return r.timeFrame},this.toString=function(){return r.stringValue},this.toUnit=function(e){return H(e),A(e).between(r.startDate,r.endDate)},this.toMilliseconds=function(){return r.toUnit("milliseconds")},this.toSeconds=function(){return r.toUnit("seconds")},this.toMinutes=function(){return r.toUnit("minutes")},this.toHours=function(){return r.toUnit("hours")},this.toDays=function(){return r.toUnit("days")},this.toWeeks=function(){return r.toUnit("weeks")},this.toMonths=function(){return r.toUnit("months")},this.toYears=function(){return r.toUnit("years")},Y(t),Y(n),F(t,n),this.startDate=t,this.endDate=n,this.timeFrame=function(e,t){var n={years:0,months:0,weeks:0,days:0,hours:0,minutes:0,seconds:0,milliseconds:0},r=new Date(e);n.years=A("years").between(e,t);var i=A("months").between(e,t);n.months=Math.floor(i-n.years*w.MonthsPerYear),r.setUTCFullYear(r.getUTCFullYear()+n.years),r.setUTCMonth(r.getUTCMonth()+n.months);var o=t.getTime()-e.getTime()-(r.getTime()-e.getTime()),u=A("week").millisecondsPerUnit,s=A("days").millisecondsPerUnit,a=A("hours").millisecondsPerUnit,l=A("minutes").millisecondsPerUnit,c=A("seconds").millisecondsPerUnit;return n.weeks=Math.floor(o/u),o%=u,n.days=Math.floor(o/s),o%=s,n.hours=Math.floor(o/a),o%=a,n.minutes=Math.floor(o/l),o%=l,n.seconds=Math.floor(o/c),o%=c,n.milliseconds=o,n}(t,n),this.stringValue=function(e){var t=[],n=!0,r=!1,i=void 0;try{for(var o,u=Object.keys(e)[Symbol.iterator]();!(n=(o=u.next()).done);n=!0){var s=o.value,a=Number(e[s]);if(0!==a&&j(s)){var l=A(s).abbreviation;t.push("".concat(a).concat(l))}}}catch(e){r=!0,i=e}finally{try{n||null==u.return||u.return()}finally{if(r)throw i}}return t.join(" ")}(this.timeFrame)}return s(e,[{key:"start",get:function(){return this.startDate}},{key:"end",get:function(){return this.endDate}}]),e}();B.fromString=function(e,t){var n=t||new Date;Y(n),function(e){if(null===e||"string"!=typeof e||e.length>w.MaxInputStringLength)throw new Error("Invalid input string")}(e),function(e){if(null===e||"string"!=typeof e||!w.AllowedCharactersRegexPattern.test(e))throw new Error("Invalid input string")}(e);var r={years:0,months:0,weeks:0,days:0,hours:0,minutes:0,seconds:0,milliseconds:0},i=w.InputRegexPattern.exec(e);if(!i)throw new Error("Invalid unit");for(;i;){var o=Number(i[w.InputRegexValueIndex]),u=i[w.InputRegexUnitIndex];H(u),r[A(u).plural]=o,i=w.InputRegexPattern.exec(e)}var s=new Date(n);return s.setUTCFullYear(s.getUTCFullYear()+r.years),s.setUTCMonth(s.getUTCMonth()+r.months),s.setUTCDate(s.getUTCDate()+r.weeks*w.DaysPerWeek),s.setUTCDate(s.getUTCDate()+r.days),s.setUTCHours(s.getUTCHours()+r.hours),s.setUTCMinutes(s.getUTCMinutes()+r.minutes),s.setUTCSeconds(s.getUTCSeconds()+r.seconds),s.setUTCMilliseconds(s.getUTCMilliseconds()+r.milliseconds),new B(n,s)},B.fromUnits=function(e,t,n){var r=n||new Date;if(Y(r),H(t),e<0)throw new Error("Invalid unit input ".concat(e,"."));var i=A(t).add(e,r);return F(r,i),new B(r,i)},B.fromMilliseconds=function(e,t){return B.fromUnits(e,"milliseconds",t)},B.fromSeconds=function(e,t){return B.fromUnits(e,"seconds",t)},B.fromMinutes=function(e,t){return B.fromUnits(e,"minutes",t)},B.fromHours=function(e,t){return B.fromUnits(e,"hours",t)},B.fromDays=function(e,t){return B.fromUnits(e,"days",t)},B.fromWeeks=function(e,t){return B.fromUnits(e,"weeks",t)},B.fromMonths=function(e,t){return B.fromUnits(e,"months",t)},B.fromYears=function(e,t){return B.fromUnits(e,"years",t)};var L=B,N=L;return t}();return e(r)})); //# sourceMappingURL=index.umd.js.map