Skip to content
Browse files

[fix] Patch ms to only convert numbers in to strings when a option is…

… provided. Fixes #8
  • Loading branch information...
1 parent 97d43d6 commit f87ce4fda1b6d7f5f8ce0a40d64e71b76391677f @3rd-Eden committed
Showing with 113 additions and 4 deletions.
  1. +1 −1 index.js
  2. +108 −0 ms.js
  3. +1 −2 package.json
  4. +3 −1 test/jackpot.test.js
View
2 index.js
@@ -1,7 +1,7 @@
'use strict';
var retry = require('retry')
- , ms = require('ms');
+ , ms = require('./ms');
/**
* A net.Stream connection pool.
View
108 ms.js
@@ -0,0 +1,108 @@
+'use strict';
+
+/**
+ * Helpers.
+ */
+
+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, options){
+ if ('string' === typeof val) return parse(val);
+ if (!options) return +val;
+
+ return options.long
+ ? long(val)
+ : short(val);
+};
+
+/**
+ * Parse the given `str` and return milliseconds.
+ *
+ * @param {String} str
+ * @return {Number}
+ * @api private
+ */
+
+function parse(str) {
+ 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 * y;
+ case 'days':
+ case 'day':
+ case 'd':
+ return n * d;
+ case 'hours':
+ case 'hour':
+ case 'h':
+ return n * h;
+ case 'minutes':
+ case 'minute':
+ case 'm':
+ return n * m;
+ case 'seconds':
+ case 'second':
+ case 's':
+ return n * s;
+ case 'ms':
+ return n;
+ }
+}
+
+/**
+ * Short format for `ms`.
+ *
+ * @param {Number} ms
+ * @return {String}
+ * @api private
+ */
+
+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) {
+ 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';
+}
View
3 package.json
@@ -19,8 +19,7 @@
"test": "make test"
},
"dependencies": {
- "retry": "0.6.0",
- "ms": "~0.6.1"
+ "retry": "0.6.0"
},
"devDependencies": {
"mocha": "1.2.1",
View
4 test/jackpot.test.js
@@ -58,10 +58,12 @@ describe('jackpot', function () {
it('parses human readable strings', function () {
var pool = new ConnectionPool(10, {
- min: '10 seconds'
+ min: '10 seconds',
+ max: 10000
});
expect(pool.minTimeout).to.equal(10 * 1000);
+ expect(pool.maxTimeout).to.equal(10000);
});
});

0 comments on commit f87ce4f

Please sign in to comment.
Something went wrong with that request. Please try again.