Skip to content

Commit 014edf2

Browse files
authoredJan 18, 2024
Support BigInt (#66)
1 parent 2f55b19 commit 014edf2

File tree

5 files changed

+356
-274
lines changed

5 files changed

+356
-274
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ prettyMilliseconds(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5))
117117
```
118118
*/
119119
export default function prettyMilliseconds(
120-
milliseconds: number,
120+
milliseconds: number | bigint,
121121
options?: Options
122122
): string;
123123

‎index.js

+26-14
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const isZero = value => value === 0 || value === 0n;
44
const pluralize = (word, count) => (count === 1 || count === 1n) ? word : `${word}s`;
55

66
const SECOND_ROUNDING_EPSILON = 0.000_000_1;
7+
const ONE_DAY_IN_MILLISECONDS = 24n * 60n * 60n * 1000n;
78

89
export default function prettyMilliseconds(milliseconds, options) {
9-
if (!Number.isFinite(milliseconds)) {
10-
throw new TypeError('Expected a finite number');
10+
const isBigInt = typeof milliseconds === 'bigint';
11+
if (!isBigInt && !Number.isFinite(milliseconds)) {
12+
throw new TypeError('Expected a finite number or bigint');
1113
}
1214

1315
options = {...options};
@@ -58,27 +60,34 @@ export default function prettyMilliseconds(milliseconds, options) {
5860
};
5961

6062
const parsed = parseMilliseconds(milliseconds);
63+
const days = BigInt(parsed.days);
6164

62-
add(BigInt(parsed.days) / 365n, 'year', 'y');
63-
add(parsed.days % 365, 'day', 'd');
64-
add(parsed.hours, 'hour', 'h');
65-
add(parsed.minutes, 'minute', 'm');
65+
add(days / 365n, 'year', 'y');
66+
add(days % 365n, 'day', 'd');
67+
add(Number(parsed.hours), 'hour', 'h');
68+
add(Number(parsed.minutes), 'minute', 'm');
6669

6770
if (
6871
options.separateMilliseconds
6972
|| options.formatSubMilliseconds
7073
|| (!options.colonNotation && milliseconds < 1000)
7174
) {
72-
add(parsed.seconds, 'second', 's');
75+
const seconds = Number(parsed.seconds);
76+
const milliseconds = Number(parsed.milliseconds);
77+
const microseconds = Number(parsed.microseconds);
78+
const nanoseconds = Number(parsed.nanoseconds);
79+
80+
add(seconds, 'second', 's');
81+
7382
if (options.formatSubMilliseconds) {
74-
add(parsed.milliseconds, 'millisecond', 'ms');
75-
add(parsed.microseconds, 'microsecond', 'µs');
76-
add(parsed.nanoseconds, 'nanosecond', 'ns');
83+
add(milliseconds, 'millisecond', 'ms');
84+
add(microseconds, 'microsecond', 'µs');
85+
add(nanoseconds, 'nanosecond', 'ns');
7786
} else {
7887
const millisecondsAndBelow
79-
= parsed.milliseconds
80-
+ (parsed.microseconds / 1000)
81-
+ (parsed.nanoseconds / 1e6);
88+
= milliseconds
89+
+ (microseconds / 1000)
90+
+ (nanoseconds / 1e6);
8291

8392
const millisecondsDecimalDigits
8493
= typeof options.millisecondsDecimalDigits === 'number'
@@ -101,7 +110,10 @@ export default function prettyMilliseconds(milliseconds, options) {
101110
);
102111
}
103112
} else {
104-
const seconds = (milliseconds / 1000) % 60;
113+
const seconds = (
114+
(isBigInt ? Number(milliseconds % ONE_DAY_IN_MILLISECONDS) : milliseconds)
115+
/ 1000
116+
) % 60;
105117
const secondsDecimalDigits
106118
= typeof options.secondsDecimalDigits === 'number'
107119
? options.secondsDecimalDigits

‎index.test-d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {expectType} from 'tsd';
22
import prettyMilliseconds from './index.js';
33

44
expectType<string>(prettyMilliseconds(1_335_669_000));
5+
expectType<string>(prettyMilliseconds(1_335_669_000n));
56
expectType<string>(prettyMilliseconds(1_335_669_000, {secondsDecimalDigits: 1}));
67
expectType<string>(
78
prettyMilliseconds(1_335_669_000, {millisecondsDecimalDigits: 2}),

‎readme.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import prettyMilliseconds from 'pretty-ms';
1616
prettyMilliseconds(1337000000);
1717
//=> '15d 11h 23m 20s'
1818

19+
prettyMilliseconds(1337000000n);
20+
//=> '15d 11h 23m 20s'
21+
1922
prettyMilliseconds(1337);
2023
//=> '1.3s'
2124

@@ -49,7 +52,7 @@ prettyMilliseconds(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5))
4952

5053
#### milliseconds
5154

52-
Type: `number`
55+
Type: `number | bigint`
5356

5457
Milliseconds to humanize.
5558

‎test.js

+324-258
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.