timestring
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"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" : "Mike Barrett <mike182uk@gmail.com", | ||
"repository" : {"type": "git", "url": "git://github.com/mike182uk/timestring.git"}, | ||
"main" : "timestring.js", | ||
"version" : "1.0.0" | ||
"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" : "Mike Barrett <mike182uk@gmail.com>", | ||
"repository" : { | ||
"type": "git", | ||
"url": "git://github.com/mike182uk/timestring.git" | ||
}, | ||
"main" : "timestring.js", | ||
"version" : "1.0.1" | ||
} |
@@ -30,3 +30,3 @@ #timestring | ||
```js | ||
var str = '1 d 3h 25 m 1 8s'; | ||
var str = '1 d 3HOurS 25 min 1 8s'; | ||
var time = str.parseTime(); | ||
@@ -169,3 +169,3 @@ | ||
Timestring is also node compatible. To install for a project, navigate to the projects root folder and in your terminal and type the following: | ||
Timestring is also node compatible. To install for a project, navigate to the projects root folder and in your terminal and type the following: | ||
@@ -184,2 +184,2 @@ ``` | ||
Once you have done this, you will beable to use timestring in node, the same way you do in the browser! | ||
Once you have done this, you will beable to use timestring in node, the same way you do in the browser! |
@@ -50,5 +50,19 @@ (function(){ | ||
// 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[unit]; | ||
var baseValue = self.unitValues[getUnitKey(unit)]; | ||
return value / baseValue; | ||
@@ -59,3 +73,3 @@ } | ||
function getSeconds(value, unit) { | ||
var baseValue = self.unitValues[unit]; | ||
var baseValue = self.unitValues[getUnitKey(unit)]; | ||
return value * baseValue; | ||
@@ -67,19 +81,7 @@ } | ||
// dynamically generate units regular expression | ||
var unitsRegExp = ''; | ||
for(var unit in this.units) { | ||
unitsRegExp += ',' + this.units[unit].join(); | ||
} | ||
unitsRegExp = '(' + | ||
unitsRegExp | ||
.replace(/,/g, '|') // convert commas to pipes | ||
.replace('|', '') // removing leading pipe | ||
+ ')'; | ||
// split string into groups and get total seconds for each group | ||
var groups = string | ||
.replace(/[^\w+-]+/g, '') // remove white space | ||
.match(/[-+]?[0-9]*\.?[0-9]+\D/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups) | ||
.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) | ||
@@ -89,4 +91,4 @@ if (groups !== null) { | ||
var g = groups[group], | ||
value = g.match(/[-+]?[0-9]*\.?[0-9]+/g), | ||
unit = g.match(new RegExp(unitsRegExp, 'g')); | ||
value = g.match(/[0-9]+/g)[0], | ||
unit = g.match(/[a-z]+/g)[0]; | ||
@@ -93,0 +95,0 @@ totalSeconds += getSeconds(value, unit); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9070
93
183