timestring/dist/timestring.min.js
2015-04-30 11:45:48 +01:00

2 lines
8.3 KiB
JavaScript

(function(){"use strict";var t=function(t){var s={hoursPerDay:24,daysPerWeek:7,weeksPerMonth:4,monthsPerYear:12};t=t||{},this.settings=s;for(var e in t)this.settings[e]=t[e];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};t.prototype.parse=function(t,s){function e(t){for(var s in r.units)for(var e in r.units[s])if(t===r.units[s][e])return s;throw new Error("The unit ["+t+"] is not supported by timestring")}function i(t,s){var i=r.unitValues[e(s)];return t/i}function n(t,s){var i=r.unitValues[e(s)];return t*i}var r=this,u=0,h=t.toLowerCase().replace(/[^\.\w+-]+/g,"").match(/[-+]?[0-9]+[a-z]+/g);if(null!==h)for(var a=0;a<h.length;a++){var o=h[a],m=o.match(/[0-9]+/g)[0],l=o.match(/[a-z]+/g)[0];u+=n(m,l)}return s?i(u,s):u},String.prototype.parseTime=function(s,e){return new t(e).parse(this,s)},"undefined"!=typeof module&&module.exports?module.exports=t:this.Timestring=t}).call(this);
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRpbWVzdHJpbmcuanMiXSwibmFtZXMiOlsiVGltZXN0cmluZyIsInNldHRpbmdzIiwiZGVmYXVsdHMiLCJob3Vyc1BlckRheSIsImRheXNQZXJXZWVrIiwid2Vla3NQZXJNb250aCIsIm1vbnRoc1BlclllYXIiLCJ0aGlzIiwicyIsInVuaXRzIiwibSIsImgiLCJkIiwidyIsIm10aCIsInkiLCJ1bml0VmF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