122 lines
11 KiB
JavaScript
122 lines
11 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', {
|
|
value: true
|
|
});
|
|
|
|
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
|
|
|
|
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; }; })();
|
|
|
|
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'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