Skip to content

Commit a04b906

Browse files
adoyle-hleo
authored andcommitted
throw an error if the val is invalid (#52)
* 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
1 parent 71616a6 commit a04b906

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@ var y = d * 365.25;
1717
*
1818
* @param {String|Number} val
1919
* @param {Object} options
20+
* @throws {Error} throw an error if val is not a non-empty string or a number
2021
* @return {String|Number}
2122
* @api public
2223
*/
2324

2425
module.exports = function(val, options){
2526
options = options || {};
26-
if ('string' == typeof val) return parse(val);
27-
return options['long']
28-
? fmtLong(val)
29-
: fmtShort(val);
27+
var type = typeof val;
28+
if ('string' === type && val.length > 0) {
29+
return parse(val);
30+
} else if ('number' === type && isNaN(val) === false) {
31+
return options['long']
32+
? fmtLong(val)
33+
: fmtShort(val);
34+
} else {
35+
throw new Error('val is not a non-empty string or a valid number. val=' + JSON.stringify(val));
36+
}
3037
};
3138

3239
/**

test/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ if ('undefined' != typeof require) {
1111
// strings
1212

1313
describe('ms(string)', function(){
14+
it('should not throw an error', function() {
15+
expect(function() {
16+
ms('1m');
17+
}).to.not.throwError();
18+
});
19+
1420
it('should preserve ms', function () {
1521
expect(ms('100')).to.be(100);
1622
});
@@ -59,6 +65,12 @@ describe('ms(string)', function(){
5965
// long strings
6066

6167
describe('ms(long string)', function(){
68+
it('should not throw an error', function() {
69+
expect(function() {
70+
ms('53 milliseconds');
71+
}).to.not.throwError();
72+
});
73+
6274
it('should convert milliseconds to ms', function () {
6375
expect(ms('53 milliseconds')).to.be(53);
6476
});
@@ -91,6 +103,12 @@ describe('ms(long string)', function(){
91103
// numbers
92104

93105
describe('ms(number, { long: true })', function(){
106+
it('should not throw an error', function() {
107+
expect(function() {
108+
ms(500, { long: true });
109+
}).to.not.throwError();
110+
});
111+
94112
it('should support milliseconds', function(){
95113
expect(ms(500, { long: true })).to.be('500 ms');
96114
})
@@ -127,6 +145,12 @@ describe('ms(number, { long: true })', function(){
127145
// numbers
128146

129147
describe('ms(number)', function(){
148+
it('should not throw an error', function() {
149+
expect(function() {
150+
ms(500);
151+
}).to.not.throwError();
152+
});
153+
130154
it('should support milliseconds', function(){
131155
expect(ms(500)).to.be('500ms');
132156
})
@@ -155,3 +179,44 @@ describe('ms(number)', function(){
155179
expect(ms(234234234)).to.be('3d');
156180
})
157181
})
182+
183+
184+
// invalid inputs
185+
186+
describe('ms(invalid inputs)', function() {
187+
it('should throw an error, when ms("")', function() {
188+
expect(function() {
189+
ms('');
190+
}).to.throwError();
191+
});
192+
193+
it('should throw an error, when ms(undefined)', function() {
194+
expect(function() {
195+
ms(undefined);
196+
}).to.throwError();
197+
});
198+
199+
it('should throw an error, when ms(null)', function() {
200+
expect(function() {
201+
ms(null);
202+
}).to.throwError();
203+
});
204+
205+
it('should throw an error, when ms([])', function() {
206+
expect(function() {
207+
ms([]);
208+
}).to.throwError();
209+
});
210+
211+
it('should throw an error, when ms({})', function() {
212+
expect(function() {
213+
ms({});
214+
}).to.throwError();
215+
});
216+
217+
it('should throw an error, when ms(NaN)', function() {
218+
expect(function() {
219+
ms(NaN);
220+
}).to.throwError();
221+
});
222+
});

0 commit comments

Comments
 (0)