Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Update included version of ms.js

Mike Pennisi9377b993cec3
diff --git a/lib/ms.js b/lib/ms.js
index 54472660..c5847f8d 100644
--- a/lib/ms.js
+++ b/lib/ms.js
@@ -1,4 +1,3 @@
-
/**
* Helpers.
*/
@@ -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.
@@ -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';
-}
\ No newline at end of file
+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';
+}
diff --git a/package.json b/package.json
index 3eb8f60a..679da5dd 100644
--- a/package.json
+++ b/package.json
@@ -32,7 +32,6 @@
"diff": "1.0.2",
"debug": "*",
"mkdirp": "0.3.5",
- "ms": "0.6.0",
"glob": "3.2.1"
},
"devDependencies": {