[Pkg-javascript-commits] [node-ms] 01/03: New upstream version 2.0.0
Paolo Greppi
paolog-guest at moszumanska.debian.org
Wed May 31 07:25:32 UTC 2017
This is an automated email from the git hooks/post-receive script.
paolog-guest pushed a commit to branch master
in repository node-ms.
commit c37d361aee820e7f9c1747a0d97c90b45fe1d014
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date: Wed May 31 09:09:17 2017 +0200
New upstream version 2.0.0
---
.gitignore | 5 ++
.npmignore | 5 --
.npmrc | 1 +
.travis.yml | 7 ++
History.md | 53 --------------
Makefile | 8 ---
README.md | 33 ---------
component.json | 15 ----
index.js | 83 ++++++++++++++++------
license.md | 21 ++++++
package.json | 43 +++++++----
readme.md | 51 +++++++++++++
test/test.js | 121 -------------------------------
tests.js | 221 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14 files changed, 398 insertions(+), 269 deletions(-)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..22f7aa0
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+# dependencies
+node_modules
+
+# logs
+npm-debug.log
diff --git a/.npmignore b/.npmignore
deleted file mode 100644
index d1aa0ce..0000000
--- a/.npmignore
+++ /dev/null
@@ -1,5 +0,0 @@
-node_modules
-test
-History.md
-Makefile
-component.json
diff --git a/.npmrc b/.npmrc
new file mode 100644
index 0000000..1dab4ed
--- /dev/null
+++ b/.npmrc
@@ -0,0 +1 @@
+save-exact = true
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..ab0d211
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,7 @@
+{
+ "language": "node_js",
+ "node_js": [
+ "6",
+ "node"
+ ]
+}
diff --git a/History.md b/History.md
deleted file mode 100644
index a47a1c3..0000000
--- a/History.md
+++ /dev/null
@@ -1,53 +0,0 @@
-
-0.6.2 / 2013-12-05
-==================
-
- * Adding repository section to package.json to suppress warning from NPM.
-
-0.6.1 / 2013-05-10
-==================
-
- * fix singularization [visionmedia]
-
-0.6.0 / 2013-03-15
-==================
-
- * fix minutes
-
-0.5.1 / 2013-02-24
-==================
-
- * add component namespace
-
-0.5.0 / 2012-11-09
-==================
-
- * add short formatting as default and .long option
- * add .license property to component.json
- * add version to component.json
-
-0.4.0 / 2012-10-22
-==================
-
- * add rounding to fix crazy decimals
-
-0.3.0 / 2012-09-07
-==================
-
- * fix `ms(<String>)` [visionmedia]
-
-0.2.0 / 2012-09-03
-==================
-
- * add component.json [visionmedia]
- * add days support [visionmedia]
- * add hours support [visionmedia]
- * add minutes support [visionmedia]
- * add seconds support [visionmedia]
- * add ms string support [visionmedia]
- * refactor tests to facilitate ms(number) [visionmedia]
-
-0.1.0 / 2012-03-07
-==================
-
- * Initial release
diff --git a/Makefile b/Makefile
deleted file mode 100644
index 0ae97b3..0000000
--- a/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-
-test:
- @./node_modules/.bin/mocha test/test.js
-
-test-browser:
- ./node_modules/.bin/serve test/
-
-.PHONY: test
diff --git a/README.md b/README.md
deleted file mode 100644
index d4ab12a..0000000
--- a/README.md
+++ /dev/null
@@ -1,33 +0,0 @@
-# ms.js: miliseconds conversion utility
-
-```js
-ms('1d') // 86400000
-ms('10h') // 36000000
-ms('2h') // 7200000
-ms('1m') // 60000
-ms('5s') // 5000
-ms('100') // 100
-```
-
-```js
-ms(60000) // "1m"
-ms(2 * 60000) // "2m"
-ms(ms('10 hours')) // "10h"
-```
-
-```js
-ms(60000, { long: true }) // "1 minute"
-ms(2 * 60000, { long: true }) // "2 minutes"
-ms(ms('10 hours', { long: true })) // "10 hours"
-```
-
-- Node/Browser compatible. Published as `ms` in NPM.
-- 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.
-
-## License
-
-MIT
\ No newline at end of file
diff --git a/component.json b/component.json
deleted file mode 100644
index 2d7bbff..0000000
--- a/component.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "name": "ms",
- "repo": "guille/ms.js",
- "version": "0.6.1",
- "description": "ms parsing / formatting",
- "keywords": [
- "ms",
- "parse",
- "format"
- ],
- "scripts": [
- "index.js"
- ],
- "license": "MIT"
-}
diff --git a/index.js b/index.js
index c5847f8..6a522b1 100644
--- a/index.js
+++ b/index.js
@@ -16,17 +16,24 @@ var y = d * 365.25;
* - `long` verbose formatting [false]
*
* @param {String|Number} val
- * @param {Object} options
+ * @param {Object} [options]
+ * @throws {Error} throw an error if val is not a non-empty string or a number
* @return {String|Number}
* @api public
*/
-module.exports = function(val, options){
+module.exports = function(val, options) {
options = options || {};
- if ('string' == typeof val) return parse(val);
- return options.long
- ? long(val)
- : short(val);
+ 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)
+ );
};
/**
@@ -38,13 +45,23 @@ module.exports = function(val, options){
*/
function parse(str) {
- var match = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)?$/i.exec(str);
- if (!match) return;
+ str = String(str);
+ if (str.length > 100) {
+ 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;
case 'days':
@@ -53,18 +70,30 @@ function parse(str) {
return n * d;
case 'hours':
case 'hour':
+ case 'hrs':
+ case 'hr':
case 'h':
return n * h;
case 'minutes':
case 'minute':
+ case 'mins':
+ case 'min':
case 'm':
return n * m;
case 'seconds':
case 'second':
+ case 'secs':
+ case 'sec':
case 's':
return n * s;
+ case 'milliseconds':
+ case 'millisecond':
+ case 'msecs':
+ case 'msec':
case 'ms':
return n;
+ default:
+ return undefined;
}
}
@@ -76,11 +105,19 @@ function parse(str) {
* @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';
+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';
}
@@ -92,12 +129,12 @@ function short(ms) {
* @api private
*/
-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';
}
/**
@@ -105,7 +142,11 @@ function long(ms) {
*/
function plural(ms, n, name) {
- if (ms < n) return;
- if (ms < n * 1.5) return Math.floor(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';
}
diff --git a/license.md b/license.md
new file mode 100644
index 0000000..69b6125
--- /dev/null
+++ b/license.md
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2016 Zeit, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/package.json b/package.json
index f21f90f..6a31c81 100644
--- a/package.json
+++ b/package.json
@@ -1,20 +1,37 @@
{
"name": "ms",
- "version": "0.6.2",
- "description": "Tiny ms conversion utility",
- "repository": {
- "type": "git",
- "url": "git://github.com/guille/ms.js.git"
- },
+ "version": "2.0.0",
+ "description": "Tiny milisecond conversion utility",
+ "repository": "zeit/ms",
"main": "./index",
- "devDependencies": {
- "mocha": "*",
- "expect.js": "*",
- "serve": "*"
+ "files": [
+ "index.js"
+ ],
+ "scripts": {
+ "precommit": "lint-staged",
+ "lint": "eslint lib/* bin/*",
+ "test": "mocha tests.js"
},
- "component": {
- "scripts": {
- "ms/index.js": "index.js"
+ "eslintConfig": {
+ "extends": "eslint:recommended",
+ "env": {
+ "node": true,
+ "es6": true
}
+ },
+ "lint-staged": {
+ "*.js": [
+ "npm run lint",
+ "prettier --single-quote --write",
+ "git add"
+ ]
+ },
+ "license": "MIT",
+ "devDependencies": {
+ "eslint": "3.19.0",
+ "expect.js": "0.3.1",
+ "husky": "0.13.3",
+ "lint-staged": "3.4.1",
+ "mocha": "3.4.1"
}
}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..84a9974
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,51 @@
+# ms
+
+[![Build Status](https://travis-ci.org/zeit/ms.svg?branch=master)](https://travis-ci.org/zeit/ms)
+[![Slack Channel](http://zeit-slackin.now.sh/badge.svg)](https://zeit.chat/)
+
+Use this package to easily convert various time formats to milliseconds.
+
+## Examples
+
+```js
+ms('2 days') // 172800000
+ms('1d') // 86400000
+ms('10h') // 36000000
+ms('2.5 hrs') // 9000000
+ms('2h') // 7200000
+ms('1m') // 60000
+ms('5s') // 5000
+ms('1y') // 31557600000
+ms('100') // 100
+```
+
+### Convert from milliseconds
+
+```js
+ms(60000) // "1m"
+ms(2 * 60000) // "2m"
+ms(ms('10 hours')) // "10h"
+```
+
+### Time format written-out
+
+```js
+ms(60000, { long: true }) // "1 minute"
+ms(2 * 60000, { long: true }) // "2 minutes"
+ms(ms('10 hours'), { long: true }) // "10 hours"
+```
+
+## 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.
+
+## Caught a bug?
+
+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`
diff --git a/test/test.js b/test/test.js
deleted file mode 100644
index 653b3d0..0000000
--- a/test/test.js
+++ /dev/null
@@ -1,121 +0,0 @@
-
-/**
- * Dependencies.
- */
-
-if ('undefined' != typeof require) {
- expect = require('expect.js');
- ms = require('../');
-}
-
-// strings
-
-describe('ms(string)', function(){
- it('should preserve ms', function () {
- expect(ms('100')).to.be(100);
- });
-
- it('should convert from m to ms', function () {
- expect(ms('1m')).to.be(60000);
- });
-
- it('should convert from h to ms', function () {
- expect(ms('1h')).to.be(3600000);
- });
-
- it('should convert d to ms', function () {
- expect(ms('2d')).to.be(172800000);
- });
-
- it('should convert s to ms', function () {
- expect(ms('1s')).to.be(1000);
- });
-
- it('should convert ms to ms', function () {
- expect(ms('100ms')).to.be(100);
- });
-
- it('should work with decimals', function () {
- expect(ms('1.5h')).to.be(5400000);
- });
-
- it('should return NaN if invalid', function () {
- expect(isNaN(ms('☃'))).to.be(true);
- });
-
- it('should be case-insensitive', function () {
- expect(ms('1.5H')).to.be(5400000);
- });
-
- it('should work with numbers starting with .', function () {
- expect(ms('.5ms')).to.be(.5);
- });
-})
-
-// numbers
-
-describe('ms(number, { long: true })', function(){
- it('should support milliseconds', function(){
- expect(ms(500, { long: true })).to.be('500 ms');
- })
-
- it('should support seconds', function(){
- expect(ms(1000, { long: true })).to.be('1 second');
- expect(ms(1200, { long: true })).to.be('1 second');
- expect(ms(10000, { long: true })).to.be('10 seconds');
- })
-
- it('should support minutes', function(){
- expect(ms(60 * 1000, { long: true })).to.be('1 minute');
- expect(ms(60 * 1200, { long: true })).to.be('1 minute');
- expect(ms(60 * 10000, { long: true })).to.be('10 minutes');
- })
-
- it('should support hours', function(){
- expect(ms(60 * 60 * 1000, { long: true })).to.be('1 hour');
- expect(ms(60 * 60 * 1200, { long: true })).to.be('1 hour');
- expect(ms(60 * 60 * 10000, { long: true })).to.be('10 hours');
- })
-
- it('should support days', function(){
- expect(ms(24 * 60 * 60 * 1000, { long: true })).to.be('1 day');
- expect(ms(24 * 60 * 60 * 1200, { long: true })).to.be('1 day');
- expect(ms(24 * 60 * 60 * 10000, { long: true })).to.be('10 days');
- })
-
- it('should round', function(){
- expect(ms(234234234, { long: true })).to.be('3 days');
- })
-})
-
-// numbers
-
-describe('ms(number)', function(){
- it('should support milliseconds', function(){
- expect(ms(500)).to.be('500ms');
- })
-
- it('should support seconds', function(){
- expect(ms(1000)).to.be('1s');
- expect(ms(10000)).to.be('10s');
- })
-
- it('should support minutes', function(){
- expect(ms(60 * 1000)).to.be('1m');
- expect(ms(60 * 10000)).to.be('10m');
- })
-
- it('should support hours', function(){
- expect(ms(60 * 60 * 1000)).to.be('1h');
- expect(ms(60 * 60 * 10000)).to.be('10h');
- })
-
- it('should support days', function(){
- expect(ms(24 * 60 * 60 * 1000)).to.be('1d');
- expect(ms(24 * 60 * 60 * 10000)).to.be('10d');
- })
-
- it('should round', function(){
- expect(ms(234234234)).to.be('3d');
- })
-})
diff --git a/tests.js b/tests.js
new file mode 100644
index 0000000..9b437f4
--- /dev/null
+++ b/tests.js
@@ -0,0 +1,221 @@
+/* eslint-disable no-undef */
+/**
+ * Dependencies.
+ */
+
+if (typeof require !== 'undefined') {
+ expect = require('expect.js');
+ ms = require('./');
+}
+
+// strings
+
+describe('ms(string)', function() {
+ it('should not throw an error', function() {
+ expect(function() {
+ ms('1m');
+ }).to.not.throwError();
+ });
+
+ it('should preserve ms', function() {
+ expect(ms('100')).to.be(100);
+ });
+
+ it('should convert from m to ms', function() {
+ expect(ms('1m')).to.be(60000);
+ });
+
+ it('should convert from h to ms', function() {
+ expect(ms('1h')).to.be(3600000);
+ });
+
+ it('should convert d to ms', function() {
+ expect(ms('2d')).to.be(172800000);
+ });
+
+ it('should convert s to ms', function() {
+ expect(ms('1s')).to.be(1000);
+ });
+
+ it('should convert ms to ms', function() {
+ expect(ms('100ms')).to.be(100);
+ });
+
+ it('should work with decimals', function() {
+ expect(ms('1.5h')).to.be(5400000);
+ });
+
+ it('should work with multiple spaces', function() {
+ expect(ms('1 s')).to.be(1000);
+ });
+
+ it('should return NaN if invalid', function() {
+ expect(isNaN(ms('☃'))).to.be(true);
+ });
+
+ it('should be case-insensitive', function() {
+ expect(ms('1.5H')).to.be(5400000);
+ });
+
+ it('should work with numbers starting with .', function() {
+ expect(ms('.5ms')).to.be(0.5);
+ });
+});
+
+// long strings
+
+describe('ms(long string)', function() {
+ it('should not throw an error', function() {
+ expect(function() {
+ ms('53 milliseconds');
+ }).to.not.throwError();
+ });
+
+ it('should convert milliseconds to ms', function() {
+ expect(ms('53 milliseconds')).to.be(53);
+ });
+
+ it('should convert msecs to ms', function() {
+ expect(ms('17 msecs')).to.be(17);
+ });
+
+ it('should convert sec to ms', function() {
+ expect(ms('1 sec')).to.be(1000);
+ });
+
+ it('should convert from min to ms', function() {
+ expect(ms('1 min')).to.be(60000);
+ });
+
+ it('should convert from hr to ms', function() {
+ expect(ms('1 hr')).to.be(3600000);
+ });
+
+ it('should convert days to ms', function() {
+ expect(ms('2 days')).to.be(172800000);
+ });
+
+ it('should work with decimals', function() {
+ expect(ms('1.5 hours')).to.be(5400000);
+ });
+});
+
+// numbers
+
+describe('ms(number, { long: true })', function() {
+ it('should not throw an error', function() {
+ expect(function() {
+ ms(500, { long: true });
+ }).to.not.throwError();
+ });
+
+ it('should support milliseconds', function() {
+ expect(ms(500, { long: true })).to.be('500 ms');
+ });
+
+ it('should support seconds', function() {
+ expect(ms(1000, { long: true })).to.be('1 second');
+ expect(ms(1200, { long: true })).to.be('1 second');
+ expect(ms(10000, { long: true })).to.be('10 seconds');
+ });
+
+ it('should support minutes', function() {
+ expect(ms(60 * 1000, { long: true })).to.be('1 minute');
+ expect(ms(60 * 1200, { long: true })).to.be('1 minute');
+ expect(ms(60 * 10000, { long: true })).to.be('10 minutes');
+ });
+
+ it('should support hours', function() {
+ expect(ms(60 * 60 * 1000, { long: true })).to.be('1 hour');
+ expect(ms(60 * 60 * 1200, { long: true })).to.be('1 hour');
+ expect(ms(60 * 60 * 10000, { long: true })).to.be('10 hours');
+ });
+
+ it('should support days', function() {
+ expect(ms(24 * 60 * 60 * 1000, { long: true })).to.be('1 day');
+ expect(ms(24 * 60 * 60 * 1200, { long: true })).to.be('1 day');
+ expect(ms(24 * 60 * 60 * 10000, { long: true })).to.be('10 days');
+ });
+
+ it('should round', function() {
+ expect(ms(234234234, { long: true })).to.be('3 days');
+ });
+});
+
+// numbers
+
+describe('ms(number)', function() {
+ it('should not throw an error', function() {
+ expect(function() {
+ ms(500);
+ }).to.not.throwError();
+ });
+
+ it('should support milliseconds', function() {
+ expect(ms(500)).to.be('500ms');
+ });
+
+ it('should support seconds', function() {
+ expect(ms(1000)).to.be('1s');
+ expect(ms(10000)).to.be('10s');
+ });
+
+ it('should support minutes', function() {
+ expect(ms(60 * 1000)).to.be('1m');
+ expect(ms(60 * 10000)).to.be('10m');
+ });
+
+ it('should support hours', function() {
+ expect(ms(60 * 60 * 1000)).to.be('1h');
+ expect(ms(60 * 60 * 10000)).to.be('10h');
+ });
+
+ it('should support days', function() {
+ expect(ms(24 * 60 * 60 * 1000)).to.be('1d');
+ expect(ms(24 * 60 * 60 * 10000)).to.be('10d');
+ });
+
+ it('should round', function() {
+ expect(ms(234234234)).to.be('3d');
+ });
+});
+
+// invalid inputs
+
+describe('ms(invalid inputs)', function() {
+ it('should throw an error, when ms("")', function() {
+ expect(function() {
+ ms('');
+ }).to.throwError();
+ });
+
+ it('should throw an error, when ms(undefined)', function() {
+ expect(function() {
+ ms(undefined);
+ }).to.throwError();
+ });
+
+ it('should throw an error, when ms(null)', function() {
+ expect(function() {
+ ms(null);
+ }).to.throwError();
+ });
+
+ it('should throw an error, when ms([])', function() {
+ expect(function() {
+ ms([]);
+ }).to.throwError();
+ });
+
+ it('should throw an error, when ms({})', function() {
+ expect(function() {
+ ms({});
+ }).to.throwError();
+ });
+
+ it('should throw an error, when ms(NaN)', function() {
+ expect(function() {
+ ms(NaN);
+ }).to.throwError();
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-ms.git
More information about the Pkg-javascript-commits
mailing list