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

Support BigInt #66

Merged
merged 8 commits into from
Jan 18, 2024
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
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ prettyMilliseconds(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5))
```
*/
export default function prettyMilliseconds(
milliseconds: number,
milliseconds: number | bigint,
options?: Options
): string;

40 changes: 26 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const isZero = value => value === 0 || value === 0n;
const pluralize = (word, count) => (count === 1 || count === 1n) ? word : `${word}s`;

const SECOND_ROUNDING_EPSILON = 0.000_000_1;
const ONE_DAY_IN_MILLISECONDS = 24n * 60n * 60n * 1000n;

export default function prettyMilliseconds(milliseconds, options) {
if (!Number.isFinite(milliseconds)) {
throw new TypeError('Expected a finite number');
const isBigInt = typeof milliseconds === 'bigint';
if (!isBigInt && !Number.isFinite(milliseconds)) {
throw new TypeError('Expected a finite number or bigint');
}

options = {...options};
Expand Down Expand Up @@ -58,27 +60,34 @@ export default function prettyMilliseconds(milliseconds, options) {
};

const parsed = parseMilliseconds(milliseconds);
const days = BigInt(parsed.days);

add(BigInt(parsed.days) / 365n, 'year', 'y');
add(parsed.days % 365, 'day', 'd');
add(parsed.hours, 'hour', 'h');
add(parsed.minutes, 'minute', 'm');
add(days / 365n, 'year', 'y');
add(days % 365n, 'day', 'd');
add(Number(parsed.hours), 'hour', 'h');
add(Number(parsed.minutes), 'minute', 'm');

if (
options.separateMilliseconds
|| options.formatSubMilliseconds
|| (!options.colonNotation && milliseconds < 1000)
) {
add(parsed.seconds, 'second', 's');
const seconds = Number(parsed.seconds);
const milliseconds = Number(parsed.milliseconds);
const microseconds = Number(parsed.microseconds);
const nanoseconds = Number(parsed.nanoseconds);

add(seconds, 'second', 's');

if (options.formatSubMilliseconds) {
add(parsed.milliseconds, 'millisecond', 'ms');
add(parsed.microseconds, 'microsecond', 'µs');
add(parsed.nanoseconds, 'nanosecond', 'ns');
add(milliseconds, 'millisecond', 'ms');
add(microseconds, 'microsecond', 'µs');
add(nanoseconds, 'nanosecond', 'ns');
} else {
const millisecondsAndBelow
= parsed.milliseconds
+ (parsed.microseconds / 1000)
+ (parsed.nanoseconds / 1e6);
= milliseconds
+ (microseconds / 1000)
+ (nanoseconds / 1e6);

const millisecondsDecimalDigits
= typeof options.millisecondsDecimalDigits === 'number'
Expand All @@ -101,7 +110,10 @@ export default function prettyMilliseconds(milliseconds, options) {
);
}
} else {
const seconds = (milliseconds / 1000) % 60;
const seconds = (
(isBigInt ? Number(milliseconds % ONE_DAY_IN_MILLISECONDS) : milliseconds)
/ 1000
) % 60;
const secondsDecimalDigits
= typeof options.secondsDecimalDigits === 'number'
? options.secondsDecimalDigits
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {expectType} from 'tsd';
import prettyMilliseconds from './index.js';

expectType<string>(prettyMilliseconds(1_335_669_000));
expectType<string>(prettyMilliseconds(1_335_669_000n));
expectType<string>(prettyMilliseconds(1_335_669_000, {secondsDecimalDigits: 1}));
expectType<string>(
prettyMilliseconds(1_335_669_000, {millisecondsDecimalDigits: 2}),
Expand Down
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import prettyMilliseconds from 'pretty-ms';
prettyMilliseconds(1337000000);
//=> '15d 11h 23m 20s'

prettyMilliseconds(1337000000n);
//=> '15d 11h 23m 20s'

prettyMilliseconds(1337);
//=> '1.3s'

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

#### milliseconds

Type: `number`
Type: `number | bigint`

Milliseconds to humanize.

Expand Down