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){
2015-05-05 12:05:24 +02:00
"use strict";
2015-04-30 18:03:33 +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 = {
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 = {
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-04-30 18:03:33 +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;
2015-05-01 18:31:25 +02:00
}
_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;
}
2015-04-30 18:03:33 +02:00
}
}
2015-05-01 18:31:25 +02:00
// 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
};
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
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)
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-05 12:05:24 +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);
};
2015-05-05 12:05:24 +02:00
module.exports = exports["default"];
2015-04-30 18:03:33 +02:00
2015-05-01 18:31:25 +02:00
},{}]},{},[1])(1)
});
2015-05-05 12:05:24 +02:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyaWZ5L25vZGVfbW9k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