diff --git a/package.json b/package.json index d34ebf9..c7675ed 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,12 @@ { - "name" : "timestring", - "description" : "Parse a human readable time string into a time based value", - "homepage" : "https://github.com/mike182uk/timestring", - "keywords" : [ - "util", - "functional", - "time" - ], - "author" : "Michael David Barrett ", - "repository" : { - "type": "git", - "url": "git://github.com/mike182uk/timestring.git" - }, - "main" : "timestring.js", - "version" : "1.0.2" + "name" : "timestring", + "description" : "Parse a human readable time string into a time based value", + "homepage" : "https://github.com/mike182uk/timestring", + "author" : "Michael David Barrett ", + "repository" : { + "type": "git", + "url": "git://github.com/mike182uk/timestring.git" + }, + "main" : "timestring.js", + "version" : "1.0.2" } diff --git a/timestring.js b/timestring.js index fe53b7b..d3698b4 100644 --- a/timestring.js +++ b/timestring.js @@ -1,111 +1,119 @@ (function(){ - "use strict"; + "use strict"; - var Timestring = function(settings) { - // 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; + var Timestring = function(settings) { + // default settings + var defaults = { + hoursPerDay: 24, + daysPerWeek: 7, + weeksPerMonth: 4, + monthsPerYear: 12 }; - Timestring.prototype.parse = function(string, returnUnit) { - // reference to this - var self = this; + // merge default settings with user settings + var settings = settings || {}; + this.settings = {}; - // 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'; - } - - // convert a value to a specific unit - function convert(value, unit) { - var baseValue = self.unitValues[getUnitKey(unit)]; - return value / baseValue; - } - - // get a value in seconds based on a specific unit - function getSeconds(value, unit) { - var baseValue = self.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 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); - } - } - - // return total, convert if needed - return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds; + for (var property in defaults) { + this.settings[property] = defaults[property]; } - // add convenience method to string prototype - String.prototype.parseTime = function (unit, settings) { - return (new Timestring(settings)).parse(this, unit); + for (var property in settings) { + this.settings[property] = settings[property]; } - // export Timestring object for either the browser or node - if (typeof module !== 'undefined' && module.exports) { - module.exports = Timestring; + // 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; + + // 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'; } - else { - this.Timestring = Timestring; + + // convert a value to a specific unit + function convert(value, unit) { + var baseValue = self.unitValues[getUnitKey(unit)]; + + return value / baseValue; } + // get a value in seconds based on a specific unit + function getSeconds(value, unit) { + var baseValue = self.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 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); + } + } + + // 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);