Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 18, 2018
1 parent 4717127 commit 1ee8417
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 73 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"parse-ms": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^0.25.0",
"xo": "^0.23.0"
}
}
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ prettyMs(1337);
prettyMs(133);
//=> '133ms'

// compact option
// `compact` option
prettyMs(1337, {compact: true});
//=> '~1s'

// verbose option
// `verbose` option
prettyMs(1335669000, {verbose: true});
//=> '15 days 11 hours 1 minute 9 seconds'

// formatSubMs option
// `formatSubMs` option
prettyMs(100.400080, {formatSubMs: true})
//=> '100ms 400µs 80ns'

// can be useful for time durations
// Can be useful for time durations
prettyMs(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5))
//=> '35m'
```
Expand Down Expand Up @@ -86,7 +86,7 @@ Useful when you are showing a number of seconds spent on an operation and don't
Type: `boolean`<br>
Default: `false`

Only show the first unit: `1h 10m``~1h`.
Only show the first unit: `1h 10m``~1h`.
Also ensures that `msDecimalDigits` and `secDecimalDigits` are both set to `0`.

##### unitCount
Expand All @@ -103,14 +103,14 @@ Default: `false`

Use full-length units: `5h 1m 45s``5 hours 1 minute 45 seconds`

#### separateMs
##### separateMs

Type: `boolean`<br>
Default: `false`

Show milliseconds separately. This means they won't be included in the decimal part of the seconds.

#### formatSubMs
##### formatSubMs

Type: `boolean`<br>
Default: `false`
Expand Down
128 changes: 64 additions & 64 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
import test from 'ava';
import m from '.';
import prettyMs from '.';

test('prettify milliseconds', t => {
t.is(m(0), '0ms');
t.is(m(0.1), '1ms');
t.is(m(1), '1ms');
t.is(m(1000 + 400), '1.4s');
t.is(m((1000 * 2) + 400), '2.4s');
t.is(m(1000 * 55), '55s');
t.is(m(1000 * 67), '1m 7s');
t.is(m(1000 * 60 * 5), '5m');
t.is(m(1000 * 60 * 67), '1h 7m');
t.is(m(1000 * 60 * 60 * 12), '12h');
t.is(m(1000 * 60 * 60 * 40), '1d 16h');
t.is(m(1000 * 60 * 60 * 999), '41d 15h');
t.is(m(1000 * 60 * 60 * 24 * 465), '1y 100d');
t.is(m(1000 * 60 * 67 * 24 * 465), '1y 154d 6h');
t.is(prettyMs(0), '0ms');
t.is(prettyMs(0.1), '1ms');
t.is(prettyMs(1), '1ms');
t.is(prettyMs(1000 + 400), '1.4s');
t.is(prettyMs((1000 * 2) + 400), '2.4s');
t.is(prettyMs(1000 * 55), '55s');
t.is(prettyMs(1000 * 67), '1m 7s');
t.is(prettyMs(1000 * 60 * 5), '5m');
t.is(prettyMs(1000 * 60 * 67), '1h 7m');
t.is(prettyMs(1000 * 60 * 60 * 12), '12h');
t.is(prettyMs(1000 * 60 * 60 * 40), '1d 16h');
t.is(prettyMs(1000 * 60 * 60 * 999), '41d 15h');
t.is(prettyMs(1000 * 60 * 60 * 24 * 465), '1y 100d');
t.is(prettyMs(1000 * 60 * 67 * 24 * 465), '1y 154d 6h');
});

test('have a compact option', t => {
t.is(m(1000 + 4, {compact: true}), '~1s');
t.is(m(1000 * 60 * 60 * 999, {compact: true}), '~41d');
t.is(m(1000 * 60 * 60 * 24 * 465, {compact: true}), '~1y');
t.is(m(1000 * 60 * 67 * 24 * 465, {compact: true}), '~1y');
t.is(prettyMs(1000 + 4, {compact: true}), '~1s');
t.is(prettyMs(1000 * 60 * 60 * 999, {compact: true}), '~41d');
t.is(prettyMs(1000 * 60 * 60 * 24 * 465, {compact: true}), '~1y');
t.is(prettyMs(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');
t.is(prettyMs(1000 * 60, {unitCount: 0}), '~1m');
t.is(prettyMs(1000 * 60, {unitCount: 1}), '~1m');
t.is(prettyMs(1000 * 60 * 67, {unitCount: 1}), '~1h');
t.is(prettyMs(1000 * 60 * 67, {unitCount: 2}), '~1h 7m');
t.is(prettyMs(1000 * 60 * 67 * 24 * 465, {unitCount: 1}), '~1y');
t.is(prettyMs(1000 * 60 * 67 * 24 * 465, {unitCount: 2}), '~1y 154d');
t.is(prettyMs(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');
t.is(m(33333, {secDecimalDigits: 0}), '33s');
t.is(m(33333, {secDecimalDigits: 4}), '33.3330s');
t.is(prettyMs(10000), '10s');
t.is(prettyMs(33333), '33.3s');
t.is(prettyMs(33333, {secDecimalDigits: 0}), '33s');
t.is(prettyMs(33333, {secDecimalDigits: 4}), '33.3330s');
});

test('have a msDecimalDigits option', t => {
t.is(m(33.333), '34ms');
t.is(m(33.333, {msDecimalDigits: 0}), '34ms');
t.is(m(33.333, {msDecimalDigits: 4}), '33.3330ms');
t.is(prettyMs(33.333), '34ms');
t.is(prettyMs(33.333, {msDecimalDigits: 0}), '34ms');
t.is(prettyMs(33.333, {msDecimalDigits: 4}), '33.3330ms');
});

test('have a keepDecimalsOnWholeSeconds option', t => {
t.is(m(1000 * 33, {secDecimalDigits: 2, keepDecimalsOnWholeSeconds: true}), '33.00s');
t.is(m(1000 * 33.00004, {secDecimalDigits: 2, keepDecimalsOnWholeSeconds: true}), '33.00s');
t.is(prettyMs(1000 * 33, {secDecimalDigits: 2, keepDecimalsOnWholeSeconds: true}), '33.00s');
t.is(prettyMs(1000 * 33.00004, {secDecimalDigits: 2, keepDecimalsOnWholeSeconds: true}), '33.00s');
});

test('have a verbose option', t => {
const fn = ms => m(ms, {verbose: true});
const fn = ms => prettyMs(ms, {verbose: true});

t.is(fn(0), '0 milliseconds');
t.is(fn(0.1), '1 millisecond');
Expand All @@ -75,19 +75,19 @@ test('have a verbose option', t => {
});

test('have a separateMs option', t => {
t.is(m(1100, {separateMs: false}), '1.1s');
t.is(m(1100, {separateMs: true}), '1s 100ms');
t.is(prettyMs(1100, {separateMs: false}), '1.1s');
t.is(prettyMs(1100, {separateMs: true}), '1s 100ms');
});

test('have a formatSubMs option', t => {
t.is(m(0.400, {formatSubMs: true}), '400µs');
t.is(m(0.123571, {formatSubMs: true}), '123µs 571ns');
t.is(m(0.123456789, {formatSubMs: true}), '123µs 456ns');
t.is(m((60 * 60 * 1000) + (23 * 1000) + 433 + 0.123456, {formatSubMs: true}), '1h 23s 433ms 123µs 456ns');
t.is(prettyMs(0.400, {formatSubMs: true}), '400µs');
t.is(prettyMs(0.123571, {formatSubMs: true}), '123µs 571ns');
t.is(prettyMs(0.123456789, {formatSubMs: true}), '123µs 456ns');
t.is(prettyMs((60 * 60 * 1000) + (23 * 1000) + 433 + 0.123456, {formatSubMs: true}), '1h 23s 433ms 123µs 456ns');
});

test('work with verbose and compact options', t => {
const fn = ms => m(ms, {
const fn = ms => prettyMs(ms, {
verbose: true,
compact: true
});
Expand All @@ -108,16 +108,16 @@ test('work with verbose and compact options', t => {
});

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');
t.is(prettyMs(1000 * 60, {verbose: true, unitCount: 1}), '~1 minute');
t.is(prettyMs(1000 * 60 * 67, {verbose: true, unitCount: 1}), '~1 hour');
t.is(prettyMs(1000 * 60 * 67, {verbose: true, unitCount: 2}), '~1 hour 7 minutes');
t.is(prettyMs(1000 * 60 * 67 * 24 * 465, {verbose: true, unitCount: 1}), '~1 year');
t.is(prettyMs(1000 * 60 * 67 * 24 * 465, {verbose: true, unitCount: 2}), '~1 year 154 days');
t.is(prettyMs(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, {
const fn = ms => prettyMs(ms, {
verbose: true,
secDecimalDigits: 4
});
Expand All @@ -130,7 +130,7 @@ test('work with verbose and secDecimalDigits options', t => {
});

test('work with verbose and msDecimalDigits options', t => {
const fn = ms => m(ms, {
const fn = ms => prettyMs(ms, {
verbose: true,
msDecimalDigits: 4
});
Expand All @@ -143,39 +143,39 @@ test('work with verbose and msDecimalDigits options', t => {
});

test('work with verbose and formatSubMs options', t => {
t.is(m(0.400, {formatSubMs: true, verbose: true}), '400 microseconds');
t.is(m(0.123571, {formatSubMs: true, verbose: true}), '123 microseconds 571 nanoseconds');
t.is(m(0.123456789, {formatSubMs: true, verbose: true}), '123 microseconds 456 nanoseconds');
t.is(m(0.001, {formatSubMs: true, verbose: true}), '1 microsecond');
t.is(prettyMs(0.400, {formatSubMs: true, verbose: true}), '400 microseconds');
t.is(prettyMs(0.123571, {formatSubMs: true, verbose: true}), '123 microseconds 571 nanoseconds');
t.is(prettyMs(0.123456789, {formatSubMs: true, verbose: true}), '123 microseconds 456 nanoseconds');
t.is(prettyMs(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');
t.is(prettyMs(1000 * 60 * 67 * 24 * 465, {verbose: true, compact: true, unitCount: 1}), '~1 year');
t.is(prettyMs(1000 * 60 * 67 * 24 * 465, {verbose: true, compact: true, unitCount: 2}), '~1 year');
t.is(prettyMs(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');
t.is(prettyMs(1010.340067, {separateMs: true, formatSubMs: true}), '1s 10ms 340µs 67ns');
t.is(prettyMs(((60 * 1000) + 34 + 0.000005), {separateMs: true, formatSubMs: true}), '1m 34ms 5ns');
});

test('throw on invalid', t => {
t.throws(() => {
m('foo');
prettyMs('foo');
});

t.throws(() => {
m(NaN);
prettyMs(NaN);
});

t.throws(() => {
m(Infinity);
prettyMs(Infinity);
});
});

test('properly rounds milliseconds with secDecimalDigits', t => {
const fn = ms => m(ms, {verbose: true, secDecimalDigits: 0});
const fn = ms => prettyMs(ms, {verbose: true, secDecimalDigits: 0});
t.is(fn(179700), '3 minutes');
t.is(fn((365 * 24 * 3600 * 1e3) - 1), '1 year');
t.is(fn((24 * 3600 * 1e3) - 1), '1 day');
Expand Down

0 comments on commit 1ee8417

Please sign in to comment.