Skip to content

Commit

Permalink
Add unitCount option (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackw01 authored and sindresorhus committed Sep 18, 2018
1 parent 0950e70 commit 4717127
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,9 @@ module.exports = (ms, options = {}) => {
return '~' + ret[0];
}

if (typeof options.unitCount === 'number') {
return '~' + ret.slice(0, Math.max(options.unitCount, 1)).join(' ');
}

return ret.join(' ');
};
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ Default: `false`
Only show the first unit: `1h 10m``~1h`.
Also ensures that `msDecimalDigits` and `secDecimalDigits` are both set to `0`.

##### unitCount

Type: `number`<br>
Default: `Infinity`

Number of units to show. Setting `compact` to `true` overrides this option.

##### verbose

Type: `boolean`<br>
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ test('have a compact option', t => {
t.is(m(1000 * 60 * 67 * 24 * 465, {compact: true}), '~1y');
});

test('have a unitCount option', t => {
t.is(m(1000 * 60, {unitCount: 0}), '~1m');
t.is(m(1000 * 60, {unitCount: 1}), '~1m');
t.is(m(1000 * 60 * 67, {unitCount: 1}), '~1h');
t.is(m(1000 * 60 * 67, {unitCount: 2}), '~1h 7m');
t.is(m(1000 * 60 * 67 * 24 * 465, {unitCount: 1}), '~1y');
t.is(m(1000 * 60 * 67 * 24 * 465, {unitCount: 2}), '~1y 154d');
t.is(m(1000 * 60 * 67 * 24 * 465, {unitCount: 3}), '~1y 154d 6h');
});

test('have a secDecimalDigits option', t => {
t.is(m(10000), '10s');
t.is(m(33333), '33.3s');
Expand Down Expand Up @@ -97,6 +107,15 @@ test('work with verbose and compact options', t => {
t.is(fn(1000 * 60 * 67 * 24 * 750), '~2 years');
});

test('work with verbose and unitCount options', t => {
t.is(m(1000 * 60, {verbose: true, unitCount: 1}), '~1 minute');
t.is(m(1000 * 60 * 67, {verbose: true, unitCount: 1}), '~1 hour');
t.is(m(1000 * 60 * 67, {verbose: true, unitCount: 2}), '~1 hour 7 minutes');
t.is(m(1000 * 60 * 67 * 24 * 465, {verbose: true, unitCount: 1}), '~1 year');
t.is(m(1000 * 60 * 67 * 24 * 465, {verbose: true, unitCount: 2}), '~1 year 154 days');
t.is(m(1000 * 60 * 67 * 24 * 465, {verbose: true, unitCount: 3}), '~1 year 154 days 6 hours');
});

test('work with verbose and secDecimalDigits options', t => {
const fn = ms => m(ms, {
verbose: true,
Expand Down Expand Up @@ -130,6 +149,12 @@ test('work with verbose and formatSubMs options', t => {
t.is(m(0.001, {formatSubMs: true, verbose: true}), '1 microsecond');
});

test('compact option overrides unitCount option', t => {
t.is(m(1000 * 60 * 67 * 24 * 465, {verbose: true, compact: true, unitCount: 1}), '~1 year');
t.is(m(1000 * 60 * 67 * 24 * 465, {verbose: true, compact: true, unitCount: 2}), '~1 year');
t.is(m(1000 * 60 * 67 * 24 * 465, {verbose: true, compact: true, unitCount: 3}), '~1 year');
});

test('work with separateMs and formatSubMs options', t => {
t.is(m(1010.340067, {separateMs: true, formatSubMs: true}), '1s 10ms 340µs 67ns');
t.is(m(((60 * 1000) + 34 + 0.000005), {separateMs: true, formatSubMs: true}), '1m 34ms 5ns');
Expand Down

0 comments on commit 4717127

Please sign in to comment.