Skip to content

Commit

Permalink
Support BigInt (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 18, 2024
1 parent 2f55b19 commit 014edf2
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 274 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
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
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
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
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

0 comments on commit 014edf2

Please sign in to comment.