1 line
1.9 KiB
JavaScript
1 line
1.9 KiB
JavaScript
"use strict";function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function t(t,e){for(var s=0;s<e.length;s++){var n=e[s];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,s,n){return s&&t(e.prototype,s),n&&t(e,n),e}}(),Timestring=function(){function t(){var e=void 0===arguments[0]?{}:arguments[0];_classCallCheck(this,t);var s={hoursPerDay:24,daysPerWeek:7,weeksPerMonth:4,monthsPerYear:12};this.settings=s;for(var n in e)this.settings[n]=e[n];this.units={s:["s","sec","secs","second","seconds"],m:["m","min","mins","minute","minutes"],h:["h","hr","hrs","hour","hours"],d:["d","day","days"],w:["w","week","weeks"],mth:["mth","mths","month","months"],y:["y","yr","yrs","year","years"]},this.unitValues={s:1,m:60,h:3600},this.unitValues.d=this.settings.hoursPerDay*this.unitValues.h,this.unitValues.w=this.settings.daysPerWeek*this.unitValues.d,this.unitValues.mth=this.settings.weeksPerMonth*this.unitValues.w,this.unitValues.y=this.settings.monthsPerYear*this.unitValues.mth}return _createClass(t,[{key:"parse",value:function(t){var e=this,s=void 0===arguments[1]?"s":arguments[1],n=function(t){for(var s in e.units)for(var n in e.units[s])if(t===e.units[s][n])return s;throw new Error("The unit ["+t+"] is not supported by timestring")},r=function(t,s){var r=e.unitValues[n(s)];return t/r},i=function(t,s){var r=e.unitValues[n(s)];return t*r},a=0,u=t.toLowerCase().replace(/[^\.\w+-]+/g,"").match(/[-+]?[0-9]+[a-z]+/g);if(null!==u)for(var o in u){var h=u[o],l=h.match(/[0-9]+/g)[0],c=h.match(/[a-z]+/g)[0];a+=i(l,c)}return r(a,s)}}]),t}();exports["default"]=Timestring,String.prototype.parseTime=function(t,e){return new Timestring(e).parse(this,t)},module.exports=exports["default"]; |