diff --git a/.gitignore b/.gitignore index 2a531ed..22f7aa0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ # dependencies node_modules + +# logs +npm-debug.log 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 index b37faa2..ab0d211 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,7 @@ { "language": "node_js", - "node_js": "node" + "node_js": [ + "6", + "node" + ] } diff --git a/component.json b/component.json deleted file mode 100644 index 93e4d37..0000000 --- a/component.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "ms", - "repo": "zeit/ms", - "version": "0.7.1", - "description": "Tiny milisecond conversion utility", - "keywords": [ - "ms", - "parse", - "format" - ], - "scripts": [ - "index.js" - ], - "license": "MIT" -} diff --git a/index.js b/index.js index 824b37e..6a522b1 100644 --- a/index.js +++ b/index.js @@ -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`. @@ -16,24 +16,25 @@ 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) { - 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. @@ -44,53 +45,55 @@ module.exports = function (val, options) { */ function parse(str) { - str = String(str) - if (str.length > 10000) { - 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) + 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; } } @@ -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'; } /** @@ -131,7 +134,7 @@ function fmtLong(ms) { plural(ms, h, 'hour') || plural(ms, m, 'minute') || plural(ms, s, 'second') || - ms + ' ms' + ms + ' ms'; } /** @@ -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'; } diff --git a/LICENSE.md b/license.md similarity index 100% rename from LICENSE.md rename to license.md diff --git a/package.json b/package.json index 58060aa..6a31c81 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ms", - "version": "0.7.2", + "version": "2.0.0", "description": "Tiny milisecond conversion utility", "repository": "zeit/ms", "main": "./index", @@ -8,29 +8,30 @@ "index.js" ], "scripts": { - "test": "xo && mocha test/index.js", - "test-browser": "serve ./test" + "precommit": "lint-staged", + "lint": "eslint lib/* bin/*", + "test": "mocha tests.js" }, - "license": "MIT", - "devDependencies": { - "expect.js": "^0.3.1", - "mocha": "^3.0.2", - "serve": "^1.4.0", - "xo": "^0.17.0" - }, - "component": { - "scripts": { - "ms/index.js": "index.js" + "eslintConfig": { + "extends": "eslint:recommended", + "env": { + "node": true, + "es6": true } }, - "xo": { - "space": true, - "semicolon": false, - "envs": [ - "mocha" - ], - "rules": { - "complexity": 0 - } + "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 similarity index 89% rename from README.md rename to readme.md index 5b47570..84a9974 100644 --- a/README.md +++ b/readme.md @@ -1,8 +1,7 @@ # 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/) +[![Slack Channel](http://zeit-slackin.now.sh/badge.svg)](https://zeit.chat/) Use this package to easily convert various time formats to milliseconds. diff --git a/test/index.js b/test/index.js deleted file mode 100644 index 31b23f9..0000000 --- a/test/index.js +++ /dev/null @@ -1,221 +0,0 @@ -/* 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() - }) -}) 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(); + }); +});