Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/mocha-3.1.2/lib/ms.js')
-rw-r--r--tests/lib/mocha-3.1.2/lib/ms.js130
1 files changed, 130 insertions, 0 deletions
diff --git a/tests/lib/mocha-3.1.2/lib/ms.js b/tests/lib/mocha-3.1.2/lib/ms.js
new file mode 100644
index 0000000000..9590856052
--- /dev/null
+++ b/tests/lib/mocha-3.1.2/lib/ms.js
@@ -0,0 +1,130 @@
+'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]
+ *
+ * @api public
+ * @param {string|number} val
+ * @param {Object} options
+ * @return {string|number}
+ */
+module.exports = function (val, options) {
+ options = options || {};
+ if (typeof val === 'string') {
+ return parse(val);
+ }
+ // https://github.com/mochajs/mocha/pull/1035
+ return options['long'] ? longFormat(val) : shortFormat(val);
+};
+
+/**
+ * Parse the given `str` and return milliseconds.
+ *
+ * @api private
+ * @param {string} str
+ * @return {number}
+ */
+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;
+ default:
+ // No default case
+ }
+}
+
+/**
+ * Short format for `ms`.
+ *
+ * @api private
+ * @param {number} ms
+ * @return {string}
+ */
+function shortFormat (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`.
+ *
+ * @api private
+ * @param {number} ms
+ * @return {string}
+ */
+function longFormat (ms) {
+ return plural(ms, d, 'day') ||
+ plural(ms, h, 'hour') ||
+ plural(ms, m, 'minute') ||
+ plural(ms, s, 'second') ||
+ ms + ' ms';
+}
+
+/**
+ * Pluralization helper.
+ *
+ * @api private
+ * @param {number} ms
+ * @param {number} n
+ * @param {string} name
+ */
+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';
+}