Skip to content

Commit

Permalink
Use prettier and eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Mar 19, 2017
1 parent 94068ea commit 57b3ef8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
75 changes: 39 additions & 36 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,18 +22,19 @@ var y = d * 365.25
* @api public
*/

module.exports = function (val, options) {
options = options || {}
var type = typeof val
module.exports = function(val, options) {
options = options || {};
var type = typeof val;
if (type === 'string' && val.length > 0) {
return parse(val)
return parse(val);
} else if (type === 'number' && isNaN(val) === false) {
return options.long ?
fmtLong(val) :
fmtShort(val)
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))
}
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 @@ -44,53 +45,55 @@ module.exports = function (val, options) {
*/

function parse(str) {
str = String(str)
str = String(str);
if (str.length > 10000) {
return
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)
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
return;
}
var n = parseFloat(match[1])
var type = (match[2] || 'ms').toLowerCase()
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
return undefined;
}
}

Expand All @@ -104,18 +107,18 @@ function parse(str) {

function fmtShort(ms) {
if (ms >= d) {
return Math.round(ms / d) + 'd'
return Math.round(ms / d) + 'd';
}
if (ms >= h) {
return Math.round(ms / h) + 'h'
return Math.round(ms / h) + 'h';
}
if (ms >= m) {
return Math.round(ms / m) + 'm'
return Math.round(ms / m) + 'm';
}
if (ms >= s) {
return Math.round(ms / s) + 's'
return Math.round(ms / s) + 's';
}
return ms + 'ms'
return ms + 'ms';
}

/**
Expand All @@ -131,7 +134,7 @@ function fmtLong(ms) {
plural(ms, h, 'hour') ||
plural(ms, m, 'minute') ||
plural(ms, s, 'second') ||
ms + ' ms'
ms + ' ms';
}

/**
Expand All @@ -140,10 +143,10 @@ function fmtLong(ms) {

function plural(ms, n, name) {
if (ms < n) {
return
return;
}
if (ms < n * 1.5) {
return Math.floor(ms / n) + ' ' + name
return Math.floor(ms / n) + ' ' + name;
}
return Math.ceil(ms / n) + ' ' + name + 's'
return Math.ceil(ms / n) + ' ' + name + 's';
}
29 changes: 24 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,37 @@
"index.js"
],
"scripts": {
"precommit": "lint-staged",
"lint": "eslint lib/* bin/*",
"test": "mocha test/index.js",
"test-browser": "serve ./test"
},
"license": "MIT",
"devDependencies": {
"expect.js": "0.3.1",
"mocha": "3.0.2",
"serve": "5.0.4",
"eslintConfig": {
"extends": "eslint:recommended",
"env": {
"node": true,
"es6": true
}
},
"lint-staged": {
"*.js": [
"npm run lint",
"prettier --single-quote --write",
"git add"
]
},
"component": {
"scripts": {
"ms/index.js": "index.js"
}
},
"license": "MIT",
"devDependencies": {
"eslint": "3.18.0",
"expect.js": "0.3.1",
"husky": "0.13.2",
"lint-staged": "3.4.0",
"mocha": "3.0.2",
"serve": "5.0.4"
}
}

0 comments on commit 57b3ef8

Please sign in to comment.