122 lines
12 KiB
JavaScript
122 lines
12 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
|
|
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
var Timestring = (function () {
|
|
function Timestring() {
|
|
var settings = arguments[0] === undefined ? {} : arguments[0];
|
|
|
|
_classCallCheck(this, Timestring);
|
|
|
|
var DEFAULT_SETTINGS = {
|
|
hoursPerDay: 24,
|
|
daysPerWeek: 7,
|
|
weeksPerMonth: 4,
|
|
monthsPerYear: 12
|
|
};
|
|
|
|
// merge default settings with user settings
|
|
this.settings = DEFAULT_SETTINGS;
|
|
for (var s in settings) {
|
|
this.settings[s] = settings[s];
|
|
}
|
|
|
|
// time units
|
|
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"]
|
|
};
|
|
|
|
// time unit seconds mappings
|
|
this.unitValues = {
|
|
s: 1,
|
|
m: 60,
|
|
h: 3600
|
|
};
|
|
|
|
// dynamic time unit seconds mappings
|
|
// these are dynamic based on the settings
|
|
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;
|
|
}
|
|
|
|
_createClass(Timestring, [{
|
|
key: "parse",
|
|
value: function parse(string) {
|
|
var _this = this;
|
|
|
|
var returnUnit = arguments[1] === undefined ? "s" : arguments[1];
|
|
|
|
// get unit key helper
|
|
var getUnitKey = function getUnitKey(unit) {
|
|
for (var k in _this.units) {
|
|
for (var u in _this.units[k]) {
|
|
if (unit === _this.units[k][u]) {
|
|
return k;
|
|
}
|
|
}
|
|
}
|
|
|
|
// throw error if invalid unit was passed
|
|
throw new Error("The unit [" + unit + "] is not supported by timestring");
|
|
};
|
|
|
|
// convert a value to a specific unit
|
|
var convert = function convert(value, unit) {
|
|
var baseValue = _this.unitValues[getUnitKey(unit)];
|
|
|
|
return value / baseValue;
|
|
};
|
|
|
|
// get a value in seconds based on a specific unit
|
|
var getSeconds = function getSeconds(value, unit) {
|
|
var baseValue = _this.unitValues[getUnitKey(unit)];
|
|
|
|
return value * baseValue;
|
|
};
|
|
|
|
// seconds counter
|
|
var totalSeconds = 0;
|
|
|
|
// split string into groups and get total seconds for each group
|
|
var groups = string.toLowerCase() // convert words to lower case
|
|
.replace(/[^\.\w+-]+/g, "") // remove white space
|
|
.match(/[-+]?[0-9]+[a-z]+/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups)
|
|
|
|
if (groups !== null) {
|
|
for (var group in groups) {
|
|
var g = groups[group];
|
|
var value = g.match(/[0-9]+/g)[0];
|
|
var unit = g.match(/[a-z]+/g)[0];
|
|
|
|
totalSeconds += getSeconds(value, unit);
|
|
}
|
|
}
|
|
|
|
return convert(totalSeconds, returnUnit);
|
|
}
|
|
}]);
|
|
|
|
return Timestring;
|
|
})();
|
|
|
|
exports["default"] = Timestring;
|
|
|
|
String.prototype.parseTime = function (unit, settings) {
|
|
return new Timestring(settings).parse(this, unit);
|
|
};
|
|
module.exports = exports["default"];
|
|
//# sourceMappingURL=data:application/json;base64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