timestring/dist/lib/timestring.js

122 lines
12 KiB
JavaScript
Raw Normal View History

2015-05-05 12:05:24 +02:00
"use strict";
2015-05-01 18:31:25 +02:00
2015-05-05 12:05:24 +02:00
Object.defineProperty(exports, "__esModule", {
2015-05-01 18:31:25 +02:00
value: true
});
2015-05-05 12:05:24 +02:00
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; }; })();
2015-05-01 18:31:25 +02:00
2015-05-05 12:05:24 +02:00
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2015-05-05 10:05:13 +02:00
2015-05-01 18:31:25 +02:00
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 = {
2015-05-05 12:05:24 +02:00
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"]
2015-05-01 18:31:25 +02:00
};
// 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, [{
2015-05-05 12:05:24 +02:00
key: "parse",
2015-05-01 18:31:25 +02:00
value: function parse(string) {
var _this = this;
2015-05-05 12:05:24 +02:00
var returnUnit = arguments[1] === undefined ? "s" : arguments[1];
2015-05-01 18:31:25 +02:00
// 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
2015-05-05 12:05:24 +02:00
throw new Error("The unit [" + unit + "] is not supported by timestring");
2015-05-01 18:31:25 +02:00
};
// 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
2015-05-05 12:05:24 +02:00
.replace(/[^\.\w+-]+/g, "") // remove white space
2015-05-01 18:31:25 +02:00
.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;
})();
2015-05-05 12:05:24 +02:00
exports["default"] = Timestring;
2015-05-01 18:31:25 +02:00
String.prototype.parseTime = function (unit, settings) {
return new Timestring(settings).parse(this, unit);
};
2015-05-05 12:05:24 +02:00
module.exports = exports["default"];
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIlRpbWVzdHJpbmcuanMiLCJTdHJpbmcucGFyc2VUaW1lLmpz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