Skip to content

Commit

Permalink
Add full support for negative numbers (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
ballomud authored and leo committed Nov 30, 2017
1 parent 845c302 commit 7437f92
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 23 deletions.
41 changes: 22 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,17 @@ function parse(str) {
*/

function fmtShort(ms) {
if (ms >= d) {
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return Math.round(ms / d) + 'd';
}
if (ms >= h) {
if (msAbs >= h) {
return Math.round(ms / h) + 'h';
}
if (ms >= m) {
if (msAbs >= m) {
return Math.round(ms / m) + 'm';
}
if (ms >= s) {
if (msAbs >= s) {
return Math.round(ms / s) + 's';
}
return ms + 'ms';
Expand All @@ -135,25 +136,27 @@ function fmtShort(ms) {
*/

function fmtLong(ms) {
return (
plural(ms, d, 'day') ||
plural(ms, h, 'hour') ||
plural(ms, m, 'minute') ||
plural(ms, s, 'second') ||
ms + ' ms'
);
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return plural(ms, msAbs, d, 'day');
}
if (msAbs >= h) {
return plural(ms, msAbs, h, 'hour');
}
if (msAbs >= m) {
return plural(ms, msAbs, m, 'minute');
}
if (msAbs >= s) {
return plural(ms, msAbs, s, 'second');
}
return ms + ' ms';
}

/**
* Pluralization helper.
*/

function plural(ms, n, name) {
if (ms < n) {
return;
}
if (ms < n * 1.5) {
return Math.floor(ms / n) + ' ' + name;
}
return Math.ceil(ms / n) + ' ' + name + 's';
function plural(ms, msAbs, n, name) {
var isPlural = msAbs >= n * 1.5;
return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
}
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ ms('1m') // 60000
ms('5s') // 5000
ms('1y') // 31557600000
ms('100') // 100
ms('-3 days') // -259200000
ms('-1h') // -3600000
ms('-200') // -200
```

### Convert from Milliseconds

```js
ms(60000) // "1m"
ms(2 * 60000) // "2m"
ms(-3 * 60000) // "-3m"
ms(ms('10 hours')) // "10h"
```

Expand All @@ -32,6 +36,7 @@ ms(ms('10 hours')) // "10h"
```js
ms(60000, { long: true }) // "1 minute"
ms(2 * 60000, { long: true }) // "2 minutes"
ms(-3 * 60000, { long: true }) // "-3 minutes"
ms(ms('10 hours'), { long: true }) // "10 hours"
```

Expand Down
62 changes: 58 additions & 4 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ describe('ms(string)', function() {
expect(ms('.5ms')).to.be(0.5);
});

it('should work with numbers starting with -', function() {
expect(ms('-5')).to.be(-5);
expect(ms('-.5ms')).to.be(-0.5);
expect(ms('-0.5ms')).to.be(-0.5);
it('should work with negative integers', function() {
expect(ms('-100ms')).to.be(-100);
});

it('should work with negative decimals', function() {
expect(ms('-1.5h')).to.be(-5400000);
});

it('should work with negative decimals starting with "."', function() {
expect(ms('-.5h')).to.be(-1800000);
});
});

Expand Down Expand Up @@ -108,6 +114,18 @@ describe('ms(long string)', function() {
it('should work with decimals', function() {
expect(ms('1.5 hours')).to.be(5400000);
});

it('should work with negative integers', function() {
expect(ms('-100 milliseconds')).to.be(-100);
});

it('should work with negative decimals', function() {
expect(ms('-1.5 hours')).to.be(-5400000);
});

it('should work with negative decimals starting with "."', function() {
expect(ms('-.5 hr')).to.be(-1800000);
});
});

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

it('should support milliseconds', function() {
expect(ms(500, { long: true })).to.be('500 ms');

expect(ms(-500, { long: true })).to.be('-500 ms');
});

it('should support seconds', function() {
expect(ms(1000, { long: true })).to.be('1 second');
expect(ms(1200, { long: true })).to.be('1 second');
expect(ms(10000, { long: true })).to.be('10 seconds');

expect(ms(-1000, { long: true })).to.be('-1 second');
expect(ms(-1200, { long: true })).to.be('-1 second');
expect(ms(-10000, { long: true })).to.be('-10 seconds');
});

it('should support minutes', function() {
expect(ms(60 * 1000, { long: true })).to.be('1 minute');
expect(ms(60 * 1200, { long: true })).to.be('1 minute');
expect(ms(60 * 10000, { long: true })).to.be('10 minutes');

expect(ms(-1 * 60 * 1000, { long: true })).to.be('-1 minute');
expect(ms(-1 * 60 * 1200, { long: true })).to.be('-1 minute');
expect(ms(-1 * 60 * 10000, { long: true })).to.be('-10 minutes');
});

it('should support hours', function() {
expect(ms(60 * 60 * 1000, { long: true })).to.be('1 hour');
expect(ms(60 * 60 * 1200, { long: true })).to.be('1 hour');
expect(ms(60 * 60 * 10000, { long: true })).to.be('10 hours');

expect(ms(-1 * 60 * 60 * 1000, { long: true })).to.be('-1 hour');
expect(ms(-1 * 60 * 60 * 1200, { long: true })).to.be('-1 hour');
expect(ms(-1 * 60 * 60 * 10000, { long: true })).to.be('-10 hours');
});

it('should support days', function() {
expect(ms(24 * 60 * 60 * 1000, { long: true })).to.be('1 day');
expect(ms(24 * 60 * 60 * 1200, { long: true })).to.be('1 day');
expect(ms(24 * 60 * 60 * 10000, { long: true })).to.be('10 days');

expect(ms(-1 * 24 * 60 * 60 * 1000, { long: true })).to.be('-1 day');
expect(ms(-1 * 24 * 60 * 60 * 1200, { long: true })).to.be('-1 day');
expect(ms(-1 * 24 * 60 * 60 * 10000, { long: true })).to.be('-10 days');
});

it('should round', function() {
expect(ms(234234234, { long: true })).to.be('3 days');

expect(ms(-234234234, { long: true })).to.be('-3 days');
});
});

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

it('should support milliseconds', function() {
expect(ms(500)).to.be('500ms');

expect(ms(-500)).to.be('-500ms');
});

it('should support seconds', function() {
expect(ms(1000)).to.be('1s');
expect(ms(10000)).to.be('10s');

expect(ms(-1000)).to.be('-1s');
expect(ms(-10000)).to.be('-10s');
});

it('should support minutes', function() {
expect(ms(60 * 1000)).to.be('1m');
expect(ms(60 * 10000)).to.be('10m');

expect(ms(-1 * 60 * 1000)).to.be('-1m');
expect(ms(-1 * 60 * 10000)).to.be('-10m');
});

it('should support hours', function() {
expect(ms(60 * 60 * 1000)).to.be('1h');
expect(ms(60 * 60 * 10000)).to.be('10h');

expect(ms(-1 * 60 * 60 * 1000)).to.be('-1h');
expect(ms(-1 * 60 * 60 * 10000)).to.be('-10h');
});

it('should support days', function() {
expect(ms(24 * 60 * 60 * 1000)).to.be('1d');
expect(ms(24 * 60 * 60 * 10000)).to.be('10d');

expect(ms(-1 * 24 * 60 * 60 * 1000)).to.be('-1d');
expect(ms(-1 * 24 * 60 * 60 * 10000)).to.be('-10d');
});

it('should round', function() {
expect(ms(234234234)).to.be('3d');

expect(ms(-234234234)).to.be('-3d');
});
});

Expand Down

0 comments on commit 7437f92

Please sign in to comment.