Comparing version 0.7.1 to 0.7.2
102
index.js
@@ -5,7 +5,7 @@ /** | ||
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 | ||
@@ -21,2 +21,3 @@ /** | ||
* @param {Object} options | ||
* @throws {Error} throw an error if val is not a non-empty string or a number | ||
* @return {String|Number} | ||
@@ -26,9 +27,14 @@ * @api public | ||
module.exports = function(val, options){ | ||
options = options || {}; | ||
if ('string' == typeof val) return parse(val); | ||
return options.long | ||
? long(val) | ||
: short(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)) | ||
} | ||
@@ -44,8 +50,12 @@ /** | ||
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) { | ||
@@ -57,7 +67,7 @@ case 'years': | ||
case 'y': | ||
return n * y; | ||
return n * y | ||
case 'days': | ||
case 'day': | ||
case 'd': | ||
return n * d; | ||
return n * d | ||
case 'hours': | ||
@@ -68,3 +78,3 @@ case 'hour': | ||
case 'h': | ||
return n * h; | ||
return n * h | ||
case 'minutes': | ||
@@ -75,3 +85,3 @@ case 'minute': | ||
case 'm': | ||
return n * m; | ||
return n * m | ||
case 'seconds': | ||
@@ -82,3 +92,3 @@ case 'second': | ||
case 's': | ||
return n * s; | ||
return n * s | ||
case 'milliseconds': | ||
@@ -89,3 +99,5 @@ case 'millisecond': | ||
case 'ms': | ||
return n; | ||
return n | ||
default: | ||
return undefined | ||
} | ||
@@ -102,8 +114,16 @@ } | ||
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'; | ||
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' | ||
} | ||
@@ -119,8 +139,8 @@ | ||
function long(ms) { | ||
return plural(ms, d, 'day') | ||
|| plural(ms, h, 'hour') | ||
|| plural(ms, m, 'minute') | ||
|| plural(ms, s, 'second') | ||
|| ms + ' ms'; | ||
function fmtLong(ms) { | ||
return plural(ms, d, 'day') || | ||
plural(ms, h, 'hour') || | ||
plural(ms, m, 'minute') || | ||
plural(ms, s, 'second') || | ||
ms + ' ms' | ||
} | ||
@@ -133,5 +153,9 @@ | ||
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' | ||
} |
{ | ||
"name": "ms", | ||
"version": "0.7.1", | ||
"description": "Tiny ms conversion utility", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/guille/ms.js.git" | ||
"version": "0.7.2", | ||
"description": "Tiny milisecond conversion utility", | ||
"repository": "zeit/ms", | ||
"main": "./index", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "xo && mocha test/index.js", | ||
"test-browser": "serve ./test" | ||
}, | ||
"main": "./index", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"mocha": "*", | ||
"expect.js": "*", | ||
"serve": "*" | ||
"expect.js": "^0.3.1", | ||
"mocha": "^3.0.2", | ||
"serve": "^1.4.0", | ||
"xo": "^0.17.0" | ||
}, | ||
@@ -19,3 +25,13 @@ "component": { | ||
} | ||
}, | ||
"xo": { | ||
"space": true, | ||
"semicolon": false, | ||
"envs": [ | ||
"mocha" | ||
], | ||
"rules": { | ||
"complexity": 0 | ||
} | ||
} | ||
} |
@@ -1,3 +0,11 @@ | ||
# ms.js: miliseconds conversion utility | ||
# ms | ||
[![Build Status](https://travis-ci.org/zeit/ms.svg?branch=master)](https://travis-ci.org/zeit/ms) | ||
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) | ||
[![Slack Channel](https://zeit-slackin.now.sh/badge.svg)](https://zeit.chat/) | ||
Use this package to easily convert various time formats to milliseconds. | ||
## Examples | ||
```js | ||
@@ -11,5 +19,8 @@ ms('2 days') // 172800000 | ||
ms('5s') // 5000 | ||
ms('1y') // 31557600000 | ||
ms('100') // 100 | ||
``` | ||
### Convert from milliseconds | ||
```js | ||
@@ -21,2 +32,4 @@ ms(60000) // "1m" | ||
### Time format written-out | ||
```js | ||
@@ -28,11 +41,15 @@ ms(60000, { long: true }) // "1 minute" | ||
- Node/Browser compatible. Published as [`ms`](https://www.npmjs.org/package/ms) in [NPM](http://nodejs.org/download). | ||
## Features | ||
- Works both in [node](https://nodejs.org) and in the browser. | ||
- If a number is supplied to `ms`, a string with a unit is returned. | ||
- If a string that contains the number is supplied, it returns it as | ||
a number (e.g: it returns `100` for `'100'`). | ||
- If you pass a string with a number and a valid unit, the number of | ||
equivalent ms is returned. | ||
- If a string that contains the number is supplied, it returns it as a number (e.g.: it returns `100` for `'100'`). | ||
- If you pass a string with a number and a valid unit, the number of equivalent ms is returned. | ||
## License | ||
## Caught a bug? | ||
MIT | ||
1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device | ||
2. Link the package to the global module directory: `npm link` | ||
3. Within the module you want to test your local development instance of ms, just link it to the dependencies: `npm link ms`. Instead of the default one from npm, node will now use your clone of ms! | ||
As always, you can run the tests using: `npm test` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6252
138
53
4
4
1
1