Skip to content

Commit 25509bc

Browse files
gerbermichisindresorhus
andauthoredNov 15, 2024··
Add hideYears, hideYearsAndDays, and hideSeconds options (#72)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent d00183f commit 25509bc

File tree

4 files changed

+172
-57
lines changed

4 files changed

+172
-57
lines changed
 

‎index.d.ts

+21
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ export type Options = {
7575
@default false
7676
*/
7777
readonly colonNotation?: boolean;
78+
79+
/**
80+
Hides the year and shows the hidden year additionally as days (365 per year): `1y 3d 5h 1m 45s` → `368d 5h 1m 45s`.
81+
82+
@default false
83+
*/
84+
readonly hideYear?: boolean;
85+
86+
/**
87+
Hides the year and days and shows the hidden values additionally as hours: `1y 3d 5h 1m 45s` → `8837h 1m 45s`.
88+
89+
@default false
90+
*/
91+
readonly hideYearAndDays?: boolean;
92+
93+
/**
94+
Hides the seconds: `1y 3d 5h 1m 45s` → `1y 3d 5h 1m`.
95+
96+
@default false
97+
*/
98+
readonly hideSeconds?: boolean;
7899
};
79100

80101
/**

‎index.js

+69-57
Original file line numberDiff line numberDiff line change
@@ -61,67 +61,79 @@ export default function prettyMilliseconds(milliseconds, options) {
6161
const parsed = parseMilliseconds(milliseconds);
6262
const days = BigInt(parsed.days);
6363

64-
add(days / 365n, 'year', 'y');
65-
add(days % 365n, 'day', 'd');
66-
add(Number(parsed.hours), 'hour', 'h');
64+
if (options.hideYearAndDays) {
65+
add((BigInt(days) * 24n) + BigInt(parsed.hours), 'hour', 'h');
66+
} else {
67+
if (options.hideYear) {
68+
add(days, 'day', 'd');
69+
} else {
70+
add(days / 365n, 'year', 'y');
71+
add(days % 365n, 'day', 'd');
72+
}
73+
74+
add(Number(parsed.hours), 'hour', 'h');
75+
}
76+
6777
add(Number(parsed.minutes), 'minute', 'm');
6878

69-
if (
70-
options.separateMilliseconds
71-
|| options.formatSubMilliseconds
72-
|| (!options.colonNotation && milliseconds < 1000)
73-
) {
74-
const seconds = Number(parsed.seconds);
75-
const milliseconds = Number(parsed.milliseconds);
76-
const microseconds = Number(parsed.microseconds);
77-
const nanoseconds = Number(parsed.nanoseconds);
78-
79-
add(seconds, 'second', 's');
80-
81-
if (options.formatSubMilliseconds) {
82-
add(milliseconds, 'millisecond', 'ms');
83-
add(microseconds, 'microsecond', 'µs');
84-
add(nanoseconds, 'nanosecond', 'ns');
79+
if (!options.hideSeconds) {
80+
if (
81+
options.separateMilliseconds
82+
|| options.formatSubMilliseconds
83+
|| (!options.colonNotation && milliseconds < 1000)
84+
) {
85+
const seconds = Number(parsed.seconds);
86+
const milliseconds = Number(parsed.milliseconds);
87+
const microseconds = Number(parsed.microseconds);
88+
const nanoseconds = Number(parsed.nanoseconds);
89+
90+
add(seconds, 'second', 's');
91+
92+
if (options.formatSubMilliseconds) {
93+
add(milliseconds, 'millisecond', 'ms');
94+
add(microseconds, 'microsecond', 'µs');
95+
add(nanoseconds, 'nanosecond', 'ns');
96+
} else {
97+
const millisecondsAndBelow
98+
= milliseconds
99+
+ (microseconds / 1000)
100+
+ (nanoseconds / 1e6);
101+
102+
const millisecondsDecimalDigits
103+
= typeof options.millisecondsDecimalDigits === 'number'
104+
? options.millisecondsDecimalDigits
105+
: 0;
106+
107+
const roundedMilliseconds = millisecondsAndBelow >= 1
108+
? Math.round(millisecondsAndBelow)
109+
: Math.ceil(millisecondsAndBelow);
110+
111+
const millisecondsString = millisecondsDecimalDigits
112+
? millisecondsAndBelow.toFixed(millisecondsDecimalDigits)
113+
: roundedMilliseconds;
114+
115+
add(
116+
Number.parseFloat(millisecondsString),
117+
'millisecond',
118+
'ms',
119+
millisecondsString,
120+
);
121+
}
85122
} else {
86-
const millisecondsAndBelow
87-
= milliseconds
88-
+ (microseconds / 1000)
89-
+ (nanoseconds / 1e6);
90-
91-
const millisecondsDecimalDigits
92-
= typeof options.millisecondsDecimalDigits === 'number'
93-
? options.millisecondsDecimalDigits
94-
: 0;
95-
96-
const roundedMilliseconds = millisecondsAndBelow >= 1
97-
? Math.round(millisecondsAndBelow)
98-
: Math.ceil(millisecondsAndBelow);
99-
100-
const millisecondsString = millisecondsDecimalDigits
101-
? millisecondsAndBelow.toFixed(millisecondsDecimalDigits)
102-
: roundedMilliseconds;
103-
104-
add(
105-
Number.parseFloat(millisecondsString),
106-
'millisecond',
107-
'ms',
108-
millisecondsString,
109-
);
123+
const seconds = (
124+
(isBigInt ? Number(milliseconds % ONE_DAY_IN_MILLISECONDS) : milliseconds)
125+
/ 1000
126+
) % 60;
127+
const secondsDecimalDigits
128+
= typeof options.secondsDecimalDigits === 'number'
129+
? options.secondsDecimalDigits
130+
: 1;
131+
const secondsFixed = floorDecimals(seconds, secondsDecimalDigits);
132+
const secondsString = options.keepDecimalsOnWholeSeconds
133+
? secondsFixed
134+
: secondsFixed.replace(/\.0+$/, '');
135+
add(Number.parseFloat(secondsString), 'second', 's', secondsString);
110136
}
111-
} else {
112-
const seconds = (
113-
(isBigInt ? Number(milliseconds % ONE_DAY_IN_MILLISECONDS) : milliseconds)
114-
/ 1000
115-
) % 60;
116-
const secondsDecimalDigits
117-
= typeof options.secondsDecimalDigits === 'number'
118-
? options.secondsDecimalDigits
119-
: 1;
120-
const secondsFixed = floorDecimals(seconds, secondsDecimalDigits);
121-
const secondsString = options.keepDecimalsOnWholeSeconds
122-
? secondsFixed
123-
: secondsFixed.replace(/\.0+$/, '');
124-
add(Number.parseFloat(secondsString), 'second', 's', secondsString);
125137
}
126138

127139
if (result.length === 0) {

‎readme.md

+21
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ Setting `colonNotation` to `true` overrides the following options to `false`:
137137
- `separateMilliseconds`
138138
- `verbose`
139139

140+
##### hideYear
141+
142+
Type: `boolean`\
143+
Default: `false`
144+
145+
Hides the year and shows the hidden year additionally as days (365 per year): `1y 3d 5h 1m 45s``368d 5h 1m 45s`.
146+
147+
##### hideYearAndDays
148+
149+
Type: `boolean`\
150+
Default: `false`
151+
152+
Hides the year and days and shows the hidden values additionally as hours: `1y 3d 5h 1m 45s``8837h 1m 45s`.
153+
154+
##### hideSeconds
155+
156+
Type: `boolean`\
157+
Default: `false`
158+
159+
Hides the seconds: `1y 3d 5h 1m 45s``1y 3d 5h 1m`.
160+
140161
## Related
141162

142163
- [pretty-ms-cli](https://github.com/sindresorhus/pretty-ms-cli) - CLI for this module

‎test.js

+61
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,67 @@ runTests({
379379
],
380380
});
381381

382+
runTests({
383+
title: 'have a hideYear option',
384+
cases: [
385+
[1000 * 60, {hideYear: true}, '1m'],
386+
[1000 * 60, {hideYear: false}, '1m'],
387+
[1000 * 60 * 67, {hideYear: true}, '1h 7m'],
388+
[1000 * 60 * 67, {hideYear: false}, '1h 7m'],
389+
[1000 * 60 * 67 * 24 * 465, {hideYear: false}, '1y 154d 6h'],
390+
[1000 * 60 * 67 * 24 * 465, {hideYear: true}, '519d 6h'],
391+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: false}, '1y 154d 6h 1m 6.5s'],
392+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true}, '519d 6h 1m 6.5s'],
393+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true, secondsDecimalDigits: 0}, '519d 6h 1m 6s'],
394+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6000, {hideYear: true, keepDecimalsOnWholeSeconds: true}, '519d 6h 1m 6.0s'],
395+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true, separateMilliseconds: true}, '519d 6h 1m 6s 500ms'],
396+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true, verbose: true}, '519 days 6 hours 1 minute 6.5 seconds'],
397+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYear: true, compact: true}, '519d'],
398+
],
399+
});
400+
401+
runTests({
402+
title: 'have a hideYearAndDays option',
403+
cases: [
404+
[1000 * 60, {hideYearAndDays: true}, '1m'],
405+
[1000 * 60, {hideYearAndDays: false}, '1m'],
406+
[1000 * 60 * 67, {hideYearAndDays: false}, '1h 7m'],
407+
[1000 * 60 * 67, {hideYearAndDays: true}, '1h 7m'],
408+
[1000 * 60 * 67 * 24 * 465, {hideYearAndDays: false}, '1y 154d 6h'],
409+
[1000 * 60 * 67 * 24 * 465, {hideYearAndDays: true}, '12462h'],
410+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: false}, '1y 154d 6h 1m 6.5s'],
411+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true}, '12462h 1m 6.5s'],
412+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true, secondsDecimalDigits: 0}, '12462h 1m 6s'],
413+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6000, {hideYearAndDays: true, keepDecimalsOnWholeSeconds: true}, '12462h 1m 6.0s'],
414+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true, separateMilliseconds: true}, '12462h 1m 6s 500ms'],
415+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true, verbose: true}, '12462 hours 1 minute 6.5 seconds'],
416+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60) + 6500, {hideYearAndDays: true, compact: true}, '12462h'],
417+
],
418+
});
419+
420+
runTests({
421+
title: 'have a hideSeconds option',
422+
cases: [
423+
[(1000 * 60) + 6500, {hideSeconds: false}, '1m 6.5s'],
424+
[(1000 * 60) + 6500, {hideSeconds: true}, '1m'],
425+
[(1000 * 60) + 6500, {hideSeconds: true, secondsDecimalDigits: 3}, '1m'],
426+
[(1000 * 60) + 6500, {hideSeconds: true, keepDecimalsOnWholeSeconds: true}, '1m'],
427+
[(1000 * 60) + 6500, {hideSeconds: true, formatSubMilliseconds: true}, '1m'],
428+
[(1000 * 60) + 6500, {hideSeconds: true, separateMilliseconds: true}, '1m'],
429+
[(1000 * 60) + 6500, {hideSeconds: true, verbose: true}, '1 minute'],
430+
[(1000 * 60) + 6500, {hideSeconds: true, compact: true}, '1m'],
431+
],
432+
});
433+
434+
runTests({
435+
title: 'have hideYearAndDays,hideSeconds and colonNotation options',
436+
cases: [
437+
[(1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearAndDays: true, colonNotation: true}, '15:59'],
438+
[(1000 * 60 * 67 * 24 * 465) + (1000 * 60 * 60 * 15) + (1000 * 60 * 59) + (1000 * 59) + 543, {hideSeconds: true, hideYearAndDays: true, colonNotation: true}, '12477:59'],
439+
[BigInt(Number.MAX_VALUE), {hideSeconds: true, hideYearAndDays: true, colonNotation: true}, '49935920412842103004035395481028987999464046534956943499699299111988127994452371877941544064657466158761238598198439573398422590802628939657907651862093754718347197382375356132290413913997035817798852363459759428417939788028673041157169044258923152298554951723373534213538382550255361078125112229495590:14'],
440+
],
441+
});
442+
382443
test('Big numbers', t => {
383444
t.is(
384445
prettyMilliseconds(Number.MAX_VALUE),

0 commit comments

Comments
 (0)
Please sign in to comment.