Comparing version 0.1.0 to 0.2.0
100
ms.js
@@ -0,35 +1,81 @@ | ||
/** | ||
* Helpers. | ||
*/ | ||
# ms.js | ||
var s = 1000; | ||
var m = s * 60; | ||
var h = m * 60; | ||
var d = h * 24; | ||
No more painful `setTimeout(fn, 60 * 4 * 3 * 2 * 1 * Infinity * NaN * '☃')`. | ||
/** | ||
* Parse or format the given `val`. | ||
* | ||
* @param {String|Number} val | ||
* @return {String|Number} | ||
* @api public | ||
*/ | ||
ms('2d') // 172800000 | ||
ms('1.5h') // 5400000 | ||
ms('1h') // 3600000 | ||
ms('1m') // 60000 | ||
ms('5s') // 5000 | ||
ms('500ms') // 500 | ||
ms('100') // '100' | ||
ms(100) // 100 | ||
module.exports = function(val){ | ||
if ('string' == typeof val) return parse(val); | ||
return format(val); | ||
} | ||
**/ | ||
/** | ||
* Parse the given `str` and return milliseconds. | ||
* | ||
* @param {String} str | ||
* @return {Number} | ||
* @api private | ||
*/ | ||
(function (g) { | ||
var r = /(\d*.?\d+)([mshd]+)/ | ||
, _ = {} | ||
function parse(str) { | ||
var m = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)$/i.exec(str); | ||
if (!m) return; | ||
var n = parseFloat(m[1]); | ||
var type = m[2].toLowerCase(); | ||
switch (type) { | ||
case 'years': | ||
case 'year': | ||
case 'y': | ||
return n * 31557600000; | ||
case 'days': | ||
case 'day': | ||
case 'd': | ||
return n * 86400000; | ||
case 'hours': | ||
case 'hour': | ||
case 'h': | ||
return n * 3600000; | ||
case 'minutes': | ||
case 'minute': | ||
case 'm': | ||
return n * 60000; | ||
case 'seconds': | ||
case 'second': | ||
case 's': | ||
return n * 1000; | ||
case 'ms': | ||
return n; | ||
} | ||
} | ||
_.ms = 1; | ||
_.s = 1000; | ||
_.m = _.s * 60; | ||
_.h = _.m * 60; | ||
_.d = _.h * 24; | ||
/** | ||
* Format the given `ms`. | ||
* | ||
* @param {Number} ms | ||
* @return {String} | ||
* @api public | ||
*/ | ||
function ms (s) { | ||
if (s == Number(s)) return Number(s); | ||
r.exec(s.toLowerCase()); | ||
return RegExp.$1 * _[RegExp.$2]; | ||
} | ||
g.top ? g.ms = ms : module.exports = ms; | ||
})(this); | ||
function format(ms) { | ||
if (ms == d) return (ms / d) + ' day'; | ||
if (ms > d) return (ms / d) + ' days'; | ||
if (ms == h) return (ms / h) + ' hour'; | ||
if (ms > h) return (ms / h) + ' hours'; | ||
if (ms == m) return (ms / m) + ' minute'; | ||
if (ms > m) return (ms / m) + ' minutes'; | ||
if (ms == s) return (ms / s) + ' second'; | ||
if (ms > s) return (ms / s) + ' seconds'; | ||
return ms + ' ms'; | ||
} |
{ | ||
"name": "ms" | ||
, "version": "0.1.0" | ||
, "version": "0.2.0" | ||
, "description": "Tiny ms conversion utility" | ||
@@ -5,0 +5,0 @@ , "main": "./ms" |
@@ -0,18 +1,3 @@ | ||
# ms.js: miliseconds conversion utility | ||
# ms.js | ||
Ever find yourself doing math in your head or writing `1000 * 60 * 60 …`? | ||
Don't want to add obstrusive `Number` prototype extensions to your reusable | ||
/ distributable modules and projects? | ||
`ms` is a tiny utility that you can leverage when your application needs to | ||
accept a number of miliseconds as a parameter. | ||
If a number is supplied to `ms`, it returns it immediately (e.g: | ||
If a string that contains the number is supplied, it returns it immediately as | ||
a number (e.g: it returns `100` for `'100'`). | ||
However, if you pass a string with a number and a valid unit, hte number of | ||
equivalent ms is returned. | ||
```js | ||
@@ -23,44 +8,18 @@ ms('1d') // 86400000 | ||
ms('1m') // 60000 | ||
ms('5ms') // 5000 | ||
ms('100') // '100' | ||
ms('5s') // 5000 | ||
ms('100') // 100 | ||
ms(100) // 100 | ||
``` | ||
## How to use | ||
### Node | ||
```js | ||
require('ms') | ||
ms(60000) // "1 minute" | ||
ms(2 * 60000) // "2 minutes" | ||
ms(ms('10 hours')) // "10 hours" | ||
``` | ||
### Browser | ||
```html | ||
<script src="ms.js"></script> | ||
``` | ||
## Credits | ||
(The MIT License) | ||
Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com> | ||
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. | ||
- Node/Browser compatible. Published as `ms` in NPM. | ||
- If a number is supplied to `ms`, it returns it immediately. | ||
- 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. |
@@ -11,8 +11,5 @@ | ||
/** | ||
* Test. | ||
*/ | ||
// strings | ||
describe('ms.js', function () { | ||
describe('ms(string)', function(){ | ||
it('should preserve ms', function () { | ||
@@ -22,6 +19,2 @@ expect(100).to.be(100); | ||
it('should convert number strings to number', function () { | ||
expect(ms('1e+5')).to.be(1e+5); | ||
}); | ||
it('should convert from m to ms', function () { | ||
@@ -62,3 +55,34 @@ expect(ms('1m')).to.be(60000); | ||
}); | ||
}) | ||
}); | ||
// numbers | ||
describe('ms(number)', function(){ | ||
it('should support milliseconds', function(){ | ||
expect(ms(500)).to.be('500 ms'); | ||
}) | ||
it('should support seconds', function(){ | ||
expect(ms(1000)).to.be('1 second'); | ||
expect(ms(1500)).to.be('1.5 seconds'); | ||
expect(ms(10000)).to.be('10 seconds'); | ||
}) | ||
it('should support minutes', function(){ | ||
expect(ms(60 * 1000)).to.be('1 minute'); | ||
expect(ms(60 * 1500)).to.be('1.5 minutes'); | ||
expect(ms(60 * 10000)).to.be('10 minutes'); | ||
}) | ||
it('should support hours', function(){ | ||
expect(ms(60 * 60 * 1000)).to.be('1 hour'); | ||
expect(ms(60 * 60 * 1500)).to.be('1.5 hours'); | ||
expect(ms(60 * 60 * 10000)).to.be('10 hours'); | ||
}) | ||
it('should support days', function(){ | ||
expect(ms(24 * 60 * 60 * 1000)).to.be('1 day'); | ||
expect(ms(24 * 60 * 60 * 1500)).to.be('1.5 days'); | ||
expect(ms(24 * 60 * 60 * 10000)).to.be('10 days'); | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
253794
10
7779
25