Skip to content

Commit

Permalink
Merge pull request #87 from mozilla/rfk/duration-integer-string
Browse files Browse the repository at this point in the history
Accept integer millisecond durations in string form, e.g. from env vars.
  • Loading branch information
zaach committed Aug 11, 2015
2 parents 27e5d43 + 1cc5e76 commit d6477df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/convict.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,14 @@ function coerce(k, v, schema) {
case 'timestamp': v = moment(v).valueOf(); break;
case 'duration':
var split = v.split(' ');
// Add an "s" as the unit of measurement used in Moment
if (!split[1].match(/s$/)) split[1] += 's';
v = moment.duration(parseInt(split[0], 10), split[1]).valueOf();
if (split.length == 1) {
// It must be an integer in string form.
v = parseInt(v, 10);
} else {
// Add an "s" as the unit of measurement used in Moment
if (!split[1].match(/s$/)) split[1] += 's';
v = moment.duration(parseInt(split[0], 10), split[1]).valueOf();
}
break;
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/format-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ describe('convict formats', function() {
format: 'duration',
default: '5 minutes'
},
duration3: {
format: 'duration',
default: '12345'
},
host: {
format: 'ipaddress',
default: '127.0.0.1'
Expand Down Expand Up @@ -134,6 +138,10 @@ describe('convict formats', function() {
it('must handle duration in a human readable string', function() {
conf.get('foo.duration2').must.be(60 * 5 * 1000);
});

it('must handle duration in milliseconds as a string', function() {
conf.get('foo.duration3').must.be(12345);
});
});

it('must throw with unknown format', function() {
Expand Down

0 comments on commit d6477df

Please sign in to comment.