Skip to content

Commit

Permalink
throw an error if the val is invalid (#52)
Browse files Browse the repository at this point in the history
* throw an error if the val is invalid

- give an error response to developers immediately, when calling `ms(null)` or `ms(undefined)` or other invalid things.
- to help developers avoid mistakes, and less debug.

* add test cases
  • Loading branch information
adoyle-h authored and leo committed Oct 5, 2016
1 parent 71616a6 commit a04b906
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ var y = d * 365.25;
*
* @param {String|Number} val
* @param {Object} options
* @throws {Error} throw an error if val is not a non-empty string or a number
* @return {String|Number}
* @api public
*/

module.exports = function(val, options){
options = options || {};
if ('string' == typeof val) return parse(val);
return options['long']
? fmtLong(val)
: fmtShort(val);
var type = typeof val;
if ('string' === type && val.length > 0) {
return parse(val);
} else if ('number' === type && isNaN(val) === false) {
return options['long']
? fmtLong(val)
: fmtShort(val);
} else {
throw new Error('val is not a non-empty string or a valid number. val=' + JSON.stringify(val));
}
};

/**
Expand Down
65 changes: 65 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ if ('undefined' != typeof require) {
// strings

describe('ms(string)', function(){
it('should not throw an error', function() {
expect(function() {
ms('1m');
}).to.not.throwError();
});

it('should preserve ms', function () {
expect(ms('100')).to.be(100);
});
Expand Down Expand Up @@ -59,6 +65,12 @@ describe('ms(string)', function(){
// long strings

describe('ms(long string)', function(){
it('should not throw an error', function() {
expect(function() {
ms('53 milliseconds');
}).to.not.throwError();
});

it('should convert milliseconds to ms', function () {
expect(ms('53 milliseconds')).to.be(53);
});
Expand Down Expand Up @@ -91,6 +103,12 @@ describe('ms(long string)', function(){
// numbers

describe('ms(number, { long: true })', function(){
it('should not throw an error', function() {
expect(function() {
ms(500, { long: true });
}).to.not.throwError();
});

it('should support milliseconds', function(){
expect(ms(500, { long: true })).to.be('500 ms');
})
Expand Down Expand Up @@ -127,6 +145,12 @@ describe('ms(number, { long: true })', function(){
// numbers

describe('ms(number)', function(){
it('should not throw an error', function() {
expect(function() {
ms(500);
}).to.not.throwError();
});

it('should support milliseconds', function(){
expect(ms(500)).to.be('500ms');
})
Expand Down Expand Up @@ -155,3 +179,44 @@ describe('ms(number)', function(){
expect(ms(234234234)).to.be('3d');
})
})


// invalid inputs

describe('ms(invalid inputs)', function() {
it('should throw an error, when ms("")', function() {
expect(function() {
ms('');
}).to.throwError();
});

it('should throw an error, when ms(undefined)', function() {
expect(function() {
ms(undefined);
}).to.throwError();
});

it('should throw an error, when ms(null)', function() {
expect(function() {
ms(null);
}).to.throwError();
});

it('should throw an error, when ms([])', function() {
expect(function() {
ms([]);
}).to.throwError();
});

it('should throw an error, when ms({})', function() {
expect(function() {
ms({});
}).to.throwError();
});

it('should throw an error, when ms(NaN)', function() {
expect(function() {
ms(NaN);
}).to.throwError();
});
});

0 comments on commit a04b906

Please sign in to comment.