timestring/dist/lib/timestring.js

122 lines
11 KiB
JavaScript
Raw Normal View History

2015-05-01 18:31:25 +02:00
'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'];
2015-05-02 23:38:39 +02:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIlRpbWVzdHJpbmcuanMiLCJTdHJpbmcucGFyc2VUaW1lLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7SUFBQSxVQUFBO0FBQ0EsV0FEQSxVQUFBLEdBQ0E7UUFBQSxRQUFBLGdDQUFBLEVBQUE7OzBCQURBLFVBQUE7O0FBRUEsUUFBQSxnQkFBQSxHQUFBO0FBQ0EsaUJBQUEsRUFBQSxFQUFBO0FBQ0EsaUJBQUEsRUFBQSxDQUFBO0FBQ0EsbUJBQUEsRUFBQSxDQUFBO0FBQ0EsbUJBQUEsRUFBQSxFQUFBO0tBQ0EsQ0FBQTs7O0FBR0EsUUFBQSxDQUFBLFFBQUEsR0FBQSxnQk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