Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update included version of ms.js #957

Merged
merged 1 commit into from
Aug 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 56 additions & 26 deletions lib/ms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Helpers.
*/
Expand All @@ -7,19 +6,28 @@ var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var y = d * 365.25;

/**
* Parse or format the given `val`.
*
* Options:
*
* - `long` verbose formatting [false]
*
* @param {String|Number} val
* @param {Object} options
* @return {String|Number}
* @api public
*/

module.exports = function(val){
module.exports = function(val, options){
options = options || {};
if ('string' == typeof val) return parse(val);
return format(val);
}
return options.long
? long(val)
: short(val);
};

/**
* Parse the given `str` and return milliseconds.
Expand All @@ -30,52 +38,74 @@ module.exports = function(val){
*/

function parse(str) {
var m = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)?$/i.exec(str);
if (!m) return;
var n = parseFloat(m[1]);
var type = (m[2] || 'ms').toLowerCase();
var match = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)?$/i.exec(str);
if (!match) return;
var n = parseFloat(match[1]);
var type = (match[2] || 'ms').toLowerCase();
switch (type) {
case 'years':
case 'year':
case 'y':
return n * 31557600000;
return n * y;
case 'days':
case 'day':
case 'd':
return n * 86400000;
return n * d;
case 'hours':
case 'hour':
case 'h':
return n * 3600000;
return n * h;
case 'minutes':
case 'minute':
case 'm':
return n * 60000;
return n * m;
case 'seconds':
case 'second':
case 's':
return n * 1000;
return n * s;
case 'ms':
return n;
}
}

/**
* Format the given `ms`.
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api public
* @api private
*/

function format(ms) {
if (ms == d) return Math.round(ms / d) + ' day';
if (ms > d) return Math.round(ms / d) + ' days';
if (ms == h) return Math.round(ms / h) + ' hour';
if (ms > h) return Math.round(ms / h) + ' hours';
if (ms == m) return Math.round(ms / m) + ' minute';
if (ms > m) return Math.round(ms / m) + ' minutes';
if (ms == s) return Math.round(ms / s) + ' second';
if (ms > s) return Math.round(ms / s) + ' seconds';
return ms + ' ms';
}
function short(ms) {
if (ms >= d) return Math.round(ms / d) + 'd';
if (ms >= h) return Math.round(ms / h) + 'h';
if (ms >= m) return Math.round(ms / m) + 'm';
if (ms >= s) return Math.round(ms / s) + 's';
return ms + 'ms';
}

/**
* Long format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/

function long(ms) {
return plural(ms, d, 'day')
|| plural(ms, h, 'hour')
|| plural(ms, m, 'minute')
|| plural(ms, s, 'second')
|| ms + ' ms';
}

/**
* Pluralization helper.
*/

function plural(ms, n, name) {
if (ms < n) return;
if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
return Math.ceil(ms / n) + ' ' + name + 's';
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"diff": "1.0.2",
"debug": "*",
"mkdirp": "0.3.5",
"ms": "0.6.0",
"glob": "3.2.1"
},
"devDependencies": {
Expand Down