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

feat: Better print for sparse arrays #11326

Merged
merged 5 commits into from Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 0 deletions packages/pretty-format/src/__tests__/prettyFormat.test.ts
Expand Up @@ -41,6 +41,26 @@ describe('prettyFormat()', () => {
expect(prettyFormat(val)).toEqual('Array [\n 1,\n 2,\n 3,\n]');
});

it('prints a sparse array with only holes', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [, , ,];
expect(prettyFormat(val)).toEqual('Array [\n ,\n ,\n ,\n]');
});

it('prints a sparse array with items', () => {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-sparse-arrays
const val = [1, , , 4];
expect(prettyFormat(val)).toEqual('Array [\n 1,\n ,\n ,\n 4,\n]');
});

it('prints a sparse array also containing undefined values', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [1, , undefined, undefined, , 4];
expect(prettyFormat(val)).toEqual(
'Array [\n 1,\n ,\n undefined,\n undefined,\n ,\n 4,\n]',
);
});

it('prints a empty typed array', () => {
const val = new Uint32Array(0);
expect(prettyFormat(val)).toEqual('Uint32Array []');
Expand Down
8 changes: 5 additions & 3 deletions packages/pretty-format/src/collections.ts
Expand Up @@ -142,9 +142,11 @@ export function printListItems(
const indentationNext = indentation + config.indent;

for (let i = 0; i < list.length; i++) {
result +=
indentationNext +
printer(list[i], config, indentationNext, depth, refs);
result += indentationNext;

if (i in list) {
result += printer(list[i], config, indentationNext, depth, refs);
}

if (i < list.length - 1) {
result += ',' + config.spacingInner;
Expand Down