Skip to content

Commit

Permalink
Add xo as a linting tool (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels authored and leo committed Oct 14, 2016
1 parent 9d67c5f commit 8e33385
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 183 deletions.
101 changes: 59 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Helpers.
*/

var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var y = d * 365.25;
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`.
Expand All @@ -22,19 +22,18 @@ var y = d * 365.25;
* @api public
*/

module.exports = function(val, options){
options = options || {};
var type = typeof val;
if ('string' === type && val.length > 0) {
return parse(val);
} else if ('number' === type && isNaN(val) === false) {
return options['long']
? fmtLong(val)
: fmtShort(val);
} else {
throw new Error('val is not a non-empty string or a valid number. val=' + JSON.stringify(val));
module.exports = function (val, options) {
options = options || {}
var type = typeof val
if (type === 'string' && val.length > 0) {
return parse(val)
} else if (type === 'number' && isNaN(val) === false) {
return options.long ?
fmtLong(val) :
fmtShort(val)
}
};
throw new Error('val is not a non-empty string or a valid number. val=' + JSON.stringify(val))
}

/**
* Parse the given `str` and return milliseconds.
Expand All @@ -45,47 +44,53 @@ module.exports = function(val, options){
*/

function parse(str) {
str = '' + str;
if (str.length > 10000) return;
var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str);
if (!match) return;
var n = parseFloat(match[1]);
var type = (match[2] || 'ms').toLowerCase();
str = String(str)
if (str.length > 10000) {
return
}
var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|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 'yrs':
case 'yr':
case 'y':
return n * y;
return n * y
case 'days':
case 'day':
case 'd':
return n * d;
return n * d
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
return n * h;
return n * h
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
return n * m;
return n * m
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
return n * s;
return n * s
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
return n;
return n
default:
return undefined
}
}

Expand All @@ -98,11 +103,19 @@ function parse(str) {
*/

function fmtShort(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';
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'
}

/**
Expand All @@ -114,19 +127,23 @@ function fmtShort(ms) {
*/

function fmtLong(ms) {
return plural(ms, d, 'day')
|| plural(ms, h, 'hour')
|| plural(ms, m, 'minute')
|| plural(ms, s, 'second')
|| ms + ' 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';
if (ms < n) {
return
}
if (ms < n * 1.5) {
return Math.floor(ms / n) + ' ' + name
}
return Math.ceil(ms / n) + ' ' + name + 's'
}
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@
"index.js"
],
"scripts": {
"test": "mocha test/index.js",
"test": "xo && mocha test/index.js",
"test-browser": "serve ./test"
},
"license": "MIT",
"devDependencies": {
"mocha": "^3.0.2",
"expect.js": "^0.3.1",
"serve": "^1.4.0"
"mocha": "^3.0.2",
"serve": "^1.4.0",
"xo": "^0.17.0"
},
"component": {
"scripts": {
"ms/index.js": "index.js"
}
},
"xo": {
"space": true,
"semicolon": false,
"envs": [
"mocha"
]
}
}
Loading

0 comments on commit 8e33385

Please sign in to comment.