timestring/src/timestring.js

120 lines
3.3 KiB
JavaScript
Raw Normal View History

(function(){
2014-07-17 22:37:26 +02:00
"use strict";
var Timestring = function(settings) {
// default settings
var defaults = {
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
monthsPerYear: 12
2012-12-15 22:12:53 +01:00
};
2014-07-17 22:37:26 +02:00
// merge default settings with user settings
var settings = settings || {};
this.settings = {};
2012-12-15 22:12:53 +01:00
2014-07-17 22:37:26 +02:00
for (var property in defaults) {
this.settings[property] = defaults[property];
}
2012-12-15 22:12:53 +01:00
2014-07-17 22:37:26 +02:00
for (var property in settings) {
this.settings[property] = settings[property];
}
2012-12-15 22:12:53 +01:00
2014-07-17 22:37:26 +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'],
mth: ['mth', 'mths','month', 'months'],
y: ['y', 'yr', 'yrs', 'year', 'years']
};
2012-12-15 22:12:53 +01:00
2014-07-17 22:37:26 +02:00
// time unit seconds mappings
this.unitValues = {
s: 1,
m: 60,
h: 3600
};
2014-05-27 21:45:04 +02:00
2014-07-17 22:37:26 +02:00
// 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.w;
};
Timestring.prototype.parse = function(string, returnUnit) {
// reference to this
var self = this;
// get unit key helper
function getUnitKey(unit) {
for (var key in self.units) {
for (var u in self.units[key]) {
if (unit === self.units[key][u]) {
return key;
}
2012-12-15 22:12:53 +01:00
}
2014-07-17 22:37:26 +02:00
}
2012-12-15 22:12:53 +01:00
2014-07-17 22:37:26 +02:00
// throw exception if invalid unit is passed
throw 'The unit [' + unit + '] is not supported by timestring';
2012-12-15 22:12:53 +01:00
}
2014-07-17 22:37:26 +02:00
// convert a value to a specific unit
function convert(value, unit) {
var baseValue = self.unitValues[getUnitKey(unit)];
return value / baseValue;
2012-12-15 22:12:53 +01:00
}
2012-12-15 22:07:43 +01:00
2014-07-17 22:37:26 +02:00
// get a value in seconds based on a specific unit
function getSeconds(value, unit) {
var baseValue = self.unitValues[getUnitKey(unit)];
return value * baseValue;
2012-12-24 13:53:31 +01:00
}
2014-07-17 22:37:26 +02:00
// 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 i = 0; i < groups.length; i++) {
var g = groups[i];
var value = g.match(/[0-9]+/g)[0];
var unit = g.match(/[a-z]+/g)[0];
totalSeconds += getSeconds(value, unit);
}
2012-12-24 13:53:31 +01:00
}
2014-07-17 22:37:26 +02:00
// return total, convert if needed
return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds;
}
// add convenience method to string prototype
String.prototype.parseTime = function (unit, settings) {
return (new Timestring(settings)).parse(this, unit);
}
// export Timestring object for either the browser or node
if (typeof module !== 'undefined' && module.exports) {
module.exports = Timestring;
}
else {
this.Timestring = Timestring;
}
}).call(this);