timestring/dist/timestring.js

127 lines
13 KiB
JavaScript
Raw Normal View History

2015-05-01 18:31:25 +02:00
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Timestring = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
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 = {
2015-04-30 18:03:33 +02:00
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
monthsPerYear: 12
};
// merge default settings with user settings
2015-05-01 18:31:25 +02:00
this.settings = DEFAULT_SETTINGS;
for (var s in settings) {
this.settings[s] = settings[s];
}
2015-04-30 18:03:33 +02:00
// 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'],
2015-05-01 18:31:25 +02:00
mth: ['mth', 'mths', 'month', 'months'],
2015-04-30 18:03:33 +02:00
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;
2015-05-01 18:31:25 +02:00
}
_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;
}
2015-04-30 18:03:33 +02:00
}
}
2015-05-01 18:31:25 +02:00
// throw error if invalid unit was passed
throw new Error('The unit [' + unit + '] is not supported by timestring');
};
2015-04-30 18:03:33 +02:00
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)];
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
return value / baseValue;
};
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
// get a value in seconds based on a specific unit
var getSeconds = function getSeconds(value, unit) {
var baseValue = _this.unitValues[getUnitKey(unit)];
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
return value * baseValue;
};
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
// seconds counter
var totalSeconds = 0;
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
// 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)
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
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];
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
totalSeconds += getSeconds(value, unit);
}
2015-04-30 18:03:33 +02:00
}
2015-05-01 18:31:25 +02:00
return convert(totalSeconds, returnUnit);
2015-04-30 18:03:33 +02:00
}
2015-05-01 18:31:25 +02:00
}]);
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
return Timestring;
})();
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
exports['default'] = Timestring;
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
String.prototype.parseTime = function (unit, settings) {
return new Timestring(settings).parse(this, unit);
};
module.exports = exports['default'];
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
},{}]},{},[1])(1)
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyaWZ5L25vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJ0aW1lc3RyaW5nLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7Ozs7OztJQ0FxQixVQUFVO0FBQ2xCLFdBRFEsVUFBVSxHQUNGO1FBQWYsUUFBUSxnQ0FBRyxFQUFFOzswQkFETixVQUFVOztBQUUzQixRQUFNLGdCQUFnQixHQUFHO0FBQ3ZCLGlCQUFXLEVBQUUsRUFBRTtBQUNmLGlCQUFXLEVBQUUsQ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