timestring/index.js

145 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-12-16 20:52:21 +01:00
var _ = require('lodash')
2016-01-15 21:05:30 +01:00
2016-01-15 12:00:03 +01:00
/**
* Exports
*/
2016-12-16 20:52:21 +01:00
module.exports = parseTimestring
2016-01-15 11:00:12 +01:00
2016-01-15 12:00:03 +01:00
/**
2016-01-15 21:05:30 +01:00
* Default options to use when parsing a timestring
2016-01-15 12:00:03 +01:00
*
2016-01-15 21:05:30 +01:00
* @type {Object}
2016-01-15 12:00:03 +01:00
*/
2016-01-15 21:05:30 +01:00
var defaultOpts = {
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
2016-12-16 20:52:21 +01:00
monthsPerYear: 12
}
2016-01-15 11:00:12 +01:00
2016-01-15 21:05:30 +01:00
/**
* Map of accepted strings to unit
*
* @type {Object}
*/
var unitMap = {
2016-07-30 16:56:51 +02:00
ms: ['ms', 'milli', 'millisecond', 'milliseconds'],
2016-01-15 21:05:30 +01:00
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'],
2016-12-16 20:52:21 +01:00
mth: ['mon', 'mth', 'mths', 'month', 'months'],
y: ['y', 'yr', 'yrs', 'year', 'years']
}
2016-01-15 21:05:30 +01:00
/**
* Parse a timestring
*
* @param {string} string
* @param {string} [returnUnit]
* @param {Object} [opts]
* @return {number}
*/
2016-12-16 20:52:21 +01:00
function parseTimestring (string, returnUnit, opts) {
opts = _.extend(_.clone(defaultOpts), opts || {})
2016-01-15 21:05:30 +01:00
2016-12-16 20:52:21 +01:00
var totalSeconds = 0
var unitValues = getUnitValues(opts)
2016-01-15 21:05:30 +01:00
var groups = string
.toLowerCase()
2016-12-16 20:52:21 +01:00
.replace(/[^.\w+-]+/g, '')
.match(/[-+]?[0-9]+[a-z]+/g)
2016-01-15 21:05:30 +01:00
if (groups !== null) {
2016-12-16 20:52:21 +01:00
_.each(groups, function (group) {
var value = group.match(/[0-9]+/g)[0]
var unit = group.match(/[a-z]+/g)[0]
2016-01-15 21:05:30 +01:00
2016-12-16 20:52:21 +01:00
totalSeconds += getSeconds(value, unit, unitValues)
})
2016-01-15 21:05:30 +01:00
}
if (returnUnit) {
2016-12-16 20:52:21 +01:00
return convert(totalSeconds, returnUnit, unitValues)
2016-01-15 21:05:30 +01:00
}
2016-12-16 20:52:21 +01:00
return totalSeconds
2016-01-15 21:05:30 +01:00
}
2016-01-15 11:00:12 +01:00
2016-01-15 21:05:30 +01:00
/**
* Get unit values based on the passed options
*
* @param {Object} opts
* @returns {Object}
*/
2016-12-16 20:52:21 +01:00
function getUnitValues (opts) {
2016-01-15 21:05:30 +01:00
var unitValues = {
2016-07-30 16:56:51 +02:00
ms: 0.001,
2016-01-15 11:00:12 +01:00
s: 1,
m: 60,
2016-12-16 20:52:21 +01:00
h: 3600
}
2016-01-15 11:00:12 +01:00
2016-12-16 20:52:21 +01:00
unitValues.d = opts.hoursPerDay * unitValues.h
unitValues.w = opts.daysPerWeek * unitValues.d
unitValues.mth = opts.weeksPerMonth * unitValues.w
unitValues.y = opts.monthsPerYear * unitValues.mth
2016-01-15 21:05:30 +01:00
2016-12-16 20:52:21 +01:00
return unitValues
2016-01-15 11:00:12 +01:00
}
2016-01-15 12:00:03 +01:00
/**
2016-01-15 21:05:30 +01:00
* Get the key for a unit
2016-01-15 12:00:03 +01:00
*
2016-01-15 21:05:30 +01:00
* @param {string} unit
* @returns {string}
2016-01-15 12:00:03 +01:00
*/
2016-01-15 11:00:12 +01:00
2016-12-16 20:52:21 +01:00
function getUnitKey (unit) {
2016-01-15 21:05:30 +01:00
for (var k in unitMap) {
for (var u in unitMap[k]) {
if (unit === unitMap[k][u]) {
2016-12-16 20:52:21 +01:00
return k
2016-01-15 11:00:12 +01:00
}
}
}
2016-12-16 20:52:21 +01:00
throw new Error('The unit [' + unit + '] is not supported by timestring')
2016-01-15 21:05:30 +01:00
}
2016-01-15 11:00:12 +01:00
2016-01-15 21:05:30 +01:00
/**
* Get the number of seconds for a value, based on the unit
*
* @param {number} value
* @param {string} unit
* @param {Object} unitValues
* @returns {number}
*/
2016-01-15 11:00:12 +01:00
2016-12-16 20:52:21 +01:00
function getSeconds (value, unit, unitValues) {
var baseValue = unitValues[getUnitKey(unit)]
2016-01-15 11:00:12 +01:00
2016-12-16 20:52:21 +01:00
return value * baseValue
2016-01-15 21:05:30 +01:00
}
2016-01-15 11:00:12 +01:00
2016-01-15 21:05:30 +01:00
/**
* Convert a value from its existing unit to a new unit
*
* @param {number} value
* @param {string} unit
* @param {Object} unitValues
* @returns {number}
*/
2016-01-15 11:00:12 +01:00
2016-12-16 20:52:21 +01:00
function convert (value, unit, unitValues) {
var baseValue = unitValues[getUnitKey(unit)]
2016-01-15 11:00:12 +01:00
2016-12-16 20:52:21 +01:00
return value / baseValue
2016-01-15 21:05:30 +01:00
}