Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full support for negative numbers #104

Merged
merged 2 commits into from Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 22 additions & 19 deletions index.js
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
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
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