Skip to content

Commit

Permalink
Merge pull request #2 from unshiftio/weeks
Browse files Browse the repository at this point in the history
Add support for weeks
  • Loading branch information
3rd-Eden committed Jun 8, 2015
2 parents 64d471d + 94f09f5 commit 2eccf4f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# millisecond

[![Made by unshift][made-by]](http://unshift.io)[![Version npm][version]](http://browsenpm.org/package/millisecond)[![Build Status][build]](https://travis-ci.org/unshiftio/millisecond)[![Dependencies][david]](https://david-dm.org/unshiftio/millisecond)[![Coverage Status][cover]](https://coveralls.io/r/unshiftio/millisecond?branch=master)[![IRC channel][irc]](http://webchat.freenode.net/?channels=unshift)
[![Made by unshift][made-by]](http://unshift.io)[![Version npm][version]](http://browsenpm.org/package/millisecond)[![Build Status][build]](https://travis-ci.org/unshiftio/millisecond)[![Dependencies][david]](https://david-dm.org/unshiftio/millisecond)[![Coverage Status][cover]](https://coveralls.io/r/unshiftio/millisecond?branch=master)[![IRC channel][irc]](https://webchat.freenode.net/?channels=unshift)

[made-by]: https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square
[version]: http://img.shields.io/npm/v/millisecond.svg?style=flat-square
[build]: http://img.shields.io/travis/unshiftio/millisecond/master.svg?style=flat-square
[version]: https://img.shields.io/npm/v/millisecond.svg?style=flat-square
[build]: https://img.shields.io/travis/unshiftio/millisecond/master.svg?style=flat-square
[david]: https://img.shields.io/david/unshiftio/millisecond.svg?style=flat-square
[cover]: http://img.shields.io/coveralls/unshiftio/millisecond/master.svg?style=flat-square
[irc]: http://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square
[cover]: https://img.shields.io/coveralls/unshiftio/millisecond/master.svg?style=flat-square
[irc]: https://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square

Parse strings that indicate a time to their millisecond equivalents.

Expand Down Expand Up @@ -65,6 +65,11 @@ It understands the following strings:
- `x days`
- `x day`
- `x d`
- `x weeks`
- `x week`
- `x wks`
- `x wk`
- `x w`
- `x years`
- `x year`
- `x yrs`
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ module.exports = function millisecond(ms) {

if ('string' !== typeof ms || '0' === ms || +ms) return +ms;

var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(ms)
var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|wks?|w|years?|yrs?|y)?$/i.exec(ms)
, second = 1000
, minute = second * 60
, hour = minute * 60
, day = hour * 24
, week = day * 7
, year = day * 365
, amount;

Expand All @@ -30,6 +31,13 @@ module.exports = function millisecond(ms) {
case 'y':
return amount * year;

case 'weeks':
case 'week':
case 'wks':
case 'wk':
case 'w':
return amount * week;

case 'days':
case 'day':
case 'd':
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ describe('millisecond', function () {
assume(ms('2days')).to.equal(172800000);
});

it('should convert w to ms', function () {
assume(ms('1w')).to.equal(604800000);
assume(ms('1wk')).to.equal(604800000);
assume(ms('1wks')).to.equal(604800000);
assume(ms('1week')).to.equal(604800000);
assume(ms('1weeks')).to.equal(604800000);
});

it('should convert y to ms', function () {
assume(ms('1y')).to.equal(31536000000);
assume(ms('1yr')).to.equal(31536000000);
Expand Down

0 comments on commit 2eccf4f

Please sign in to comment.