Skip to content

Commit 7437f92

Browse files
ballomudleo
authored andcommitted
Add full support for negative numbers (#104)
1 parent 845c302 commit 7437f92

File tree

3 files changed

+85
-23
lines changed

3 files changed

+85
-23
lines changed

index.js

+22-19
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,17 @@ function parse(str) {
111111
*/
112112

113113
function fmtShort(ms) {
114-
if (ms >= d) {
114+
var msAbs = Math.abs(ms);
115+
if (msAbs >= d) {
115116
return Math.round(ms / d) + 'd';
116117
}
117-
if (ms >= h) {
118+
if (msAbs >= h) {
118119
return Math.round(ms / h) + 'h';
119120
}
120-
if (ms >= m) {
121+
if (msAbs >= m) {
121122
return Math.round(ms / m) + 'm';
122123
}
123-
if (ms >= s) {
124+
if (msAbs >= s) {
124125
return Math.round(ms / s) + 's';
125126
}
126127
return ms + 'ms';
@@ -135,25 +136,27 @@ function fmtShort(ms) {
135136
*/
136137

137138
function fmtLong(ms) {
138-
return (
139-
plural(ms, d, 'day') ||
140-
plural(ms, h, 'hour') ||
141-
plural(ms, m, 'minute') ||
142-
plural(ms, s, 'second') ||
143-
ms + ' ms'
144-
);
139+
var msAbs = Math.abs(ms);
140+
if (msAbs >= d) {
141+
return plural(ms, msAbs, d, 'day');
142+
}
143+
if (msAbs >= h) {
144+
return plural(ms, msAbs, h, 'hour');
145+
}
146+
if (msAbs >= m) {
147+
return plural(ms, msAbs, m, 'minute');
148+
}
149+
if (msAbs >= s) {
150+
return plural(ms, msAbs, s, 'second');
151+
}
152+
return ms + ' ms';
145153
}
146154

147155
/**
148156
* Pluralization helper.
149157
*/
150158

151-
function plural(ms, n, name) {
152-
if (ms < n) {
153-
return;
154-
}
155-
if (ms < n * 1.5) {
156-
return Math.floor(ms / n) + ' ' + name;
157-
}
158-
return Math.ceil(ms / n) + ' ' + name + 's';
159+
function plural(ms, msAbs, n, name) {
160+
var isPlural = msAbs >= n * 1.5;
161+
return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
159162
}

readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ ms('1m') // 60000
1717
ms('5s') // 5000
1818
ms('1y') // 31557600000
1919
ms('100') // 100
20+
ms('-3 days') // -259200000
21+
ms('-1h') // -3600000
22+
ms('-200') // -200
2023
```
2124

2225
### Convert from Milliseconds
2326

2427
```js
2528
ms(60000) // "1m"
2629
ms(2 * 60000) // "2m"
30+
ms(-3 * 60000) // "-3m"
2731
ms(ms('10 hours')) // "10h"
2832
```
2933

@@ -32,6 +36,7 @@ ms(ms('10 hours')) // "10h"
3236
```js
3337
ms(60000, { long: true }) // "1 minute"
3438
ms(2 * 60000, { long: true }) // "2 minutes"
39+
ms(-3 * 60000, { long: true }) // "-3 minutes"
3540
ms(ms('10 hours'), { long: true }) // "10 hours"
3641
```
3742

tests.js

+58-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ describe('ms(string)', function() {
6565
expect(ms('.5ms')).to.be(0.5);
6666
});
6767

68-
it('should work with numbers starting with -', function() {
69-
expect(ms('-5')).to.be(-5);
70-
expect(ms('-.5ms')).to.be(-0.5);
71-
expect(ms('-0.5ms')).to.be(-0.5);
68+
it('should work with negative integers', function() {
69+
expect(ms('-100ms')).to.be(-100);
70+
});
71+
72+
it('should work with negative decimals', function() {
73+
expect(ms('-1.5h')).to.be(-5400000);
74+
});
75+
76+
it('should work with negative decimals starting with "."', function() {
77+
expect(ms('-.5h')).to.be(-1800000);
7278
});
7379
});
7480

@@ -108,6 +114,18 @@ describe('ms(long string)', function() {
108114
it('should work with decimals', function() {
109115
expect(ms('1.5 hours')).to.be(5400000);
110116
});
117+
118+
it('should work with negative integers', function() {
119+
expect(ms('-100 milliseconds')).to.be(-100);
120+
});
121+
122+
it('should work with negative decimals', function() {
123+
expect(ms('-1.5 hours')).to.be(-5400000);
124+
});
125+
126+
it('should work with negative decimals starting with "."', function() {
127+
expect(ms('-.5 hr')).to.be(-1800000);
128+
});
111129
});
112130

113131
// numbers
@@ -121,34 +139,54 @@ describe('ms(number, { long: true })', function() {
121139

122140
it('should support milliseconds', function() {
123141
expect(ms(500, { long: true })).to.be('500 ms');
142+
143+
expect(ms(-500, { long: true })).to.be('-500 ms');
124144
});
125145

126146
it('should support seconds', function() {
127147
expect(ms(1000, { long: true })).to.be('1 second');
128148
expect(ms(1200, { long: true })).to.be('1 second');
129149
expect(ms(10000, { long: true })).to.be('10 seconds');
150+
151+
expect(ms(-1000, { long: true })).to.be('-1 second');
152+
expect(ms(-1200, { long: true })).to.be('-1 second');
153+
expect(ms(-10000, { long: true })).to.be('-10 seconds');
130154
});
131155

132156
it('should support minutes', function() {
133157
expect(ms(60 * 1000, { long: true })).to.be('1 minute');
134158
expect(ms(60 * 1200, { long: true })).to.be('1 minute');
135159
expect(ms(60 * 10000, { long: true })).to.be('10 minutes');
160+
161+
expect(ms(-1 * 60 * 1000, { long: true })).to.be('-1 minute');
162+
expect(ms(-1 * 60 * 1200, { long: true })).to.be('-1 minute');
163+
expect(ms(-1 * 60 * 10000, { long: true })).to.be('-10 minutes');
136164
});
137165

138166
it('should support hours', function() {
139167
expect(ms(60 * 60 * 1000, { long: true })).to.be('1 hour');
140168
expect(ms(60 * 60 * 1200, { long: true })).to.be('1 hour');
141169
expect(ms(60 * 60 * 10000, { long: true })).to.be('10 hours');
170+
171+
expect(ms(-1 * 60 * 60 * 1000, { long: true })).to.be('-1 hour');
172+
expect(ms(-1 * 60 * 60 * 1200, { long: true })).to.be('-1 hour');
173+
expect(ms(-1 * 60 * 60 * 10000, { long: true })).to.be('-10 hours');
142174
});
143175

144176
it('should support days', function() {
145177
expect(ms(24 * 60 * 60 * 1000, { long: true })).to.be('1 day');
146178
expect(ms(24 * 60 * 60 * 1200, { long: true })).to.be('1 day');
147179
expect(ms(24 * 60 * 60 * 10000, { long: true })).to.be('10 days');
180+
181+
expect(ms(-1 * 24 * 60 * 60 * 1000, { long: true })).to.be('-1 day');
182+
expect(ms(-1 * 24 * 60 * 60 * 1200, { long: true })).to.be('-1 day');
183+
expect(ms(-1 * 24 * 60 * 60 * 10000, { long: true })).to.be('-10 days');
148184
});
149185

150186
it('should round', function() {
151187
expect(ms(234234234, { long: true })).to.be('3 days');
188+
189+
expect(ms(-234234234, { long: true })).to.be('-3 days');
152190
});
153191
});
154192

@@ -163,30 +201,46 @@ describe('ms(number)', function() {
163201

164202
it('should support milliseconds', function() {
165203
expect(ms(500)).to.be('500ms');
204+
205+
expect(ms(-500)).to.be('-500ms');
166206
});
167207

168208
it('should support seconds', function() {
169209
expect(ms(1000)).to.be('1s');
170210
expect(ms(10000)).to.be('10s');
211+
212+
expect(ms(-1000)).to.be('-1s');
213+
expect(ms(-10000)).to.be('-10s');
171214
});
172215

173216
it('should support minutes', function() {
174217
expect(ms(60 * 1000)).to.be('1m');
175218
expect(ms(60 * 10000)).to.be('10m');
219+
220+
expect(ms(-1 * 60 * 1000)).to.be('-1m');
221+
expect(ms(-1 * 60 * 10000)).to.be('-10m');
176222
});
177223

178224
it('should support hours', function() {
179225
expect(ms(60 * 60 * 1000)).to.be('1h');
180226
expect(ms(60 * 60 * 10000)).to.be('10h');
227+
228+
expect(ms(-1 * 60 * 60 * 1000)).to.be('-1h');
229+
expect(ms(-1 * 60 * 60 * 10000)).to.be('-10h');
181230
});
182231

183232
it('should support days', function() {
184233
expect(ms(24 * 60 * 60 * 1000)).to.be('1d');
185234
expect(ms(24 * 60 * 60 * 10000)).to.be('10d');
235+
236+
expect(ms(-1 * 24 * 60 * 60 * 1000)).to.be('-1d');
237+
expect(ms(-1 * 24 * 60 * 60 * 10000)).to.be('-10d');
186238
});
187239

188240
it('should round', function() {
189241
expect(ms(234234234)).to.be('3d');
242+
243+
expect(ms(-234234234)).to.be('-3d');
190244
});
191245
});
192246

0 commit comments

Comments
 (0)