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

Add support for bigint to pretty-format #8138

Merged
merged 2 commits into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions packages/pretty-format/src/__tests__/prettyFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,41 @@ describe('prettyFormat()', () => {
expect(prettyFormat(val)).toEqual('123');
});

it('prints a positive bigint', () => {
msafi marked this conversation as resolved.
Show resolved Hide resolved
const val = BigInt(123);
msafi marked this conversation as resolved.
Show resolved Hide resolved
expect(prettyFormat(val)).toEqual('123n');
});

it('prints a negative number', () => {
const val = -123;
expect(prettyFormat(val)).toEqual('-123');
});

it('prints a negative bigint', () => {
const val = BigInt(-123);
expect(prettyFormat(val)).toEqual('-123n');
});

it('prints zero', () => {
const val = 0;
expect(prettyFormat(val)).toEqual('0');
});

it('prints zero bigint', () => {
const val = BigInt(0);
expect(prettyFormat(val)).toEqual('0n');
});

it('prints negative zero', () => {
const val = -0;
expect(prettyFormat(val)).toEqual('-0');
});

it('prints negative zero bigint', () => {
const val = BigInt(-0);
expect(prettyFormat(val)).toEqual('0n');
});

it('prints a date', () => {
const val = new Date(10e11);
expect(prettyFormat(val)).toEqual('2001-09-09T01:46:40.000Z');
Expand Down
7 changes: 7 additions & 0 deletions packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ function printNumber(val: number): string {
return Object.is(val, -0) ? '-0' : String(val);
}

function printBigInt(val: bigint): string {
return String(`${val}n`);
}

function printFunction(val: Function, printFunctionName: boolean): string {
if (!printFunctionName) {
return '[Function]';
Expand Down Expand Up @@ -112,6 +116,9 @@ function printBasicValue(
if (typeOf === 'number') {
return printNumber(val);
}
if (typeOf === 'bigint') {
return printBigInt(val);
}
if (typeOf === 'string') {
if (escapeString) {
return '"' + val.replace(/"|\\/g, '\\$&') + '"';
Expand Down