timestring/dist/timestring.js
Michael Barrett ba2014d106 fix paths
2015-05-05 09:05:13 +01:00

127 lines
13 KiB
JavaScript

(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';
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'];
},{}]},{},[1])(1)
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZX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