[Pkg-javascript-commits] [node-ms] 01/03: Imported Upstream version 0.6.2
Leo Iannacone
l3on-guest at moszumanska.debian.org
Sat Jul 12 10:31:27 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-ms.
commit b840fe87be8d07378051305cdac4cd7176aa1139
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Fri Jul 11 10:54:23 2014 +0200
Imported Upstream version 0.6.2
---
.npmignore | 5 +++
History.md | 53 +++++++++++++++++++++++++
Makefile | 8 ++++
README.md | 33 ++++++++++++++++
component.json | 15 +++++++
index.js | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 20 ++++++++++
test/test.js | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 366 insertions(+)
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..d1aa0ce
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,5 @@
+node_modules
+test
+History.md
+Makefile
+component.json
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..a47a1c3
--- /dev/null
+++ b/History.md
@@ -0,0 +1,53 @@
+
+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
new file mode 100644
index 0000000..0ae97b3
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+
+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
new file mode 100644
index 0000000..d4ab12a
--- /dev/null
+++ b/README.md
@@ -0,0 +1,33 @@
+# 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
new file mode 100644
index 0000000..2d7bbff
--- /dev/null
+++ b/component.json
@@ -0,0 +1,15 @@
+{
+ "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
new file mode 100644
index 0000000..c5847f8
--- /dev/null
+++ b/index.js
@@ -0,0 +1,111 @@
+/**
+ * Helpers.
+ */
+
+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`.
+ *
+ * Options:
+ *
+ * - `long` verbose formatting [false]
+ *
+ * @param {String|Number} val
+ * @param {Object} options
+ * @return {String|Number}
+ * @api public
+ */
+
+module.exports = function(val, options){
+ options = options || {};
+ if ('string' == typeof val) return parse(val);
+ return options.long
+ ? long(val)
+ : short(val);
+};
+
+/**
+ * Parse the given `str` and return milliseconds.
+ *
+ * @param {String} str
+ * @return {Number}
+ * @api private
+ */
+
+function parse(str) {
+ var match = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|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 'y':
+ return n * y;
+ case 'days':
+ case 'day':
+ case 'd':
+ return n * d;
+ case 'hours':
+ case 'hour':
+ case 'h':
+ return n * h;
+ case 'minutes':
+ case 'minute':
+ case 'm':
+ return n * m;
+ case 'seconds':
+ case 'second':
+ case 's':
+ return n * s;
+ case 'ms':
+ return n;
+ }
+}
+
+/**
+ * Short format for `ms`.
+ *
+ * @param {Number} ms
+ * @return {String}
+ * @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';
+ return ms + 'ms';
+}
+
+/**
+ * Long format for `ms`.
+ *
+ * @param {Number} ms
+ * @return {String}
+ * @api private
+ */
+
+function long(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';
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..f21f90f
--- /dev/null
+++ b/package.json
@@ -0,0 +1,20 @@
+{
+ "name": "ms",
+ "version": "0.6.2",
+ "description": "Tiny ms conversion utility",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/guille/ms.js.git"
+ },
+ "main": "./index",
+ "devDependencies": {
+ "mocha": "*",
+ "expect.js": "*",
+ "serve": "*"
+ },
+ "component": {
+ "scripts": {
+ "ms/index.js": "index.js"
+ }
+ }
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..653b3d0
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,121 @@
+
+/**
+ * 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');
+ })
+})
--
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