-
-
Save tubalmartin/1997544 to your computer and use it in GitHub Desktop.
Milliseconds conversion utility.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
# ms.js | |
No more painful `setTimeout(fn, 60 * 4 * 3 * 2 * 1 * Infinity * NaN * '☃')`. | |
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 | |
**/ | |
!function (g) { | |
var r = /(\d*.?\d+)([mshd]+)/ | |
, _ = {} | |
, m; | |
_.ms = 1; | |
_.s = 1000; | |
_.m = _.s * 60; | |
_.h = _.m * 60; | |
_.d = _.h * 24; | |
function ms (s) { | |
return +s || ((m = r.exec(s.toLowerCase())) ? m[1] * _[m[2]] : NaN); | |
} | |
g.top ? g.ms = ms : module.exports = ms; | |
}(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment