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.

1 lines 13.1 kB
"use strict";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=a(t);return d(e,U()?Reflect.construct(t,n||[],a(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 u(e,t,n){if(t)o(e.prototype,t);if(n)o(e,n);return e}function a(e){a=Object.setPrototypeOf?Object.getPrototypeOf:function e(e){return e.__proto__||Object.getPrototypeOf(e)};return a(e)}function s(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)y(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&&(h(t)==="object"||typeof t==="function")){return t}return n(e)}function y(e,t){y=Object.setPrototypeOf||function e(e,t){e.__proto__=t;return e};return y(e,t)}function m(e){return t(e)||c(e)||v(e)||f()}function h(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})()}var g=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var P=function(e,t){for(var n in t)g(e,n,{get:t[n],enumerable:!0})},T=function(e,t,n,r){var i=true,o=false,u=undefined;if(t&&(typeof t==="undefined"?"undefined":h(t))=="object"||typeof t=="function")try{var a=function(){var i=l.value;!p.call(e,i)&&i!==n&&g(e,i,{get:function(){return t[i]},enumerable:!(r=b(t,i))||r.enumerable})};for(var s=w(t)[Symbol.iterator](),l;!(i=(l=s.next()).done);i=true)a()}catch(e){o=true;u=e}finally{try{if(!i&&s.return!=null){s.return()}}finally{if(o){throw u}}}return e};var M=function(e){return T(g({},"__esModule",{value:!0}),e)};var D={};P(D,{Timespan:function(){return ee},default:function(){return et}});module.exports=M(D);function C(){return{years:0,months:0,weeks:0,days:0,hours:0,minutes:0,seconds:0,milliseconds:0}}var _={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 k=/*#__PURE__*/function(){function e(){i(this,e)}u(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%_.LeapYearDivisor===0}},{key:"isDivisibleByCenturyDivisor",value:function e(e){return e%_.CenturyDivisor===0}},{key:"isDivisibleByQuadricentennialDivisor",value:function e(e){return e%_.QuadricentennialDivisor===0}}]);return e}();var S=/*#__PURE__*/function(e){s(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=_.HoursPerDay*_.MinutesPerHour*_.SecondsPerMinute*_.MillisecondsPerSecond;e.aliases=["day","days","dys","dy","d"];return e}u(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}(k);var x=/*#__PURE__*/function(e){s(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=_.MinutesPerHour*_.SecondsPerMinute*_.MillisecondsPerSecond;e.aliases=["hours","hour","hrs","hr","h"];return e}u(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}(k);var I=/*#__PURE__*/function(e){s(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}u(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}(k);var Y=/*#__PURE__*/function(e){s(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=_.SecondsPerMinute*_.MillisecondsPerSecond;e.aliases=["minutes","minute","mins","min","m"];return e}u(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}(k);var E=/*#__PURE__*/function(e){s(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}u(t,[{key:"between",value:function e(e,t){var n=e.getUTCFullYear(),r=e.getUTCMonth(),i=e.getUTCDate(),o=t.getUTCFullYear(),u=t.getUTCMonth(),a=t.getUTCDate(),s=(o-n)*_.MonthsPerYear+(u-r);return a<i&&s--,Math.floor(s)}},{key:"add",value:function e(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}}]);return t}(k);var R=/*#__PURE__*/function(e){s(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}u(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}(k);var O=/*#__PURE__*/function(e){s(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=_.DaysPerWeek*_.HoursPerDay*_.MinutesPerHour*_.SecondsPerMinute*_.MillisecondsPerSecond;e.aliases=["w","wk","wks","week","weeks"];return e}u(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}(k);var j=/*#__PURE__*/function(e){s(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=_.DaysPerYear*_.HoursPerDay*_.MinutesPerHour*_.SecondsPerMinute*_.MillisecondsPerSecond;e.aliases=["years","year","yrs","yr","y"];return e}u(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}(k);j.february28=28,j.february29=29,j.february=1;var H=j;function F(e){return l(e,Date)&&!isNaN(e.getTime())}function A(e){if(!F(e))throw new Error("Invalid date input.")}function B(e){return q().includes(e)}function L(e){if(!B(e))throw new Error("Invalid date unit: ".concat(e,"."))}function N(e,t){return e.getTime()<=t.getTime()}function W(e,t){if(!N(e,t))throw new Error("Start date cannot be greater than end date.")}function Q(e){if(e===null||typeof e!="string"||!_.AllowedCharactersRegexPattern.test(e))throw new Error("Invalid input string")}function V(e){if(e===null||typeof e!="string"||e.length>_.MaxInputStringLength)throw new Error("Invalid input string")}var $=[new H,new E,new O,new S,new x,new Y,new R,new I],z=function(){var e={};var t=true,n=false,r=undefined,i=true,o=false,u=undefined;try{for(var a=$[Symbol.iterator](),s;!(i=(s=a.next()).done);i=true){var l=s.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;u=e}finally{try{if(!i&&a.return!=null){a.return()}}finally{if(o){throw u}}}return e}(),Z=function(){var e=new Set;var t=true,n=false,r=undefined;try{for(var i=$[Symbol.iterator](),o;!(t=(o=i.next()).done);t=true){var u=o.value;u.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 m(e)}();function q(){return Z}function G(e){return L(e),z[e]}function J(e,t){var n=C(),r=new Date(e);n.years=G("years").between(e,t);var i=G("months").between(e,t);n.months=Math.floor(i-n.years*_.MonthsPerYear),r.setUTCFullYear(r.getUTCFullYear()+n.years),r.setUTCMonth(r.getUTCMonth()+n.months);var o=t.getTime()-e.getTime()-(r.getTime()-e.getTime()),u=G("week").millisecondsPerUnit,a=G("days").millisecondsPerUnit,s=G("hours").millisecondsPerUnit,l=G("minutes").millisecondsPerUnit,c=G("seconds").millisecondsPerUnit;return n.weeks=Math.floor(o/u),o%=u,n.days=Math.floor(o/a),o%=a,n.hours=Math.floor(o/s),o%=s,n.minutes=Math.floor(o/l),o%=l,n.seconds=Math.floor(o/c),o%=c,n.milliseconds=o,n}function K(e){var t=[];var n=true,r=false,i=undefined;try{for(var o=Object.keys(e)[Symbol.iterator](),u;!(n=(u=o.next()).done);n=true){var a=u.value;var s=Number(e[a]);if(s!==0&&B(a)){var l=G(a).abbreviation;t.push("".concat(s).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 X=/*#__PURE__*/function(){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 L(e),G(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")};A(t),A(n),W(t,n),this.startDate=t,this.endDate=n,this.timeFrame=J(t,n),this.stringValue=K(this.timeFrame)}u(e,[{key:"start",get:function e(){return this.startDate}},{key:"end",get:function e(){return this.endDate}}]);return e}();X.fromString=function(e,t){var n=t||new Date;A(n),V(e),Q(e);var r=C(),i=_.InputRegexPattern.exec(e);if(!i)throw new Error("Invalid unit");for(;i;){var o=Number(i[_.InputRegexValueIndex]),u=i[_.InputRegexUnitIndex];L(u);var a=G(u).plural;r[a]=o,i=_.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*_.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 X(n,s)},X.fromUnits=function(e,t,n){var r=n||new Date;if(A(r),L(t),e<0)throw new Error("Invalid unit input ".concat(e,"."));var i=G(t).add(e,r);return W(r,i),new X(r,i)},X.fromMilliseconds=function(e,t){return X.fromUnits(e,"milliseconds",t)},X.fromSeconds=function(e,t){return X.fromUnits(e,"seconds",t)},X.fromMinutes=function(e,t){return X.fromUnits(e,"minutes",t)},X.fromHours=function(e,t){return X.fromUnits(e,"hours",t)},X.fromDays=function(e,t){return X.fromUnits(e,"days",t)},X.fromWeeks=function(e,t){return X.fromUnits(e,"weeks",t)},X.fromMonths=function(e,t){return X.fromUnits(e,"months",t)},X.fromYears=function(e,t){return X.fromUnits(e,"years",t)};var ee=X;var et=ee;0&&(module.exports={Timespan:Timespan});//# sourceMappingURL=index.js.map