timestring/timestring.js

113 lines
3.7 KiB
JavaScript
Raw Normal View History

(function(){
2012-12-15 22:07:43 +01:00
2012-12-15 22:12:53 +01:00
"use strict";
2012-12-23 17:31:54 +01:00
var Timestring = function(settings) {
2012-12-15 22:12:53 +01:00
// default settings
var defaults = {
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
monthsPerYear: 12
};
// merge default settings with user settings
var settings = settings || {};
this.settings = {};
for (var property in defaults) { this.settings[property] = defaults[property]; }
for (var property in settings) { this.settings[property] = settings[property]; }
// 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.w;
};
Timestring.prototype.parse = function(string, returnUnit) {
// reference to this
var self = this;
2012-12-31 00:39:06 +01:00
// 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;
}
}
}
// throw exception if invalid unit is passed
throw 'The unit [' + unit + '] is not supported by timestring';
2012-12-31 00:39:06 +01:00
}
2012-12-15 22:12:53 +01:00
// convert a value to a specific unit
function convert(value, unit) {
2012-12-31 00:39:06 +01:00
var baseValue = self.unitValues[getUnitKey(unit)];
2012-12-15 22:12:53 +01:00
return value / baseValue;
}
// get a value in seconds based on a specific unit
function getSeconds(value, unit) {
2012-12-31 00:39:06 +01:00
var baseValue = self.unitValues[getUnitKey(unit)];
2012-12-15 22:12:53 +01:00
return value * baseValue;
}
// seconds counter
var totalSeconds = 0;
// split string into groups and get total seconds for each group
var groups = string
2012-12-31 00:42:39 +01:00
.toLowerCase() // convert words to lower case
.replace(/[^\.\w+-]+/g, '') // remove white space
2012-12-31 00:27:17 +01:00
.match(/[-+]?[0-9]+[a-z]+/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups)
2012-12-15 22:12:53 +01:00
if (groups !== null) {
for(var group in groups) {
var g = groups[group],
2012-12-31 00:39:06 +01:00
value = g.match(/[0-9]+/g)[0],
unit = g.match(/[a-z]+/g)[0];
2012-12-15 22:12:53 +01:00
totalSeconds += getSeconds(value, unit);
}
}
// return total, convert if needed
return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds;
}
// add convenience method to string proto
String.prototype.parseTime = function (unit, settings) {
return (new Timestring(settings)).parse(this, unit);
}
2012-12-15 22:07:43 +01:00
2012-12-24 13:53:31 +01:00
// export Timestring object for either the browser or node.js
2012-12-24 17:10:27 +01:00
if (typeof module !== 'undefined' && module.exports) {
module.exports = Timestring;
2012-12-24 13:53:31 +01:00
}
else {
this.Timestring = Timestring;
}
}).call(this);