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 negative milliseconds. #71

Merged
merged 5 commits into from
Jul 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default function prettyMilliseconds(milliseconds, options) {

options = {...options};

const sign = milliseconds < 0 ? '-' : '';
milliseconds = milliseconds < 0 ? -milliseconds : milliseconds; // Cannot use `Math.abs()` because of BigInt support.

if (options.colonNotation) {
options.compact = false;
options.formatSubMilliseconds = false;
Expand Down Expand Up @@ -122,13 +125,13 @@ export default function prettyMilliseconds(milliseconds, options) {
}

if (result.length === 0) {
return '0' + (options.verbose ? ' milliseconds' : 'ms');
return sign + '0' + (options.verbose ? ' milliseconds' : 'ms');
}

const separator = options.colonNotation ? ':' : ' ';
if (typeof options.unitCount === 'number') {
result = result.slice(0, Math.max(options.unitCount, 1));
}

return result.join(separator);
return sign + result.join(separator);
}
35 changes: 35 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,41 @@ runTests({
],
});

runTests({
title: 'negative milliseconds with options',
cases: [
[-0, '0ms'],
[-0.1, '-1ms'],
[-1, '-1ms'],
[-999, '-999ms'],
[-1000, '-1s'],
[-1000 + 400, '-600ms'],
[(-1000 * 2) + 400, '-1.6s'],
[-13_370, '-13.3s'],
[Number.MIN_SAFE_INTEGER, '-285616y 151d 8h 59m 0.9s'],
// With compact option
[-1000 * 60 * 60 * 999, {compact: true}, '-41d'],
[-1000 * 60 * 60 * 24 * 465, {compact: true}, '-1y'],
// With unit-count
[-1000 * 60 * 67, {unitCount: 2}, '-1h 7m'],
[-1000 * 60 * 67 * 24 * 465, {unitCount: 1}, '-1y'],
[-1000 * 60 * 67 * 24 * 465, {unitCount: 2}, '-1y 154d'],
// With verbose and secondsDecimalDigits
[(-1000 * 5) - 254, {verbose: true, secondsDecimalDigits: 4}, '-5.2540 seconds'],
[-33_333, {verbose: true, secondsDecimalDigits: 4}, '-33.3330 seconds'],
// With verbose and compact
[-1000 * 60 * 5, {verbose: true, compact: true}, '-5 minutes'],
[-1000 * 60 * 67, {verbose: true, compact: true}, '-1 hour'],
[-1000 * 60 * 60 * 12, {verbose: true, compact: true}, '-12 hours'],
// With separateMilliseconds option
[-1001, {separateMilliseconds: true}, '-1s 1ms'],
[-1234, {separateMilliseconds: true}, '-1s 234ms'],
// With formatSubMilliseconds option
[-1.234_567, {formatSubMilliseconds: true}, '-1ms 234µs 567ns'],
[-1234.567, {formatSubMilliseconds: true}, '-1s 234ms 567µs'],
],
});

runTests({
title: '`colonNotation` option',
defaultOptions: {colonNotation: true},
Expand Down