timestring
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -1,3 +0,8 @@ | ||
#Changelog | ||
# Changelog | ||
## 3.0.0 | ||
- Remove `String.parseTime` | ||
- Export function instead of object | ||
## 2.0.0 | ||
@@ -9,3 +14,3 @@ | ||
##1.1.1 | ||
## 1.1.1 | ||
@@ -23,3 +28,3 @@ - src now uses ES6 | ||
##1.1.0 | ||
## 1.1.0 | ||
@@ -26,0 +31,0 @@ - add MIT license |
185
index.js
@@ -0,1 +1,3 @@ | ||
var _ = require('lodash'); | ||
/** | ||
@@ -5,34 +7,77 @@ * Exports | ||
module.exports = Timestring; | ||
module.exports = parseTimestring; | ||
/** | ||
* Create a new Timestring instance | ||
* Default options to use when parsing a timestring | ||
* | ||
* @param {Object} opts | ||
* @constructor | ||
* @type {Object} | ||
*/ | ||
function Timestring(opts) { | ||
var defaultOpts = { | ||
hoursPerDay: 24, | ||
daysPerWeek: 7, | ||
weeksPerMonth: 4, | ||
monthsPerYear: 12, | ||
}; | ||
var defaultOpts = { | ||
hoursPerDay: 24, | ||
daysPerWeek: 7, | ||
weeksPerMonth: 4, | ||
monthsPerYear: 12, | ||
}; | ||
opts = opts || {}; | ||
this.opts = defaultOpts; | ||
for (var s in opts) { this.opts[s] = opts[s]; } | ||
/** | ||
* Map of accepted strings to unit | ||
* | ||
* @type {Object} | ||
*/ | ||
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'], | ||
}; | ||
var unitMap = { | ||
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'], | ||
}; | ||
this.unitValues = { | ||
/** | ||
* Parse a timestring | ||
* | ||
* @param {string} string | ||
* @param {string} [returnUnit] | ||
* @param {Object} [opts] | ||
* @return {number} | ||
*/ | ||
function parseTimestring(string, returnUnit, opts) { | ||
opts = _.extend(_.clone(defaultOpts), opts || {}); | ||
var totalSeconds = 0; | ||
var unitValues = getUnitValues(opts); | ||
var groups = string | ||
.toLowerCase() | ||
.replace(/[^\.\w+-]+/g, '') | ||
.match(/[-+]?[0-9]+[a-z]+/g); | ||
if (groups !== null) { | ||
_.each(groups, function(group) { | ||
var value = group.match(/[0-9]+/g)[0]; | ||
var unit = group.match(/[a-z]+/g)[0]; | ||
totalSeconds += getSeconds(value, unit, unitValues); | ||
}); | ||
} | ||
if (returnUnit) { | ||
return convert(totalSeconds, returnUnit, unitValues); | ||
} | ||
return totalSeconds; | ||
} | ||
/** | ||
* Get unit values based on the passed options | ||
* | ||
* @param {Object} opts | ||
* @returns {Object} | ||
*/ | ||
function getUnitValues(opts) { | ||
var unitValues = { | ||
s: 1, | ||
@@ -43,71 +88,57 @@ m: 60, | ||
this.unitValues.d = this.opts.hoursPerDay * this.unitValues.h; | ||
this.unitValues.w = this.opts.daysPerWeek * this.unitValues.d; | ||
this.unitValues.mth = this.opts.weeksPerMonth * this.unitValues.w; | ||
this.unitValues.y = this.opts.monthsPerYear * this.unitValues.mth; | ||
unitValues.d = opts.hoursPerDay * unitValues.h; | ||
unitValues.w = opts.daysPerWeek * unitValues.d; | ||
unitValues.mth = opts.weeksPerMonth * unitValues.w; | ||
unitValues.y = opts.monthsPerYear * unitValues.mth; | ||
return unitValues; | ||
} | ||
/** | ||
* Parse a timestring | ||
* Get the key for a unit | ||
* | ||
* @param {string} string | ||
* @param {string} returnUnit | ||
* @return {string} | ||
* @param {string} unit | ||
* @returns {string} | ||
*/ | ||
Timestring.prototype.parse = function parse(string, returnUnit) { | ||
function getUnitKey(unit) { | ||
for (var k in this.units) { | ||
for (var u in this.units[k]) { | ||
if (unit === this.units[k][u]) { | ||
return k; | ||
} | ||
function getUnitKey(unit) { | ||
for (var k in unitMap) { | ||
for (var u in unitMap[k]) { | ||
if (unit === unitMap[k][u]) { | ||
return k; | ||
} | ||
} | ||
throw new Error('The unit [' + unit + '] is not supported by timestring'); | ||
} | ||
function convert(value, unit) { | ||
var baseValue = this.unitValues[getUnitKey.call(this, unit)]; | ||
throw new Error('The unit [' + unit + '] is not supported by timestring'); | ||
} | ||
return value / baseValue; | ||
} | ||
/** | ||
* Get the number of seconds for a value, based on the unit | ||
* | ||
* @param {number} value | ||
* @param {string} unit | ||
* @param {Object} unitValues | ||
* @returns {number} | ||
*/ | ||
function getSeconds(value, unit) { | ||
var baseValue = this.unitValues[getUnitKey.call(this, unit)]; | ||
function getSeconds(value, unit, unitValues) { | ||
var baseValue = unitValues[getUnitKey(unit)]; | ||
return value * baseValue; | ||
} | ||
return value * baseValue; | ||
} | ||
var totalSeconds = 0; | ||
var groups = string | ||
.toLowerCase() | ||
.replace(/[^\.\w+-]+/g, '') | ||
.match(/[-+]?[0-9]+[a-z]+/g); | ||
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.call(this, value, unit); | ||
} | ||
} | ||
return (returnUnit) ? | ||
convert.call(this, totalSeconds, returnUnit) : | ||
totalSeconds; | ||
}; | ||
/** | ||
* Parse a timestring | ||
* Convert a value from its existing unit to a new unit | ||
* | ||
* @param {string} unit | ||
* @param {Object} opts | ||
* @return {string} | ||
* @param {number} value | ||
* @param {string} unit | ||
* @param {Object} unitValues | ||
* @returns {number} | ||
*/ | ||
String.prototype.parseTime = function parseTime(unit, opts) { | ||
return (new Timestring(opts)).parse(this, unit); | ||
}; | ||
function convert(value, unit, unitValues) { | ||
var baseValue = unitValues[getUnitKey(unit)]; | ||
return value / baseValue; | ||
} |
@@ -12,2 +12,3 @@ { | ||
"scripts": { | ||
"dev": "watch 'clear; npm test -s;' ./ -d", | ||
"sa": "jshint index.js && jscs index.js", | ||
@@ -17,6 +18,6 @@ "test": "mocha test.js" | ||
"main": "index.js", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"devDependencies": { | ||
"chai": "^3.4.1", | ||
"codeclimate-test-reporter": "^0.1.0", | ||
"codeclimate-test-reporter": "^0.2.0", | ||
"coveralls": "^2.11.2", | ||
@@ -27,4 +28,8 @@ "istanbul": "^0.4.1", | ||
"mocha": "^2.2.4", | ||
"mocha-lcov-reporter": "1.0.0" | ||
"mocha-lcov-reporter": "1.0.0", | ||
"watch": "^0.17.1" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.0.0" | ||
} | ||
} |
@@ -23,4 +23,6 @@ # Timestring | ||
```js | ||
var timestring = require('timestring'); | ||
var str = '1h 15m'; | ||
var time = str.parseTime(); | ||
var time = timestring(str); | ||
@@ -30,6 +32,4 @@ console.log(time); // will log 4500 | ||
In the example above `str` is just a plain old `String` object. A new method is added to the `String` objects prototype named `parseTime`. This method parses the string and returns a time based value. | ||
**By default the returned time value from `timestring` will be in seconds.** | ||
**By default the returned time value will be in seconds.** | ||
The time string can contain as many time groups as needed: | ||
@@ -39,3 +39,3 @@ | ||
var str = '1d 3h 25m 18s'; | ||
var time = str.parseTime(); | ||
var time = timestring(str); | ||
@@ -49,3 +49,3 @@ console.log(time); // will log 98718 | ||
var str = '1 d 3HOurS 25 min 1 8s'; | ||
var time = str.parseTime(); | ||
var time = timestring(str); | ||
@@ -55,11 +55,2 @@ console.log(time); // will log 98718 | ||
As well as using the `String` objects `parseTime` method you can create a `Timestring` object and parse the string manually: | ||
```js | ||
var str = '1h 15m'; | ||
var time = (new Timestring()).parse(str); | ||
console.log(time); // will log 4500 | ||
``` | ||
### Keywords | ||
@@ -81,3 +72,3 @@ | ||
var str = '1day 15h 20minutes 15s'; | ||
var time = str.parseTime(); | ||
var time = timestring(str); | ||
@@ -89,3 +80,3 @@ console.log(time); // will log 141615 | ||
By default the return time value will be in seconds. This can be changed by passing one of the following strings as an argument to `String.parseTime` or `Timestring.parse`: | ||
By default the return time value will be in seconds. This can be changed by passing one of the following strings as an argument to `timestring`: | ||
@@ -103,11 +94,5 @@ 1. `s` - Seconds | ||
var hours = str.parseTime('h'); // 22.266666666666666 | ||
var days = str.parseTime('d'); // 0.9277777777777778 | ||
var weeks = str.parseTime('w'); // 0.13253968253968254 | ||
// or | ||
var hours = (new Timestring()).parse(str, 'h'); // 22.266666666666666 | ||
var days = (new Timestring()).parse(str, 'd'); // 0.9277777777777778 | ||
var weeks = (new Timestring()).parse(str, 'w'); // 0.13253968253968254 | ||
var hours = timestring(str, 'h'); // 22.266666666666666 | ||
var days = timestring(str, 'd'); // 0.9277777777777778 | ||
var weeks = timestring(str, 'w'); // 0.13253968253968254 | ||
``` | ||
@@ -124,3 +109,3 @@ | ||
These options can be changed by passing a options object as an argument to `String.parseTime` or to the `Timestring` objects constructor. | ||
These options can be changed by passing an options object as an argument to `timestring`. | ||
@@ -136,3 +121,2 @@ The following options are configurable: | ||
var str = '1d'; | ||
var opts = { | ||
@@ -142,9 +126,4 @@ hoursPerDay: 1 | ||
var time = str.parseTime('h', opts); | ||
var time = timestring(str, 'h', opts); | ||
// or | ||
var time = (new Timestring(opts)).parse(str, 'h'); | ||
console.log(time); // will log 1 | ||
@@ -165,18 +144,7 @@ ``` | ||
// get time values from form input | ||
var today = document.querySelector('time-input').value, // '1d' | ||
thisWeek = document.querySelector('time-input').value; // '1w' | ||
var hoursToday = timestring('1d', 'h', opts); | ||
var daysThisWeek = timestring('1w', 'd', opts); | ||
// parse times | ||
var hoursToday = today.parseTime('h', opts), | ||
daysThisWeek = thisWeek.parseTime('d', opts); | ||
// or | ||
var hoursToday = (new Timestring(opts)).parse(today, 'h'), | ||
daysThisWeek = (new Timestring(opts)).parse(thisWeek, 'd'); | ||
console.log(hoursToday); // will log 7.5 | ||
console.log(daysThisWeek); // will log 5 | ||
``` |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
116
0
9630
1
9
139
+ Addedlodash@^4.0.0
+ Addedlodash@4.17.21(transitive)