'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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIlRpbWVzdHJpbmcuanMiLCJTdHJpbmcucGFyc2VUaW1lLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7SUFBQSxVQUFBO0FBQ0EsV0FEQSxVQUFBLEdBQ0E7UUFBQSxRQUFBLGdDQUFBLEVBQUE7OzBCQURBLFVBQUE7O0FBRUEsUUFBQSxnQkFBQSxHQUFBO0FBQ0EsaUJBQUEsRUFBQSxFQUFBO0FBQ0EsaUJBQUEsRUFBQSxDQUFBO0FBQ0EsbUJBQUEsRUFBQSxDQUFBO0FBQ0EsbUJBQUEsRUFBQSxFQUFBO0tBQ0EsQ0FBQTs7O0FBR0EsUUFBQSxDQUFBLFFBQUEsR0FBQSxnQkFBQSxDQUFBO0FBQ0EsU0FBQSxJQUFBLENBQUEsSUFBQSxRQUFBLEVBQUE7QUFBQSxVQUFBLENBQUEsUUFBQSxDQUFBLENBQUEsQ0FBQSxHQUFBLFFBQUEsQ0FBQSxDQUFBLENBQUEsQ0FBQTtLQUFBO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