Skip to content

Commit

Permalink
Add "week" / "w" support
Browse files Browse the repository at this point in the history
Closes #31.
  • Loading branch information
juliangruber authored and TooTallNate committed Nov 28, 2017
1 parent e359eee commit a2caead
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var w = d * 7;
var y = d * 365.25;

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ function parse(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(
var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
str
);
if (!match) {
Expand All @@ -64,6 +65,10 @@ function parse(str) {
case 'yr':
case 'y':
return n * y;
case 'weeks':
case 'week':
case 'w':
return n * w;
case 'days':
case 'day':
case 'd':
Expand Down Expand Up @@ -130,11 +135,13 @@ function fmtShort(ms) {
*/

function fmtLong(ms) {
return plural(ms, d, 'day') ||
return (
plural(ms, d, 'day') ||
plural(ms, h, 'hour') ||
plural(ms, m, 'minute') ||
plural(ms, s, 'second') ||
ms + ' ms';
ms + ' ms'
);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe('ms(string)', function() {
expect(ms('2d')).to.be(172800000);
});

it('should convert w to ms', function() {
expect(ms('3w')).to.be(1814400000);
});

it('should convert s to ms', function() {
expect(ms('1s')).to.be(1000);
});
Expand Down

0 comments on commit a2caead

Please sign in to comment.