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